| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_browsertest.h" | 5 #include "chrome/test/base/web_ui_browsertest.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 } // namespace | 106 } // namespace |
| 107 | 107 |
| 108 WebUIBrowserTest::~WebUIBrowserTest() {} | 108 WebUIBrowserTest::~WebUIBrowserTest() {} |
| 109 | 109 |
| 110 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { | 110 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { |
| 111 user_libraries_.push_back(library_path); | 111 user_libraries_.push_back(library_path); |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Add a helper JS library to the given WebUIBrowserTest from a path relative to | |
| 115 // base::DIR_SOURCE_ROOT. | |
| 116 // static | |
| 117 void AddLibraryFromSourceRoot(WebUIBrowserTest* browser_test, | |
| 118 const base::FilePath& path) { | |
| 119 base::FilePath filePath; | |
| 120 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &filePath)); | |
| 121 filePath = filePath.Append(path); | |
| 122 browser_test->AddLibrary(filePath); | |
| 123 } | |
| 124 | |
| 125 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { | 114 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { |
| 126 ConstValueVector empty_args; | 115 ConstValueVector empty_args; |
| 127 return RunJavascriptFunction(function_name, empty_args); | 116 return RunJavascriptFunction(function_name, empty_args); |
| 128 } | 117 } |
| 129 | 118 |
| 130 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, | 119 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, |
| 131 base::Value* arg) { | 120 base::Value* arg) { |
| 132 ConstValueVector args; | 121 ConstValueVector args; |
| 133 args.push_back(arg); | 122 args.push_back(arg); |
| 134 return RunJavascriptFunction(function_name, args); | 123 return RunJavascriptFunction(function_name, args); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 394 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 406 ChromeWebUIControllerFactory::GetInstance()); | 395 ChromeWebUIControllerFactory::GetInstance()); |
| 407 | 396 |
| 408 test_factory_.reset(new TestChromeWebUIControllerFactory); | 397 test_factory_.reset(new TestChromeWebUIControllerFactory); |
| 409 | 398 |
| 410 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); | 399 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); |
| 411 | 400 |
| 412 test_factory_->AddFactoryOverride( | 401 test_factory_->AddFactoryOverride( |
| 413 GURL(kDummyURL).host(), mock_provider_.Pointer()); | 402 GURL(kDummyURL).host(), mock_provider_.Pointer()); |
| 414 | 403 |
| 415 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); | 404 base::FilePath test_data_directory; |
| 416 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); | 405 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); |
| 406 test_data_directory = test_data_directory.Append(kWebUITestFolder); |
| 407 library_search_paths_.push_back(test_data_directory); |
| 408 |
| 409 base::FilePath gen_test_data_directory; |
| 417 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, | 410 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, |
| 418 &gen_test_data_directory_)); | 411 &gen_test_data_directory)); |
| 412 library_search_paths_.push_back(gen_test_data_directory); |
| 413 |
| 414 base::FilePath source_root_directory; |
| 415 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root_directory)); |
| 416 library_search_paths_.push_back(source_root_directory); |
| 419 | 417 |
| 420 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. | 418 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. |
| 421 base::FilePath resources_pack_path; | 419 base::FilePath resources_pack_path; |
| 422 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | 420 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 423 ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 421 ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| 424 resources_pack_path, ui::SCALE_FACTOR_NONE); | 422 resources_pack_path, ui::SCALE_FACTOR_NONE); |
| 425 | 423 |
| 426 AddLibraryFromSourceRoot(this, base::FilePath(kA11yAuditLibraryJSPath)); | 424 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); |
| 427 AddLibraryFromSourceRoot(this, base::FilePath(kMockJSPath)); | 425 AddLibrary(base::FilePath(kMockJSPath)); |
| 428 AddLibrary(base::FilePath(kWebUILibraryJS)); | 426 AddLibrary(base::FilePath(kWebUILibraryJS)); |
| 429 } | 427 } |
| 430 | 428 |
| 431 void WebUIBrowserTest::CleanUpOnMainThread() { | 429 void WebUIBrowserTest::CleanUpOnMainThread() { |
| 432 logging::SetLogMessageHandler(NULL); | 430 logging::SetLogMessageHandler(NULL); |
| 433 | 431 |
| 434 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); | 432 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 435 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 433 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 436 test_factory_.get()); | 434 test_factory_.get()); |
| 437 | 435 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 453 | 451 |
| 454 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 452 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 455 const base::FilePath::StringType& path) { | 453 const base::FilePath::StringType& path) { |
| 456 base::FilePath dir_test_data; | 454 base::FilePath dir_test_data; |
| 457 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 455 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 458 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); | 456 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); |
| 459 EXPECT_TRUE(base::PathExists(test_path)); | 457 EXPECT_TRUE(base::PathExists(test_path)); |
| 460 return net::FilePathToFileURL(test_path); | 458 return net::FilePathToFileURL(test_path); |
| 461 } | 459 } |
| 462 | 460 |
| 463 void WebUIBrowserTest::BuildJavascriptLibraries(base::string16* content) { | 461 void WebUIBrowserTest::BuildJavascriptLibraries( |
| 464 ASSERT_TRUE(content != NULL); | 462 std::vector<base::string16>* libraries) { |
| 465 std::string utf8_content; | 463 ASSERT_TRUE(libraries != NULL); |
| 466 std::vector<base::FilePath>::iterator user_libraries_iterator; | 464 std::vector<base::FilePath>::iterator user_libraries_iterator; |
| 467 for (user_libraries_iterator = user_libraries_.begin(); | 465 for (user_libraries_iterator = user_libraries_.begin(); |
| 468 user_libraries_iterator != user_libraries_.end(); | 466 user_libraries_iterator != user_libraries_.end(); |
| 469 ++user_libraries_iterator) { | 467 ++user_libraries_iterator) { |
| 470 std::string library_content; | 468 std::string library_content; |
| 471 if (user_libraries_iterator->IsAbsolute()) { | 469 if (user_libraries_iterator->IsAbsolute()) { |
| 472 ASSERT_TRUE(base::ReadFileToString(*user_libraries_iterator, | 470 ASSERT_TRUE(base::ReadFileToString(*user_libraries_iterator, |
| 473 &library_content)) | 471 &library_content)) |
| 474 << user_libraries_iterator->value(); | 472 << user_libraries_iterator->value(); |
| 475 } else { | 473 } else { |
| 476 bool ok = base::ReadFileToString( | 474 bool ok = false; |
| 477 gen_test_data_directory_.Append(*user_libraries_iterator), | 475 std::vector<base::FilePath>::iterator library_search_path_iterator; |
| 478 &library_content); | 476 for (library_search_path_iterator = library_search_paths_.begin(); |
| 479 if (!ok) { | 477 library_search_path_iterator != library_search_paths_.end(); |
| 478 ++library_search_path_iterator) { |
| 480 ok = base::ReadFileToString( | 479 ok = base::ReadFileToString( |
| 481 test_data_directory_.Append(*user_libraries_iterator), | 480 base::MakeAbsoluteFilePath( |
| 481 library_search_path_iterator->Append(*user_libraries_iterator)), |
| 482 &library_content); | 482 &library_content); |
| 483 if (ok) |
| 484 break; |
| 483 } | 485 } |
| 484 ASSERT_TRUE(ok) << user_libraries_iterator->value(); | 486 ASSERT_TRUE(ok) << "User library not found: " |
| 487 << user_libraries_iterator->value(); |
| 485 } | 488 } |
| 486 utf8_content.append(library_content); | 489 library_content.append(";\n"); |
| 487 utf8_content.append(";\n"); | 490 |
| 491 // This magic code puts filenames in stack traces. |
| 492 library_content.append("//# sourceURL="); |
| 493 library_content.append(user_libraries_iterator->BaseName().value()); |
| 494 library_content.append("\n"); |
| 495 libraries->push_back(base::UTF8ToUTF16(library_content)); |
| 488 } | 496 } |
| 489 content->append(base::UTF8ToUTF16(utf8_content)); | |
| 490 } | 497 } |
| 491 | 498 |
| 492 base::string16 WebUIBrowserTest::BuildRunTestJSCall( | 499 base::string16 WebUIBrowserTest::BuildRunTestJSCall( |
| 493 bool is_async, | 500 bool is_async, |
| 494 const std::string& function_name, | 501 const std::string& function_name, |
| 495 const WebUIBrowserTest::ConstValueVector& test_func_args) { | 502 const WebUIBrowserTest::ConstValueVector& test_func_args) { |
| 496 ConstValueVector arguments; | 503 ConstValueVector arguments; |
| 497 base::FundamentalValue* is_async_arg = new base::FundamentalValue(is_async); | 504 base::FundamentalValue* is_async_arg = new base::FundamentalValue(is_async); |
| 498 arguments.push_back(is_async_arg); | 505 arguments.push_back(is_async_arg); |
| 499 base::StringValue* function_name_arg = new base::StringValue(function_name); | 506 base::StringValue* function_name_arg = new base::StringValue(function_name); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 510 arguments.get()); | 517 arguments.get()); |
| 511 } | 518 } |
| 512 | 519 |
| 513 bool WebUIBrowserTest::RunJavascriptUsingHandler( | 520 bool WebUIBrowserTest::RunJavascriptUsingHandler( |
| 514 const std::string& function_name, | 521 const std::string& function_name, |
| 515 const ConstValueVector& function_arguments, | 522 const ConstValueVector& function_arguments, |
| 516 bool is_test, | 523 bool is_test, |
| 517 bool is_async, | 524 bool is_async, |
| 518 RenderViewHost* preload_host) { | 525 RenderViewHost* preload_host) { |
| 519 | 526 |
| 527 std::vector<base::string16> libraries; |
| 528 if (!libraries_preloaded_) |
| 529 BuildJavascriptLibraries(&libraries); |
| 520 base::string16 content; | 530 base::string16 content; |
| 521 if (!libraries_preloaded_) | |
| 522 BuildJavascriptLibraries(&content); | |
| 523 | 531 |
| 524 if (!function_name.empty()) { | 532 if (!function_name.empty()) { |
| 525 base::string16 called_function; | 533 base::string16 called_function; |
| 526 if (is_test) { | 534 if (is_test) { |
| 527 called_function = | 535 called_function = |
| 528 BuildRunTestJSCall(is_async, function_name, function_arguments); | 536 BuildRunTestJSCall(is_async, function_name, function_arguments); |
| 529 } else { | 537 } else { |
| 530 called_function = | 538 called_function = |
| 531 content::WebUI::GetJavascriptCall(function_name, | 539 content::WebUI::GetJavascriptCall(function_name, |
| 532 function_arguments.get()); | 540 function_arguments.get()); |
| 533 } | 541 } |
| 534 content.append(called_function); | 542 content.append(called_function); |
| 535 } | 543 } |
| 536 | 544 |
| 537 if (!preload_host) | 545 if (!preload_host) |
| 538 SetupHandlers(); | 546 SetupHandlers(); |
| 539 | 547 |
| 540 bool result = true; | 548 bool result = true; |
| 541 | 549 |
| 550 for (size_t i = 0; i < libraries.size(); ++i) |
| 551 test_handler_->PreloadJavaScript(libraries[i], preload_host); |
| 552 |
| 542 if (is_test) | 553 if (is_test) |
| 543 result = test_handler_->RunJavaScriptTestWithResult(content); | 554 result = test_handler_->RunJavaScriptTestWithResult(content); |
| 544 else if (preload_host) | 555 else if (preload_host) |
| 545 test_handler_->PreloadJavaScript(content, preload_host); | 556 test_handler_->PreloadJavaScript(content, preload_host); |
| 546 else | 557 else |
| 547 test_handler_->RunJavaScript(content); | 558 test_handler_->RunJavaScript(content); |
| 548 | 559 |
| 549 if (error_messages_.Get().size() > 0) { | 560 if (error_messages_.Get().size() > 0) { |
| 550 LOG(ERROR) << "Encountered javascript console error(s)"; | 561 LOG(ERROR) << "Encountered javascript console error(s)"; |
| 551 result = false; | 562 result = false; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 // testDone directly and expect pass result. | 799 // testDone directly and expect pass result. |
| 789 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | 800 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { |
| 790 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | 801 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); |
| 791 } | 802 } |
| 792 | 803 |
| 793 // Test that calling testDone during RunJavascriptTest still completes when | 804 // Test that calling testDone during RunJavascriptTest still completes when |
| 794 // waiting for async result. | 805 // waiting for async result. |
| 795 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | 806 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { |
| 796 ASSERT_TRUE(RunJavascriptTest("testDone")); | 807 ASSERT_TRUE(RunJavascriptTest("testDone")); |
| 797 } | 808 } |
| OLD | NEW |