| 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/ui_test_utils.h" | 5 #include "chrome/test/base/ui_test_utils.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 #include "ui/gfx/size.h" | 61 #include "ui/gfx/size.h" |
| 62 | 62 |
| 63 #if defined(TOOLKIT_VIEWS) | 63 #if defined(TOOLKIT_VIEWS) |
| 64 #include "ui/views/focus/accelerator_handler.h" | 64 #include "ui/views/focus/accelerator_handler.h" |
| 65 #endif | 65 #endif |
| 66 | 66 |
| 67 #if defined(USE_AURA) | 67 #if defined(USE_AURA) |
| 68 #include "ui/aura/root_window.h" | 68 #include "ui/aura/root_window.h" |
| 69 #endif | 69 #endif |
| 70 | 70 |
| 71 using content::NavigationController; |
| 71 using content::NavigationEntry; | 72 using content::NavigationEntry; |
| 72 using content::OpenURLParams; | 73 using content::OpenURLParams; |
| 73 using content::Referrer; | 74 using content::Referrer; |
| 74 using content::WebContents; | 75 using content::WebContents; |
| 75 | 76 |
| 76 static const int kDefaultWsPort = 8880; | 77 static const int kDefaultWsPort = 8880; |
| 77 | 78 |
| 78 namespace ui_test_utils { | 79 namespace ui_test_utils { |
| 79 | 80 |
| 80 namespace { | 81 namespace { |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 WebContents* web_contents = browser->GetSelectedWebContents(); | 308 WebContents* web_contents = browser->GetSelectedWebContents(); |
| 308 if (!web_contents) | 309 if (!web_contents) |
| 309 return false; | 310 return false; |
| 310 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); | 311 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); |
| 311 if (!last_entry) | 312 if (!last_entry) |
| 312 return false; | 313 return false; |
| 313 title->assign(last_entry->GetTitleForDisplay("")); | 314 title->assign(last_entry->GetTitleForDisplay("")); |
| 314 return true; | 315 return true; |
| 315 } | 316 } |
| 316 | 317 |
| 317 void WaitForNavigations(content::NavigationController* controller, | 318 void WaitForNavigations(NavigationController* controller, |
| 318 int number_of_navigations) { | 319 int number_of_navigations) { |
| 319 TestNavigationObserver observer( | 320 TestNavigationObserver observer( |
| 320 content::Source<content::NavigationController>(controller), NULL, | 321 content::Source<NavigationController>(controller), NULL, |
| 321 number_of_navigations); | 322 number_of_navigations); |
| 322 observer.WaitForObservation( | 323 observer.WaitForObservation( |
| 323 base::Bind(&ui_test_utils::RunMessageLoop), | 324 base::Bind(&ui_test_utils::RunMessageLoop), |
| 324 base::Bind(&MessageLoop::Quit, | 325 base::Bind(&MessageLoop::Quit, |
| 325 base::Unretained(MessageLoopForUI::current()))); | 326 base::Unretained(MessageLoopForUI::current()))); |
| 326 } | 327 } |
| 327 | 328 |
| 328 void WaitForNewTab(Browser* browser) { | 329 void WaitForNewTab(Browser* browser) { |
| 329 TestNotificationObserver observer; | 330 TestNotificationObserver observer; |
| 330 RegisterAndWait(&observer, content::NOTIFICATION_TAB_ADDED, | 331 RegisterAndWait(&observer, content::NOTIFICATION_TAB_ADDED, |
| 331 content::Source<content::WebContentsDelegate>(browser)); | 332 content::Source<content::WebContentsDelegate>(browser)); |
| 332 } | 333 } |
| 333 | 334 |
| 334 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { | 335 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { |
| 335 TestNotificationObserver observer; | 336 TestNotificationObserver observer; |
| 336 RegisterAndWait(&observer, | 337 RegisterAndWait(&observer, |
| 337 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 338 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 338 content::Source<ExtensionAction>(browser_action)); | 339 content::Source<ExtensionAction>(browser_action)); |
| 339 } | 340 } |
| 340 | 341 |
| 341 void WaitForLoadStop(WebContents* tab) { | 342 void WaitForLoadStop(WebContents* tab) { |
| 342 WindowedNotificationObserver load_stop_observer( | 343 WindowedNotificationObserver load_stop_observer( |
| 343 content::NOTIFICATION_LOAD_STOP, | 344 content::NOTIFICATION_LOAD_STOP, |
| 344 content::Source<content::NavigationController>(&tab->GetController())); | 345 content::Source<NavigationController>(&tab->GetController())); |
| 345 // In many cases, the load may have finished before we get here. Only wait if | 346 // In many cases, the load may have finished before we get here. Only wait if |
| 346 // the tab still has a pending navigation. | 347 // the tab still has a pending navigation. |
| 347 if (!tab->IsLoading()) | 348 if (!tab->IsLoading()) |
| 348 return; | 349 return; |
| 349 load_stop_observer.Wait(); | 350 load_stop_observer.Wait(); |
| 350 } | 351 } |
| 351 | 352 |
| 352 Browser* WaitForNewBrowser() { | 353 Browser* WaitForNewBrowser() { |
| 353 TestNotificationObserver observer; | 354 TestNotificationObserver observer; |
| 354 RegisterAndWait(&observer, chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 355 RegisterAndWait(&observer, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 // |disposition| indicates what tab the download occurs in, and | 396 // |disposition| indicates what tab the download occurs in, and |
| 396 // |browser_test_flags| controls what to wait for before continuing. | 397 // |browser_test_flags| controls what to wait for before continuing. |
| 397 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( | 398 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( |
| 398 Browser* browser, | 399 Browser* browser, |
| 399 const GURL& url, | 400 const GURL& url, |
| 400 int number_of_navigations, | 401 int number_of_navigations, |
| 401 WindowOpenDisposition disposition, | 402 WindowOpenDisposition disposition, |
| 402 int browser_test_flags) { | 403 int browser_test_flags) { |
| 403 if (disposition == CURRENT_TAB && browser->GetSelectedWebContents()) | 404 if (disposition == CURRENT_TAB && browser->GetSelectedWebContents()) |
| 404 WaitForLoadStop(browser->GetSelectedWebContents()); | 405 WaitForLoadStop(browser->GetSelectedWebContents()); |
| 405 content::NavigationController* controller = | 406 NavigationController* controller = |
| 406 browser->GetSelectedWebContents() ? | 407 browser->GetSelectedWebContents() ? |
| 407 &browser->GetSelectedWebContents()->GetController() : NULL; | 408 &browser->GetSelectedWebContents()->GetController() : NULL; |
| 408 TestNavigationObserver same_tab_observer( | 409 TestNavigationObserver same_tab_observer( |
| 409 content::Source<content::NavigationController>(controller), | 410 content::Source<NavigationController>(controller), |
| 410 NULL, | 411 NULL, |
| 411 number_of_navigations); | 412 number_of_navigations); |
| 412 | 413 |
| 413 std::set<Browser*> initial_browsers; | 414 std::set<Browser*> initial_browsers; |
| 414 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); | 415 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); |
| 415 iter != BrowserList::end(); | 416 iter != BrowserList::end(); |
| 416 ++iter) { | 417 ++iter) { |
| 417 initial_browsers.insert(*iter); | 418 initial_browsers.insert(*iter); |
| 418 } | 419 } |
| 419 | 420 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 445 // The currently selected tab is the right one. | 446 // The currently selected tab is the right one. |
| 446 web_contents = browser->GetSelectedWebContents(); | 447 web_contents = browser->GetSelectedWebContents(); |
| 447 } | 448 } |
| 448 if (disposition == CURRENT_TAB) { | 449 if (disposition == CURRENT_TAB) { |
| 449 same_tab_observer.WaitForObservation( | 450 same_tab_observer.WaitForObservation( |
| 450 base::Bind(&ui_test_utils::RunMessageLoop), | 451 base::Bind(&ui_test_utils::RunMessageLoop), |
| 451 base::Bind(&MessageLoop::Quit, | 452 base::Bind(&MessageLoop::Quit, |
| 452 base::Unretained(MessageLoopForUI::current()))); | 453 base::Unretained(MessageLoopForUI::current()))); |
| 453 return; | 454 return; |
| 454 } else if (web_contents) { | 455 } else if (web_contents) { |
| 455 content::NavigationController* controller = &web_contents->GetController(); | 456 NavigationController* controller = &web_contents->GetController(); |
| 456 WaitForNavigations(controller, number_of_navigations); | 457 WaitForNavigations(controller, number_of_navigations); |
| 457 return; | 458 return; |
| 458 } | 459 } |
| 459 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" | 460 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" |
| 460 << url.spec() << "\"" | 461 << url.spec() << "\"" |
| 461 << " because we can't get the tab contents"; | 462 << " because we can't get the tab contents"; |
| 462 } | 463 } |
| 463 | 464 |
| 464 void NavigateToURLWithDisposition(Browser* browser, | 465 void NavigateToURLWithDisposition(Browser* browser, |
| 465 const GURL& url, | 466 const GURL& url, |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 924 content::Source<WebContents>(web_contents)); | 925 content::Source<WebContents>(web_contents)); |
| 925 | 926 |
| 926 // When navigating through the history, the restored NavigationEntry's title | 927 // When navigating through the history, the restored NavigationEntry's title |
| 927 // will be used. If the entry ends up having the same title after we return | 928 // will be used. If the entry ends up having the same title after we return |
| 928 // to it, as will usually be the case, the | 929 // to it, as will usually be the case, the |
| 929 // NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED will then be suppressed, since the | 930 // NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED will then be suppressed, since the |
| 930 // NavigationEntry's title hasn't changed. | 931 // NavigationEntry's title hasn't changed. |
| 931 notification_registrar_.Add( | 932 notification_registrar_.Add( |
| 932 this, | 933 this, |
| 933 content::NOTIFICATION_LOAD_STOP, | 934 content::NOTIFICATION_LOAD_STOP, |
| 934 content::Source<content::NavigationController>( | 935 content::Source<NavigationController>(&web_contents->GetController())); |
| 935 &web_contents->GetController())); | |
| 936 } | 936 } |
| 937 | 937 |
| 938 void TitleWatcher::AlsoWaitForTitle(const string16& expected_title) { | 938 void TitleWatcher::AlsoWaitForTitle(const string16& expected_title) { |
| 939 expected_titles_.push_back(expected_title); | 939 expected_titles_.push_back(expected_title); |
| 940 } | 940 } |
| 941 | 941 |
| 942 TitleWatcher::~TitleWatcher() { | 942 TitleWatcher::~TitleWatcher() { |
| 943 } | 943 } |
| 944 | 944 |
| 945 const string16& TitleWatcher::WaitAndGetTitle() { | 945 const string16& TitleWatcher::WaitAndGetTitle() { |
| 946 if (expected_title_observed_) | 946 if (expected_title_observed_) |
| 947 return observed_title_; | 947 return observed_title_; |
| 948 quit_loop_on_observation_ = true; | 948 quit_loop_on_observation_ = true; |
| 949 ui_test_utils::RunMessageLoop(); | 949 ui_test_utils::RunMessageLoop(); |
| 950 return observed_title_; | 950 return observed_title_; |
| 951 } | 951 } |
| 952 | 952 |
| 953 void TitleWatcher::Observe(int type, | 953 void TitleWatcher::Observe(int type, |
| 954 const content::NotificationSource& source, | 954 const content::NotificationSource& source, |
| 955 const content::NotificationDetails& details) { | 955 const content::NotificationDetails& details) { |
| 956 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { | 956 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { |
| 957 WebContents* source_contents = content::Source<WebContents>(source).ptr(); | 957 WebContents* source_contents = content::Source<WebContents>(source).ptr(); |
| 958 ASSERT_EQ(web_contents_, source_contents); | 958 ASSERT_EQ(web_contents_, source_contents); |
| 959 } else if (type == content::NOTIFICATION_LOAD_STOP) { | 959 } else if (type == content::NOTIFICATION_LOAD_STOP) { |
| 960 content::NavigationController* controller = | 960 NavigationController* controller = |
| 961 content::Source<content::NavigationController>(source).ptr(); | 961 content::Source<NavigationController>(source).ptr(); |
| 962 ASSERT_EQ(&web_contents_->GetController(), controller); | 962 ASSERT_EQ(&web_contents_->GetController(), controller); |
| 963 } else { | 963 } else { |
| 964 FAIL() << "Unexpected notification received."; | 964 FAIL() << "Unexpected notification received."; |
| 965 } | 965 } |
| 966 | 966 |
| 967 std::vector<string16>::const_iterator it = | 967 std::vector<string16>::const_iterator it = |
| 968 std::find(expected_titles_.begin(), | 968 std::find(expected_titles_.begin(), |
| 969 expected_titles_.end(), | 969 expected_titles_.end(), |
| 970 web_contents_->GetTitle()); | 970 web_contents_->GetTitle()); |
| 971 if (it == expected_titles_.end()) | 971 if (it == expected_titles_.end()) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); | 1081 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); |
| 1082 } | 1082 } |
| 1083 | 1083 |
| 1084 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { | 1084 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { |
| 1085 DCHECK(bitmap); | 1085 DCHECK(bitmap); |
| 1086 SnapshotTaker taker; | 1086 SnapshotTaker taker; |
| 1087 return taker.TakeEntirePageSnapshot(rvh, bitmap); | 1087 return taker.TakeEntirePageSnapshot(rvh, bitmap); |
| 1088 } | 1088 } |
| 1089 | 1089 |
| 1090 } // namespace ui_test_utils | 1090 } // namespace ui_test_utils |
| OLD | NEW |