| 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 #include "chrome/browser/ui/webui/web_ui_browsertest.h" | 4 #include "chrome/browser/ui/webui/web_ui_browsertest.h" |
| 5 | 5 |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 return RunJavascriptFunction(function_name, args); | 72 return RunJavascriptFunction(function_name, args); |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool WebUIBrowserTest::RunJavascriptFunction( | 75 bool WebUIBrowserTest::RunJavascriptFunction( |
| 76 const std::string& function_name, | 76 const std::string& function_name, |
| 77 const ConstValueVector& function_arguments) { | 77 const ConstValueVector& function_arguments) { |
| 78 return RunJavascriptUsingHandler( | 78 return RunJavascriptUsingHandler( |
| 79 function_name, function_arguments, false, false, NULL); | 79 function_name, function_arguments, false, false, NULL); |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool WebUIBrowserTest::RunJavascriptTestF(const std::string& test_fixture, | 82 bool WebUIBrowserTest::RunJavascriptTestF(bool is_async, |
| 83 const std::string& test_fixture, |
| 83 const std::string& test_name) { | 84 const std::string& test_name) { |
| 84 ConstValueVector args; | 85 ConstValueVector args; |
| 85 args.push_back(Value::CreateStringValue(test_fixture)); | 86 args.push_back(Value::CreateStringValue(test_fixture)); |
| 86 args.push_back(Value::CreateStringValue(test_name)); | 87 args.push_back(Value::CreateStringValue(test_name)); |
| 87 return RunJavascriptTest("RUN_TEST_F", args); | 88 |
| 89 if (is_async) |
| 90 return RunJavascriptAsyncTest("RUN_TEST_F", args); |
| 91 else |
| 92 return RunJavascriptTest("RUN_TEST_F", args); |
| 88 } | 93 } |
| 89 | 94 |
| 90 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name) { | 95 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name) { |
| 91 return RunJavascriptTest(test_name, ConstValueVector()); | 96 return RunJavascriptTest(test_name, ConstValueVector()); |
| 92 } | 97 } |
| 93 | 98 |
| 94 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, | 99 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, |
| 95 Value* arg) { | 100 Value* arg) { |
| 96 ConstValueVector args; | 101 ConstValueVector args; |
| 97 args.push_back(arg); | 102 args.push_back(arg); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 preload_test_name_ = preload_test_name; | 196 preload_test_name_ = preload_test_name; |
| 192 | 197 |
| 193 ui_test_utils::NavigateToURL(browser(), browse_to); | 198 ui_test_utils::NavigateToURL(browser(), browse_to); |
| 194 | 199 |
| 195 TestTabStripModelObserver tabstrip_observer( | 200 TestTabStripModelObserver tabstrip_observer( |
| 196 browser()->tabstrip_model(), this); | 201 browser()->tabstrip_model(), this); |
| 197 browser()->Print(); | 202 browser()->Print(); |
| 198 tabstrip_observer.WaitForObservation(); | 203 tabstrip_observer.WaitForObservation(); |
| 199 } | 204 } |
| 200 | 205 |
| 206 const char WebUIBrowserTest::kDummyURL[] = "chrome://DummyURL"; |
| 207 |
| 201 WebUIBrowserTest::WebUIBrowserTest() | 208 WebUIBrowserTest::WebUIBrowserTest() |
| 202 : test_handler_(new WebUITestHandler()), | 209 : test_handler_(new WebUITestHandler()), |
| 203 libraries_preloaded_(false) {} | 210 libraries_preloaded_(false) {} |
| 204 | 211 |
| 212 namespace { |
| 213 |
| 214 class MockWebUIProvider : public TestChromeWebUIFactory::WebUIProvider { |
| 215 public: |
| 216 MockWebUIProvider() {} |
| 217 |
| 218 // Returns a new ChromeWebUI |
| 219 WebUI* NewWebUI(TabContents* tab_contents, const GURL& url) OVERRIDE { |
| 220 return new ChromeWebUI(tab_contents); |
| 221 } |
| 222 }; |
| 223 |
| 224 base::LazyInstance<MockWebUIProvider> mock_provider_( |
| 225 base::LINKER_INITIALIZED); |
| 226 |
| 227 } // namespace |
| 228 |
| 205 void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { | 229 void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { |
| 230 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 231 TestChromeWebUIFactory::AddFactoryOverride(GURL(kDummyURL).host(), |
| 232 mock_provider_.Pointer()); |
| 233 |
| 206 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); | 234 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); |
| 207 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); | 235 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); |
| 208 | 236 |
| 209 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. | 237 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. |
| 210 FilePath resources_pack_path; | 238 FilePath resources_pack_path; |
| 211 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | 239 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 212 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); | 240 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); |
| 213 | 241 |
| 214 FilePath mockPath; | 242 FilePath mockPath; |
| 215 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); | 243 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); |
| 216 mockPath = mockPath.AppendASCII("chrome"); | 244 mockPath = mockPath.AppendASCII("chrome"); |
| 217 mockPath = mockPath.AppendASCII("third_party"); | 245 mockPath = mockPath.AppendASCII("third_party"); |
| 218 mockPath = mockPath.AppendASCII("mock4js"); | 246 mockPath = mockPath.AppendASCII("mock4js"); |
| 219 mockPath = mockPath.Append(kMockJS); | 247 mockPath = mockPath.Append(kMockJS); |
| 220 AddLibrary(mockPath); | 248 AddLibrary(mockPath); |
| 221 AddLibrary(FilePath(kWebUILibraryJS)); | 249 AddLibrary(FilePath(kWebUILibraryJS)); |
| 222 } | 250 } |
| 223 | 251 |
| 252 void WebUIBrowserTest::TearDownInProcessBrowserTestFixture() { |
| 253 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
| 254 TestChromeWebUIFactory::RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 255 } |
| 256 |
| 224 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { | 257 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { |
| 225 return NULL; | 258 return NULL; |
| 226 } | 259 } |
| 227 | 260 |
| 228 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 261 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 229 const FilePath::StringType& path) { | 262 const FilePath::StringType& path) { |
| 230 FilePath dir_test_data; | 263 FilePath dir_test_data; |
| 231 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 264 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 232 FilePath test_path(dir_test_data.AppendASCII("webui")); | 265 FilePath test_path(dir_test_data.AppendASCII("webui")); |
| 233 test_path = test_path.Append(path); | 266 test_path = test_path.Append(path); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 } | 440 } |
| 408 | 441 |
| 409 // Starts a passing test. | 442 // Starts a passing test. |
| 410 void RunTestPasses() { | 443 void RunTestPasses() { |
| 411 RunJavascriptFunction("runAsync", Value::CreateStringValue("testPasses")); | 444 RunJavascriptFunction("runAsync", Value::CreateStringValue("testPasses")); |
| 412 } | 445 } |
| 413 | 446 |
| 414 protected: | 447 protected: |
| 415 WebUIBrowserAsyncTest() {} | 448 WebUIBrowserAsyncTest() {} |
| 416 | 449 |
| 417 static const char kDummyURL[]; | |
| 418 | |
| 419 // Class to synchronize asynchronous javascript activity with the tests. | 450 // Class to synchronize asynchronous javascript activity with the tests. |
| 420 class AsyncWebUIMessageHandler : public WebUIMessageHandler { | 451 class AsyncWebUIMessageHandler : public WebUIMessageHandler { |
| 421 public: | 452 public: |
| 422 AsyncWebUIMessageHandler() {} | 453 AsyncWebUIMessageHandler() {} |
| 423 | 454 |
| 424 MOCK_METHOD1(HandleTestContinues, void(const ListValue*)); | 455 MOCK_METHOD1(HandleTestContinues, void(const ListValue*)); |
| 425 MOCK_METHOD1(HandleTestFails, void(const ListValue*)); | 456 MOCK_METHOD1(HandleTestFails, void(const ListValue*)); |
| 426 MOCK_METHOD1(HandleTestPasses, void(const ListValue*)); | 457 MOCK_METHOD1(HandleTestPasses, void(const ListValue*)); |
| 427 | 458 |
| 428 private: | 459 private: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 444 web_ui_->CallJavascriptFunction("runAsync", *test_name); | 475 web_ui_->CallJavascriptFunction("runAsync", *test_name); |
| 445 } | 476 } |
| 446 | 477 |
| 447 DISALLOW_COPY_AND_ASSIGN(AsyncWebUIMessageHandler); | 478 DISALLOW_COPY_AND_ASSIGN(AsyncWebUIMessageHandler); |
| 448 }; | 479 }; |
| 449 | 480 |
| 450 // Handler for this object. | 481 // Handler for this object. |
| 451 ::testing::StrictMock<AsyncWebUIMessageHandler> message_handler_; | 482 ::testing::StrictMock<AsyncWebUIMessageHandler> message_handler_; |
| 452 | 483 |
| 453 private: | 484 private: |
| 454 // Class to provide a ChromeWebUI for |kDummyURL|. | |
| 455 class MockWebUIProvider : public TestChromeWebUIFactory::WebUIProvider { | |
| 456 public: | |
| 457 MockWebUIProvider() {} | |
| 458 | |
| 459 // Returns a new ChromeWebUI | |
| 460 WebUI* NewWebUI(TabContents* tab_contents, const GURL& url) OVERRIDE { | |
| 461 return new ChromeWebUI(tab_contents); | |
| 462 } | |
| 463 }; | |
| 464 | |
| 465 // Provide this object's handler. | 485 // Provide this object's handler. |
| 466 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { | 486 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { |
| 467 return &message_handler_; | 487 return &message_handler_; |
| 468 } | 488 } |
| 469 | 489 |
| 470 // Set up the kDummyURL to be a WebUI page. | |
| 471 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 472 WebUIBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 473 TestChromeWebUIFactory::AddFactoryOverride(GURL(kDummyURL).host(), | |
| 474 &mock_provider_); | |
| 475 } | |
| 476 | |
| 477 // Tear down the kDummyURL WebUI page. | |
| 478 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
| 479 WebUIBrowserTest::TearDownInProcessBrowserTestFixture(); | |
| 480 TestChromeWebUIFactory::RemoveFactoryOverride(GURL(kDummyURL).host()); | |
| 481 } | |
| 482 | |
| 483 // Set up and browse to kDummyURL for all tests. | 490 // Set up and browse to kDummyURL for all tests. |
| 484 virtual void SetUpOnMainThread() OVERRIDE { | 491 virtual void SetUpOnMainThread() OVERRIDE { |
| 485 WebUIBrowserTest::SetUpOnMainThread(); | 492 WebUIBrowserTest::SetUpOnMainThread(); |
| 486 AddLibrary(FilePath(FILE_PATH_LITERAL("async.js"))); | 493 AddLibrary(FilePath(FILE_PATH_LITERAL("async.js"))); |
| 487 ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL)); | 494 ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL)); |
| 488 } | 495 } |
| 489 | 496 |
| 490 // Provider for this object. | |
| 491 MockWebUIProvider mock_provider_; | |
| 492 | |
| 493 DISALLOW_COPY_AND_ASSIGN(WebUIBrowserAsyncTest); | 497 DISALLOW_COPY_AND_ASSIGN(WebUIBrowserAsyncTest); |
| 494 }; | 498 }; |
| 495 | 499 |
| 496 const char WebUIBrowserAsyncTest::kDummyURL[] = "chrome://Dummy"; | |
| 497 | |
| 498 // Test that assertions fail immediately after assertion fails (no testContinues | 500 // Test that assertions fail immediately after assertion fails (no testContinues |
| 499 // message). (Sync version). | 501 // message). (Sync version). |
| 500 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncOkTestFail) { | 502 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncOkTestFail) { |
| 501 ASSERT_FALSE(RunJavascriptTest("testFailsAssert")); | 503 ASSERT_FALSE(RunJavascriptTest("testFailsAssert")); |
| 502 } | 504 } |
| 503 | 505 |
| 504 // Test that assertions fail immediately after assertion fails (no testContinues | 506 // Test that assertions fail immediately after assertion fails (no testContinues |
| 505 // message). (Async version). | 507 // message). (Async version). |
| 506 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsAssert) { | 508 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsAssert) { |
| 507 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); | 509 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 // testDone directly and expect pass result. | 581 // testDone directly and expect pass result. |
| 580 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | 582 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { |
| 581 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | 583 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); |
| 582 } | 584 } |
| 583 | 585 |
| 584 // Test that calling testDone during RunJavascriptTest still completes when | 586 // Test that calling testDone during RunJavascriptTest still completes when |
| 585 // waiting for async result. | 587 // waiting for async result. |
| 586 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | 588 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { |
| 587 ASSERT_TRUE(RunJavascriptTest("testDone")); | 589 ASSERT_TRUE(RunJavascriptTest("testDone")); |
| 588 } | 590 } |
| OLD | NEW |