Chromium Code Reviews| 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" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_navigator.h" | 14 #include "chrome/browser/ui/browser_navigator.h" |
| 15 #include "chrome/browser/ui/webui/chrome_web_ui.h" | 15 #include "chrome/browser/ui/webui/chrome_web_ui.h" |
| 16 #include "chrome/browser/ui/webui/test_chrome_web_ui_factory.h" | 16 #include "chrome/browser/ui/webui/test_chrome_web_ui_factory.h" |
| 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | |
| 18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 19 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
| 20 #include "chrome/test/base/test_tab_strip_model_observer.h" | 21 #include "chrome/test/base/test_tab_strip_model_observer.h" |
| 21 #include "chrome/test/base/ui_test_utils.h" | 22 #include "chrome/test/base/ui_test_utils.h" |
| 22 #include "content/browser/tab_contents/tab_contents.h" | 23 #include "content/browser/tab_contents/tab_contents.h" |
| 24 #include "content/common/content_notification_types.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest-spi.h" | 26 #include "testing/gtest/include/gtest/gtest-spi.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 const FilePath::CharType kMockJS[] = FILE_PATH_LITERAL("mock4js.js"); | 31 const FilePath::CharType kMockJS[] = FILE_PATH_LITERAL("mock4js.js"); |
| 30 const FilePath::CharType kWebUILibraryJS[] = FILE_PATH_LITERAL("test_api.js"); | 32 const FilePath::CharType kWebUILibraryJS[] = FILE_PATH_LITERAL("test_api.js"); |
| 31 const FilePath::CharType kWebUITestFolder[] = FILE_PATH_LITERAL("webui"); | 33 const FilePath::CharType kWebUITestFolder[] = FILE_PATH_LITERAL("webui"); |
| 32 base::LazyInstance<std::vector<std::string> > error_messages_( | 34 base::LazyInstance<std::vector<std::string> > error_messages_( |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 TestTabStripModelObserver tabstrip_observer( | 202 TestTabStripModelObserver tabstrip_observer( |
| 201 browser()->tabstrip_model(), this); | 203 browser()->tabstrip_model(), this); |
| 202 browser()->Print(); | 204 browser()->Print(); |
| 203 tabstrip_observer.WaitForObservation(); | 205 tabstrip_observer.WaitForObservation(); |
| 204 } | 206 } |
| 205 | 207 |
| 206 const char WebUIBrowserTest::kDummyURL[] = "chrome://DummyURL"; | 208 const char WebUIBrowserTest::kDummyURL[] = "chrome://DummyURL"; |
| 207 | 209 |
| 208 WebUIBrowserTest::WebUIBrowserTest() | 210 WebUIBrowserTest::WebUIBrowserTest() |
| 209 : test_handler_(new WebUITestHandler()), | 211 : test_handler_(new WebUITestHandler()), |
| 210 libraries_preloaded_(false) {} | 212 libraries_preloaded_(false), |
| 213 web_ui_instance_(NULL), | |
| 214 web_ui_instance_ready_(false), | |
| 215 message_loop_running_(false) {} | |
| 211 | 216 |
| 212 namespace { | 217 namespace { |
| 213 | 218 |
| 214 class MockWebUIProvider : public TestChromeWebUIFactory::WebUIProvider { | 219 class MockWebUIProvider : public TestChromeWebUIFactory::WebUIProvider { |
| 215 public: | 220 public: |
| 216 MockWebUIProvider() {} | 221 MockWebUIProvider() {} |
| 217 | 222 |
| 218 // Returns a new ChromeWebUI | 223 // Returns a new ChromeWebUI |
| 219 WebUI* NewWebUI(TabContents* tab_contents, const GURL& url) OVERRIDE { | 224 WebUI* NewWebUI(TabContents* tab_contents, const GURL& url) OVERRIDE { |
| 220 return new ChromeWebUI(tab_contents); | 225 return new ChromeWebUI(tab_contents); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 242 FilePath mockPath; | 247 FilePath mockPath; |
| 243 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); | 248 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); |
| 244 mockPath = mockPath.AppendASCII("chrome"); | 249 mockPath = mockPath.AppendASCII("chrome"); |
| 245 mockPath = mockPath.AppendASCII("third_party"); | 250 mockPath = mockPath.AppendASCII("third_party"); |
| 246 mockPath = mockPath.AppendASCII("mock4js"); | 251 mockPath = mockPath.AppendASCII("mock4js"); |
| 247 mockPath = mockPath.Append(kMockJS); | 252 mockPath = mockPath.Append(kMockJS); |
| 248 AddLibrary(mockPath); | 253 AddLibrary(mockPath); |
| 249 AddLibrary(FilePath(kWebUILibraryJS)); | 254 AddLibrary(FilePath(kWebUILibraryJS)); |
| 250 } | 255 } |
| 251 | 256 |
| 257 void WebUIBrowserTest::ObserveNextHtmlDialog() { | |
| 258 registrar_.Add(this, chrome::NOTIFICATION_HTML_DIALOG_SHOWN, | |
| 259 NotificationService::AllSources()); | |
| 260 } | |
| 261 | |
| 252 void WebUIBrowserTest::TearDownInProcessBrowserTestFixture() { | 262 void WebUIBrowserTest::TearDownInProcessBrowserTestFixture() { |
| 253 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); | 263 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
| 254 TestChromeWebUIFactory::RemoveFactoryOverride(GURL(kDummyURL).host()); | 264 TestChromeWebUIFactory::RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 255 } | 265 } |
| 256 | 266 |
| 257 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { | 267 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { |
| 258 return NULL; | 268 return NULL; |
| 259 } | 269 } |
| 260 | 270 |
| 261 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 271 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 262 const FilePath::StringType& path) { | 272 const FilePath::StringType& path) { |
| 263 FilePath dir_test_data; | 273 FilePath dir_test_data; |
| 264 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 274 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 265 FilePath test_path(dir_test_data.AppendASCII("webui")); | 275 FilePath test_path(dir_test_data.AppendASCII("webui")); |
| 266 test_path = test_path.Append(path); | 276 test_path = test_path.Append(path); |
| 267 EXPECT_TRUE(file_util::PathExists(test_path)); | 277 EXPECT_TRUE(file_util::PathExists(test_path)); |
| 268 return net::FilePathToFileURL(test_path); | 278 return net::FilePathToFileURL(test_path); |
| 269 } | 279 } |
| 270 | 280 |
| 271 void WebUIBrowserTest::OnJsInjectionReady(RenderViewHost* render_view_host) { | 281 void WebUIBrowserTest::OnJsInjectionReady(RenderViewHost* render_view_host) { |
| 272 PreLoadJavascriptLibraries(preload_test_fixture_, preload_test_name_, | 282 PreLoadJavascriptLibraries(preload_test_fixture_, preload_test_name_, |
| 273 render_view_host); | 283 render_view_host); |
| 274 } | 284 } |
| 275 | 285 |
| 286 void WebUIBrowserTest::Observe(int type, | |
|
Sheridan Rawlins
2011/09/09 15:47:50
I would prefer to see this and other logic broken
flackr
2011/09/15 18:41:52
Done.
| |
| 287 const NotificationSource& source, | |
| 288 const NotificationDetails& details) { | |
| 289 if (type == chrome::NOTIFICATION_HTML_DIALOG_SHOWN) { | |
|
Sheridan Rawlins
2011/09/09 15:47:50
Even though only 2 cases, this looks like a switch
flackr
2011/09/15 18:41:52
Done.
| |
| 290 web_ui_instance_ = Source<WebUI>(source).ptr(); | |
| 291 registrar_.Remove(this, chrome::NOTIFICATION_HTML_DIALOG_SHOWN, | |
| 292 NotificationService::AllSources()); | |
| 293 // Wait for navigation to complete. | |
| 294 web_ui_instance_ready_ = false; | |
| 295 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | |
|
Paweł Hajdan Jr.
2011/09/09 22:13:55
Is this possibly racy?
flackr
2011/09/15 18:41:52
I don't so, but I'm not certain. The first notific
| |
| 296 Source<NavigationController>( | |
| 297 &web_ui_instance_->tab_contents()->controller())); | |
| 298 } else if (type == content::NOTIFICATION_LOAD_STOP) { | |
| 299 registrar_.Remove(this, content::NOTIFICATION_LOAD_STOP, | |
| 300 Source<NavigationController>( | |
| 301 &web_ui_instance_->tab_contents()->controller())); | |
| 302 web_ui_instance_ready_ = true; | |
| 303 if (message_loop_running_) { | |
| 304 message_loop_running_ = false; | |
| 305 MessageLoopForUI::current()->Quit(); | |
| 306 } | |
| 307 } else { | |
| 308 NOTREACHED() << "Unhandled notification type"; | |
|
Sheridan Rawlins
2011/09/09 15:47:50
Don't bloat with message that doesn't add info (yo
flackr
2011/09/15 18:41:52
Done.
| |
| 309 } | |
| 310 } | |
| 311 | |
| 312 void WebUIBrowserTest::WaitForHtmlDialogLoad() { | |
| 313 if (!web_ui_instance_ready_) { | |
| 314 message_loop_running_ = true; | |
| 315 ui_test_utils::RunMessageLoop(); | |
| 316 } | |
| 317 } | |
| 318 | |
| 276 void WebUIBrowserTest::BuildJavascriptLibraries(string16* content) { | 319 void WebUIBrowserTest::BuildJavascriptLibraries(string16* content) { |
| 277 ASSERT_TRUE(content != NULL); | 320 ASSERT_TRUE(content != NULL); |
| 278 std::string utf8_content; | 321 std::string utf8_content; |
| 279 std::vector<FilePath>::iterator user_libraries_iterator; | 322 std::vector<FilePath>::iterator user_libraries_iterator; |
| 280 for (user_libraries_iterator = user_libraries_.begin(); | 323 for (user_libraries_iterator = user_libraries_.begin(); |
| 281 user_libraries_iterator != user_libraries_.end(); | 324 user_libraries_iterator != user_libraries_.end(); |
| 282 ++user_libraries_iterator) { | 325 ++user_libraries_iterator) { |
| 283 std::string library_content; | 326 std::string library_content; |
| 284 if (user_libraries_iterator->IsAbsolute()) { | 327 if (user_libraries_iterator->IsAbsolute()) { |
| 285 ASSERT_TRUE(file_util::ReadFileToString(*user_libraries_iterator, | 328 ASSERT_TRUE(file_util::ReadFileToString(*user_libraries_iterator, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 | 399 |
| 357 if (error_messages_.Get().size() > 0) { | 400 if (error_messages_.Get().size() > 0) { |
| 358 LOG(ERROR) << "Encountered javascript console error(s)"; | 401 LOG(ERROR) << "Encountered javascript console error(s)"; |
| 359 result = false; | 402 result = false; |
| 360 error_messages_.Get().clear(); | 403 error_messages_.Get().clear(); |
| 361 } | 404 } |
| 362 return result; | 405 return result; |
| 363 } | 406 } |
| 364 | 407 |
| 365 void WebUIBrowserTest::SetupHandlers() { | 408 void WebUIBrowserTest::SetupHandlers() { |
| 366 WebUI* web_ui_instance = | 409 WebUI* web_ui_instance = web_ui_instance_ ? web_ui_instance_ : |
| 367 browser()->GetSelectedTabContents()->web_ui(); | 410 browser()->GetSelectedTabContents()->web_ui(); |
| 368 ASSERT_TRUE(web_ui_instance != NULL); | 411 ASSERT_TRUE(web_ui_instance != NULL); |
| 369 web_ui_instance->register_callback_overwrites(true); | 412 web_ui_instance->register_callback_overwrites(true); |
| 370 test_handler_->Attach(web_ui_instance); | 413 test_handler_->Attach(web_ui_instance); |
| 371 | 414 |
| 372 if (GetMockMessageHandler()) | 415 if (GetMockMessageHandler()) |
| 373 GetMockMessageHandler()->Attach(web_ui_instance); | 416 GetMockMessageHandler()->Attach(web_ui_instance); |
| 374 } | 417 } |
| 375 | 418 |
| 376 // According to the interface for EXPECT_FATAL_FAILURE | 419 // According to the interface for EXPECT_FATAL_FAILURE |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 // testDone directly and expect pass result. | 624 // testDone directly and expect pass result. |
| 582 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | 625 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { |
| 583 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | 626 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); |
| 584 } | 627 } |
| 585 | 628 |
| 586 // Test that calling testDone during RunJavascriptTest still completes when | 629 // Test that calling testDone during RunJavascriptTest still completes when |
| 587 // waiting for async result. | 630 // waiting for async result. |
| 588 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | 631 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { |
| 589 ASSERT_TRUE(RunJavascriptTest("testDone")); | 632 ASSERT_TRUE(RunJavascriptTest("testDone")); |
| 590 } | 633 } |
| OLD | NEW |