| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/dom_ui/dom_ui_browsertest.h" | 5 #include "chrome/browser/dom_ui/dom_ui_browsertest.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents.h" | 8 #include "chrome/browser/tab_contents/tab_contents.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| 11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/test/ui_test_utils.h" | 12 #include "chrome/test/ui_test_utils.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 13 | 14 |
| 14 static const FilePath::CharType* kDOMUILibraryJS = | 15 static const FilePath::CharType* kDOMUILibraryJS = |
| 15 FILE_PATH_LITERAL("test_api.js"); | 16 FILE_PATH_LITERAL("test_api.js"); |
| 16 static const FilePath::CharType* kDOMUITestFolder = | 17 static const FilePath::CharType* kDOMUITestFolder = |
| 17 FILE_PATH_LITERAL("dom_ui"); | 18 FILE_PATH_LITERAL("dom_ui"); |
| 18 | 19 |
| 19 bool DOMUITest::RunDOMUITest(const FilePath::CharType* src_path) { | 20 bool DOMUITest::RunDOMUITest(const FilePath::CharType* src_path) { |
| 20 std::string content; | 21 std::string content; |
| 21 BuildJavaScriptTest(FilePath(src_path), &content); | 22 BuildJavaScriptTest(FilePath(src_path), &content); |
| 22 handler_->Attach( | 23 SetupHandlers(); |
| 23 browser()->GetSelectedTabContents()->dom_ui()); | 24 return test_handler_->Execute(content); |
| 24 return handler_->Execute(content); | |
| 25 } | 25 } |
| 26 | 26 |
| 27 DOMUITest::DOMUITest() : handler_(new DOMUITestHandler()) {} | 27 DOMUITest::DOMUITest() : test_handler_(new DOMUITestHandler()) {} |
| 28 | 28 |
| 29 void DOMUITest::SetUpInProcessBrowserTestFixture() { | 29 void DOMUITest::SetUpInProcessBrowserTestFixture() { |
| 30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); | 30 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); |
| 31 test_data_directory_ = test_data_directory_.Append(kDOMUITestFolder); | 31 test_data_directory_ = test_data_directory_.Append(kDOMUITestFolder); |
| 32 |
| 33 // TODO(dtseng): should this be part of every BrowserTest or just DOMUI test. |
| 34 FilePath resources_pack_path; |
| 35 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 36 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); |
| 32 } | 37 } |
| 33 | 38 |
| 34 void DOMUITest::BuildJavaScriptTest(const FilePath& src_path, | 39 void DOMUITest::BuildJavaScriptTest(const FilePath& src_path, |
| 35 std::string* content) { | 40 std::string* content) { |
| 36 ASSERT_TRUE(content != NULL); | 41 ASSERT_TRUE(content != NULL); |
| 37 std::string library_content, src_content; | 42 std::string library_content, src_content; |
| 38 ASSERT_TRUE(file_util::ReadFileToString( | 43 ASSERT_TRUE(file_util::ReadFileToString( |
| 39 test_data_directory_.Append(FilePath(kDOMUILibraryJS)), | 44 test_data_directory_.Append(FilePath(kDOMUILibraryJS)), |
| 40 &library_content)); | 45 &library_content)); |
| 41 ASSERT_TRUE(file_util::ReadFileToString( | 46 ASSERT_TRUE(file_util::ReadFileToString( |
| 42 test_data_directory_.Append(src_path), &src_content)); | 47 test_data_directory_.Append(src_path), &src_content)); |
| 43 | 48 |
| 44 content->append(library_content); | 49 content->append(library_content); |
| 45 content->append(";\n"); | 50 content->append(";\n"); |
| 46 content->append(src_content); | 51 content->append(src_content); |
| 47 } | 52 } |
| 48 | 53 |
| 54 void DOMUITest::SetupHandlers() { |
| 55 DOMUI* dom_ui_instance = |
| 56 browser()->GetSelectedTabContents()->dom_ui(); |
| 57 ASSERT_TRUE(dom_ui_instance != NULL); |
| 58 dom_ui_instance->register_callback_overwrites(true); |
| 59 test_handler_->Attach(dom_ui_instance); |
| 60 |
| 61 if (GetMockMessageHandler()) |
| 62 GetMockMessageHandler()->Attach(dom_ui_instance); |
| 63 } |
| 64 |
| 49 IN_PROC_BROWSER_TEST_F(DOMUITest, TestSamplePass) { | 65 IN_PROC_BROWSER_TEST_F(DOMUITest, TestSamplePass) { |
| 50 // Navigate to UI. | 66 // Navigate to UI. |
| 51 // TODO(dtseng): make accessor for subclasses to return? | 67 // TODO(dtseng): make accessor for subclasses to return? |
| 52 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); | 68 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); |
| 53 | 69 |
| 54 ASSERT_TRUE(RunDOMUITest(FILE_PATH_LITERAL("sample_downloads.js"))); | 70 ASSERT_TRUE(RunDOMUITest(FILE_PATH_LITERAL("sample_downloads.js"))); |
| 55 } | 71 } |
| OLD | NEW |