| 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 const Value& arg) { | 100 const Value& arg) { |
| 96 ConstValueVector args; | 101 ConstValueVector args; |
| 97 args.push_back(&arg); | 102 args.push_back(&arg); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 preload_test_name_ = preload_test_name; | 183 preload_test_name_ = preload_test_name; |
| 179 | 184 |
| 180 ui_test_utils::NavigateToURL(browser(), browse_to); | 185 ui_test_utils::NavigateToURL(browser(), browse_to); |
| 181 | 186 |
| 182 TestTabStripModelObserver tabstrip_observer( | 187 TestTabStripModelObserver tabstrip_observer( |
| 183 browser()->tabstrip_model(), this); | 188 browser()->tabstrip_model(), this); |
| 184 browser()->Print(); | 189 browser()->Print(); |
| 185 tabstrip_observer.WaitForObservation(); | 190 tabstrip_observer.WaitForObservation(); |
| 186 } | 191 } |
| 187 | 192 |
| 193 const char WebUIBrowserTest::kDummyURL[] = "chrome://DummyURL"; |
| 194 |
| 188 WebUIBrowserTest::WebUIBrowserTest() | 195 WebUIBrowserTest::WebUIBrowserTest() |
| 189 : test_handler_(new WebUITestHandler()), | 196 : test_handler_(new WebUITestHandler()), |
| 190 libraries_preloaded_(false) {} | 197 libraries_preloaded_(false) {} |
| 191 | 198 |
| 199 namespace { |
| 200 |
| 201 class MockWebUIProvider : public TestChromeWebUIFactory::WebUIProvider { |
| 202 public: |
| 203 MockWebUIProvider() {} |
| 204 |
| 205 // Returns a new ChromeWebUI |
| 206 WebUI* NewWebUI(TabContents* tab_contents, const GURL& url) OVERRIDE { |
| 207 return new ChromeWebUI(tab_contents); |
| 208 } |
| 209 }; |
| 210 |
| 211 base::LazyInstance<MockWebUIProvider> mock_provider_( |
| 212 base::LINKER_INITIALIZED); |
| 213 |
| 214 } // namespace |
| 215 |
| 192 void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { | 216 void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { |
| 217 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 218 TestChromeWebUIFactory::AddFactoryOverride(GURL(kDummyURL).host(), |
| 219 mock_provider_.Pointer()); |
| 220 |
| 193 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); | 221 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); |
| 194 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); | 222 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); |
| 195 | 223 |
| 196 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. | 224 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. |
| 197 FilePath resources_pack_path; | 225 FilePath resources_pack_path; |
| 198 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | 226 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 199 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); | 227 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); |
| 200 | 228 |
| 201 FilePath mockPath; | 229 FilePath mockPath; |
| 202 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); | 230 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); |
| 203 mockPath = mockPath.AppendASCII("chrome"); | 231 mockPath = mockPath.AppendASCII("chrome"); |
| 204 mockPath = mockPath.AppendASCII("third_party"); | 232 mockPath = mockPath.AppendASCII("third_party"); |
| 205 mockPath = mockPath.AppendASCII("mock4js"); | 233 mockPath = mockPath.AppendASCII("mock4js"); |
| 206 mockPath = mockPath.Append(kMockJS); | 234 mockPath = mockPath.Append(kMockJS); |
| 207 AddLibrary(mockPath); | 235 AddLibrary(mockPath); |
| 208 AddLibrary(FilePath(kWebUILibraryJS)); | 236 AddLibrary(FilePath(kWebUILibraryJS)); |
| 209 } | 237 } |
| 210 | 238 |
| 239 void WebUIBrowserTest::TearDownInProcessBrowserTestFixture() { |
| 240 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
| 241 TestChromeWebUIFactory::RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 242 } |
| 243 |
| 211 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { | 244 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { |
| 212 return NULL; | 245 return NULL; |
| 213 } | 246 } |
| 214 | 247 |
| 215 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 248 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 216 const FilePath::StringType& path) { | 249 const FilePath::StringType& path) { |
| 217 FilePath dir_test_data; | 250 FilePath dir_test_data; |
| 218 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 251 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 219 FilePath test_path(dir_test_data.AppendASCII("webui")); | 252 FilePath test_path(dir_test_data.AppendASCII("webui")); |
| 220 test_path = test_path.Append(path); | 253 test_path = test_path.Append(path); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 } | 427 } |
| 395 | 428 |
| 396 // Starts a passing test. | 429 // Starts a passing test. |
| 397 void RunTestPasses() { | 430 void RunTestPasses() { |
| 398 RunJavascriptFunction("runAsync", *Value::CreateStringValue("testPasses")); | 431 RunJavascriptFunction("runAsync", *Value::CreateStringValue("testPasses")); |
| 399 } | 432 } |
| 400 | 433 |
| 401 protected: | 434 protected: |
| 402 WebUIBrowserAsyncTest() {} | 435 WebUIBrowserAsyncTest() {} |
| 403 | 436 |
| 404 static const char kDummyURL[]; | |
| 405 | |
| 406 // Class to synchronize asynchronous javascript activity with the tests. | 437 // Class to synchronize asynchronous javascript activity with the tests. |
| 407 class AsyncWebUIMessageHandler : public WebUIMessageHandler { | 438 class AsyncWebUIMessageHandler : public WebUIMessageHandler { |
| 408 public: | 439 public: |
| 409 AsyncWebUIMessageHandler() {} | 440 AsyncWebUIMessageHandler() {} |
| 410 | 441 |
| 411 MOCK_METHOD1(HandleTestContinues, void(const ListValue*)); | 442 MOCK_METHOD1(HandleTestContinues, void(const ListValue*)); |
| 412 MOCK_METHOD1(HandleTestFails, void(const ListValue*)); | 443 MOCK_METHOD1(HandleTestFails, void(const ListValue*)); |
| 413 MOCK_METHOD1(HandleTestPasses, void(const ListValue*)); | 444 MOCK_METHOD1(HandleTestPasses, void(const ListValue*)); |
| 414 | 445 |
| 415 private: | 446 private: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 431 web_ui_->CallJavascriptFunction("runAsync", *test_name); | 462 web_ui_->CallJavascriptFunction("runAsync", *test_name); |
| 432 } | 463 } |
| 433 | 464 |
| 434 DISALLOW_COPY_AND_ASSIGN(AsyncWebUIMessageHandler); | 465 DISALLOW_COPY_AND_ASSIGN(AsyncWebUIMessageHandler); |
| 435 }; | 466 }; |
| 436 | 467 |
| 437 // Handler for this object. | 468 // Handler for this object. |
| 438 ::testing::StrictMock<AsyncWebUIMessageHandler> message_handler_; | 469 ::testing::StrictMock<AsyncWebUIMessageHandler> message_handler_; |
| 439 | 470 |
| 440 private: | 471 private: |
| 441 // Class to provide a ChromeWebUI for |kDummyURL|. | |
| 442 class MockWebUIProvider : public TestChromeWebUIFactory::WebUIProvider { | |
| 443 public: | |
| 444 MockWebUIProvider() {} | |
| 445 | |
| 446 // Returns a new ChromeWebUI | |
| 447 WebUI* NewWebUI(TabContents* tab_contents, const GURL& url) OVERRIDE { | |
| 448 return new ChromeWebUI(tab_contents); | |
| 449 } | |
| 450 }; | |
| 451 | |
| 452 // Provide this object's handler. | 472 // Provide this object's handler. |
| 453 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { | 473 virtual WebUIMessageHandler* GetMockMessageHandler() OVERRIDE { |
| 454 return &message_handler_; | 474 return &message_handler_; |
| 455 } | 475 } |
| 456 | 476 |
| 457 // Set up the kDummyURL to be a WebUI page. | |
| 458 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 459 WebUIBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 460 TestChromeWebUIFactory::AddFactoryOverride(GURL(kDummyURL).host(), | |
| 461 &mock_provider_); | |
| 462 } | |
| 463 | |
| 464 // Tear down the kDummyURL WebUI page. | |
| 465 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
| 466 WebUIBrowserTest::TearDownInProcessBrowserTestFixture(); | |
| 467 TestChromeWebUIFactory::RemoveFactoryOverride(GURL(kDummyURL).host()); | |
| 468 } | |
| 469 | |
| 470 // Set up and browse to kDummyURL for all tests. | 477 // Set up and browse to kDummyURL for all tests. |
| 471 virtual void SetUpOnMainThread() OVERRIDE { | 478 virtual void SetUpOnMainThread() OVERRIDE { |
| 472 WebUIBrowserTest::SetUpOnMainThread(); | 479 WebUIBrowserTest::SetUpOnMainThread(); |
| 473 AddLibrary(FilePath(FILE_PATH_LITERAL("async.js"))); | 480 AddLibrary(FilePath(FILE_PATH_LITERAL("async.js"))); |
| 474 ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL)); | 481 ui_test_utils::NavigateToURL(browser(), GURL(kDummyURL)); |
| 475 } | 482 } |
| 476 | 483 |
| 477 // Provider for this object. | |
| 478 MockWebUIProvider mock_provider_; | |
| 479 | |
| 480 DISALLOW_COPY_AND_ASSIGN(WebUIBrowserAsyncTest); | 484 DISALLOW_COPY_AND_ASSIGN(WebUIBrowserAsyncTest); |
| 481 }; | 485 }; |
| 482 | 486 |
| 483 const char WebUIBrowserAsyncTest::kDummyURL[] = "chrome://Dummy"; | |
| 484 | |
| 485 // Test that assertions fail immediately after assertion fails (no testContinues | 487 // Test that assertions fail immediately after assertion fails (no testContinues |
| 486 // message). (Sync version). | 488 // message). (Sync version). |
| 487 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncOkTestFail) { | 489 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestSyncOkTestFail) { |
| 488 ASSERT_FALSE(RunJavascriptTest("testFailsAssert")); | 490 ASSERT_FALSE(RunJavascriptTest("testFailsAssert")); |
| 489 } | 491 } |
| 490 | 492 |
| 491 // Test that assertions fail immediately after assertion fails (no testContinues | 493 // Test that assertions fail immediately after assertion fails (no testContinues |
| 492 // message). (Async version). | 494 // message). (Async version). |
| 493 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsAssert) { | 495 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestAsyncFailsAssert) { |
| 494 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); | 496 EXPECT_CALL(message_handler_, HandleTestFails(::testing::_)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 // testDone directly and expect pass result. | 568 // testDone directly and expect pass result. |
| 567 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | 569 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { |
| 568 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | 570 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); |
| 569 } | 571 } |
| 570 | 572 |
| 571 // Test that calling testDone during RunJavascriptTest still completes when | 573 // Test that calling testDone during RunJavascriptTest still completes when |
| 572 // waiting for async result. | 574 // waiting for async result. |
| 573 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | 575 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { |
| 574 ASSERT_TRUE(RunJavascriptTest("testDone")); | 576 ASSERT_TRUE(RunJavascriptTest("testDone")); |
| 575 } | 577 } |
| OLD | NEW |