diff options
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc | 7 | ||||
-rw-r--r-- | chrome/browser/automation/ui_controls.h | 5 | ||||
-rw-r--r-- | chrome/browser/browser_focus_uitest.cc | 71 | ||||
-rw-r--r-- | chrome/browser/browser_keyevents_browsertest.cc | 8 | ||||
-rw-r--r-- | chrome/browser/views/find_bar_host_interactive_uitest.cc | 39 | ||||
-rw-r--r-- | chrome/test/in_process_browser_test.cc | 5 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.cc | 20 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 13 |
8 files changed, 91 insertions, 77 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc b/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc index e1dc676..5b00bab 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc +++ b/chrome/browser/autocomplete/autocomplete_edit_view_browsertest.cc @@ -125,10 +125,9 @@ class AutocompleteEditViewTest : public InProcessBrowserTest, void SendKey(base::KeyboardCode key, bool control, bool shift, bool alt) { gfx::NativeWindow window = NULL; ASSERT_NO_FATAL_FAILURE(GetNativeWindow(&window)); - ui_controls::SendKeyPressNotifyWhenDone(window, key, control, shift, alt, - false /* command */, - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE( + ui_test_utils::SendKeyPressSync( + window, key, control, shift, alt, false /* command */)); } void SendKeySequence(const wchar_t* keys) { diff --git a/chrome/browser/automation/ui_controls.h b/chrome/browser/automation/ui_controls.h index 4d1ce79..2b26dfe 100644 --- a/chrome/browser/automation/ui_controls.h +++ b/chrome/browser/automation/ui_controls.h @@ -12,9 +12,9 @@ #include <wtypes.h> #endif +#include "base/keyboard_codes.h" #include "gfx/native_widget_types.h" #include "gfx/point.h" -#include "base/keyboard_codes.h" #if defined(TOOLKIT_VIEWS) namespace views { @@ -42,6 +42,9 @@ namespace ui_controls { // these functions, so passing NULL is ok. // Send a key press with/without modifier keys. +// +// If you're writing a test chances are you want the variant in ui_test_utils. +// See it for details. bool SendKeyPress(gfx::NativeWindow window, base::KeyboardCode key, bool control, diff --git a/chrome/browser/browser_focus_uitest.cc b/chrome/browser/browser_focus_uitest.cc index 6f2e0ad..7c28705 100644 --- a/chrome/browser/browser_focus_uitest.cc +++ b/chrome/browser/browser_focus_uitest.cc @@ -305,10 +305,8 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { VIEW_ID_LOCATION_BAR; ASSERT_TRUE(IsViewFocused(vid)); - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, - false, false, false, - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_TAB, true, false, false, false)); } // As above, but with ctrl+shift+tab. @@ -318,10 +316,8 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { VIEW_ID_LOCATION_BAR; ASSERT_TRUE(IsViewFocused(vid)); - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_TAB, true, - true, false, false, - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_TAB, true, true, false, false)); } } } @@ -488,11 +484,13 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { false, false, false, false)); if (j < arraysize(kExpElementIDs) - 1) { - ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> - render_view_host()); + ASSERT_NO_FATAL_FAILURE( + ui_test_utils::WaitForFocusChange( + browser()->GetSelectedTabContents()->render_view_host())); } else { // On the last tab key press, the focus returns to the browser. - ui_test_utils::WaitForFocusInBrowser(browser()); + ASSERT_NO_FATAL_FAILURE( + ui_test_utils::WaitForFocusInBrowser(browser())); } } @@ -515,11 +513,13 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { false, true, false, false)); if (j < arraysize(kExpElementIDs) - 1) { - ui_test_utils::WaitForFocusChange(browser()->GetSelectedTabContents()-> - render_view_host()); + ASSERT_NO_FATAL_FAILURE( + ui_test_utils::WaitForFocusChange( + browser()->GetSelectedTabContents()->render_view_host())); } else { // On the last tab key press, the focus returns to the browser. - ui_test_utils::WaitForFocusInBrowser(browser()); + ASSERT_NO_FATAL_FAILURE( + ui_test_utils::WaitForFocusInBrowser(browser())); } // Let's make sure the focus is on the expected element in the page. @@ -588,10 +588,11 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { false, false, false, false)); if (j < arraysize(kExpElementIDs) - 1) { - interstitial_page->WaitForFocusChange(); + ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); } else { // On the last tab key press, the focus returns to the browser. - ui_test_utils::WaitForFocusInBrowser(browser()); + ASSERT_NO_FATAL_FAILURE( + ui_test_utils::WaitForFocusInBrowser(browser())); } } @@ -612,10 +613,11 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { false, true, false, false)); if (j < arraysize(kExpElementIDs) - 1) { - interstitial_page->WaitForFocusChange(); + ASSERT_NO_FATAL_FAILURE(interstitial_page->WaitForFocusChange()); } else { // On the last tab key press, the focus returns to the browser. - ui_test_utils::WaitForFocusInBrowser(browser()); + ASSERT_NO_FATAL_FAILURE( + ui_test_utils::WaitForFocusInBrowser(browser())); } // Let's make sure the focus is on the expected element in the page. @@ -679,16 +681,13 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { #if defined(OS_MACOSX) // Press Cmd+F, which will make the Find box open and request focus. - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, - false, false, true, - new MessageLoop::QuitTask()); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F, false, false, false, true)); #else // Press Ctrl+F, which will make the Find box open and request focus. - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, - false, false, false, - new MessageLoop::QuitTask()); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F, true, false, false, false)); #endif - ui_test_utils::RunMessageLoop(); // Ideally, we wouldn't sleep here and instead would intercept the // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we @@ -706,15 +705,12 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { // Now press Ctrl+F again and focus should move to the Find box. #if defined(OS_MACOSX) - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, - false, false, true, - new MessageLoop::QuitTask()); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F, false, false, false, true)); #else - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, - false, false, false, - new MessageLoop::QuitTask()); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F, true, false, false, false)); #endif - ui_test_utils::RunMessageLoop(); ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); // Set focus to the page. @@ -723,15 +719,12 @@ IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { // Now press Ctrl+F again and focus should move to the Find box. #if defined(OS_MACOSX) - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, false, - false, false, true, - new MessageLoop::QuitTask()); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F, false, false, false, true)); #else - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F, true, - false, false, false, - new MessageLoop::QuitTask()); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F, true, false, false, false)); #endif - ui_test_utils::RunMessageLoop(); // See remark above on why we wait. MessageLoop::current()->PostDelayedTask( diff --git a/chrome/browser/browser_keyevents_browsertest.cc b/chrome/browser/browser_keyevents_browsertest.cc index 89f8c08..87a6bedf 100644 --- a/chrome/browser/browser_keyevents_browsertest.cc +++ b/chrome/browser/browser_keyevents_browsertest.cc @@ -149,10 +149,8 @@ class BrowserKeyEventsTest : public InProcessBrowserTest { bool command) { gfx::NativeWindow window = NULL; ASSERT_NO_FATAL_FAILURE(GetNativeWindow(&window)); - EXPECT_TRUE(ui_controls::SendKeyPressNotifyWhenDone( - window, key, control, shift, alt, command, - new MessageLoop::QuitTask())); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, key, control, shift, alt, command)); } bool IsViewFocused(ViewID vid) { @@ -790,7 +788,7 @@ 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(base::VKEY_F4, true, false, false, false)); + ASSERT_NO_FATAL_FAILURE(SendKey(base::VKEY_F4, true, false, false, false)); ASSERT_EQ(1, browser()->tab_count()); #endif } diff --git a/chrome/browser/views/find_bar_host_interactive_uitest.cc b/chrome/browser/views/find_bar_host_interactive_uitest.cc index f866f09..69d7284 100644 --- a/chrome/browser/views/find_bar_host_interactive_uitest.cc +++ b/chrome/browser/views/find_bar_host_interactive_uitest.cc @@ -49,7 +49,7 @@ class FindInPageTest : public InProcessBrowserTest { ui_controls::LEFT, ui_controls::DOWN | ui_controls::UP, new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_NO_FATAL_FAILURE(ui_test_utils::RunMessageLoop()); } int GetFocusedViewID() { @@ -113,17 +113,16 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { browser()->CloseTabContents(browser()->GetTabContentsAt(1)); // Click on the location bar so that Find box loses focus. - ClickOnView(VIEW_ID_LOCATION_BAR); + ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_LOCATION_BAR)); #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) // Check the location bar is focused. EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); #endif // This used to crash until bug 1303709 was fixed. - ui_controls::SendKeyPressNotifyWhenDone( + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( browser()->window()->GetNativeHandle(), base::VKEY_ESCAPE, - false, false, false, false, new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + false, false, false, false)); } IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) { @@ -187,28 +186,22 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { browser()->GetFindBarController()->Show(); // Search for "a". - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_A, - false, false, false, false, // No modifiers. - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_A, false, false, false, false)); // No modifiers // We should find "a" here. EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText()); // Delete "a". - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_BACK, - false, false, false, false, // No modifiers. - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_BACK, false, false, false, false)); // No modifiers. // Validate we have cleared the text. EXPECT_EQ(string16(), GetFindBarText()); // Close the Find box. - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_ESCAPE, - false, false, false, false, // No modifiers. - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_ESCAPE, false, false, false, false)); // No modifiers. // Show the Find bar. browser()->GetFindBarController()->Show(); @@ -218,16 +211,12 @@ IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) { EXPECT_EQ(string16(), GetFindBarText()); // Close the Find box. - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_ESCAPE, - false, false, false, false, // No modifiers. - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_ESCAPE, false, false, false, false)); // No modifiers. // Press F3 to trigger FindNext. - ui_controls::SendKeyPressNotifyWhenDone(window, base::VKEY_F3, - false, false, false, false, // No modifiers. - new MessageLoop::QuitTask()); - ui_test_utils::RunMessageLoop(); + ASSERT_TRUE(ui_test_utils::SendKeyPressSync( + window, base::VKEY_F3, false, false, false, false)); // No modifiers. // After the Find box has been reopened, it should still have no prepopulate // value. diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc index 9fc2544..61ece39 100644 --- a/chrome/test/in_process_browser_test.cc +++ b/chrome/test/in_process_browser_test.cc @@ -327,9 +327,10 @@ void InProcessBrowserTest::TimedOut() { error_message += base::IntToString(initial_timeout_); error_message += " ms (kInitialTimeoutInMS)."; - GTEST_NONFATAL_FAILURE_(error_message.c_str()); - MessageLoopForUI::current()->Quit(); + + // WARNING: This must be after Quit as it returns. + FAIL() << error_message; } void InProcessBrowserTest::SetInitialTimeoutInMS(int timeout_value) { diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index 3d36b6e..9c86038 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -14,6 +14,7 @@ #include "base/process_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_list.h" #include "chrome/browser/dom_operation_notification_details.h" @@ -549,6 +550,25 @@ void WaitForBookmarkModelToLoad(BookmarkModel* model) { ASSERT_TRUE(model->IsLoaded()); } +bool SendKeyPressSync(gfx::NativeWindow window, + base::KeyboardCode key, + bool control, + bool shift, + bool alt, + bool command) { + if (!ui_controls::SendKeyPressNotifyWhenDone( + window, key, control, shift, alt, command, + new MessageLoop::QuitTask())) { + LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed"; + return false; + } + // Run the message loop. It'll stop running when either the key was received + // or the test timed out (in which case testing::Test::HasFatalFailure should + // be set). + RunMessageLoop(); + return !testing::Test::HasFatalFailure(); +} + TimedMessageLoopRunner::TimedMessageLoopRunner() : loop_(new MessageLoopForUI()), owned_(true), diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index 8173348..8ca8b02 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -10,8 +10,8 @@ #include <string> #include <set> -#include "gfx/native_widget_types.h" #include "base/basictypes.h" +#include "base/keyboard_codes.h" #include "base/message_loop.h" #include "base/scoped_temp_dir.h" #include "base/string16.h" @@ -21,6 +21,7 @@ #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" class AppModalDialog; class BookmarkModel; @@ -188,6 +189,16 @@ void RegisterAndWait(NotificationObserver* observer, // Blocks until |model| finishes loading. void WaitForBookmarkModelToLoad(BookmarkModel* model); +// 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, + base::KeyboardCode key, + bool control, + bool shift, + bool alt, + bool command) WARN_UNUSED_RESULT; + // Run a message loop only for the specified amount of time. class TimedMessageLoopRunner { public: |