| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "chrome/browser/browser.h" | 6 #include "chrome/browser/browser.h" |
| 7 #include "chrome/browser/find_notification_details.h" | 7 #include "chrome/browser/find_notification_details.h" |
| 8 #include "chrome/browser/renderer_host/render_view_host.h" | 8 #include "chrome/browser/renderer_host/render_view_host.h" |
| 9 #include "chrome/browser/tab_contents/tab_contents.h" | 9 #include "chrome/browser/tab_contents/tab_contents.h" |
| 10 #include "chrome/browser/tab_contents/web_contents.h" | 10 #include "chrome/browser/tab_contents/web_contents.h" |
| 11 #include "chrome/browser/tab_contents/web_contents_view.h" | 11 #include "chrome/browser/tab_contents/web_contents_view.h" |
| 12 #include "chrome/browser/views/find_bar_win.h" | 12 #include "chrome/browser/views/find_bar_win.h" |
| 13 #include "chrome/common/notification_service.h" | 13 #include "chrome/common/notification_service.h" |
| 14 #include "chrome/test/in_process_browser_test.h" | 14 #include "chrome/test/in_process_browser_test.h" |
| 15 #include "chrome/test/ui_test_utils.h" | 15 #include "chrome/test/ui_test_utils.h" |
| 16 | 16 |
| 17 const std::wstring kFramePage = L"files/find_in_page/frames.html"; | 17 const std::wstring kFramePage = L"files/find_in_page/frames.html"; |
| 18 const std::wstring kFrameData = L"files/find_in_page/framedata_general.html"; | 18 const std::wstring kFrameData = L"files/find_in_page/framedata_general.html"; |
| 19 const std::wstring kUserSelectPage = L"files/find_in_page/user-select.html"; | 19 const std::wstring kUserSelectPage = L"files/find_in_page/user-select.html"; |
| 20 const std::wstring kCrashPage = L"files/find_in_page/crash_1341577.html"; | 20 const std::wstring kCrashPage = L"files/find_in_page/crash_1341577.html"; |
| 21 const std::wstring kTooFewMatchesPage = L"files/find_in_page/bug_1155639.html"; | 21 const std::wstring kTooFewMatchesPage = L"files/find_in_page/bug_1155639.html"; |
| 22 const std::wstring kEndState = L"files/find_in_page/end_state.html"; |
| 22 | 23 |
| 23 class FindInPageNotificationObserver : public NotificationObserver { | 24 class FindInPageNotificationObserver : public NotificationObserver { |
| 24 public: | 25 public: |
| 25 explicit FindInPageNotificationObserver(TabContents* parent_tab) | 26 explicit FindInPageNotificationObserver(TabContents* parent_tab) |
| 26 : parent_tab_(parent_tab), | 27 : parent_tab_(parent_tab), |
| 27 active_match_ordinal_(-1), | 28 active_match_ordinal_(-1), |
| 28 number_of_matches_(0) { | 29 number_of_matches_(0) { |
| 29 registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE, | 30 registrar_.Add(this, NotificationType::FIND_RESULT_AVAILABLE, |
| 30 Source<TabContents>(parent_tab_)); | 31 Source<TabContents>(parent_tab_)); |
| 31 ui_test_utils::RunMessageLoop(); | 32 ui_test_utils::RunMessageLoop(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // We will at some point (before final update) be notified of the ordinal and | 72 // We will at some point (before final update) be notified of the ordinal and |
| 72 // we need to preserve it so we can send it later. | 73 // we need to preserve it so we can send it later. |
| 73 int active_match_ordinal_; | 74 int active_match_ordinal_; |
| 74 int number_of_matches_; | 75 int number_of_matches_; |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 typedef enum FindInPageDirection { BACK = 0, FWD = 1 }; | 78 typedef enum FindInPageDirection { BACK = 0, FWD = 1 }; |
| 78 typedef enum FindInPageCase { IGNORE_CASE = 0, CASE_SENSITIVE = 1 }; | 79 typedef enum FindInPageCase { IGNORE_CASE = 0, CASE_SENSITIVE = 1 }; |
| 79 | 80 |
| 80 class FindInPageControllerTest : public InProcessBrowserTest { | 81 class FindInPageControllerTest : public InProcessBrowserTest { |
| 82 public: |
| 83 FindInPageControllerTest() { |
| 84 EnableDOMAutomation(); |
| 85 } |
| 86 |
| 81 protected: | 87 protected: |
| 82 int FindInPage(const std::wstring& search_string, | 88 int FindInPage(const std::wstring& search_string, |
| 83 FindInPageDirection forward, | 89 FindInPageDirection forward, |
| 84 FindInPageCase match_case, | 90 FindInPageCase match_case, |
| 85 bool find_next) { | 91 bool find_next) { |
| 86 WebContents* web_contents = | 92 WebContents* web_contents = |
| 87 browser()->GetSelectedTabContents()->AsWebContents(); | 93 browser()->GetSelectedTabContents()->AsWebContents(); |
| 88 if (web_contents) { | 94 if (web_contents) { |
| 89 web_contents->set_current_find_request_id( | 95 web_contents->set_current_find_request_id( |
| 90 FindInPageNotificationObserver::kFindInPageRequestId); | 96 FindInPageNotificationObserver::kFindInPageRequestId); |
| 91 web_contents->render_view_host()->StartFinding( | 97 web_contents->render_view_host()->StartFinding( |
| 92 FindInPageNotificationObserver::kFindInPageRequestId, | 98 FindInPageNotificationObserver::kFindInPageRequestId, |
| 93 search_string, forward == FWD, match_case == CASE_SENSITIVE, | 99 search_string, forward == FWD, match_case == CASE_SENSITIVE, |
| 94 find_next); | 100 find_next); |
| 95 return FindInPageNotificationObserver(web_contents).number_of_matches(); | 101 return FindInPageNotificationObserver(web_contents).number_of_matches(); |
| 96 } | 102 } |
| 97 return 0; | 103 return 0; |
| 98 } | 104 } |
| 99 }; | 105 }; |
| 100 | 106 |
| 101 // This test loads a page with frames and starts FindInPage requests | 107 // This test loads a page with frames and starts FindInPage requests. |
| 102 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) { | 108 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) { |
| 103 HTTPTestServer* server = StartHTTPServer(); | 109 HTTPTestServer* server = StartHTTPServer(); |
| 104 | 110 |
| 105 // First we navigate to our frames page. | 111 // First we navigate to our frames page. |
| 106 GURL url = server->TestServerPageW(kFramePage); | 112 GURL url = server->TestServerPageW(kFramePage); |
| 107 ui_test_utils::NavigateToURL(browser(), url); | 113 ui_test_utils::NavigateToURL(browser(), url); |
| 108 | 114 |
| 109 // Try incremental search (mimicking user typing in). | 115 // Try incremental search (mimicking user typing in). |
| 110 EXPECT_EQ(18, FindInPage(L"g", FWD, IGNORE_CASE, false)); | 116 EXPECT_EQ(18, FindInPage(L"g", FWD, IGNORE_CASE, false)); |
| 111 EXPECT_EQ(11, FindInPage(L"go", FWD, IGNORE_CASE, false)); | 117 EXPECT_EQ(11, FindInPage(L"go", FWD, IGNORE_CASE, false)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 134 EXPECT_EQ(0, FindInPage(L"CAT", FWD, CASE_SENSITIVE, false)); | 140 EXPECT_EQ(0, FindInPage(L"CAT", FWD, CASE_SENSITIVE, false)); |
| 135 | 141 |
| 136 // Try again case sensitive, but this time with right case. | 142 // Try again case sensitive, but this time with right case. |
| 137 EXPECT_EQ(1, FindInPage(L"dog", FWD, CASE_SENSITIVE, false)); | 143 EXPECT_EQ(1, FindInPage(L"dog", FWD, CASE_SENSITIVE, false)); |
| 138 | 144 |
| 139 // Try non-Latin characters ('Hreggvidur' with 'eth' for 'd' in left frame). | 145 // Try non-Latin characters ('Hreggvidur' with 'eth' for 'd' in left frame). |
| 140 EXPECT_EQ(1, FindInPage(L"Hreggvi\u00F0ur", FWD, IGNORE_CASE, false)); | 146 EXPECT_EQ(1, FindInPage(L"Hreggvi\u00F0ur", FWD, IGNORE_CASE, false)); |
| 141 EXPECT_EQ(1, FindInPage(L"Hreggvi\u00F0ur", FWD, CASE_SENSITIVE, false)); | 147 EXPECT_EQ(1, FindInPage(L"Hreggvi\u00F0ur", FWD, CASE_SENSITIVE, false)); |
| 142 EXPECT_EQ(0, FindInPage(L"hreggvi\u00F0ur", FWD, CASE_SENSITIVE, false)); | 148 EXPECT_EQ(0, FindInPage(L"hreggvi\u00F0ur", FWD, CASE_SENSITIVE, false)); |
| 143 } | 149 } |
| 150 |
| 151 std::string FocusedOnPage(WebContents* web_contents) { |
| 152 ui_test_utils::JavaScriptRunner js_runner( |
| 153 web_contents, |
| 154 L"", |
| 155 L"window.domAutomationController.send(getFocusedElement());"); |
| 156 return js_runner.Run(); |
| 157 } |
| 158 |
| 159 // This tests the FindInPage end-state, in other words: what is focused when you |
| 160 // close the Find box (ie. if you find within a link the link should be |
| 161 // focused). |
| 162 // TODO(jcampan): This test needs to be enabled once Jay fixes the issues with |
| 163 // running two InProc browser tests that both start a web server (crashes). |
| 164 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, DISABLED_FindInPageEndState) { |
| 165 HTTPTestServer* server = StartHTTPServer(); |
| 166 |
| 167 // First we navigate to our special focus tracking page. |
| 168 GURL url = server->TestServerPageW(kEndState); |
| 169 ui_test_utils::NavigateToURL(browser(), url); |
| 170 |
| 171 WebContents* web_contents = |
| 172 browser()->GetSelectedTabContents()->AsWebContents(); |
| 173 ASSERT_TRUE(NULL != web_contents); |
| 174 |
| 175 // Verify that nothing has focus. |
| 176 ASSERT_STREQ("{nothing focused}", FocusedOnPage(web_contents).c_str()); |
| 177 |
| 178 // Search for a text that exists within a link on the page. |
| 179 EXPECT_EQ(1, FindInPage(L"nk", FWD, IGNORE_CASE, false)); |
| 180 |
| 181 // End the find session, which should set focus to the link. |
| 182 web_contents->StopFinding(false); |
| 183 |
| 184 // Verify that the link is focused. |
| 185 EXPECT_STREQ("link1", FocusedOnPage(web_contents).c_str()); |
| 186 |
| 187 // Search for a text that exists within a link on the page. |
| 188 EXPECT_EQ(1, FindInPage(L"Google", FWD, IGNORE_CASE, false)); |
| 189 |
| 190 // Move the selection to link 1, after searching. |
| 191 ui_test_utils::JavaScriptRunner js_runner( |
| 192 web_contents, |
| 193 L"", |
| 194 L"window.domAutomationController.send(selectLink1());"); |
| 195 js_runner.Run(); |
| 196 |
| 197 // End the find session. |
| 198 web_contents->StopFinding(false); |
| 199 |
| 200 // Verify that link2 is not focused. |
| 201 EXPECT_STREQ("", FocusedOnPage(web_contents).c_str()); |
| 202 } |
| OLD | NEW |