| 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 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 #if defined(ENABLE_PRINT_PREVIEW) | 41 #if defined(ENABLE_PRINT_PREVIEW) |
| 42 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 42 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 43 #endif | 43 #endif |
| 44 | 44 |
| 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 { | |
| 51 | |
| 52 base::LazyInstance<std::vector<std::string> > error_messages_ = | |
| 53 LAZY_INSTANCE_INITIALIZER; | |
| 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 { | 50 class WebUIJsInjectionReadyObserver : public content::WebContentsObserver { |
| 70 public: | 51 public: |
| 71 WebUIJsInjectionReadyObserver(content::WebContents* web_contents, | 52 WebUIJsInjectionReadyObserver(content::WebContents* web_contents, |
| 72 WebUIBrowserTest* browser_test, | 53 WebUIBrowserTest* browser_test, |
| 73 const std::string& preload_test_fixture, | 54 const std::string& preload_test_fixture, |
| 74 const std::string& preload_test_name) | 55 const std::string& preload_test_name) |
| 75 : content::WebContentsObserver(web_contents), | 56 : content::WebContentsObserver(web_contents), |
| 76 browser_test_(browser_test), | 57 browser_test_(browser_test), |
| 77 preload_test_fixture_(preload_test_fixture), | 58 preload_test_fixture_(preload_test_fixture), |
| 78 preload_test_name_(preload_test_name) {} | 59 preload_test_name_(preload_test_name) {} |
| 79 | 60 |
| 80 void RenderViewCreated(content::RenderViewHost* rvh) override { | 61 void RenderViewCreated(content::RenderViewHost* rvh) override { |
| 81 browser_test_->PreLoadJavascriptLibraries( | 62 browser_test_->PreLoadJavascriptLibraries( |
| 82 preload_test_fixture_, preload_test_name_, rvh); | 63 preload_test_fixture_, preload_test_name_, rvh); |
| 83 } | 64 } |
| 84 | 65 |
| 85 private: | 66 private: |
| 86 WebUIBrowserTest* browser_test_; | 67 WebUIBrowserTest* browser_test_; |
| 87 std::string preload_test_fixture_; | 68 std::string preload_test_fixture_; |
| 88 std::string preload_test_name_; | 69 std::string preload_test_name_; |
| 89 }; | 70 }; |
| 90 | 71 |
| 91 } // namespace | |
| 92 | |
| 93 WebUIBrowserTest::~WebUIBrowserTest() { | 72 WebUIBrowserTest::~WebUIBrowserTest() { |
| 94 } | 73 } |
| 95 | 74 |
| 96 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { | 75 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { |
| 97 ConstValueVector empty_args; | 76 ConstValueVector empty_args; |
| 98 return RunJavascriptFunction(function_name, empty_args); | 77 return RunJavascriptFunction(function_name, empty_args); |
| 99 } | 78 } |
| 100 | 79 |
| 101 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, | 80 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, |
| 102 base::Value* arg) { | 81 base::Value* arg) { |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 }; | 344 }; |
| 366 | 345 |
| 367 base::LazyInstance<MockWebUIProvider> mock_provider_ = | 346 base::LazyInstance<MockWebUIProvider> mock_provider_ = |
| 368 LAZY_INSTANCE_INITIALIZER; | 347 LAZY_INSTANCE_INITIALIZER; |
| 369 | 348 |
| 370 } // namespace | 349 } // namespace |
| 371 | 350 |
| 372 void WebUIBrowserTest::SetUpOnMainThread() { | 351 void WebUIBrowserTest::SetUpOnMainThread() { |
| 373 JavaScriptBrowserTest::SetUpOnMainThread(); | 352 JavaScriptBrowserTest::SetUpOnMainThread(); |
| 374 | 353 |
| 375 logging::SetLogMessageHandler(&LogHandler); | |
| 376 | |
| 377 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); | 354 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); |
| 378 | 355 |
| 379 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 356 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 380 ChromeWebUIControllerFactory::GetInstance()); | 357 ChromeWebUIControllerFactory::GetInstance()); |
| 381 | 358 |
| 382 test_factory_.reset(new TestChromeWebUIControllerFactory); | 359 test_factory_.reset(new TestChromeWebUIControllerFactory); |
| 383 | 360 |
| 384 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); | 361 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); |
| 385 | 362 |
| 386 test_factory_->AddFactoryOverride(GURL(kDummyURL).host(), | 363 test_factory_->AddFactoryOverride(GURL(kDummyURL).host(), |
| 387 mock_provider_.Pointer()); | 364 mock_provider_.Pointer()); |
| 388 test_factory_->AddFactoryOverride(content::kChromeUIResourcesHost, | 365 test_factory_->AddFactoryOverride(content::kChromeUIResourcesHost, |
| 389 mock_provider_.Pointer()); | 366 mock_provider_.Pointer()); |
| 390 } | 367 } |
| 391 | 368 |
| 392 void WebUIBrowserTest::TearDownOnMainThread() { | 369 void WebUIBrowserTest::TearDownOnMainThread() { |
| 393 logging::SetLogMessageHandler(NULL); | |
| 394 | |
| 395 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); | 370 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 396 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 371 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 397 test_factory_.get()); | 372 test_factory_.get()); |
| 398 | 373 |
| 399 // This is needed to avoid a debug assert after the test completes, see stack | 374 // This is needed to avoid a debug assert after the test completes, see stack |
| 400 // trace in http://crrev.com/179347 | 375 // trace in http://crrev.com/179347 |
| 401 content::WebUIControllerFactory::RegisterFactory( | 376 content::WebUIControllerFactory::RegisterFactory( |
| 402 ChromeWebUIControllerFactory::GetInstance()); | 377 ChromeWebUIControllerFactory::GetInstance()); |
| 403 | 378 |
| 404 test_factory_.reset(); | 379 test_factory_.reset(); |
| 380 |
| 381 JavaScriptBrowserTest::TearDownOnMainThread(); |
| 405 } | 382 } |
| 406 | 383 |
| 407 void WebUIBrowserTest::SetWebUIInstance(content::WebUI* web_ui) { | 384 void WebUIBrowserTest::SetWebUIInstance(content::WebUI* web_ui) { |
| 408 override_selected_web_ui_ = web_ui; | 385 override_selected_web_ui_ = web_ui; |
| 409 } | 386 } |
| 410 | 387 |
| 411 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { | 388 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { |
| 412 return NULL; | 389 return NULL; |
| 413 } | 390 } |
| 414 | 391 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 for (size_t i = 0; i < libraries.size(); ++i) | 428 for (size_t i = 0; i < libraries.size(); ++i) |
| 452 test_handler_->PreloadJavaScript(libraries[i], preload_host); | 429 test_handler_->PreloadJavaScript(libraries[i], preload_host); |
| 453 | 430 |
| 454 if (is_test) | 431 if (is_test) |
| 455 result = test_handler_->RunJavaScriptTestWithResult(content); | 432 result = test_handler_->RunJavaScriptTestWithResult(content); |
| 456 else if (preload_host) | 433 else if (preload_host) |
| 457 test_handler_->PreloadJavaScript(content, preload_host); | 434 test_handler_->PreloadJavaScript(content, preload_host); |
| 458 else | 435 else |
| 459 test_handler_->RunJavaScript(content); | 436 test_handler_->RunJavaScript(content); |
| 460 | 437 |
| 461 if (error_messages_.Get().size() > 0) { | 438 if (has_console_errors()) { |
| 462 LOG(ERROR) << "Encountered javascript console error(s)"; | 439 LOG(ERROR) << "Encountered javascript console error(s)"; |
| 463 result = false; | 440 result = false; |
| 464 error_messages_.Get().clear(); | 441 set_has_console_errors(false); |
| 465 } | 442 } |
| 443 |
| 466 return result; | 444 return result; |
| 467 } | 445 } |
| 468 | 446 |
| 469 void WebUIBrowserTest::SetupHandlers() { | 447 void WebUIBrowserTest::SetupHandlers() { |
| 470 content::WebUI* web_ui_instance = | 448 content::WebUI* web_ui_instance = |
| 471 override_selected_web_ui_ | 449 override_selected_web_ui_ |
| 472 ? override_selected_web_ui_ | 450 ? override_selected_web_ui_ |
| 473 : browser()->tab_strip_model()->GetActiveWebContents()->GetWebUI(); | 451 : browser()->tab_strip_model()->GetActiveWebContents()->GetWebUI(); |
| 474 ASSERT_TRUE(web_ui_instance != NULL); | 452 ASSERT_TRUE(web_ui_instance != NULL); |
| 475 | 453 |
| 476 test_handler_->set_web_ui(web_ui_instance); | 454 test_handler_->set_web_ui(web_ui_instance); |
| 477 test_handler_->RegisterMessages(); | 455 test_handler_->RegisterMessages(); |
| 478 | 456 |
| 479 if (GetMockMessageHandler()) { | 457 if (GetMockMessageHandler()) { |
| 480 GetMockMessageHandler()->set_web_ui(web_ui_instance); | 458 GetMockMessageHandler()->set_web_ui(web_ui_instance); |
| 481 GetMockMessageHandler()->RegisterMessages(); | 459 GetMockMessageHandler()->RegisterMessages(); |
| 482 } | 460 } |
| 483 } | 461 } |
| 484 | 462 |
| 485 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 463 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 486 const base::FilePath::StringType& path) { | 464 const base::FilePath::StringType& path) { |
| 487 base::FilePath dir_test_data; | 465 base::FilePath dir_test_data; |
| 488 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 466 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 489 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); | 467 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); |
| 490 EXPECT_TRUE(base::PathExists(test_path)); | 468 EXPECT_TRUE(base::PathExists(test_path)); |
| 491 return net::FilePathToFileURL(test_path); | 469 return net::FilePathToFileURL(test_path); |
| 492 } | 470 } |
| OLD | NEW |