diff options
author | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-28 22:28:30 +0000 |
---|---|---|
committer | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-28 22:28:30 +0000 |
commit | a6e602f4041198222584e5f449f145d0b886dba5 (patch) | |
tree | af3ac167919a62663e53f52453f98a973edac9f5 | |
parent | 25da2f324b3ff67bde74b4d339429c582e54147a (diff) | |
download | chromium_src-a6e602f4041198222584e5f449f145d0b886dba5.zip chromium_src-a6e602f4041198222584e5f449f145d0b886dba5.tar.gz chromium_src-a6e602f4041198222584e5f449f145d0b886dba5.tar.bz2 |
Refactoring ui_test_utils code
BUG=none
TEST=all tests that used to pass should continue to pass
Review URL: http://codereview.chromium.org/3402030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60858 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc | 27 | ||||
-rw-r--r-- | chrome/browser/browser_focus_uitest.cc | 197 | ||||
-rw-r--r-- | chrome/browser/browser_keyevents_browsertest.cc | 65 | ||||
-rw-r--r-- | chrome/browser/ssl/ssl_browser_tests.cc | 15 | ||||
-rw-r--r-- | chrome/browser/views/find_bar_host_interactive_uitest.cc | 91 | ||||
-rw-r--r-- | chrome/common/notification_source.h | 2 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.cc | 98 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 108 |
8 files changed, 279 insertions, 324 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc b/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc index 5ae42b7..16d0d56 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc @@ -6,8 +6,6 @@ #include "app/keyboard_codes.h" #include "base/message_loop.h" -#include "base/platform_thread.h" -#include "base/ref_counted.h" #include "base/string16.h" #include "base/string_util.h" #include "base/time.h" @@ -17,7 +15,6 @@ #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/autocomplete/autocomplete_edit_view.h" -#include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_window.h" @@ -105,13 +102,6 @@ class AutocompleteEditViewTest : public InProcessBrowserTest, set_show_window(true); } - void GetNativeWindow(gfx::NativeWindow* native_window) { - BrowserWindow* window = browser()->window(); - ASSERT_TRUE(window); - *native_window = window->GetNativeHandle(); - ASSERT_TRUE(*native_window); - } - void GetAutocompleteEditView(AutocompleteEditView** edit_view) { BrowserWindow* window = browser()->window(); ASSERT_TRUE(window); @@ -122,11 +112,9 @@ class AutocompleteEditViewTest : public InProcessBrowserTest, } void SendKey(app::KeyboardCode key, bool control, bool shift, bool alt) { - gfx::NativeWindow window = NULL; - ASSERT_NO_FATAL_FAILURE(GetNativeWindow(&window)); ASSERT_TRUE( ui_test_utils::SendKeyPressSync( - window, key, control, shift, alt, false /* command */)); + browser(), key, control, shift, alt, false /* command */)); } void SendKeySequence(const wchar_t* keys) { @@ -267,19 +255,6 @@ class AutocompleteEditViewTest : public InProcessBrowserTest, } MessageLoopForUI::current()->Quit(); } - -#if defined(OS_WIN) - virtual void CleanUpOnMainThread() { - // A hack to avoid hitting issue http://crbug.com/18372 and - // http://crbug.com/18373 - // Weird that it only happens on Windows. - // TODO(suzhe): Remove this hack as soon as these bugs are fixed. - MessageLoop::current()->PostDelayedTask(FROM_HERE, - new MessageLoop::QuitTask(), - 2000); - ui_test_utils::RunMessageLoop(); - } -#endif }; // Test if ctrl-* accelerators are workable in omnibox. diff --git a/chrome/browser/browser_focus_uitest.cc b/chrome/browser/browser_focus_uitest.cc index 03824b9..90a3b19 100644 --- a/chrome/browser/browser_focus_uitest.cc +++ b/chrome/browser/browser_focus_uitest.cc @@ -6,13 +6,10 @@ #include "base/file_util.h" #include "base/format_macros.h" -#include "base/logging.h" #include "base/message_loop.h" #include "base/path_service.h" -#include "base/ref_counted.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_window.h" #include "chrome/browser/renderer_host/render_view_host.h" @@ -85,19 +82,12 @@ class BrowserFocusTest : public InProcessBrowserTest { void ClickOnView(ViewID vid) { ui_test_utils::ClickOnView(browser(), vid); } - - void BringBrowserWindowToFront() { - ui_test_utils::ShowAndFocusNativeWindow( - browser()->window()->GetNativeHandle()); - } }; class TestInterstitialPage : public InterstitialPage { public: TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) - : InterstitialPage(tab, new_navigation, url), - waiting_for_dom_response_(false), - waiting_for_focus_change_(false) { + : InterstitialPage(tab, new_navigation, url) { FilePath file_path; bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); EXPECT_TRUE(r); @@ -111,61 +101,30 @@ class TestInterstitialPage : public InterstitialPage { return html_contents_; } - virtual void DomOperationResponse(const std::string& json_string, - int automation_id) { - if (waiting_for_dom_response_) { - dom_response_ = json_string; - waiting_for_dom_response_ = false; - MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); - return; - } - InterstitialPage::DomOperationResponse(json_string, automation_id); - } - - std::string GetFocusedElement() { - std::wstring script = L"window.domAutomationController.setAutomationId(0);" - L"window.domAutomationController.send(getFocusedElement());"; - - render_view_host()->ExecuteJavascriptInWebFrame(L"", script); - DCHECK(!waiting_for_dom_response_); - waiting_for_dom_response_ = true; - ui_test_utils::RunMessageLoop(); - // Remove the JSON extra quotes. - if (dom_response_.size() >= 2 && dom_response_[0] == '"' && - dom_response_[dom_response_.size() - 1] == '"') { - dom_response_ = dom_response_.substr(1, dom_response_.size() - 2); - } - return dom_response_; + // Exposing render_view_host() to be public; it is declared as protected in + // the superclass. + virtual RenderViewHost* render_view_host() { + return InterstitialPage::render_view_host(); } bool HasFocus() { return render_view_host()->view()->HasFocus(); } - void WaitForFocusChange() { - waiting_for_focus_change_ = true; - ui_test_utils::RunMessageLoop(); - } - protected: virtual void FocusedNodeChanged() { - if (!waiting_for_focus_change_) - return; - - waiting_for_focus_change_= false; - MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); + NotificationService::current()->Notify( + NotificationType::FOCUS_CHANGED_IN_PAGE, + Source<RenderViewHost>(render_view_host()), + NotificationService::NoDetails()); } private: std::string html_contents_; - - bool waiting_for_dom_response_; - bool waiting_for_focus_change_; - std::string dom_response_; }; IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); #if defined(USE_X11) || defined(OS_MACOSX) // It seems we have to wait a little bit for the widgets to spin up before // we can start clicking on them. @@ -185,7 +144,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { } IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -249,7 +208,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { // Tabs remember focus. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -295,7 +254,6 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { ASSERT_TRUE(IsViewFocused(vid)); } - gfx::NativeWindow window = browser()->window()->GetNativeHandle(); browser()->SelectTabContentsAt(0, true); // Try the above, but with ctrl+tab. Since tab normally changes focus, // this has regressed in the past. Loop through several times to be sure. @@ -305,7 +263,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { ASSERT_TRUE(IsViewFocused(vid)); ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_TAB, true, false, false, false)); + browser(), app::VKEY_TAB, true, false, false, false)); } // As above, but with ctrl+shift+tab. @@ -316,14 +274,14 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { ASSERT_TRUE(IsViewFocused(vid)); ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_TAB, true, true, false, false)); + browser(), app::VKEY_TAB, true, true, false, false)); } } } // Tabs remember focus with find-in-page box. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -365,7 +323,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { // Background window does not steal focus. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -421,7 +379,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { // Page cannot steal focus when focus is on location bar. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // Open the page that steals focus. @@ -443,7 +401,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { // at which point this test would fail (see comment in // RenderWidget::didFocus()). IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -459,8 +417,6 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { "gmapLink" }; - gfx::NativeWindow window = browser()->window()->GetNativeHandle(); - // Test forward focus traversal. for (int i = 0; i < 3; ++i) { SCOPED_TRACE(StringPrintf("outer loop: %d", i)); @@ -479,18 +435,22 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { &actual)); ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); - ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, - false, false, false, false)); - + NotificationType::Type notification_type; + NotificationSource notification_source = + NotificationService::AllSources(); if (j < arraysize(kExpElementIDs) - 1) { - ASSERT_NO_FATAL_FAILURE( - ui_test_utils::WaitForFocusChange( - browser()->GetSelectedTabContents()->render_view_host())); + notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; + notification_source = Source<RenderViewHost>( + browser()->GetSelectedTabContents()->render_view_host()); } else { // On the last tab key press, the focus returns to the browser. - ASSERT_NO_FATAL_FAILURE( - ui_test_utils::WaitForFocusInBrowser(browser())); + notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; + notification_source = Source<Browser>(browser()); } + + ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( + browser(), app::VKEY_TAB, false, false, false, false, + notification_type, notification_source)); } // At this point the renderer has sent us a message asking to advance the @@ -506,21 +466,26 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); // Now let's press shift-tab to move the focus in reverse. - for (size_t j = 0; j < 7; ++j) { + for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); - ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, - false, true, false, false)); + NotificationType::Type notification_type; + NotificationSource notification_source = + NotificationService::AllSources(); if (j < arraysize(kExpElementIDs) - 1) { - ASSERT_NO_FATAL_FAILURE( - ui_test_utils::WaitForFocusChange( - browser()->GetSelectedTabContents()->render_view_host())); + notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; + notification_source = Source<RenderViewHost>( + browser()->GetSelectedTabContents()->render_view_host()); } else { // On the last tab key press, the focus returns to the browser. - ASSERT_NO_FATAL_FAILURE( - ui_test_utils::WaitForFocusInBrowser(browser())); + notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; + notification_source = Source<Browser>(browser()); } + ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( + browser(), app::VKEY_TAB, false, true, false, false, + notification_type, notification_source)); + // Let's make sure the focus is on the expected element in the page. std::string actual; ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( @@ -540,7 +505,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { // Focus traversal while an interstitial is showing. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -570,8 +535,6 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { "gmapLink" }; - gfx::NativeWindow window = browser()->window()->GetNativeHandle(); - // Test forward focus traversal. for (int i = 0; i < 2; ++i) { // Location bar should be focused. @@ -580,19 +543,29 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { // Now let's press tab to move the focus. for (size_t j = 0; j < 7; ++j) { // Let's make sure the focus is on the expected element in the page. - std::string actual = interstitial_page->GetFocusedElement(); + std::string actual; + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( + interstitial_page->render_view_host(), L"", + L"window.domAutomationController.send(getFocusedElement());", + &actual)); ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); - ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, - false, false, false, false)); - + NotificationType::Type notification_type; + NotificationSource notification_source = + NotificationService::AllSources(); if (j < arraysize(kExpElementIDs) - 1) { - ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); + notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; + notification_source = Source<RenderViewHost>( + interstitial_page->render_view_host()); } else { // On the last tab key press, the focus returns to the browser. - ASSERT_NO_FATAL_FAILURE( - ui_test_utils::WaitForFocusInBrowser(browser())); + notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; + notification_source = Source<Browser>(browser()); } + + ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( + browser(), app::VKEY_TAB, false, false, false, false, + notification_type, notification_source)); } // At this point the renderer has sent us a message asking to advance the @@ -608,19 +581,29 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { // Now let's press shift-tab to move the focus in reverse. for (size_t j = 0; j < 7; ++j) { - ASSERT_TRUE(ui_controls::SendKeyPress(window, app::VKEY_TAB, - false, true, false, false)); - + NotificationType::Type notification_type; + NotificationSource notification_source = + NotificationService::AllSources(); if (j < arraysize(kExpElementIDs) - 1) { - ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); + notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; + notification_source = Source<RenderViewHost>( + interstitial_page->render_view_host()); } else { // On the last tab key press, the focus returns to the browser. - ASSERT_NO_FATAL_FAILURE( - ui_test_utils::WaitForFocusInBrowser(browser())); + notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; + notification_source = Source<Browser>(browser()); } + ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( + browser(), app::VKEY_TAB, false, true, false, false, + notification_type, notification_source)); + // Let's make sure the focus is on the expected element in the page. - std::string actual = interstitial_page->GetFocusedElement(); + std::string actual; + ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( + interstitial_page->render_view_host(), L"", + L"window.domAutomationController.send(getFocusedElement());", + &actual)); ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); } @@ -633,7 +616,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { // Focus stays on page with interstitials. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // First we navigate to our test page. @@ -669,23 +652,21 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { // Make sure Find box can request focus, even when it is already open. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // Open some page (any page that doesn't steal focus). GURL url = test_server()->GetURL(kTypicalPage); ui_test_utils::NavigateToURL(browser(), url); - gfx::NativeWindow window = browser()->window()->GetNativeHandle(); - #if defined(OS_MACOSX) // Press Cmd+F, which will make the Find box open and request focus. ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F, false, false, false, true)); + browser(), app::VKEY_F, false, false, false, true)); #else // Press Ctrl+F, which will make the Find box open and request focus. ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F, true, false, false, false)); + browser(), app::VKEY_F, true, false, false, false)); #endif // Ideally, we wouldn't sleep here and instead would intercept the @@ -705,10 +686,10 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { // Now press Ctrl+F again and focus should move to the Find box. #if defined(OS_MACOSX) ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F, false, false, false, true)); + browser(), app::VKEY_F, false, false, false, true)); #else ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F, true, false, false, false)); + browser(), app::VKEY_F, true, false, false, false)); #endif ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); @@ -719,10 +700,10 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { // Now press Ctrl+F again and focus should move to the Find box. #if defined(OS_MACOSX) ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F, false, false, false, true)); + browser(), app::VKEY_F, false, false, false, true)); #else ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F, true, false, false, false)); + browser(), app::VKEY_F, true, false, false, false)); #endif // See remark above on why we wait. @@ -736,7 +717,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { // types of tabs. // Flaky, http://crbug.com/50763. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); // Open the history tab, focus should be on the tab contents. browser()->ShowHistoryTab(); @@ -761,7 +742,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) { // Tests that focus goes where expected when using reload. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // Open the new tab, reload. @@ -794,7 +775,7 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { // Tests that focus goes where expected when using reload on a crashed tab. IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) { - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); ASSERT_TRUE(test_server()->Start()); // Open a regular page, crash, reload. diff --git a/chrome/browser/browser_keyevents_browsertest.cc b/chrome/browser/browser_keyevents_browsertest.cc index 6d3c589..6e55f3e 100644 --- a/chrome/browser/browser_keyevents_browsertest.cc +++ b/chrome/browser/browser_keyevents_browsertest.cc @@ -11,9 +11,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/browser.h" -#include "chrome/browser/browser_window.h" #include "chrome/browser/dom_operation_notification_details.h" #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/renderer_host/render_widget_host_view.h" @@ -129,30 +127,6 @@ class BrowserKeyEventsTest : public InProcessBrowserTest { EnableDOMAutomation(); } - void GetNativeWindow(gfx::NativeWindow* native_window) { - BrowserWindow* window = browser()->window(); - ASSERT_TRUE(window); - *native_window = window->GetNativeHandle(); - ASSERT_TRUE(*native_window); - } - - void BringBrowserWindowToFront() { - gfx::NativeWindow window = NULL; - ASSERT_NO_FATAL_FAILURE(GetNativeWindow(&window)); - ui_test_utils::ShowAndFocusNativeWindow(window); - } - - void SendKey(app::KeyboardCode key, - bool control, - bool shift, - bool alt, - bool command) { - gfx::NativeWindow window = NULL; - ASSERT_NO_FATAL_FAILURE(GetNativeWindow(&window)); - ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, key, control, shift, alt, command)); - } - bool IsViewFocused(ViewID vid) { return ui_test_utils::IsViewFocused(browser(), vid); } @@ -284,12 +258,12 @@ class BrowserKeyEventsTest : public InProcessBrowserTest { // We need to create a finish observer before sending the key event, // because the test finished message might be arrived before returning - // from the SendKey() method. + // from the SendKeyPressSync() method. TestFinishObserver finish_observer( browser()->GetTabContentsAt(tab_index)->render_view_host()); - ASSERT_NO_FATAL_FAILURE( - SendKey(test.key, test.ctrl, test.shift, test.alt, test.command)); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + browser(), test.key, test.ctrl, test.shift, test.alt, test.command)); ASSERT_TRUE(finish_observer.WaitForFinish()); ASSERT_NO_FATAL_FAILURE(CheckResult( tab_index, test.result_length, test.result)); @@ -374,7 +348,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, NormalKeyEvents) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -459,7 +433,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, CtrlKeyEvents) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -472,8 +446,8 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, CtrlKeyEvents) { EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); // Press Escape to close the Find box and move the focus back to the web page. - ASSERT_NO_FATAL_FAILURE( - SendKey(app::VKEY_ESCAPE, false, false, false, false)); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + browser(), app::VKEY_ESCAPE, false, false, false, false)); ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); // Press Ctrl+F with keydown suppressed shall not open the find box. @@ -504,7 +478,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, CommandKeyEvents) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -517,8 +491,8 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, CommandKeyEvents) { EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); // Press Escape to close the Find box and move the focus back to the web page. - ASSERT_NO_FATAL_FAILURE( - SendKey(app::VKEY_ESCAPE, false, false, false, false)); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + browser(), app::VKEY_ESCAPE, false, false, false, false)); ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); // Press Cmd+F with keydown suppressed shall not open the find box. @@ -607,7 +581,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_AccessKeys) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -679,7 +653,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_AccessKeys) { IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_ReservedAccelerators) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -707,7 +681,8 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_ReservedAccelerators) { // Reserved accelerators can't be suppressed. ASSERT_NO_FATAL_FAILURE(SuppressAllEvents(0, true)); // Press Ctrl+W, which will close the tab. - ASSERT_NO_FATAL_FAILURE(SendKey(app::VKEY_W, true, false, false, false)); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + browser(), app::VKEY_W, true, false, false, false)); EXPECT_EQ(1, browser()->tab_count()); #elif defined(OS_MACOSX) static const KeyEventTestData kTestCmdT = { @@ -730,7 +705,8 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_ReservedAccelerators) { // Reserved accelerators can't be suppressed. ASSERT_NO_FATAL_FAILURE(SuppressAllEvents(0, true)); // Press Cmd+W, which will close the tab. - ASSERT_NO_FATAL_FAILURE(SendKey(app::VKEY_W, false, false, false, true)); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + browser(), app::VKEY_W, false, false, false, true)); EXPECT_EQ(1, browser()->tab_count()); #elif defined(TOOLKIT_GTK) // Ctrl-[a-z] are not treated as reserved accelerators on GTK. @@ -800,7 +776,8 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_ReservedAccelerators) { // Ctrl+F4 to close the tab. ASSERT_NO_FATAL_FAILURE(SuppressAllEvents(0, true)); - ASSERT_NO_FATAL_FAILURE(SendKey(app::VKEY_F4, true, false, false, false)); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + browser(), app::VKEY_F4, true, false, false, false)); ASSERT_EQ(1, browser()->tab_count()); #endif } @@ -836,7 +813,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, EditorKeyBindings) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -873,7 +850,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, PageUpDownKeys) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); @@ -914,7 +891,7 @@ IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, FocusMenuBarByAltKey) { ASSERT_TRUE(test_server()->Start()); - BringBrowserWindowToFront(); + ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); GURL url = test_server()->GetURL(kTestingPage); ui_test_utils::NavigateToURL(browser(), url); diff --git a/chrome/browser/ssl/ssl_browser_tests.cc b/chrome/browser/ssl/ssl_browser_tests.cc index fb20b0a..a897e1b 100644 --- a/chrome/browser/ssl/ssl_browser_tests.cc +++ b/chrome/browser/ssl/ssl_browser_tests.cc @@ -328,20 +328,17 @@ IN_PROC_BROWSER_TEST_F(SSLUITest, FLAKY_TestHTTPSExpiredCertAndGoForward) { IN_PROC_BROWSER_TEST_F(SSLUITest, TestHTTPSErrorWithNoNavEntry) { ASSERT_TRUE(https_server_expired_.Start()); - ui_test_utils::WindowedNotificationObserver<NavigationController> - load_stop_signal(NotificationType::LOAD_STOP, NULL); - Browser* used_browser = NULL; - TabContents* tab_contents = browser()->AddTabWithURL( - https_server_expired_.GetURL("files/ssl/google.htm"), GURL(), - PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED, - NULL, std::string(), &used_browser); - load_stop_signal.WaitFor(&(tab_contents->controller())); + GURL url = https_server_expired_.GetURL("files/ssl/google.htm"); + TabContents* tab2 = browser()->AddTabWithURL( + url, GURL(), PageTransition::TYPED, -1, TabStripModel::ADD_SELECTED, NULL, + std::string(), NULL); + ui_test_utils::WaitForLoadStop(&(tab2->controller())); // Verify our assumption that there was no prior navigation. EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_BACK)); // We should have an interstitial page showing. - ASSERT_TRUE(tab_contents->interstitial_page()); + ASSERT_TRUE(tab2->interstitial_page()); } // diff --git a/chrome/browser/views/find_bar_host_interactive_uitest.cc b/chrome/browser/views/find_bar_host_interactive_uitest.cc index 7662910..d0b5e2e 100644 --- a/chrome/browser/views/find_bar_host_interactive_uitest.cc +++ b/chrome/browser/views/find_bar_host_interactive_uitest.cc @@ -3,12 +3,9 @@ // found in the LICENSE file. #include "app/keyboard_codes.h" -#include "base/message_loop.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/browser.h" -#include "chrome/browser/browser_window.h" #include "chrome/browser/find_bar_controller.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/tabs/tab_strip_model.h" @@ -34,51 +31,6 @@ class FindInPageTest : public InProcessBrowserTest { FindBarHost::disable_animations_during_testing_ = true; } - void ClickOnView(ViewID view_id) { - BrowserWindow* browser_window = browser()->window(); - ASSERT_TRUE(browser_window); -#if defined(TOOLKIT_VIEWS) - views::View* view = - reinterpret_cast<BrowserView*>(browser_window)->GetViewByID(view_id); -#elif defined(OS_LINUX) - gfx::NativeWindow window = browser_window->GetNativeHandle(); - ASSERT_TRUE(window); - GtkWidget* view = ViewIDUtil::GetWidget(GTK_WIDGET(window), view_id); -#endif - ASSERT_TRUE(view); - ui_controls::MoveMouseToCenterAndPress(view, - ui_controls::LEFT, - ui_controls::DOWN | ui_controls::UP, - new MessageLoop::QuitTask()); - ASSERT_NO_FATAL_FAILURE(ui_test_utils::RunMessageLoop()); - } - - int GetFocusedViewID() { -#if defined(TOOLKIT_VIEWS) -#if defined(OS_LINUX) - // See http://crbug.com/26873 . - views::FocusManager* focus_manager = - views::FocusManager::GetFocusManagerForNativeView( - GTK_WIDGET(browser()->window()->GetNativeHandle())); -#else - views::FocusManager* focus_manager = - views::FocusManager::GetFocusManagerForNativeView( - browser()->window()->GetNativeHandle()); -#endif - - if (!focus_manager) { - NOTREACHED(); - return -1; - } - views::View* focused_view = focus_manager->GetFocusedView(); - if (!focused_view) - return -1; - return focused_view->GetID(); -#else - return -1; -#endif - } - string16 GetFindBarText() { FindBarTesting* find_bar = browser()->GetFindBarController()->find_bar()->GetFindBarTesting(); @@ -105,7 +57,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { EXPECT_EQ(browser(), browser_used); browser()->Find(); - EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), + VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); // Select tab A. browser()->SelectTabContentsAt(0, true); @@ -114,16 +67,16 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { browser()->CloseTabContents(browser()->GetTabContentsAt(1)); // Click on the location bar so that Find box loses focus. - ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_LOCATION_BAR)); + ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), + VIEW_ID_LOCATION_BAR)); #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) // Check the location bar is focused. - EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); #endif // This used to crash until bug 1303709 was fixed. ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - browser()->window()->GetNativeHandle(), app::VKEY_ESCAPE, - false, false, false, false)); + browser(), app::VKEY_ESCAPE, false, false, false, false)); } IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) { @@ -135,35 +88,39 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) { // Focus the location bar, open and close the find-in-page, focus should // return to the location bar. browser()->FocusLocationBar(); - EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); // Ensure the creation of the find bar controller. browser()->GetFindBarController()->Show(); - EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), + VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); browser()->GetFindBarController()->EndFindSession( FindBarController::kKeepSelection); - EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); // Focus the location bar, find something on the page, close the find box, // focus should go to the page. browser()->FocusLocationBar(); browser()->Find(); - EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), + VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), ASCIIToUTF16("a"), true, false, NULL); browser()->GetFindBarController()->EndFindSession( FindBarController::kKeepSelection); - EXPECT_EQ(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), + VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); // Focus the location bar, open and close the find box, focus should return to // the location bar (same as before, just checking that http://crbug.com/23599 // is fixed). browser()->FocusLocationBar(); - EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); browser()->GetFindBarController()->Show(); - EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), + VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); browser()->GetFindBarController()->EndFindSession( FindBarController::kKeepSelection); - EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); + EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_LOCATION_BAR)); } // This tests that whenever you clear values from the Find box and close it that @@ -181,28 +138,26 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { GURL url = test_server()->GetURL(kSimplePage); ui_test_utils::NavigateToURL(browser(), url); - gfx::NativeWindow window = browser()->window()->GetNativeHandle(); - // Show the Find bar. browser()->GetFindBarController()->Show(); // Search for "a". ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_A, false, false, false, false)); // No modifiers + browser(), app::VKEY_A, false, false, false, false)); // We should find "a" here. EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); // Delete "a". ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_BACK, false, false, false, false)); // No modifiers. + browser(), app::VKEY_BACK, false, false, false, false)); // Validate we have cleared the text. EXPECT_EQ(string16(), GetFindBarText()); // Close the Find box. ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_ESCAPE, false, false, false, false)); // No modifiers. + browser(), app::VKEY_ESCAPE, false, false, false, false)); // Show the Find bar. browser()->GetFindBarController()->Show(); @@ -213,11 +168,11 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { // Close the Find box. ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_ESCAPE, false, false, false, false)); // No modifiers. + browser(), app::VKEY_ESCAPE, false, false, false, false)); // Press F3 to trigger FindNext. ASSERT_TRUE(ui_test_utils::SendKeyPressSync( - window, app::VKEY_F3, false, false, false, false)); // No modifiers. + browser(), app::VKEY_F3, false, false, false, false)); // After the Find box has been reopened, it should still have no prepopulate // value. diff --git a/chrome/common/notification_source.h b/chrome/common/notification_source.h index 3b4da37..81ed1c1 100644 --- a/chrome/common/notification_source.h +++ b/chrome/common/notification_source.h @@ -43,7 +43,7 @@ template <class T> class Source : public NotificationSource { public: // TODO(erg): Our code hard relies on implicit conversion - Source(T* ptr) : NotificationSource(ptr) {} // NOLINT + Source(const T* ptr) : NotificationSource(ptr) {} // NOLINT Source(const NotificationSource& other) // NOLINT : NotificationSource(other) {} diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index ad0d3a7..964ae60 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -17,6 +17,7 @@ #include "chrome/browser/automation/ui_controls.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_window.h" #include "chrome/browser/dom_operation_notification_details.h" #include "chrome/browser/download/download_item.h" #include "chrome/browser/download/download_manager.h" @@ -28,8 +29,7 @@ #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/extensions/extension_action.h" -#include "chrome/common/notification_registrar.h" -#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" #include "chrome/test/automation/javascript_execution_controller.h" #include "chrome/test/bookmark_load_observer.h" #if defined(TOOLKIT_VIEWS) @@ -527,13 +527,13 @@ int FindInPage(TabContents* tab_contents, const string16& search_string, return observer.number_of_matches(); } -void WaitForNotification(NotificationType::Type type) { +void WaitForNotification(NotificationType type) { TestNotificationObserver observer; RegisterAndWait(&observer, type, NotificationService::AllSources()); } void RegisterAndWait(NotificationObserver* observer, - NotificationType::Type type, + NotificationType type, const NotificationSource& source) { NotificationRegistrar registrar; registrar.Add(observer, type, source); @@ -550,12 +550,34 @@ void WaitForBookmarkModelToLoad(BookmarkModel* model) { ASSERT_TRUE(model->IsLoaded()); } -bool SendKeyPressSync(gfx::NativeWindow window, +bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) { + BrowserWindow* window = browser->window(); + if (!window) + return false; + + *native_window = window->GetNativeHandle(); + return *native_window; +} + +bool BringBrowserWindowToFront(const Browser* browser) { + gfx::NativeWindow window = NULL; + if (!GetNativeWindow(browser, &window)) + return false; + + ui_test_utils::ShowAndFocusNativeWindow(window); + return true; +} + +bool SendKeyPressSync(const Browser* browser, app::KeyboardCode key, bool control, bool shift, bool alt, bool command) { + gfx::NativeWindow window = NULL; + if (!GetNativeWindow(browser, &window)) + return false; + if (!ui_controls::SendKeyPressNotifyWhenDone( window, key, control, shift, alt, command, new MessageLoop::QuitTask())) { @@ -569,6 +591,24 @@ bool SendKeyPressSync(gfx::NativeWindow window, return !testing::Test::HasFatalFailure(); } +bool SendKeyPressAndWait(const Browser* browser, + app::KeyboardCode key, + bool control, + bool shift, + bool alt, + bool command, + NotificationType type, + const NotificationSource& source) { + WindowedNotificationObserver observer(type, source); + + if (!SendKeyPressSync(browser, key, control, shift, alt, command)) + return false; + + observer.Wait(); + return !testing::Test::HasFatalFailure(); +} + + TimedMessageLoopRunner::TimedMessageLoopRunner() : loop_(new MessageLoopForUI()), owned_(true), @@ -681,4 +721,52 @@ TestWebSocketServer::~TestWebSocketServer() { base::LaunchApp(*cmd_line.get(), true, false, NULL); } +WindowedNotificationObserver::WindowedNotificationObserver( + NotificationType notification_type, + const NotificationSource& source) + : seen_(false), + running_(false), + waiting_for_(source) { + registrar_.Add(this, notification_type, waiting_for_); +} + +void WindowedNotificationObserver::Wait() { + if (waiting_for_ == NotificationService::AllSources()) { + LOG(FATAL) << "Wait called when monitoring all sources. You must use " + << "WaitFor in this case."; + } + + if (seen_) + return; + + running_ = true; + ui_test_utils::RunMessageLoop(); +} + +void WindowedNotificationObserver::WaitFor(const NotificationSource& source) { + if (waiting_for_ != NotificationService::AllSources()) { + LOG(FATAL) << "WaitFor called when already waiting on a specific source." + << "Use Wait in this case."; + } + + waiting_for_ = source; + if (sources_seen_.count(waiting_for_.map_key()) > 0) + return; + + running_ = true; + ui_test_utils::RunMessageLoop(); +} + +void WindowedNotificationObserver::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (waiting_for_ == source) { + seen_ = true; + if (running_) + MessageLoopForUI::current()->Quit(); + } else { + sources_seen_.insert(source.map_key()); + } +} + } // namespace ui_test_utils diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index 28552d8..9e6a2e1 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -18,7 +18,6 @@ #include "chrome/browser/view_ids.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/notification_type.h" #include "chrome/common/notification_service.h" #include "chrome/test/automation/dom_element_proxy.h" #include "gfx/native_widget_types.h" @@ -34,6 +33,7 @@ class FilePath; class GURL; class MessageLoop; class NavigationController; +class NotificationType; class Profile; class RenderViewHost; class ScopedTempDir; @@ -178,27 +178,49 @@ bool IsViewFocused(const Browser* browser, ViewID vid); void ClickOnView(const Browser* browser, ViewID vid); // Blocks until a notification for given |type| is received. -void WaitForNotification(NotificationType::Type type); +void WaitForNotification(NotificationType type); // Register |observer| for the given |type| and |source| and run // the message loop until the observer posts a quit task. void RegisterAndWait(NotificationObserver* observer, - NotificationType::Type type, + NotificationType type, const NotificationSource& source); // Blocks until |model| finishes loading. void WaitForBookmarkModelToLoad(BookmarkModel* model); -// Sends a key press blocking until the key press is received or the test times +// Puts the native window for |browser| in |native_window|. Returns true on +// success. +bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) + WARN_UNUSED_RESULT; + +// Brings the native window for |browser| to the foreground. Returns true on +// success. +bool BringBrowserWindowToFront(const Browser* browser) WARN_UNUSED_RESULT; + +// Sends a key press, blocking until the key press is received or the test times // out. This uses ui_controls::SendKeyPress, see it for details. Returns true // if the event was successfully sent and received. -bool SendKeyPressSync(gfx::NativeWindow window, +bool SendKeyPressSync(const Browser* browser, app::KeyboardCode key, bool control, bool shift, bool alt, bool command) WARN_UNUSED_RESULT; +// Sends a key press, blocking until both the key press and a notification from +// |source| of type |type| are received, or until the test times out. This uses +// ui_controls::SendKeyPress, see it for details. Returns true if the event was +// successfully sent and both the event and notification were received. +bool SendKeyPressAndWait(const Browser* browser, + app::KeyboardCode key, + bool control, + bool shift, + bool alt, + bool command, + NotificationType type, + const NotificationSource& source) WARN_UNUSED_RESULT; + // Run a message loop only for the specified amount of time. class TimedMessageLoopRunner { public: @@ -307,49 +329,29 @@ class TestNotificationObserver : public NotificationObserver { // and the observers getting registered, where a notification will be missed. // // Rather, one can do this: -// WindowedNotificationObserver<type> signal(...) +// WindowedNotificationObserver signal(...) // PerformAction() -// wait_for_signal.Wait() -template <class T> +// signal.Wait() class WindowedNotificationObserver : public NotificationObserver { public: /* Register to listen for notifications of the given type from either - * a specific source, of from all sources if |source| is NULL */ + * a specific source, or from all sources if |source| is + * NotificationService::AllSources(). */ WindowedNotificationObserver(NotificationType notification_type, - T* source) - : seen_(false), - running_(false), - waiting_for_(source) { - if (source) { - registrar_.Add(this, notification_type, waiting_for_); - } else { - registrar_.Add(this, notification_type, - NotificationService::AllSources()); - } - } + const NotificationSource& source); - /* Wait sleeps until the specified notification occurs. You must have - * specified a source in the arguments to the constructor in order to - * use this function. Otherwise, you should use WaitFor. */ - void Wait() { - if (!waiting_for_.ptr()) { - LOG(FATAL) << "Wait called when monitoring all sources. You must use " - << "WaitFor in this case."; - } - - if (seen_) - return; - - running_ = true; - ui_test_utils::RunMessageLoop(); - } + /* Wait until the specified notification occurs. You must have specified a + * source in the arguments to the constructor in order to use this function. + * Otherwise, you should use WaitFor. */ + void Wait(); /* WaitFor waits until the given notification type is received from the * given object. If the notification was emitted between the construction of * this object and this call then it returns immediately. * * Beware that this is inheriently plagued by ABA issues. Consider: - * WindowedNotificationObserver is created with NULL source + * WindowedNotificationObserver is created, listening for notifications from + * all sources * Object A is created with address x and fires a notification * Object A is freed * Object B is created with the same address @@ -359,37 +361,17 @@ class WindowedNotificationObserver : public NotificationObserver { * notification from A (because they shared an address), despite being * different objects. */ - void WaitFor(T* source) { - if (waiting_for_.ptr()) { - LOG(FATAL) << "WaitFor called when already waiting on a specific " - << "source. Use Wait in this case."; - } - - waiting_for_ = Source<T>(source); - if (sources_seen_.count(waiting_for_.map_key()) > 0) - return; - - running_ = true; - ui_test_utils::RunMessageLoop(); - } + void WaitFor(const NotificationSource& source); virtual void Observe(NotificationType type, const NotificationSource& source, - const NotificationDetails& details) { - if (waiting_for_ == source) { - seen_ = true; - if (running_) - MessageLoopForUI::current()->Quit(); - } else { - sources_seen_.insert(source.map_key()); - } - } + const NotificationDetails& details); private: bool seen_; bool running_; std::set<uintptr_t> sources_seen_; - Source<T> waiting_for_; + NotificationSource waiting_for_; NotificationRegistrar registrar_; DISALLOW_COPY_AND_ASSIGN(WindowedNotificationObserver); @@ -401,11 +383,11 @@ class WindowedNotificationObserver : public NotificationObserver { // which is the case with most notifications. template <class T, class U> class WindowedNotificationObserverWithDetails - : public WindowedNotificationObserver<T> { + : public WindowedNotificationObserver { public: WindowedNotificationObserverWithDetails(NotificationType notification_type, - T* source) - : WindowedNotificationObserver<T>(notification_type, source) {} + const T* source) + : WindowedNotificationObserver(notification_type, Source<T>(source)) {} // Fills |details| with the details of the notification received for |source|. bool GetDetailsFor(T* source, U* details) { @@ -420,7 +402,7 @@ class WindowedNotificationObserverWithDetails const NotificationSource& source, const NotificationDetails& details) { details_[Source<T>(source).ptr()] = *Details<U>(details).ptr(); - WindowedNotificationObserver<T>::Observe(type, source, details); + WindowedNotificationObserver::Observe(type, source, details); } private: |