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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "chrome/browser/chrome_content_browser_client.h" | 18 #include "chrome/browser/chrome_content_browser_client.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/browser_commands.h" | 21 #include "chrome/browser/ui/browser_commands.h" |
21 #include "chrome/browser/ui/browser_navigator.h" | 22 #include "chrome/browser/ui/browser_navigator.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 24 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
24 #include "chrome/browser/ui/webui/web_ui_test_handler.h" | 25 #include "chrome/browser/ui/webui/web_ui_test_handler.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 }; | 105 }; |
105 | 106 |
106 } // namespace | 107 } // namespace |
107 | 108 |
108 WebUIBrowserTest::~WebUIBrowserTest() {} | 109 WebUIBrowserTest::~WebUIBrowserTest() {} |
109 | 110 |
110 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { | 111 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { |
111 user_libraries_.push_back(library_path); | 112 user_libraries_.push_back(library_path); |
112 } | 113 } |
113 | 114 |
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) { | 115 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { |
126 ConstValueVector empty_args; | 116 ConstValueVector empty_args; |
127 return RunJavascriptFunction(function_name, empty_args); | 117 return RunJavascriptFunction(function_name, empty_args); |
128 } | 118 } |
129 | 119 |
130 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, | 120 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, |
131 base::Value* arg) { | 121 base::Value* arg) { |
132 ConstValueVector args; | 122 ConstValueVector args; |
133 args.push_back(arg); | 123 args.push_back(arg); |
134 return RunJavascriptFunction(function_name, args); | 124 return RunJavascriptFunction(function_name, args); |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 395 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
406 ChromeWebUIControllerFactory::GetInstance()); | 396 ChromeWebUIControllerFactory::GetInstance()); |
407 | 397 |
408 test_factory_.reset(new TestChromeWebUIControllerFactory); | 398 test_factory_.reset(new TestChromeWebUIControllerFactory); |
409 | 399 |
410 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); | 400 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); |
411 | 401 |
412 test_factory_->AddFactoryOverride( | 402 test_factory_->AddFactoryOverride( |
413 GURL(kDummyURL).host(), mock_provider_.Pointer()); | 403 GURL(kDummyURL).host(), mock_provider_.Pointer()); |
414 | 404 |
415 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); | 405 base::FilePath test_data_directory; |
416 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); | 406 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); |
| 407 test_data_directory = test_data_directory.Append(kWebUITestFolder); |
| 408 library_search_paths_.push_back(test_data_directory); |
| 409 |
| 410 base::FilePath gen_test_data_directory; |
417 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, | 411 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, |
418 &gen_test_data_directory_)); | 412 &gen_test_data_directory)); |
| 413 library_search_paths_.push_back(gen_test_data_directory); |
| 414 |
| 415 base::FilePath source_root_directory; |
| 416 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root_directory)); |
| 417 library_search_paths_.push_back(source_root_directory); |
419 | 418 |
420 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. | 419 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. |
421 base::FilePath resources_pack_path; | 420 base::FilePath resources_pack_path; |
422 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | 421 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
423 ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 422 ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
424 resources_pack_path, ui::SCALE_FACTOR_NONE); | 423 resources_pack_path, ui::SCALE_FACTOR_NONE); |
425 | 424 |
426 AddLibraryFromSourceRoot(this, base::FilePath(kA11yAuditLibraryJSPath)); | 425 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); |
427 AddLibraryFromSourceRoot(this, base::FilePath(kMockJSPath)); | 426 AddLibrary(base::FilePath(kMockJSPath)); |
428 AddLibrary(base::FilePath(kWebUILibraryJS)); | 427 AddLibrary(base::FilePath(kWebUILibraryJS)); |
429 } | 428 } |
430 | 429 |
431 void WebUIBrowserTest::CleanUpOnMainThread() { | 430 void WebUIBrowserTest::CleanUpOnMainThread() { |
432 logging::SetLogMessageHandler(NULL); | 431 logging::SetLogMessageHandler(NULL); |
433 | 432 |
434 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); | 433 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); |
435 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 434 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
436 test_factory_.get()); | 435 test_factory_.get()); |
437 | 436 |
(...skipping 15 matching lines...) Expand all Loading... |
453 | 452 |
454 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 453 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
455 const base::FilePath::StringType& path) { | 454 const base::FilePath::StringType& path) { |
456 base::FilePath dir_test_data; | 455 base::FilePath dir_test_data; |
457 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 456 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
458 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); | 457 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); |
459 EXPECT_TRUE(base::PathExists(test_path)); | 458 EXPECT_TRUE(base::PathExists(test_path)); |
460 return net::FilePathToFileURL(test_path); | 459 return net::FilePathToFileURL(test_path); |
461 } | 460 } |
462 | 461 |
463 void WebUIBrowserTest::BuildJavascriptLibraries(base::string16* content) { | 462 void WebUIBrowserTest::BuildJavascriptLibraries( |
464 ASSERT_TRUE(content != NULL); | 463 std::vector<base::string16>* libraries) { |
465 std::string utf8_content; | 464 ASSERT_TRUE(libraries != NULL); |
466 std::vector<base::FilePath>::iterator user_libraries_iterator; | 465 std::vector<base::FilePath>::iterator user_libraries_iterator; |
467 for (user_libraries_iterator = user_libraries_.begin(); | 466 for (user_libraries_iterator = user_libraries_.begin(); |
468 user_libraries_iterator != user_libraries_.end(); | 467 user_libraries_iterator != user_libraries_.end(); |
469 ++user_libraries_iterator) { | 468 ++user_libraries_iterator) { |
470 std::string library_content; | 469 std::string library_content; |
471 if (user_libraries_iterator->IsAbsolute()) { | 470 if (user_libraries_iterator->IsAbsolute()) { |
472 ASSERT_TRUE(base::ReadFileToString(*user_libraries_iterator, | 471 ASSERT_TRUE(base::ReadFileToString(*user_libraries_iterator, |
473 &library_content)) | 472 &library_content)) |
474 << user_libraries_iterator->value(); | 473 << user_libraries_iterator->value(); |
475 } else { | 474 } else { |
476 bool ok = base::ReadFileToString( | 475 bool ok = false; |
477 gen_test_data_directory_.Append(*user_libraries_iterator), | 476 std::vector<base::FilePath>::iterator library_search_path_iterator; |
478 &library_content); | 477 for (library_search_path_iterator = library_search_paths_.begin(); |
479 if (!ok) { | 478 library_search_path_iterator != library_search_paths_.end(); |
| 479 ++library_search_path_iterator) { |
480 ok = base::ReadFileToString( | 480 ok = base::ReadFileToString( |
481 test_data_directory_.Append(*user_libraries_iterator), | 481 base::MakeAbsoluteFilePath( |
| 482 library_search_path_iterator->Append(*user_libraries_iterator)), |
482 &library_content); | 483 &library_content); |
| 484 if (ok) |
| 485 break; |
483 } | 486 } |
484 ASSERT_TRUE(ok) << user_libraries_iterator->value(); | 487 ASSERT_TRUE(ok) << "User library not found: " |
| 488 << user_libraries_iterator->value(); |
485 } | 489 } |
486 utf8_content.append(library_content); | 490 library_content.append(";\n"); |
487 utf8_content.append(";\n"); | 491 |
| 492 // This magic code puts filenames in stack traces. |
| 493 library_content.append("//# sourceURL="); |
| 494 library_content.append(user_libraries_iterator->BaseName().AsUTF8Unsafe()); |
| 495 library_content.append("\n"); |
| 496 libraries->push_back(base::UTF8ToUTF16(library_content)); |
488 } | 497 } |
489 content->append(base::UTF8ToUTF16(utf8_content)); | |
490 } | 498 } |
491 | 499 |
492 base::string16 WebUIBrowserTest::BuildRunTestJSCall( | 500 base::string16 WebUIBrowserTest::BuildRunTestJSCall( |
493 bool is_async, | 501 bool is_async, |
494 const std::string& function_name, | 502 const std::string& function_name, |
495 const WebUIBrowserTest::ConstValueVector& test_func_args) { | 503 const WebUIBrowserTest::ConstValueVector& test_func_args) { |
496 ConstValueVector arguments; | 504 ConstValueVector arguments; |
497 base::FundamentalValue* is_async_arg = new base::FundamentalValue(is_async); | 505 base::FundamentalValue* is_async_arg = new base::FundamentalValue(is_async); |
498 arguments.push_back(is_async_arg); | 506 arguments.push_back(is_async_arg); |
499 base::StringValue* function_name_arg = new base::StringValue(function_name); | 507 base::StringValue* function_name_arg = new base::StringValue(function_name); |
500 arguments.push_back(function_name_arg); | 508 arguments.push_back(function_name_arg); |
501 base::ListValue* baked_argument_list = new base::ListValue(); | 509 base::ListValue* baked_argument_list = new base::ListValue(); |
502 ConstValueVector::const_iterator arguments_iterator; | 510 ConstValueVector::const_iterator arguments_iterator; |
503 for (arguments_iterator = test_func_args.begin(); | 511 for (arguments_iterator = test_func_args.begin(); |
504 arguments_iterator != test_func_args.end(); | 512 arguments_iterator != test_func_args.end(); |
505 ++arguments_iterator) { | 513 ++arguments_iterator) { |
506 baked_argument_list->Append((*arguments_iterator)->DeepCopy()); | 514 baked_argument_list->Append((*arguments_iterator)->DeepCopy()); |
507 } | 515 } |
508 arguments.push_back(baked_argument_list); | 516 arguments.push_back(baked_argument_list); |
509 return content::WebUI::GetJavascriptCall(std::string("runTest"), | 517 return content::WebUI::GetJavascriptCall(std::string("runTest"), |
510 arguments.get()); | 518 arguments.get()); |
511 } | 519 } |
512 | 520 |
513 bool WebUIBrowserTest::RunJavascriptUsingHandler( | 521 bool WebUIBrowserTest::RunJavascriptUsingHandler( |
514 const std::string& function_name, | 522 const std::string& function_name, |
515 const ConstValueVector& function_arguments, | 523 const ConstValueVector& function_arguments, |
516 bool is_test, | 524 bool is_test, |
517 bool is_async, | 525 bool is_async, |
518 RenderViewHost* preload_host) { | 526 RenderViewHost* preload_host) { |
519 | 527 // Get the user libraries. Preloading them individually is best, then |
| 528 // we can assign each one a filename for better stack traces. Otherwise |
| 529 // append them all to |content|. |
520 base::string16 content; | 530 base::string16 content; |
521 if (!libraries_preloaded_) | 531 std::vector<base::string16> libraries; |
522 BuildJavascriptLibraries(&content); | 532 if (!libraries_preloaded_) { |
| 533 BuildJavascriptLibraries(&libraries); |
| 534 if (!preload_host) { |
| 535 content = JoinString(libraries, '\n'); |
| 536 libraries.clear(); |
| 537 } |
| 538 } |
523 | 539 |
524 if (!function_name.empty()) { | 540 if (!function_name.empty()) { |
525 base::string16 called_function; | 541 base::string16 called_function; |
526 if (is_test) { | 542 if (is_test) { |
527 called_function = | 543 called_function = |
528 BuildRunTestJSCall(is_async, function_name, function_arguments); | 544 BuildRunTestJSCall(is_async, function_name, function_arguments); |
529 } else { | 545 } else { |
530 called_function = | 546 called_function = |
531 content::WebUI::GetJavascriptCall(function_name, | 547 content::WebUI::GetJavascriptCall(function_name, |
532 function_arguments.get()); | 548 function_arguments.get()); |
533 } | 549 } |
534 content.append(called_function); | 550 content.append(called_function); |
535 } | 551 } |
536 | 552 |
537 if (!preload_host) | 553 if (!preload_host) |
538 SetupHandlers(); | 554 SetupHandlers(); |
539 | 555 |
540 bool result = true; | 556 bool result = true; |
541 | 557 |
| 558 for (size_t i = 0; i < libraries.size(); ++i) |
| 559 test_handler_->PreloadJavaScript(libraries[i], preload_host); |
| 560 |
542 if (is_test) | 561 if (is_test) |
543 result = test_handler_->RunJavaScriptTestWithResult(content); | 562 result = test_handler_->RunJavaScriptTestWithResult(content); |
544 else if (preload_host) | 563 else if (preload_host) |
545 test_handler_->PreloadJavaScript(content, preload_host); | 564 test_handler_->PreloadJavaScript(content, preload_host); |
546 else | 565 else |
547 test_handler_->RunJavaScript(content); | 566 test_handler_->RunJavaScript(content); |
548 | 567 |
549 if (error_messages_.Get().size() > 0) { | 568 if (error_messages_.Get().size() > 0) { |
550 LOG(ERROR) << "Encountered javascript console error(s)"; | 569 LOG(ERROR) << "Encountered javascript console error(s)"; |
551 result = false; | 570 result = false; |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 // testDone directly and expect pass result. | 807 // testDone directly and expect pass result. |
789 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | 808 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { |
790 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | 809 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); |
791 } | 810 } |
792 | 811 |
793 // Test that calling testDone during RunJavascriptTest still completes when | 812 // Test that calling testDone during RunJavascriptTest still completes when |
794 // waiting for async result. | 813 // waiting for async result. |
795 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | 814 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { |
796 ASSERT_TRUE(RunJavascriptTest("testDone")); | 815 ASSERT_TRUE(RunJavascriptTest("testDone")); |
797 } | 816 } |
OLD | NEW |