| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/test/base/web_ui_browser_test.h" | 5 #include "chrome/test/base/web_ui_browser_test.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 using content::RenderViewHost; | 45 using content::RenderViewHost; |
| 46 using content::WebContents; | 46 using content::WebContents; |
| 47 using content::WebUIController; | 47 using content::WebUIController; |
| 48 using content::WebUIMessageHandler; | 48 using content::WebUIMessageHandler; |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 base::LazyInstance<std::vector<std::string> > error_messages_ = | 52 base::LazyInstance<std::vector<std::string> > error_messages_ = |
| 53 LAZY_INSTANCE_INITIALIZER; | 53 LAZY_INSTANCE_INITIALIZER; |
| 54 | 54 |
| 55 // Intercepts all log messages. | |
| 56 bool LogHandler(int severity, | |
| 57 const char* file, | |
| 58 int line, | |
| 59 size_t message_start, | |
| 60 const std::string& str) { | |
| 61 if (severity == logging::LOG_ERROR && file && | |
| 62 std::string("CONSOLE") == file) { | |
| 63 error_messages_.Get().push_back(str); | |
| 64 } | |
| 65 | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 class WebUIJsInjectionReadyObserver : public content::WebContentsObserver { | 55 class WebUIJsInjectionReadyObserver : public content::WebContentsObserver { |
| 70 public: | 56 public: |
| 71 WebUIJsInjectionReadyObserver(content::WebContents* web_contents, | 57 WebUIJsInjectionReadyObserver(content::WebContents* web_contents, |
| 72 WebUIBrowserTest* browser_test, | 58 WebUIBrowserTest* browser_test, |
| 73 const std::string& preload_test_fixture, | 59 const std::string& preload_test_fixture, |
| 74 const std::string& preload_test_name) | 60 const std::string& preload_test_name) |
| 75 : content::WebContentsObserver(web_contents), | 61 : content::WebContentsObserver(web_contents), |
| 76 browser_test_(browser_test), | 62 browser_test_(browser_test), |
| 77 preload_test_fixture_(preload_test_fixture), | 63 preload_test_fixture_(preload_test_fixture), |
| 78 preload_test_name_(preload_test_name) {} | 64 preload_test_name_(preload_test_name) {} |
| 79 | 65 |
| 80 void RenderViewCreated(content::RenderViewHost* rvh) override { | 66 void RenderViewCreated(content::RenderViewHost* rvh) override { |
| 81 browser_test_->PreLoadJavascriptLibraries( | 67 browser_test_->PreLoadJavascriptLibraries( |
| 82 preload_test_fixture_, preload_test_name_, rvh); | 68 preload_test_fixture_, preload_test_name_, rvh); |
| 83 } | 69 } |
| 84 | 70 |
| 85 private: | 71 private: |
| 86 WebUIBrowserTest* browser_test_; | 72 WebUIBrowserTest* browser_test_; |
| 87 std::string preload_test_fixture_; | 73 std::string preload_test_fixture_; |
| 88 std::string preload_test_name_; | 74 std::string preload_test_name_; |
| 89 }; | 75 }; |
| 90 | 76 |
| 91 } // namespace | 77 } // namespace |
| 92 | 78 |
| 79 // Intercepts all log messages. |
| 80 void WebUILogMessageListener::OnMessage(int severity, |
| 81 const char* file, |
| 82 int line, |
| 83 size_t message_start, |
| 84 const std::string& str) { |
| 85 if (severity == logging::LOG_ERROR && file && |
| 86 std::string("CONSOLE") == file) { |
| 87 error_messages_.Get().push_back(str); |
| 88 } |
| 89 } |
| 90 |
| 93 WebUIBrowserTest::~WebUIBrowserTest() { | 91 WebUIBrowserTest::~WebUIBrowserTest() { |
| 94 } | 92 } |
| 95 | 93 |
| 96 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { | 94 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { |
| 97 ConstValueVector empty_args; | 95 ConstValueVector empty_args; |
| 98 return RunJavascriptFunction(function_name, empty_args); | 96 return RunJavascriptFunction(function_name, empty_args); |
| 99 } | 97 } |
| 100 | 98 |
| 101 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, | 99 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, |
| 102 base::Value* arg) { | 100 base::Value* arg) { |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 }; | 363 }; |
| 366 | 364 |
| 367 base::LazyInstance<MockWebUIProvider> mock_provider_ = | 365 base::LazyInstance<MockWebUIProvider> mock_provider_ = |
| 368 LAZY_INSTANCE_INITIALIZER; | 366 LAZY_INSTANCE_INITIALIZER; |
| 369 | 367 |
| 370 } // namespace | 368 } // namespace |
| 371 | 369 |
| 372 void WebUIBrowserTest::SetUpOnMainThread() { | 370 void WebUIBrowserTest::SetUpOnMainThread() { |
| 373 JavaScriptBrowserTest::SetUpOnMainThread(); | 371 JavaScriptBrowserTest::SetUpOnMainThread(); |
| 374 | 372 |
| 375 logging::SetLogMessageHandler(&LogHandler); | |
| 376 | |
| 377 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); | 373 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); |
| 378 | 374 |
| 379 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 375 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 380 ChromeWebUIControllerFactory::GetInstance()); | 376 ChromeWebUIControllerFactory::GetInstance()); |
| 381 | 377 |
| 382 test_factory_.reset(new TestChromeWebUIControllerFactory); | 378 test_factory_.reset(new TestChromeWebUIControllerFactory); |
| 383 | 379 |
| 384 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); | 380 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); |
| 385 | 381 |
| 386 test_factory_->AddFactoryOverride(GURL(kDummyURL).host(), | 382 test_factory_->AddFactoryOverride(GURL(kDummyURL).host(), |
| 387 mock_provider_.Pointer()); | 383 mock_provider_.Pointer()); |
| 388 test_factory_->AddFactoryOverride(content::kChromeUIResourcesHost, | 384 test_factory_->AddFactoryOverride(content::kChromeUIResourcesHost, |
| 389 mock_provider_.Pointer()); | 385 mock_provider_.Pointer()); |
| 390 } | 386 } |
| 391 | 387 |
| 392 void WebUIBrowserTest::TearDownOnMainThread() { | 388 void WebUIBrowserTest::TearDownOnMainThread() { |
| 393 logging::SetLogMessageHandler(NULL); | |
| 394 | |
| 395 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); | 389 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 396 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 390 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 397 test_factory_.get()); | 391 test_factory_.get()); |
| 398 | 392 |
| 399 // This is needed to avoid a debug assert after the test completes, see stack | 393 // This is needed to avoid a debug assert after the test completes, see stack |
| 400 // trace in http://crrev.com/179347 | 394 // trace in http://crrev.com/179347 |
| 401 content::WebUIControllerFactory::RegisterFactory( | 395 content::WebUIControllerFactory::RegisterFactory( |
| 402 ChromeWebUIControllerFactory::GetInstance()); | 396 ChromeWebUIControllerFactory::GetInstance()); |
| 403 | 397 |
| 404 test_factory_.reset(); | 398 test_factory_.reset(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 } | 482 } |
| 489 | 483 |
| 490 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 484 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 491 const base::FilePath::StringType& path) { | 485 const base::FilePath::StringType& path) { |
| 492 base::FilePath dir_test_data; | 486 base::FilePath dir_test_data; |
| 493 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 487 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 494 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); | 488 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); |
| 495 EXPECT_TRUE(base::PathExists(test_path)); | 489 EXPECT_TRUE(base::PathExists(test_path)); |
| 496 return net::FilePathToFileURL(test_path); | 490 return net::FilePathToFileURL(test_path); |
| 497 } | 491 } |
| OLD | NEW |