diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-02 19:05:19 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-02 19:05:19 +0000 |
commit | 3a31d4855b91065ffd87d7de3f4db0ef92c94ec7 (patch) | |
tree | a6e7f7e5b57cd1cf9d8741260e81882142a74ff4 /chrome | |
parent | 8062678b6b02902058d2a688099a650783fc5556 (diff) | |
download | chromium_src-3a31d4855b91065ffd87d7de3f4db0ef92c94ec7.zip chromium_src-3a31d4855b91065ffd87d7de3f4db0ef92c94ec7.tar.gz chromium_src-3a31d4855b91065ffd87d7de3f4db0ef92c94ec7.tar.bz2 |
The focus would only be restored properly the first time the
find-in-bar box was closed.
Also wrote an interactive ui test and took the opportunity to convert the existing find-in-bar interactive ui test to be a browser test.
BUG=23599
TEST=See bug.
Review URL: http://codereview.chromium.org/251064
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27875 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/views/find_bar_host.cc | 9 | ||||
-rw-r--r-- | chrome/browser/views/find_bar_host_browsertest.cc | 248 | ||||
-rw-r--r-- | chrome/browser/views/find_bar_host_interactive_uitest.cc | 159 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.cc | 60 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 9 |
5 files changed, 302 insertions, 183 deletions
diff --git a/chrome/browser/views/find_bar_host.cc b/chrome/browser/views/find_bar_host.cc index a87edc2..46888af 100644 --- a/chrome/browser/views/find_bar_host.cc +++ b/chrome/browser/views/find_bar_host.cc @@ -53,11 +53,6 @@ FindBarHost::FindBarHost(BrowserView* browser_view) views::FocusManager::GetFocusManagerForNativeView(host_->GetNativeView()); if (focus_manager_) { focus_manager_->AddFocusChangeListener(this); - - // Stores the currently focused view, and tracks focus changes so that we - // can restore focus when the find box is closed. - focus_tracker_.reset(new views::ExternalFocusTracker(view_, - focus_manager_)); } else { // In some cases (see bug http://crbug.com/17056) it seems we may not have // a focus manager. Please reopen the bug if you hit this. @@ -74,6 +69,10 @@ FindBarHost::~FindBarHost() { } void FindBarHost::Show() { + // Stores the currently focused view, and tracks focus changes so that we can + // restore focus when the find box is closed. + focus_tracker_.reset(new views::ExternalFocusTracker(view_, focus_manager_)); + if (disable_animations_during_testing_) { animation_->Reset(1); MoveWindowIfNecessary(gfx::Rect(), true); diff --git a/chrome/browser/views/find_bar_host_browsertest.cc b/chrome/browser/views/find_bar_host_browsertest.cc index caed556..6c108de 100644 --- a/chrome/browser/views/find_bar_host_browsertest.cc +++ b/chrome/browser/views/find_bar_host_browsertest.cc @@ -28,61 +28,11 @@ const std::wstring kPrematureEnd = L"files/find_in_page/premature_end.html"; const std::wstring kMoveIfOver = L"files/find_in_page/move_if_obscuring.html"; const std::wstring kBitstackCrash = L"files/find_in_page/crash_14491.html"; -namespace { +const bool kBack = false; +const bool kFwd = true; -class FindInPageNotificationObserver : public NotificationObserver { - public: - explicit FindInPageNotificationObserver(TabContents* parent_tab) - : parent_tab_(parent_tab), - active_match_ordinal_(-1), - number_of_matches_(0) { - current_find_request_id_ = parent_tab->current_find_request_id(); - registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE, - Source<TabContents>(parent_tab_)); - ui_test_utils::RunMessageLoop(); - } - - int active_match_ordinal() const { return active_match_ordinal_; } - - int number_of_matches() const { return number_of_matches_; } - - virtual void Observe(NotificationType type, const NotificationSource& source, - const NotificationDetails& details) { - if (type == NotificationType::FIND_RESULT_AVAILABLE) { - Details<FindNotificationDetails> find_details(details); - if (find_details->request_id() == current_find_request_id_) { - // We get multiple responses and one of those will contain the ordinal. - // This message comes to us before the final update is sent. - if (find_details->active_match_ordinal() > -1) - active_match_ordinal_ = find_details->active_match_ordinal(); - if (find_details->final_update()) { - number_of_matches_ = find_details->number_of_matches(); - MessageLoopForUI::current()->Quit(); - } else { - DLOG(INFO) << "Ignoring, since we only care about the final message"; - } - } - } else { - NOTREACHED(); - } - } - - private: - NotificationRegistrar registrar_; - TabContents* parent_tab_; - // We will at some point (before final update) be notified of the ordinal and - // we need to preserve it so we can send it later. - int active_match_ordinal_; - int number_of_matches_; - // The id of the current find request, obtained from TabContents. Allows us - // to monitor when the search completes. - int current_find_request_id_; -}; - -} // namespace - -typedef enum { BACK = 0, FWD = 1 } FindInPageDirection; -typedef enum { IGNORE_CASE = 0, CASE_SENSITIVE = 1 } FindInPageCase; +const bool kIgnoreCase = false; +const bool kCaseSensitive = true; class FindInPageControllerTest : public InProcessBrowserTest { public: @@ -91,21 +41,6 @@ class FindInPageControllerTest : public InProcessBrowserTest { } protected: - int FindInPage(const std::wstring& search_string, - FindInPageDirection forward, - FindInPageCase match_case, - int* ordinal) { - TabContents* tab_contents = browser()->GetSelectedTabContents(); - tab_contents->StartFinding(search_string, forward == FWD, - match_case == CASE_SENSITIVE); - - FindInPageNotificationObserver observer = - FindInPageNotificationObserver(tab_contents); - if (ordinal) - *ordinal = observer.active_match_ordinal(); - return observer.number_of_matches(); - } - void GetFindBarWindowInfo(gfx::Point* position, bool* fully_visible) { FindBarTesting* find_bar = browser()->find_bar()->find_bar()->GetFindBarTesting(); @@ -123,55 +58,73 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) { // Try incremental search (mimicking user typing in). int ordinal = 0; - EXPECT_EQ(18, FindInPage(L"g", FWD, IGNORE_CASE, &ordinal)); + TabContents* tab = browser()->GetSelectedTabContents(); + EXPECT_EQ(18, ui_test_utils::FindInPage(tab, L"g", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(11, FindInPage(L"go", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(11, ui_test_utils::FindInPage(tab, L"go", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(04, FindInPage(L"goo", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(04, ui_test_utils::FindInPage(tab, L"goo", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(03, FindInPage(L"goog", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(03, ui_test_utils::FindInPage(tab, L"goog", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(02, FindInPage(L"googl", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(02, ui_test_utils::FindInPage(tab, L"googl", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(01, FindInPage(L"google", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(01, ui_test_utils::FindInPage(tab, L"google", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(00, FindInPage(L"google!", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(00, ui_test_utils::FindInPage(tab, L"google!", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); // Negative test (no matches should be found). - EXPECT_EQ(0, FindInPage(L"Non-existing string", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"Non-existing string", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); // 'horse' only exists in the three right frames. - EXPECT_EQ(3, FindInPage(L"horse", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"horse", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // 'cat' only exists in the first frame. - EXPECT_EQ(1, FindInPage(L"cat", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"cat", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Try searching again, should still come up with 1 match. - EXPECT_EQ(1, FindInPage(L"cat", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"cat", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Try searching backwards, ignoring case, should still come up with 1 match. - EXPECT_EQ(1, FindInPage(L"CAT", BACK, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"CAT", + kBack, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Try case sensitive, should NOT find it. - EXPECT_EQ(0, FindInPage(L"CAT", FWD, CASE_SENSITIVE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"CAT", + kFwd, kCaseSensitive, &ordinal)); EXPECT_EQ(0, ordinal); // Try again case sensitive, but this time with right case. - EXPECT_EQ(1, FindInPage(L"dog", FWD, CASE_SENSITIVE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"dog", + kFwd, kCaseSensitive, &ordinal)); EXPECT_EQ(1, ordinal); // Try non-Latin characters ('Hreggvidur' with 'eth' for 'd' in left frame). - EXPECT_EQ(1, FindInPage(L"Hreggvi\u00F0ur", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"Hreggvi\u00F0ur", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(1, FindInPage(L"Hreggvi\u00F0ur", FWD, CASE_SENSITIVE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"Hreggvi\u00F0ur", + kFwd, kCaseSensitive, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(0, FindInPage(L"hreggvi\u00F0ur", FWD, CASE_SENSITIVE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"hreggvi\u00F0ur", + kFwd, kCaseSensitive, &ordinal)); EXPECT_EQ(0, ordinal); } @@ -203,7 +156,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) { // Search for a text that exists within a link on the page. int ordinal = 0; - EXPECT_EQ(1, FindInPage(L"nk", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab_contents, L"nk", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // End the find session, which should set focus to the link. @@ -213,7 +167,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) { EXPECT_STREQ("link1", FocusedOnPage(tab_contents).c_str()); // Search for a text that exists within a link on the page. - EXPECT_EQ(1, FindInPage(L"Google", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab_contents, L"Google", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Move the selection to link 1, after searching. @@ -242,23 +197,31 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageOrdinal) { // Search for 'o', which should make the first item active and return // '1 in 3' (1st ordinal of a total of 3 matches). + TabContents* tab = browser()->GetSelectedTabContents(); int ordinal = 0; - EXPECT_EQ(3, FindInPage(L"o", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(3, FindInPage(L"o", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(2, ordinal); - EXPECT_EQ(3, FindInPage(L"o", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); // Go back one match. - EXPECT_EQ(3, FindInPage(L"o", BACK, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kBack, kIgnoreCase, &ordinal)); EXPECT_EQ(2, ordinal); - EXPECT_EQ(3, FindInPage(L"o", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); // This should wrap to the top. - EXPECT_EQ(3, FindInPage(L"o", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // This should go back to the end. - EXPECT_EQ(3, FindInPage(L"o", BACK, IGNORE_CASE, &ordinal)); + EXPECT_EQ(3, ui_test_utils::FindInPage(tab, L"o", + kBack, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); } @@ -273,31 +236,43 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageMultiFramesOrdinal) { // Search for 'a', which should make the first item active and return // '1 in 7' (1st ordinal of a total of 7 matches). + TabContents* tab = browser()->GetSelectedTabContents(); int ordinal = 0; - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(2, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(4, ordinal); // Go back one, which should go back one frame. - EXPECT_EQ(7, FindInPage(L"a", BACK, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kBack, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(4, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(5, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(6, ordinal); - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(7, ordinal); // Now we should wrap back to frame 1. - EXPECT_EQ(7, FindInPage(L"a", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Now we should wrap back to frame last frame. - EXPECT_EQ(7, FindInPage(L"a", BACK, IGNORE_CASE, &ordinal)); + EXPECT_EQ(7, + ui_test_utils::FindInPage(tab, L"a", kBack, kIgnoreCase, &ordinal)); EXPECT_EQ(7, ordinal); } @@ -312,17 +287,23 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPage_Issue5132) { // Search for 'goa' three times (6 matches on page). int ordinal = 0; - EXPECT_EQ(6, FindInPage(L"goa", FWD, IGNORE_CASE, &ordinal)); + TabContents* tab = browser()->GetSelectedTabContents(); + EXPECT_EQ(6, ui_test_utils::FindInPage(tab, L"goa", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(6, FindInPage(L"goa", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(6, ui_test_utils::FindInPage(tab, L"goa", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(2, ordinal); - EXPECT_EQ(6, FindInPage(L"goa", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(6, ui_test_utils::FindInPage(tab, L"goa", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); // Add space to search (should result in no matches). - EXPECT_EQ(0, FindInPage(L"goa ", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"goa ", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); // Remove the space, should be back to '3 out of 6') - EXPECT_EQ(6, FindInPage(L"goa", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(6, ui_test_utils::FindInPage(tab, L"goa", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(3, ordinal); } @@ -335,9 +316,12 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindUnSelectableText) { ui_test_utils::NavigateToURL(browser(), url); int ordinal = 0; - EXPECT_EQ(0, FindInPage(L"text", FWD, IGNORE_CASE, &ordinal)); + TabContents* tab = browser()->GetSelectedTabContents(); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"text", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(-1, ordinal); // Nothing is selected. - EXPECT_EQ(0, FindInPage(L"Non-existing string", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"Non-existing string", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); } @@ -359,13 +343,16 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue1341577) { // TODO(jungshik): According to a native Malayalam speaker, it's ok not // to find U+0D4C. Still need to investigate further this issue. int ordinal = 0; - FindInPage(L"\u0D4C", FWD, IGNORE_CASE, &ordinal); - FindInPage(L"\u0D4C", FWD, IGNORE_CASE, &ordinal); + TabContents* tab = browser()->GetSelectedTabContents(); + ui_test_utils::FindInPage(tab, L"\u0D4C", kFwd, kIgnoreCase, &ordinal); + ui_test_utils::FindInPage(tab, L"\u0D4C", kFwd, kIgnoreCase, &ordinal); // This should work fine. - EXPECT_EQ(1, FindInPage(L"\u0D24\u0D46", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"\u0D24\u0D46", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); - EXPECT_EQ(0, FindInPage(L"nostring", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"nostring", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); } @@ -380,7 +367,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue14491) { // This used to crash the tab. int ordinal = 0; - EXPECT_EQ(0, FindInPage(L"s", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), + L"s", kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); } @@ -402,7 +390,9 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindRestarts_Issue1155639) { // This string appears 5 times at the bottom of a long page. If Find restarts // properly after a timeout, it will find 5 matches, not just 1. int ordinal = 0; - EXPECT_EQ(5, FindInPage(L"008.xml", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(5, ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), + L"008.xml", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); } @@ -421,7 +411,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, // Search for a text that exists within a link on the page. int ordinal = 0; - EXPECT_EQ(2, FindInPage(L"html ", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(2, ui_test_utils::FindInPage(tab_contents, L"html ", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); } @@ -526,7 +517,9 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, // Search for 'dream' which the Find box is obscuring. int ordinal = 0; - EXPECT_EQ(1, FindInPage(L"dream", FWD, IGNORE_CASE, &ordinal)); + TabContents* tab = browser()->GetSelectedTabContents(); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"dream", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Make sure Find box has moved. @@ -536,7 +529,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, EXPECT_TRUE(fully_visible); // Search for 'Too much' which the Find box is not obscuring. - EXPECT_EQ(1, FindInPage(L"Too much", FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(1, ui_test_utils::FindInPage(tab, L"Too much", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(1, ordinal); // Make sure Find box has moved back to its original location. @@ -556,7 +550,9 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, // Search for 'no_match'. No matches should be found. int ordinal = 0; - EXPECT_EQ(0, FindInPage(L"no_match", FWD, IGNORE_CASE, &ordinal)); + TabContents* tab = browser()->GetSelectedTabContents(); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, L"no_match", + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); // Open another tab (tab B). @@ -565,7 +561,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, // Simulate what happens when you press F3 for FindNext. We should get a // response here (a hang means search was aborted). - EXPECT_EQ(0, FindInPage(std::wstring(), FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, std::wstring(), + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); // Open another tab (tab C). @@ -574,7 +571,8 @@ IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, // Simulate what happens when you press F3 for FindNext. We should get a // response here (a hang means search was aborted). - EXPECT_EQ(0, FindInPage(std::wstring(), FWD, IGNORE_CASE, &ordinal)); + EXPECT_EQ(0, ui_test_utils::FindInPage(tab, std::wstring(), + kFwd, kIgnoreCase, &ordinal)); EXPECT_EQ(0, ordinal); } diff --git a/chrome/browser/views/find_bar_host_interactive_uitest.cc b/chrome/browser/views/find_bar_host_interactive_uitest.cc index fe2cecd..2f10159 100644 --- a/chrome/browser/views/find_bar_host_interactive_uitest.cc +++ b/chrome/browser/views/find_bar_host_interactive_uitest.cc @@ -2,14 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. - #include "base/keyboard_codes.h" +#include "base/message_loop.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/views/find_bar_host.h" +#include "chrome/browser/views/frame/browser_view.h" #include "chrome/browser/view_ids.h" -#include "chrome/test/automation/browser_proxy.h" -#include "chrome/test/automation/window_proxy.h" -#include "chrome/test/automation/tab_proxy.h" -#include "chrome/test/ui/ui_test.h" +#include "chrome/test/in_process_browser_test.h" +#include "chrome/test/ui_test_utils.h" #include "net/url_request/url_request_unittest.h" +#include "views/focus/focus_manager.h" #include "views/view.h" namespace { @@ -19,78 +25,125 @@ static const int kActionDelayMs = 500; static const wchar_t kDocRoot[] = L"chrome/test/data"; static const wchar_t kSimplePage[] = L"404_is_enough_for_us.html"; -class FindInPageTest : public UITest { +class FindInPageTest : public InProcessBrowserTest { public: FindInPageTest() { - show_window_ = true; - dom_automation_enabled_ = true; + set_show_window(true); + FindBarHost::disable_animations_during_testing_ = true; } -}; - -// Activate a tab by clicking on it. Returns true if the call was successful -// (meaning the messages were correctly sent, but does not guarantee the tab -// has been changed). -bool ActivateTabByClick(AutomationProxy* automation, - WindowProxy* browser_window, - int tab_index) { - // Click on the tab. - gfx::Rect bounds; - - if (!browser_window->GetViewBounds(VIEW_ID_TAB_0 + tab_index, &bounds, true)) - return false; - if (!browser_window->SimulateOSClick(bounds.CenterPoint(), - views::Event::EF_LEFT_BUTTON_DOWN)) - return false; - - // Wait a bit to let the click be processed. - ::Sleep(kActionDelayMs); + 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()); + ui_test_utils::RunMessageLoop(); + } - return true; -} + int GetFocusedViewID() { +#if defined(TOOLKIT_VIEWS) + views::FocusManager* focus_manager = + views::FocusManager::GetFocusManagerForNativeView( + browser()->window()->GetNativeHandle()); + 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 + } +}; } // namespace -TEST_F(FindInPageTest, CrashEscHandlers) { +IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) { scoped_refptr<HTTPTestServer> server = HTTPTestServer::CreateServer(kDocRoot, NULL); ASSERT_TRUE(NULL != server.get()); - scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); - ASSERT_TRUE(browser.get() != NULL); - scoped_refptr<WindowProxy> window(browser->GetWindow()); - ASSERT_TRUE(window.get() != NULL); - // First we navigate to our test page (tab A). GURL url = server->TestServerPageW(kSimplePage); - scoped_refptr<TabProxy> tabA(GetActiveTab()); - EXPECT_NE(AUTOMATION_MSG_NAVIGATION_ERROR, tabA->NavigateToURL(url)); + ui_test_utils::NavigateToURL(browser(), url); - EXPECT_TRUE(browser->OpenFindInPage()); + browser()->Find(); // Open another tab (tab B). - EXPECT_TRUE(browser->AppendTab(url)); - scoped_refptr<TabProxy> tabB(GetActiveTab()); + browser()->AddTabWithURL(url, GURL(), PageTransition::TYPED, true, -1, + false, NULL); - EXPECT_TRUE(browser->OpenFindInPage()); + browser()->Find(); + EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); // Select tab A. - EXPECT_TRUE(ActivateTabByClick(automation(), window.get(), 0)); + browser()->SelectTabContentsAt(0, true); // Close tab B. - EXPECT_TRUE(tabB->Close(true)); + browser()->CloseTabContents(browser()->GetTabContentsAt(1)); // Click on the location bar so that Find box loses focus. - gfx::Rect bounds; - EXPECT_TRUE(window->GetViewBounds(VIEW_ID_LOCATION_BAR, &bounds, false)); - EXPECT_TRUE(window->SimulateOSClick(bounds.CenterPoint(), - views::Event::EF_LEFT_BUTTON_DOWN)); - ::Sleep(kActionDelayMs); - int focused_view_id; - EXPECT_TRUE(window->GetFocusedViewID(&focused_view_id)); - EXPECT_EQ(VIEW_ID_LOCATION_BAR, focused_view_id); + 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. - EXPECT_TRUE(window->SimulateOSKeyPress(base::VKEY_ESCAPE, 0)); - ::Sleep(kActionDelayMs); + ui_controls::SendKeyPressNotifyWhenDone( + browser()->window()->GetNativeHandle(), base::VKEY_ESCAPE, + true, false, false, new MessageLoop::QuitTask()); + ui_test_utils::RunMessageLoop(); +} + +IN_PROC_BROWSER_TEST_F(FindInPageTest, FocusRestore) { + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(kDocRoot, NULL); + ASSERT_TRUE(NULL != server.get()); + + GURL url = server->TestServerPageW(L"title1.html"); + ui_test_utils::NavigateToURL(browser(), url); + + // 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()); + browser()->find_bar()->Show(); + EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); + browser()->find_bar()->EndFindSession(); + EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); + + // 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()); + ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), + L"a", true, false, NULL); + browser()->find_bar()->EndFindSession(); + EXPECT_EQ(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW, GetFocusedViewID()); + + // 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()); + browser()->find_bar()->Show(); + EXPECT_EQ(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD, GetFocusedViewID()); + browser()->find_bar()->EndFindSession(); + EXPECT_EQ(VIEW_ID_LOCATION_BAR, GetFocusedViewID()); } diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index d971021..6bdc983 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -253,6 +253,57 @@ class SimpleNotificationObserver : public NotificationObserver { DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserver); }; +class FindInPageNotificationObserver : public NotificationObserver { + public: + explicit FindInPageNotificationObserver(TabContents* parent_tab) + : parent_tab_(parent_tab), + active_match_ordinal_(-1), + number_of_matches_(0) { + current_find_request_id_ = parent_tab->current_find_request_id(); + registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE, + Source<TabContents>(parent_tab_)); + ui_test_utils::RunMessageLoop(); + } + + int active_match_ordinal() const { return active_match_ordinal_; } + + int number_of_matches() const { return number_of_matches_; } + + virtual void Observe(NotificationType type, const NotificationSource& source, + const NotificationDetails& details) { + if (type == NotificationType::FIND_RESULT_AVAILABLE) { + Details<FindNotificationDetails> find_details(details); + if (find_details->request_id() == current_find_request_id_) { + // We get multiple responses and one of those will contain the ordinal. + // This message comes to us before the final update is sent. + if (find_details->active_match_ordinal() > -1) + active_match_ordinal_ = find_details->active_match_ordinal(); + if (find_details->final_update()) { + number_of_matches_ = find_details->number_of_matches(); + MessageLoopForUI::current()->Quit(); + } else { + DLOG(INFO) << "Ignoring, since we only care about the final message"; + } + } + } else { + NOTREACHED(); + } + } + + private: + NotificationRegistrar registrar_; + TabContents* parent_tab_; + // We will at some point (before final update) be notified of the ordinal and + // we need to preserve it so we can send it later. + int active_match_ordinal_; + int number_of_matches_; + // The id of the current find request, obtained from TabContents. Allows us + // to monitor when the search completes. + int current_find_request_id_; + + DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); +}; + } // namespace void RunMessageLoop() { @@ -432,4 +483,13 @@ void WaitForFocusInBrowser(Browser* browser) { browser); } +int FindInPage(TabContents* tab_contents, const string16& search_string, + bool forward, bool match_case, int* ordinal) { + tab_contents->StartFinding(search_string, forward, match_case); + FindInPageNotificationObserver observer(tab_contents); + if (ordinal) + *ordinal = observer.active_match_ordinal(); + return observer.number_of_matches(); +} + } // namespace ui_test_utils diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index ec7d6a5..2fff009 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -111,6 +111,15 @@ void WaitForFocusChange(RenderViewHost* rvh); // Waits for the renderer to return focus to the browser (happens through tab // traversal). void WaitForFocusInBrowser(Browser* browser); + +// Performs a find in the page of the specified tab. Returns the number of +// matches found. |ordinal| is an optional parameter which is set to the index +// of the current match. +int FindInPage(TabContents* tab, + const string16& search_string, + bool forward, + bool case_sensitive, + int* ordinal); } #endif // CHROME_TEST_UI_TEST_UTILS_H_ |