Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / ui / find_bar / find_bar_host_browsertest.cc
blobfa637deaa05f57beed47cb14a41e8cb0b7b5af83
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_util.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_navigator.h"
18 #include "chrome/browser/ui/browser_tabstrip.h"
19 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/browser/ui/chrome_pages.h"
21 #include "chrome/browser/ui/find_bar/find_bar.h"
22 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
23 #include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h"
24 #include "chrome/browser/ui/find_bar/find_notification_details.h"
25 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/test/base/find_in_page_observer.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h"
31 #include "components/history/core/browser/history_service.h"
32 #include "content/public/browser/download_manager.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/test/browser_test_utils.h"
38 #include "net/base/filename_util.h"
39 #include "ui/base/accelerators/accelerator.h"
40 #include "ui/events/keycodes/keyboard_codes.h"
42 #if defined(OS_WIN)
43 #include "ui/aura/window.h"
44 #include "ui/aura/window_tree_host.h"
45 #endif
47 using base::ASCIIToUTF16;
48 using base::WideToUTF16;
49 using content::NavigationController;
50 using content::WebContents;
52 namespace {
54 const char kAnchorPage[] = "anchor.html";
55 const char kAnchor[] = "#chapter2";
56 const char kFramePage[] = "frames.html";
57 const char kFrameData[] = "framedata_general.html";
58 const char kUserSelectPage[] = "user-select.html";
59 const char kCrashPage[] = "crash_1341577.html";
60 const char kTooFewMatchesPage[] = "bug_1155639.html";
61 const char kLongTextareaPage[] = "large_textarea.html";
62 const char kPrematureEnd[] = "premature_end.html";
63 const char kMoveIfOver[] = "move_if_obscuring.html";
64 const char kBitstackCrash[] = "crash_14491.html";
65 const char kSelectChangesOrdinal[] = "select_changes_ordinal.html";
66 const char kStartAfterSelection[] = "start_after_selection.html";
67 const char kSimple[] = "simple.html";
68 const char kLinkPage[] = "link.html";
70 const bool kBack = false;
71 const bool kFwd = true;
73 const bool kIgnoreCase = false;
74 const bool kCaseSensitive = true;
76 const int kMoveIterations = 30;
78 } // namespace
80 class FindInPageControllerTest : public InProcessBrowserTest {
81 public:
82 FindInPageControllerTest() {
83 chrome::DisableFindBarAnimationsDuringTesting(true);
86 protected:
87 bool GetFindBarWindowInfoForBrowser(
88 Browser* browser, gfx::Point* position, bool* fully_visible) {
89 FindBarTesting* find_bar =
90 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
91 return find_bar->GetFindBarWindowInfo(position, fully_visible);
94 bool GetFindBarWindowInfo(gfx::Point* position, bool* fully_visible) {
95 return GetFindBarWindowInfoForBrowser(browser(), position, fully_visible);
98 base::string16 GetFindBarTextForBrowser(Browser* browser) {
99 FindBar* find_bar = browser->GetFindBarController()->find_bar();
100 return find_bar->GetFindText();
103 base::string16 GetFindBarText() {
104 return GetFindBarTextForBrowser(browser());
107 base::string16 GetFindBarMatchCountTextForBrowser(Browser* browser) {
108 FindBarTesting* find_bar =
109 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
110 return find_bar->GetMatchCountText();
113 base::string16 GetMatchCountText() {
114 return GetFindBarMatchCountTextForBrowser(browser());
117 int GetFindBarWidthForBrowser(Browser* browser) {
118 FindBarTesting* find_bar =
119 browser->GetFindBarController()->find_bar()->GetFindBarTesting();
120 return find_bar->GetWidth();
123 void EnsureFindBoxOpenForBrowser(Browser* browser) {
124 chrome::ShowFindBar(browser);
125 gfx::Point position;
126 bool fully_visible = false;
127 EXPECT_TRUE(GetFindBarWindowInfoForBrowser(
128 browser, &position, &fully_visible));
129 EXPECT_TRUE(fully_visible);
132 void EnsureFindBoxOpen() {
133 EnsureFindBoxOpenForBrowser(browser());
136 int FindInPage16(WebContents* web_contents,
137 const base::string16& search_str,
138 bool forward,
139 bool case_sensitive,
140 int* ordinal) {
141 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
142 browser->GetFindBarController()->find_bar()->SetFindTextAndSelectedRange(
143 search_str, gfx::Range());
144 return ui_test_utils::FindInPage(
145 web_contents, search_str, forward, case_sensitive, ordinal, NULL);
148 int FindInPageASCII(WebContents* web_contents,
149 const std::string& search_str,
150 bool forward,
151 bool case_sensitive,
152 int* ordinal) {
153 return FindInPage16(web_contents, ASCIIToUTF16(search_str), forward,
154 case_sensitive, ordinal);
157 // Calls FindInPageASCII till the find box's x position != |start_x_position|.
158 // Return |start_x_position| if the find box has not moved after iterating
159 // through all matches of |search_str|.
160 int FindInPageTillBoxMoves(WebContents* web_contents,
161 int start_x_position,
162 const std::string& search_str,
163 int expected_matches) {
164 // Search for |search_str| which the Find box is obscuring.
165 for (int index = 0; index < expected_matches; ++index) {
166 int ordinal = 0;
167 EXPECT_EQ(expected_matches, FindInPageASCII(web_contents, search_str,
168 kFwd, kIgnoreCase, &ordinal));
170 // Check the position.
171 bool fully_visible;
172 gfx::Point position;
173 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
174 EXPECT_TRUE(fully_visible);
176 // If the Find box has moved then we are done.
177 if (position.x() != start_x_position)
178 return position.x();
180 return start_x_position;
183 GURL GetURL(const std::string& filename) {
184 return ui_test_utils::GetTestUrl(
185 base::FilePath().AppendASCII("find_in_page"),
186 base::FilePath().AppendASCII(filename));
189 void FlushHistoryService() {
190 HistoryServiceFactory::GetForProfile(browser()->profile(),
191 ServiceAccessType::IMPLICIT_ACCESS)
192 ->FlushForTest(base::Bind(
193 &base::MessageLoop::Quit,
194 base::Unretained(base::MessageLoop::current()->current())));
195 content::RunMessageLoop();
199 // This test loads a page with frames and starts FindInPage requests.
200 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) {
201 // First we navigate to our frames page.
202 GURL url = GetURL(kFramePage);
203 ui_test_utils::NavigateToURL(browser(), url);
205 // Try incremental search (mimicking user typing in).
206 int ordinal = 0;
207 WebContents* web_contents =
208 browser()->tab_strip_model()->GetActiveWebContents();
209 EXPECT_EQ(18, FindInPageASCII(web_contents, "g",
210 kFwd, kIgnoreCase, &ordinal));
211 EXPECT_EQ(1, ordinal);
212 EXPECT_EQ(11, FindInPageASCII(web_contents, "go",
213 kFwd, kIgnoreCase, &ordinal));
214 EXPECT_EQ(1, ordinal);
215 EXPECT_EQ(4, FindInPageASCII(web_contents, "goo",
216 kFwd, kIgnoreCase, &ordinal));
217 EXPECT_EQ(1, ordinal);
218 EXPECT_EQ(3, FindInPageASCII(web_contents, "goog",
219 kFwd, kIgnoreCase, &ordinal));
220 EXPECT_EQ(1, ordinal);
221 EXPECT_EQ(2, FindInPageASCII(web_contents, "googl",
222 kFwd, kIgnoreCase, &ordinal));
223 EXPECT_EQ(1, ordinal);
224 EXPECT_EQ(1, FindInPageASCII(web_contents, "google",
225 kFwd, kIgnoreCase, &ordinal));
226 EXPECT_EQ(1, ordinal);
227 EXPECT_EQ(0, FindInPageASCII(web_contents, "google!",
228 kFwd, kIgnoreCase, &ordinal));
229 EXPECT_EQ(0, ordinal);
231 // Negative test (no matches should be found).
232 EXPECT_EQ(0, FindInPageASCII(web_contents, "Non-existing string",
233 kFwd, kIgnoreCase, &ordinal));
234 EXPECT_EQ(0, ordinal);
236 // 'horse' only exists in the three right frames.
237 EXPECT_EQ(3, FindInPageASCII(web_contents, "horse",
238 kFwd, kIgnoreCase, &ordinal));
239 EXPECT_EQ(1, ordinal);
241 // 'cat' only exists in the first frame.
242 EXPECT_EQ(1, FindInPageASCII(web_contents, "cat",
243 kFwd, kIgnoreCase, &ordinal));
244 EXPECT_EQ(1, ordinal);
246 // Try searching again, should still come up with 1 match.
247 EXPECT_EQ(1, FindInPageASCII(web_contents, "cat",
248 kFwd, kIgnoreCase, &ordinal));
249 EXPECT_EQ(1, ordinal);
251 // Try searching backwards, ignoring case, should still come up with 1 match.
252 EXPECT_EQ(1, FindInPageASCII(web_contents, "CAT",
253 kBack, kIgnoreCase, &ordinal));
254 EXPECT_EQ(1, ordinal);
256 // Try case sensitive, should NOT find it.
257 EXPECT_EQ(0, FindInPageASCII(web_contents, "CAT",
258 kFwd, kCaseSensitive, &ordinal));
259 EXPECT_EQ(0, ordinal);
261 // Try again case sensitive, but this time with right case.
262 EXPECT_EQ(1, FindInPageASCII(web_contents, "dog",
263 kFwd, kCaseSensitive, &ordinal));
264 EXPECT_EQ(1, ordinal);
266 // Try non-Latin characters ('Hreggvidur' with 'eth' for 'd' in left frame).
267 EXPECT_EQ(1, FindInPage16(web_contents, WideToUTF16(L"Hreggvi\u00F0ur"),
268 kFwd, kIgnoreCase, &ordinal));
269 EXPECT_EQ(1, ordinal);
270 EXPECT_EQ(1, FindInPage16(web_contents, WideToUTF16(L"Hreggvi\u00F0ur"),
271 kFwd, kCaseSensitive, &ordinal));
272 EXPECT_EQ(1, ordinal);
273 EXPECT_EQ(0, FindInPage16(web_contents, WideToUTF16(L"hreggvi\u00F0ur"),
274 kFwd, kCaseSensitive, &ordinal));
275 EXPECT_EQ(0, ordinal);
278 // Verify search for text within various forms and text areas.
279 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFormsTextAreas) {
280 std::vector<GURL> urls;
281 urls.push_back(GetURL("textintextarea.html"));
282 urls.push_back(GetURL("smalltextarea.html"));
283 urls.push_back(GetURL("populatedform.html"));
284 WebContents* web_contents =
285 browser()->tab_strip_model()->GetActiveWebContents();
287 for (size_t i = 0; i < urls.size(); ++i) {
288 ui_test_utils::NavigateToURL(browser(), urls[i]);
289 EXPECT_EQ(1, FindInPageASCII(web_contents, "cat",
290 kFwd, kIgnoreCase, NULL));
291 EXPECT_EQ(0, FindInPageASCII(web_contents, "bat",
292 kFwd, kIgnoreCase, NULL));
296 // Verify search for text within special URLs such as chrome:history,
297 // chrome://downloads, data directory
298 #if defined(OS_WIN) || defined(OS_MACOSX)
299 // Disabled due to crbug.com/175711 and http://crbug.com/419987
300 #define MAYBE_SearchWithinSpecialURL \
301 DISABLED_SearchWithinSpecialURL
302 #else
303 #define MAYBE_SearchWithinSpecialURL \
304 SearchWithinSpecialURL
305 #endif
306 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_SearchWithinSpecialURL) {
307 WebContents* web_contents =
308 browser()->tab_strip_model()->GetActiveWebContents();
310 base::FilePath data_dir =
311 ui_test_utils::GetTestFilePath(base::FilePath(), base::FilePath());
312 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(data_dir));
313 EXPECT_EQ(1, FindInPageASCII(web_contents, "downloads",
314 kFwd, kIgnoreCase, NULL));
316 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL));
318 // The history page does an async request to the history service and then
319 // updates the renderer. So we make a query as well, and by the time it comes
320 // back we know the data is on its way to the renderer.
321 FlushHistoryService();
323 base::string16 query(data_dir.LossyDisplayName());
324 EXPECT_EQ(1,
325 ui_test_utils::FindInPage(web_contents, query,
326 kFwd, kIgnoreCase, NULL, NULL));
328 GURL download_url = ui_test_utils::GetTestUrl(
329 base::FilePath().AppendASCII("downloads"),
330 base::FilePath().AppendASCII("a_zip_file.zip"));
331 ui_test_utils::DownloadURL(browser(), download_url);
333 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
334 FlushHistoryService();
335 EXPECT_EQ(1, FindInPageASCII(web_contents, download_url.spec(),
336 kFwd, kIgnoreCase, NULL));
339 #if defined(OS_MACOSX)
340 // Disabled due to http://crbug.com/419769.
341 #define MAYBE_FindInPageSpecialURLs DISABLED_FindInPageSpecialURLs
342 #else
343 #define MAYBE_FindInPageSpecialURLs FindInPageSpecialURLs
344 #endif
345 // Verify search selection coordinates. The data file used is set-up such that
346 // the text occurs on the same line, and we verify their positions by verifying
347 // their relative positions.
348 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_FindInPageSpecialURLs) {
349 const std::wstring search_string(L"\u5728\u897f\u660c\u536b\u661f\u53d1");
350 gfx::Rect first, second, first_reverse;
351 WebContents* web_contents =
352 browser()->tab_strip_model()->GetActiveWebContents();
353 ui_test_utils::NavigateToURL(browser(), GetURL("specialchar.html"));
354 ui_test_utils::FindInPage(web_contents, WideToUTF16(search_string),
355 kFwd, kIgnoreCase, NULL, &first);
356 ui_test_utils::FindInPage(web_contents, WideToUTF16(search_string),
357 kFwd, kIgnoreCase, NULL, &second);
359 // We have search occurrence in the same row, so top-bottom coordinates should
360 // be the same even for second search.
361 ASSERT_EQ(first.y(), second.y());
362 ASSERT_EQ(first.bottom(), second.bottom());
363 ASSERT_LT(first.x(), second.x());
364 ASSERT_LT(first.right(), second.right());
366 ui_test_utils::FindInPage(
367 web_contents, WideToUTF16(search_string), kBack, kIgnoreCase, NULL,
368 &first_reverse);
369 // We find next and we go back so find coordinates should be the same as
370 // previous ones.
371 ASSERT_EQ(first, first_reverse);
374 #if defined(OS_MACOSX)
375 // Disabled due to http://crbug.com/419769.
376 #define MAYBE_CommentsAndMetaDataNotSearchable \
377 DISABLED_CommentsAndMetaDataNotSearchable
378 #else
379 #define MAYBE_CommentsAndMetaDataNotSearchable CommentsAndMetaDataNotSearchable
380 #endif
381 // Verifies that comments and meta data are not searchable.
382 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
383 MAYBE_CommentsAndMetaDataNotSearchable) {
384 WebContents* web_contents =
385 browser()->tab_strip_model()->GetActiveWebContents();
386 ui_test_utils::NavigateToURL(browser(), GetURL("specialchar.html"));
388 const std::wstring search_string =
389 L"\u4e2d\u65b0\u793e\u8bb0\u8005\u5b8b\u5409\u6cb3\u6444\u4e2d\u65b0\u7f51";
390 EXPECT_EQ(0, ui_test_utils::FindInPage(
391 web_contents, WideToUTF16(search_string), kFwd, kIgnoreCase, NULL, NULL));
394 // Verifies that span and lists are searchable.
395 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, SpanAndListsSearchable) {
396 WebContents* web_contents =
397 browser()->tab_strip_model()->GetActiveWebContents();
398 ui_test_utils::NavigateToURL(browser(), GetURL("FindRandomTests.html"));
400 std::string search_string = "has light blue eyes and my father has dark";
401 EXPECT_EQ(1,
402 ui_test_utils::FindInPage(web_contents, ASCIIToUTF16(search_string),
403 kFwd, kIgnoreCase, NULL, NULL));
405 search_string = "Google\nApple\nandroid";
406 EXPECT_EQ(1,
407 ui_test_utils::FindInPage(web_contents, ASCIIToUTF16(search_string),
408 kFwd, kIgnoreCase, NULL, NULL));
411 // Find in a very large page.
412 // Disabled due to http://crbug.com/398017
413 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, DISABLED_LargePage) {
414 WebContents* web_contents =
415 browser()->tab_strip_model()->GetActiveWebContents();
416 ui_test_utils::NavigateToURL(browser(), GetURL("largepage.html"));
418 EXPECT_EQ(373, FindInPageASCII(web_contents, "daughter of Prince",
419 kFwd, kIgnoreCase, NULL));
422 // Find a very long string in a large page.
423 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindLongString) {
424 WebContents* web_contents =
425 browser()->tab_strip_model()->GetActiveWebContents();
426 ui_test_utils::NavigateToURL(browser(), GetURL("largepage.html"));
428 base::FilePath path = ui_test_utils::GetTestFilePath(
429 base::FilePath().AppendASCII("find_in_page"),
430 base::FilePath().AppendASCII("LongFind.txt"));
431 std::string query;
432 base::ReadFileToString(path, &query);
433 EXPECT_EQ(1, FindInPage16(web_contents, base::UTF8ToUTF16(query),
434 kFwd, kIgnoreCase, NULL));
437 // Find a big font string in a page.
438 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, BigString) {
439 WebContents* web_contents =
440 browser()->tab_strip_model()->GetActiveWebContents();
441 ui_test_utils::NavigateToURL(browser(), GetURL("BigText.html"));
442 EXPECT_EQ(1, FindInPageASCII(web_contents, "SomeLargeString",
443 kFwd, kIgnoreCase, NULL));
446 // http://crbug.com/369169
447 #if defined(OS_CHROMEOS)
448 #define MAYBE_SingleOccurrence DISABLED_SingleOccurrence
449 #else
450 #define MAYBE_SingleOccurrence SingleOccurrence
451 #endif
452 // Search Back and Forward on a single occurrence.
453 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_SingleOccurrence) {
454 WebContents* web_contents =
455 browser()->tab_strip_model()->GetActiveWebContents();
456 ui_test_utils::NavigateToURL(browser(), GetURL("FindRandomTests.html"));
458 gfx::Rect first_rect;
459 EXPECT_EQ(1,
460 ui_test_utils::FindInPage(web_contents,
461 ASCIIToUTF16("2010 Pro Bowl"), kFwd,
462 kIgnoreCase, NULL, &first_rect));
464 gfx::Rect second_rect;
465 EXPECT_EQ(1,
466 ui_test_utils::FindInPage(web_contents,
467 ASCIIToUTF16("2010 Pro Bowl"), kFwd,
468 kIgnoreCase, NULL, &second_rect));
470 // Doing a fake find so we have no previous search.
471 ui_test_utils::FindInPage(web_contents, ASCIIToUTF16("ghgfjgfh201232rere"),
472 kFwd, kIgnoreCase, NULL, NULL);
474 ASSERT_EQ(first_rect, second_rect);
476 EXPECT_EQ(1,
477 ui_test_utils::FindInPage(web_contents,
478 ASCIIToUTF16("2010 Pro Bowl"), kFwd,
479 kIgnoreCase, NULL, &first_rect));
480 EXPECT_EQ(1,
481 ui_test_utils::FindInPage(web_contents,
482 ASCIIToUTF16("2010 Pro Bowl"), kBack,
483 kIgnoreCase, NULL, &second_rect));
484 ASSERT_EQ(first_rect, second_rect);
487 // Find the whole text file page and find count should be 1.
488 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindWholeFileContent) {
489 WebContents* web_contents =
490 browser()->tab_strip_model()->GetActiveWebContents();
492 base::FilePath path = ui_test_utils::GetTestFilePath(
493 base::FilePath().AppendASCII("find_in_page"),
494 base::FilePath().AppendASCII("find_test.txt"));
495 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(path));
497 std::string query;
498 base::ReadFileToString(path, &query);
499 EXPECT_EQ(1, FindInPage16(web_contents, base::UTF8ToUTF16(query),
500 false, false, NULL));
503 // This test loads a single-frame page and makes sure the ordinal returned makes
504 // sense as we FindNext over all the items.
505 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageOrdinal) {
506 // First we navigate to our page.
507 GURL url = GetURL(kFrameData);
508 ui_test_utils::NavigateToURL(browser(), url);
510 // Search for 'o', which should make the first item active and return
511 // '1 in 3' (1st ordinal of a total of 3 matches).
512 WebContents* web_contents =
513 browser()->tab_strip_model()->GetActiveWebContents();
514 int ordinal = 0;
515 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
516 kFwd, kIgnoreCase, &ordinal));
517 EXPECT_EQ(1, ordinal);
518 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
519 kFwd, kIgnoreCase, &ordinal));
520 EXPECT_EQ(2, ordinal);
521 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
522 kFwd, kIgnoreCase, &ordinal));
523 EXPECT_EQ(3, ordinal);
524 // Go back one match.
525 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
526 kBack, kIgnoreCase, &ordinal));
527 EXPECT_EQ(2, ordinal);
528 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
529 kFwd, kIgnoreCase, &ordinal));
530 EXPECT_EQ(3, ordinal);
531 // This should wrap to the top.
532 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
533 kFwd, kIgnoreCase, &ordinal));
534 EXPECT_EQ(1, ordinal);
535 // This should go back to the end.
536 EXPECT_EQ(3, FindInPageASCII(web_contents, "o",
537 kBack, kIgnoreCase, &ordinal));
538 EXPECT_EQ(3, ordinal);
541 // This tests that the ordinal is correctly adjusted after a selection
542 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
543 SelectChangesOrdinal_Issue20883) {
544 // First we navigate to our test content.
545 GURL url = GetURL(kSelectChangesOrdinal);
546 ui_test_utils::NavigateToURL(browser(), url);
548 // Search for a text that exists within a link on the page.
549 WebContents* web_contents =
550 browser()->tab_strip_model()->GetActiveWebContents();
551 ASSERT_TRUE(NULL != web_contents);
552 FindTabHelper* find_tab_helper =
553 FindTabHelper::FromWebContents(web_contents);
555 int ordinal = 0;
556 EXPECT_EQ(4, FindInPageASCII(web_contents, "google",
557 kFwd, kIgnoreCase, &ordinal));
558 EXPECT_EQ(1, ordinal);
560 // Move the selection to link 1, after searching.
561 std::string result;
562 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
563 web_contents,
564 "window.domAutomationController.send(selectLink1());",
565 &result));
567 // Do a find-next after the selection. This should move forward
568 // from there to the 3rd instance of 'google'.
569 EXPECT_EQ(4, FindInPageASCII(web_contents, "google",
570 kFwd, kIgnoreCase, &ordinal));
571 EXPECT_EQ(3, ordinal);
573 // End the find session.
574 find_tab_helper->StopFinding(FindBarController::kKeepSelectionOnPage);
577 // This tests that we start searching after selected text.
578 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
579 StartSearchAfterSelection) {
580 // First we navigate to our test content.
581 ui_test_utils::NavigateToURL(browser(), GetURL(kStartAfterSelection));
583 WebContents* web_contents =
584 browser()->tab_strip_model()->GetActiveWebContents();
585 ASSERT_TRUE(web_contents != NULL);
586 int ordinal = 0;
588 // Move the selection to the text span.
589 std::string result;
590 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
591 web_contents,
592 "window.domAutomationController.send(selectSpan());",
593 &result));
595 // Do a find-next after the selection. This should select the 2nd occurrence
596 // of the word 'find'.
597 EXPECT_EQ(4, FindInPageASCII(web_contents, "fi",
598 kFwd, kIgnoreCase, &ordinal));
599 EXPECT_EQ(2, ordinal);
601 // Refine the search, current active match should not change.
602 EXPECT_EQ(4, FindInPageASCII(web_contents, "find",
603 kFwd, kIgnoreCase, &ordinal));
604 EXPECT_EQ(2, ordinal);
606 // Refine the search to 'findMe'. The first new match is before the current
607 // active match, the second one is after it. This verifies that refining a
608 // search doesn't reset it.
609 EXPECT_EQ(2, FindInPageASCII(web_contents, "findMe",
610 kFwd, kIgnoreCase, &ordinal));
611 EXPECT_EQ(2, ordinal);
614 // This test loads a page with frames and makes sure the ordinal returned makes
615 // sense.
616 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageMultiFramesOrdinal) {
617 // First we navigate to our page.
618 GURL url = GetURL(kFramePage);
619 ui_test_utils::NavigateToURL(browser(), url);
621 // Search for 'a', which should make the first item active and return
622 // '1 in 7' (1st ordinal of a total of 7 matches).
623 WebContents* web_contents =
624 browser()->tab_strip_model()->GetActiveWebContents();
625 int ordinal = 0;
626 EXPECT_EQ(7,
627 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
628 EXPECT_EQ(1, ordinal);
629 EXPECT_EQ(7,
630 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
631 EXPECT_EQ(2, ordinal);
632 EXPECT_EQ(7,
633 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
634 EXPECT_EQ(3, ordinal);
635 EXPECT_EQ(7,
636 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
637 EXPECT_EQ(4, ordinal);
638 // Go back one, which should go back one frame.
639 EXPECT_EQ(7,
640 FindInPageASCII(web_contents, "a", kBack, kIgnoreCase, &ordinal));
641 EXPECT_EQ(3, ordinal);
642 EXPECT_EQ(7,
643 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
644 EXPECT_EQ(4, ordinal);
645 EXPECT_EQ(7,
646 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
647 EXPECT_EQ(5, ordinal);
648 EXPECT_EQ(7,
649 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
650 EXPECT_EQ(6, ordinal);
651 EXPECT_EQ(7,
652 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
653 EXPECT_EQ(7, ordinal);
654 // Now we should wrap back to frame 1.
655 EXPECT_EQ(7,
656 FindInPageASCII(web_contents, "a", kFwd, kIgnoreCase, &ordinal));
657 EXPECT_EQ(1, ordinal);
658 // Now we should wrap back to frame last frame.
659 EXPECT_EQ(7,
660 FindInPageASCII(web_contents, "a", kBack, kIgnoreCase, &ordinal));
661 EXPECT_EQ(7, ordinal);
664 // We could get ordinals out of whack when restarting search in subframes.
665 // See http://crbug.com/5132.
666 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPage_Issue5132) {
667 // First we navigate to our page.
668 GURL url = GetURL(kFramePage);
669 ui_test_utils::NavigateToURL(browser(), url);
671 // Search for 'goa' three times (6 matches on page).
672 int ordinal = 0;
673 WebContents* web_contents =
674 browser()->tab_strip_model()->GetActiveWebContents();
675 EXPECT_EQ(6, FindInPageASCII(web_contents, "goa",
676 kFwd, kIgnoreCase, &ordinal));
677 EXPECT_EQ(1, ordinal);
678 EXPECT_EQ(6, FindInPageASCII(web_contents, "goa",
679 kFwd, kIgnoreCase, &ordinal));
680 EXPECT_EQ(2, ordinal);
681 EXPECT_EQ(6, FindInPageASCII(web_contents, "goa",
682 kFwd, kIgnoreCase, &ordinal));
683 EXPECT_EQ(3, ordinal);
684 // Add space to search (should result in no matches).
685 EXPECT_EQ(0, FindInPageASCII(web_contents, "goa ",
686 kFwd, kIgnoreCase, &ordinal));
687 EXPECT_EQ(0, ordinal);
688 // Remove the space, should be back to '3 out of 6')
689 EXPECT_EQ(6, FindInPageASCII(web_contents, "goa",
690 kFwd, kIgnoreCase, &ordinal));
691 EXPECT_EQ(3, ordinal);
694 // This tests that the ordinal and match count is cleared after a navigation,
695 // as reported in issue http://crbug.com/126468.
696 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, NavigateClearsOrdinal) {
697 // First we navigate to our test content.
698 GURL url = GetURL(kSimple);
699 ui_test_utils::NavigateToURL(browser(), url);
701 // Open the Find box. In most tests we can just search without opening the
702 // box first, but in this case we are testing functionality triggered by
703 // NOTIFICATION_NAV_ENTRY_COMMITTED in the FindBarController and the observer
704 // for that event isn't setup unless the box is open.
705 EnsureFindBoxOpen();
707 // Search for a text that exists within a link on the page.
708 WebContents* web_contents =
709 browser()->tab_strip_model()->GetActiveWebContents();
710 ASSERT_TRUE(NULL != web_contents);
711 int ordinal = 0;
712 EXPECT_EQ(8, FindInPageASCII(web_contents, "e",
713 kFwd, kIgnoreCase, &ordinal));
714 EXPECT_EQ(1, ordinal);
716 // Then navigate away (to any page).
717 url = GetURL(kLinkPage);
718 ui_test_utils::NavigateToURL(browser(), url);
720 // Open the Find box again.
721 EnsureFindBoxOpen();
723 EXPECT_EQ(ASCIIToUTF16("e"), GetFindBarText());
724 EXPECT_TRUE(GetMatchCountText().empty());
727 // Load a page with no selectable text and make sure we don't crash.
728 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindUnselectableText) {
729 // First we navigate to our page.
730 GURL url = GetURL(kUserSelectPage);
731 ui_test_utils::NavigateToURL(browser(), url);
733 int ordinal = 0;
734 WebContents* web_contents =
735 browser()->tab_strip_model()->GetActiveWebContents();
736 EXPECT_EQ(1, FindInPageASCII(web_contents, "text",
737 kFwd, kIgnoreCase, &ordinal));
738 EXPECT_EQ(1, ordinal);
741 // Try to reproduce the crash seen in issue 1341577.
742 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue1341577) {
743 // First we navigate to our page.
744 GURL url = GetURL(kCrashPage);
745 ui_test_utils::NavigateToURL(browser(), url);
747 // This would crash the tab. These must be the first two find requests issued
748 // against the frame, otherwise an active frame pointer is set and it wont
749 // produce the crash.
750 // We used to check the return value and |ordinal|. With ICU 4.2, FiP does
751 // not find a stand-alone dependent vowel sign of Indic scripts. So, the
752 // exptected values are all 0. To make this test pass regardless of
753 // ICU version, we just call FiP and see if there's any crash.
754 // TODO(jungshik): According to a native Malayalam speaker, it's ok not
755 // to find U+0D4C. Still need to investigate further this issue.
756 int ordinal = 0;
757 WebContents* web_contents =
758 browser()->tab_strip_model()->GetActiveWebContents();
759 const base::string16 search_str = WideToUTF16(L"\u0D4C");
760 FindInPage16(web_contents, search_str, kFwd, kIgnoreCase, &ordinal);
761 FindInPage16(web_contents, search_str, kFwd, kIgnoreCase, &ordinal);
763 // This should work fine.
764 EXPECT_EQ(1, FindInPage16(web_contents, WideToUTF16(L"\u0D24\u0D46"),
765 kFwd, kIgnoreCase, &ordinal));
766 EXPECT_EQ(1, ordinal);
767 EXPECT_EQ(0, FindInPageASCII(web_contents, "nostring",
768 kFwd, kIgnoreCase, &ordinal));
769 EXPECT_EQ(0, ordinal);
772 // Try to reproduce the crash seen in http://crbug.com/14491, where an assert
773 // hits in the BitStack size comparison in WebKit.
774 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue14491) {
775 // First we navigate to our page.
776 GURL url = GetURL(kBitstackCrash);
777 ui_test_utils::NavigateToURL(browser(), url);
779 // This used to crash the tab.
780 int ordinal = 0;
781 EXPECT_EQ(0, FindInPageASCII(browser()->tab_strip_model()->
782 GetActiveWebContents(),
783 "s", kFwd, kIgnoreCase, &ordinal));
784 EXPECT_EQ(0, ordinal);
787 // Test to make sure Find does the right thing when restarting from a timeout.
788 // We used to have a problem where we'd stop finding matches when all of the
789 // following conditions were true:
790 // 1) The page has a lot of text to search.
791 // 2) The page contains more than one match.
792 // 3) It takes longer than the time-slice given to each Find operation (100
793 // ms) to find one or more of those matches (so Find times out and has to try
794 // again from where it left off).
795 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindRestarts_Issue1155639) {
796 // First we navigate to our page.
797 GURL url = GetURL(kTooFewMatchesPage);
798 ui_test_utils::NavigateToURL(browser(), url);
800 // This string appears 5 times at the bottom of a long page. If Find restarts
801 // properly after a timeout, it will find 5 matches, not just 1.
802 int ordinal = 0;
803 EXPECT_EQ(5, FindInPageASCII(browser()->tab_strip_model()->
804 GetActiveWebContents(),
805 "008.xml", kFwd, kIgnoreCase, &ordinal));
806 EXPECT_EQ(1, ordinal);
809 // Make sure we don't get into an infinite loop when text box contains very
810 // large amount of text.
811 // Disable the test as it started being flaky, see http://crbug/367701.
812 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
813 DISABLED_FindRestarts_Issue70505) {
814 // First we navigate to our page.
815 GURL url = GetURL(kLongTextareaPage);
816 ui_test_utils::NavigateToURL(browser(), url);
818 // If this test hangs on the FindInPage call, then it might be a regression
819 // such as the one found in issue http://crbug.com/70505.
820 int ordinal = 0;
821 FindInPageASCII(browser()->tab_strip_model()->GetActiveWebContents(),
822 "a", kFwd, kIgnoreCase, &ordinal);
823 EXPECT_EQ(1, ordinal);
824 // TODO(finnur): We cannot reliably get the matchcount for this Find call
825 // until we fix issue http://crbug.com/71176.
828 // This tests bug 11761: FindInPage terminates search prematurely.
829 // This test is not expected to pass until bug 11761 is fixed.
830 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
831 DISABLED_FindInPagePrematureEnd) {
832 // First we navigate to our special focus tracking page.
833 GURL url = GetURL(kPrematureEnd);
834 ui_test_utils::NavigateToURL(browser(), url);
836 WebContents* web_contents =
837 browser()->tab_strip_model()->GetActiveWebContents();
838 ASSERT_TRUE(NULL != web_contents);
840 // Search for a text that exists within a link on the page.
841 int ordinal = 0;
842 EXPECT_EQ(2, FindInPageASCII(web_contents, "html ",
843 kFwd, kIgnoreCase, &ordinal));
844 EXPECT_EQ(1, ordinal);
847 // Verify that the find bar is hidden on reload and navigation.
848 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
849 HideFindBarOnNavigateAndReload) {
850 // First we navigate to our special focus tracking page.
851 GURL url = GetURL(kSimple);
852 GURL url2 = GetURL(kFramePage);
853 ui_test_utils::NavigateToURL(browser(), url);
855 chrome::ShowFindBar(browser());
857 gfx::Point position;
858 bool fully_visible = false;
860 // Make sure it is open.
861 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
862 EXPECT_TRUE(fully_visible);
864 // Reload and make sure the find window goes away.
865 content::WindowedNotificationObserver observer(
866 content::NOTIFICATION_LOAD_STOP,
867 content::Source<NavigationController>(
868 &browser()->tab_strip_model()->GetActiveWebContents()->
869 GetController()));
870 chrome::Reload(browser(), CURRENT_TAB);
871 observer.Wait();
872 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
873 EXPECT_FALSE(fully_visible);
875 // Open the find bar again.
876 chrome::ShowFindBar(browser());
878 // Make sure it is open.
879 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
880 EXPECT_TRUE(fully_visible);
882 // Navigate and make sure the find window goes away.
883 ui_test_utils::NavigateToURL(browser(), url2);
885 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
886 EXPECT_FALSE(fully_visible);
889 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindStayVisibleOnAnchorLoad) {
890 // First we navigate to our special focus tracking page.
891 GURL url = GetURL(kAnchorPage);
892 ui_test_utils::NavigateToURL(browser(), url);
894 chrome::ShowFindBar(browser());
896 gfx::Point position;
897 bool fully_visible = false;
899 // Make sure it is open.
900 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
901 EXPECT_TRUE(fully_visible);
903 // Navigate to the same page (but add an anchor/ref/fragment/whatever the kids
904 // are calling it these days).
905 GURL url_with_anchor = url.Resolve(kAnchor);
906 ui_test_utils::NavigateToURL(browser(), url_with_anchor);
908 // Make sure it is still open.
909 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
910 EXPECT_TRUE(fully_visible);
913 // FindDisappearOnNewTabAndHistory is flaky, at least on Mac.
914 // See http://crbug.com/43072
915 #if defined(OS_MACOSX)
916 #define MAYBE_FindDisappearOnNewTabAndHistory DISABLED_FindDisappearOnNewTabAndHistory
917 #else
918 #define MAYBE_FindDisappearOnNewTabAndHistory FindDisappearOnNewTabAndHistory
919 #endif
921 // Make sure Find box disappears when History/Downloads page is opened, and
922 // when a New Tab is opened.
923 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
924 MAYBE_FindDisappearOnNewTabAndHistory) {
925 // First we navigate to our special focus tracking page.
926 GURL url = GetURL(kSimple);
927 ui_test_utils::NavigateToURL(browser(), url);
929 chrome::ShowFindBar(browser());
931 gfx::Point position;
932 bool fully_visible = false;
934 // Make sure it is open.
935 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
936 EXPECT_TRUE(fully_visible);
938 // Open another tab (tab B).
939 chrome::NewTab(browser());
940 ui_test_utils::NavigateToURL(browser(), url);
942 // Make sure Find box is closed.
943 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
944 EXPECT_FALSE(fully_visible);
946 // Close tab B.
947 chrome::CloseTab(browser());
949 // Make sure Find window appears again.
950 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
951 EXPECT_TRUE(fully_visible);
953 chrome::ShowHistory(browser());
955 // Make sure Find box is closed.
956 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
957 EXPECT_FALSE(fully_visible);
960 // Make sure Find box moves out of the way if it is obscuring the active match.
961 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindMovesWhenObscuring) {
962 GURL url = GetURL(kMoveIfOver);
963 ui_test_utils::NavigateToURL(browser(), url);
965 chrome::ShowFindBar(browser());
967 // This is needed on GTK because the reposition operation is asynchronous.
968 base::MessageLoop::current()->RunUntilIdle();
970 gfx::Point start_position;
971 gfx::Point position;
972 bool fully_visible = false;
973 int ordinal = 0;
975 // Make sure it is open.
976 EXPECT_TRUE(GetFindBarWindowInfo(&start_position, &fully_visible));
977 EXPECT_TRUE(fully_visible);
979 WebContents* web_contents =
980 browser()->tab_strip_model()->GetActiveWebContents();
982 int moved_x_coord = FindInPageTillBoxMoves(web_contents, start_position.x(),
983 "Chromium", kMoveIterations);
984 // The find box should have moved.
985 EXPECT_TRUE(moved_x_coord != start_position.x());
987 // Search for something guaranteed not to be obscured by the Find box.
988 EXPECT_EQ(1, FindInPageASCII(web_contents, "Done",
989 kFwd, kIgnoreCase, &ordinal));
990 // Check the position.
991 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
992 EXPECT_TRUE(fully_visible);
994 // Make sure Find box has moved back to its original location.
995 EXPECT_EQ(position.x(), start_position.x());
997 // Move the find box again.
998 moved_x_coord = FindInPageTillBoxMoves(web_contents, start_position.x(),
999 "Chromium", kMoveIterations);
1000 EXPECT_TRUE(moved_x_coord != start_position.x());
1002 // Search for an invalid string.
1003 EXPECT_EQ(0, FindInPageASCII(web_contents, "WeirdSearchString",
1004 kFwd, kIgnoreCase, &ordinal));
1006 // Check the position.
1007 EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
1008 EXPECT_TRUE(fully_visible);
1010 // Make sure Find box has moved back to its original location.
1011 EXPECT_EQ(position.x(), start_position.x());
1014 // FindNextInNewTabUsesPrepopulate times-out on Mac and Aura.
1015 // See http://crbug.com/43070
1016 #if defined(OS_MACOSX) || defined(USE_AURA)
1017 #define MAYBE_FindNextInNewTabUsesPrepopulate \
1018 DISABLED_FindNextInNewTabUsesPrepopulate
1019 #else
1020 #define MAYBE_FindNextInNewTabUsesPrepopulate FindNextInNewTabUsesPrepopulate
1021 #endif
1023 // Make sure F3 in a new tab works if Find has previous string to search for.
1024 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
1025 MAYBE_FindNextInNewTabUsesPrepopulate) {
1026 // First we navigate to any page.
1027 GURL url = GetURL(kSimple);
1028 ui_test_utils::NavigateToURL(browser(), url);
1030 // Search for 'no_match'. No matches should be found.
1031 int ordinal = 0;
1032 WebContents* web_contents =
1033 browser()->tab_strip_model()->GetActiveWebContents();
1034 EXPECT_EQ(0, FindInPageASCII(web_contents, "no_match",
1035 kFwd, kIgnoreCase, &ordinal));
1036 EXPECT_EQ(0, ordinal);
1038 // Open another tab (tab B).
1039 chrome::NewTab(browser());
1040 ui_test_utils::NavigateToURL(browser(), url);
1042 // Simulate what happens when you press F3 for FindNext. We should get a
1043 // response here (a hang means search was aborted).
1044 EXPECT_EQ(0, ui_test_utils::FindInPage(web_contents, base::string16(),
1045 kFwd, kIgnoreCase, &ordinal, NULL));
1046 EXPECT_EQ(0, ordinal);
1048 // Open another tab (tab C).
1049 chrome::NewTab(browser());
1050 ui_test_utils::NavigateToURL(browser(), url);
1052 // Simulate what happens when you press F3 for FindNext. We should get a
1053 // response here (a hang means search was aborted).
1054 EXPECT_EQ(0, ui_test_utils::FindInPage(web_contents, base::string16(),
1055 kFwd, kIgnoreCase, &ordinal, NULL));
1056 EXPECT_EQ(0, ordinal);
1059 // Make sure Find box does not become UI-inactive when no text is in the box as
1060 // we switch to a tab contents with an empty find string. See issue 13570.
1061 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, StayActive) {
1062 // First we navigate to any page.
1063 GURL url = GetURL(kSimple);
1064 ui_test_utils::NavigateToURL(browser(), url);
1066 chrome::ShowFindBar(browser());
1068 // Simulate a user clearing the search string. Ideally, we should be
1069 // simulating keypresses here for searching for something and pressing
1070 // backspace, but that's been proven flaky in the past, so we go straight to
1071 // web_contents.
1072 FindTabHelper* find_tab_helper = FindTabHelper::FromWebContents(
1073 browser()->tab_strip_model()->GetActiveWebContents());
1074 // Stop the (non-existing) find operation, and clear the selection (which
1075 // signals the UI is still active).
1076 find_tab_helper->StopFinding(FindBarController::kClearSelectionOnPage);
1077 // Make sure the Find UI flag hasn't been cleared, it must be so that the UI
1078 // still responds to browser window resizing.
1079 ASSERT_TRUE(find_tab_helper->find_ui_active());
1082 // Make sure F3 works after you FindNext a couple of times and end the Find
1083 // session. See issue http://crbug.com/28306.
1084 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, RestartSearchFromF3) {
1085 // First we navigate to a simple page.
1086 GURL url = GetURL(kSimple);
1087 ui_test_utils::NavigateToURL(browser(), url);
1089 // Search for 'page'. Should have 1 match.
1090 int ordinal = 0;
1091 WebContents* web_contents =
1092 browser()->tab_strip_model()->GetActiveWebContents();
1093 EXPECT_EQ(1, FindInPageASCII(web_contents, "page",
1094 kFwd, kIgnoreCase, &ordinal));
1095 EXPECT_EQ(1, ordinal);
1097 // Simulate what happens when you press F3 for FindNext. Still should show
1098 // one match. This cleared the pre-populate string at one point (see bug).
1099 EXPECT_EQ(1, ui_test_utils::FindInPage(web_contents, base::string16(),
1100 kFwd, kIgnoreCase, &ordinal, NULL));
1101 EXPECT_EQ(1, ordinal);
1103 // End the Find session, thereby making the next F3 start afresh.
1104 browser()->GetFindBarController()->EndFindSession(
1105 FindBarController::kKeepSelectionOnPage,
1106 FindBarController::kKeepResultsInFindBox);
1108 // Simulate F3 while Find box is closed. Should have 1 match.
1109 EXPECT_EQ(1, FindInPageASCII(web_contents, "", kFwd, kIgnoreCase, &ordinal));
1110 EXPECT_EQ(1, ordinal);
1113 // When re-opening the find bar with F3, the find bar should be re-populated
1114 // with the last search from the same tab rather than the last overall search.
1115 // The only exception is if there is a global pasteboard (for example on Mac).
1116 // http://crbug.com/30006
1117 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PreferPreviousSearch) {
1118 // First we navigate to any page.
1119 GURL url = GetURL(kSimple);
1120 ui_test_utils::NavigateToURL(browser(), url);
1122 // Find "Default".
1123 int ordinal = 0;
1124 WebContents* web_contents_1 =
1125 browser()->tab_strip_model()->GetActiveWebContents();
1126 EXPECT_EQ(1, FindInPageASCII(web_contents_1, "text",
1127 kFwd, kIgnoreCase, &ordinal));
1129 // Create a second tab.
1130 // For some reason we can't use AddSelectedTabWithURL here on ChromeOS. It
1131 // could be some delicate assumption about the tab starting off unselected or
1132 // something relating to user gesture.
1133 chrome::AddTabAt(browser(), GURL(), -1, true);
1134 ui_test_utils::NavigateToURL(browser(), url);
1135 WebContents* web_contents_2 =
1136 browser()->tab_strip_model()->GetActiveWebContents();
1137 EXPECT_NE(web_contents_1, web_contents_2);
1139 // Find "given".
1140 FindInPageASCII(web_contents_2, "given", kFwd, kIgnoreCase, &ordinal);
1142 // Switch back to first tab.
1143 browser()->tab_strip_model()->ActivateTabAt(0, false);
1144 browser()->GetFindBarController()->EndFindSession(
1145 FindBarController::kKeepSelectionOnPage,
1146 FindBarController::kKeepResultsInFindBox);
1147 // Simulate F3.
1148 ui_test_utils::FindInPage(web_contents_1, base::string16(),
1149 kFwd, kIgnoreCase, &ordinal, NULL);
1150 FindBar* find_bar = browser()->GetFindBarController()->find_bar();
1151 if (find_bar->HasGlobalFindPasteboard()) {
1152 EXPECT_EQ(FindTabHelper::FromWebContents(web_contents_1)->find_text(),
1153 ASCIIToUTF16("given"));
1154 } else {
1155 EXPECT_EQ(FindTabHelper::FromWebContents(web_contents_1)->find_text(),
1156 ASCIIToUTF16("text"));
1160 // This tests that whenever you close and reopen the Find bar, it should show
1161 // the last search entered in that tab. http://crbug.com/40121.
1162 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateSameTab) {
1163 // First we navigate to any page.
1164 GURL url = GetURL(kSimple);
1165 ui_test_utils::NavigateToURL(browser(), url);
1167 // Search for the word "page".
1168 int ordinal = 0;
1169 WebContents* web_contents =
1170 browser()->tab_strip_model()->GetActiveWebContents();
1171 EXPECT_EQ(1, FindInPageASCII(web_contents, "page",
1172 kFwd, kIgnoreCase, &ordinal));
1174 // Open the Find box.
1175 EnsureFindBoxOpen();
1177 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1178 EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
1180 // Close the Find box.
1181 browser()->GetFindBarController()->EndFindSession(
1182 FindBarController::kKeepSelectionOnPage,
1183 FindBarController::kKeepResultsInFindBox);
1185 // Open the Find box again.
1186 EnsureFindBoxOpen();
1188 // After the Find box has been reopened, it should have been prepopulated with
1189 // the word "page" again.
1190 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1191 EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
1194 // This tests that whenever you open Find in a new tab it should prepopulate
1195 // with a previous search term (in any tab), if a search has not been issued in
1196 // this tab before.
1197 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateInNewTab) {
1198 // First we navigate to any page.
1199 GURL url = GetURL(kSimple);
1200 ui_test_utils::NavigateToURL(browser(), url);
1202 // Search for the word "page".
1203 int ordinal = 0;
1204 WebContents* web_contents_1 =
1205 browser()->tab_strip_model()->GetActiveWebContents();
1206 EXPECT_EQ(1, FindInPageASCII(web_contents_1, "page",
1207 kFwd, kIgnoreCase, &ordinal));
1208 EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
1210 // Now create a second tab and load the same page.
1211 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
1212 WebContents* web_contents_2 =
1213 browser()->tab_strip_model()->GetActiveWebContents();
1214 EXPECT_NE(web_contents_1, web_contents_2);
1216 // Open the Find box.
1217 EnsureFindBoxOpen();
1219 // The new tab should have "page" prepopulated, since that was the last search
1220 // in the first tab.
1221 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1222 // But it should not seem like a search has been issued.
1223 EXPECT_EQ(base::string16(), GetMatchCountText());
1226 // This makes sure that we can search for A in tabA, then for B in tabB and
1227 // when we come back to tabA we should still see A (because that was the last
1228 // search in that tab).
1229 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulatePreserveLast) {
1230 FindBar* find_bar = browser()->GetFindBarController()->find_bar();
1231 if (find_bar->HasGlobalFindPasteboard())
1232 return;
1234 // First we navigate to any page.
1235 GURL url = GetURL(kSimple);
1236 ui_test_utils::NavigateToURL(browser(), url);
1238 // Search for the word "page".
1239 int ordinal = 0;
1240 WebContents* web_contents_1 =
1241 browser()->tab_strip_model()->GetActiveWebContents();
1242 EXPECT_EQ(1, FindInPageASCII(web_contents_1, "page",
1243 kFwd, kIgnoreCase, &ordinal));
1245 // Open the Find box.
1246 EnsureFindBoxOpen();
1248 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1250 // Close the Find box.
1251 browser()->GetFindBarController()->EndFindSession(
1252 FindBarController::kKeepSelectionOnPage,
1253 FindBarController::kKeepResultsInFindBox);
1255 // Now create a second tab and load the same page.
1256 chrome::AddTabAt(browser(), GURL(), -1, true);
1257 ui_test_utils::NavigateToURL(browser(), url);
1258 WebContents* web_contents_2 =
1259 browser()->tab_strip_model()->GetActiveWebContents();
1260 EXPECT_NE(web_contents_1, web_contents_2);
1262 // Search for the word "text".
1263 FindInPageASCII(web_contents_2, "text", kFwd, kIgnoreCase, &ordinal);
1265 // Go back to the first tab and make sure we have NOT switched the prepopulate
1266 // text to "text".
1267 browser()->tab_strip_model()->ActivateTabAt(0, false);
1269 // Open the Find box.
1270 EnsureFindBoxOpen();
1272 // After the Find box has been reopened, it should have been prepopulated with
1273 // the word "page" again, since that was the last search in that tab.
1274 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1276 // Close the Find box.
1277 browser()->GetFindBarController()->EndFindSession(
1278 FindBarController::kKeepSelectionOnPage,
1279 FindBarController::kKeepResultsInFindBox);
1281 // Re-open the Find box.
1282 // This is a special case: previous search in WebContents used to get cleared
1283 // if you opened and closed the FindBox, which would cause the global
1284 // prepopulate value to show instead of last search in this tab.
1285 EnsureFindBoxOpen();
1287 // After the Find box has been reopened, it should have been prepopulated with
1288 // the word "page" again, since that was the last search in that tab.
1289 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1292 // TODO(rohitrao): Searching in incognito tabs does not work in browser tests in
1293 // Linux views. Investigate and fix. http://crbug.com/40948
1294 #if defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
1295 #define MAYBE_NoIncognitoPrepopulate DISABLED_NoIncognitoPrepopulate
1296 #else
1297 #define MAYBE_NoIncognitoPrepopulate NoIncognitoPrepopulate
1298 #endif
1300 // This tests that search terms entered into an incognito find bar are not used
1301 // as prepopulate terms for non-incognito windows.
1302 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_NoIncognitoPrepopulate) {
1303 FindBar* find_bar = browser()->GetFindBarController()->find_bar();
1304 if (find_bar->HasGlobalFindPasteboard())
1305 return;
1307 // First we navigate to the "simple" test page.
1308 GURL url = GetURL(kSimple);
1309 ui_test_utils::NavigateToURL(browser(), url);
1311 // Search for the word "page" in the normal browser tab.
1312 int ordinal = 0;
1313 WebContents* web_contents_1 =
1314 browser()->tab_strip_model()->GetActiveWebContents();
1315 EXPECT_EQ(1, FindInPageASCII(web_contents_1, "page",
1316 kFwd, kIgnoreCase, &ordinal));
1318 // Open the Find box.
1319 EnsureFindBoxOpenForBrowser(browser());
1320 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarTextForBrowser(browser()));
1322 // Close the Find box.
1323 browser()->GetFindBarController()->EndFindSession(
1324 FindBarController::kKeepSelectionOnPage,
1325 FindBarController::kKeepResultsInFindBox);
1327 // Open a new incognito window and navigate to the same page.
1328 Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
1329 Browser* incognito_browser =
1330 new Browser(Browser::CreateParams(incognito_profile,
1331 browser()->host_desktop_type()));
1332 content::WindowedNotificationObserver observer(
1333 content::NOTIFICATION_LOAD_STOP,
1334 content::NotificationService::AllSources());
1335 chrome::AddSelectedTabWithURL(incognito_browser, url,
1336 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
1337 observer.Wait();
1338 incognito_browser->window()->Show();
1340 // Open the find box and make sure that it is prepopulated with "page".
1341 EnsureFindBoxOpenForBrowser(incognito_browser);
1342 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarTextForBrowser(incognito_browser));
1344 // Search for the word "text" in the incognito tab.
1345 WebContents* incognito_tab =
1346 incognito_browser->tab_strip_model()->GetActiveWebContents();
1347 EXPECT_EQ(1, FindInPageASCII(incognito_tab, "text",
1348 kFwd, kIgnoreCase, &ordinal));
1349 EXPECT_EQ(ASCIIToUTF16("text"), GetFindBarTextForBrowser(incognito_browser));
1351 // Close the Find box.
1352 incognito_browser->GetFindBarController()->EndFindSession(
1353 FindBarController::kKeepSelectionOnPage,
1354 FindBarController::kKeepResultsInFindBox);
1356 // Now open a new tab in the original (non-incognito) browser.
1357 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
1358 WebContents* web_contents_2 =
1359 browser()->tab_strip_model()->GetActiveWebContents();
1360 EXPECT_NE(web_contents_1, web_contents_2);
1362 // Open the Find box and make sure it is prepopulated with the search term
1363 // from the original browser, not the search term from the incognito window.
1364 EnsureFindBoxOpenForBrowser(browser());
1365 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarTextForBrowser(browser()));
1368 // This makes sure that dismissing the find bar with kActivateSelection works.
1369 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, ActivateLinkNavigatesPage) {
1370 // First we navigate to our test content.
1371 GURL url = GetURL(kLinkPage);
1372 ui_test_utils::NavigateToURL(browser(), url);
1374 WebContents* web_contents =
1375 browser()->tab_strip_model()->GetActiveWebContents();
1376 FindTabHelper* find_tab_helper =
1377 FindTabHelper::FromWebContents(web_contents);
1379 int ordinal = 0;
1380 FindInPageASCII(web_contents, "link", kFwd, kIgnoreCase, &ordinal);
1381 EXPECT_EQ(ordinal, 1);
1383 // End the find session, click on the link.
1384 content::WindowedNotificationObserver observer(
1385 content::NOTIFICATION_LOAD_STOP,
1386 content::Source<NavigationController>(&web_contents->GetController()));
1387 find_tab_helper->StopFinding(FindBarController::kActivateSelectionOnPage);
1388 observer.Wait();
1391 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FitWindow) {
1392 Browser::CreateParams params(Browser::TYPE_POPUP, browser()->profile(),
1393 browser()->host_desktop_type());
1394 params.initial_bounds = gfx::Rect(0, 0, 250, 500);
1395 Browser* popup = new Browser(params);
1396 content::WindowedNotificationObserver observer(
1397 content::NOTIFICATION_LOAD_STOP,
1398 content::NotificationService::AllSources());
1399 chrome::AddSelectedTabWithURL(
1400 popup, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
1401 // Wait for the page to finish loading.
1402 observer.Wait();
1403 popup->window()->Show();
1405 // On GTK, bounds change is asynchronous.
1406 base::MessageLoop::current()->RunUntilIdle();
1408 EnsureFindBoxOpenForBrowser(popup);
1410 // GTK adjusts FindBar size asynchronously.
1411 base::MessageLoop::current()->RunUntilIdle();
1413 ASSERT_LE(GetFindBarWidthForBrowser(popup),
1414 popup->window()->GetBounds().width());
1417 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
1418 FindMovesOnTabClose_Issue1343052) {
1419 EnsureFindBoxOpen();
1420 content::RunAllPendingInMessageLoop(); // Needed on Linux.
1422 gfx::Point position;
1423 EXPECT_TRUE(GetFindBarWindowInfo(&position, NULL));
1425 // Open another tab.
1426 GURL url = GetURL(kSimple);
1427 ui_test_utils::NavigateToURLWithDisposition(
1428 browser(), url, NEW_FOREGROUND_TAB,
1429 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1431 // Close it.
1432 chrome::CloseTab(browser());
1434 // See if the Find window has moved.
1435 gfx::Point position2;
1436 EXPECT_TRUE(GetFindBarWindowInfo(&position2, NULL));
1437 EXPECT_EQ(position, position2);
1439 // Toggle the bookmark bar state. Note that this starts an animation, and
1440 // there isn't a good way other than looping and polling to see when it's
1441 // done. So instead we change the state and open a new tab, since the new tab
1442 // animation doesn't happen on tab change.
1443 chrome::ToggleBookmarkBar(browser());
1445 ui_test_utils::NavigateToURLWithDisposition(
1446 browser(), url, NEW_FOREGROUND_TAB,
1447 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1449 EnsureFindBoxOpen();
1450 content::RunAllPendingInMessageLoop(); // Needed on Linux.
1451 EXPECT_TRUE(GetFindBarWindowInfo(&position, NULL));
1453 ui_test_utils::NavigateToURLWithDisposition(
1454 browser(), url, NEW_FOREGROUND_TAB,
1455 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1456 chrome::CloseTab(browser());
1457 EXPECT_TRUE(GetFindBarWindowInfo(&position2, NULL));
1458 EXPECT_EQ(position, position2);
1461 // Verify that if there's a global pasteboard (for example on Mac) then doing
1462 // a search on one tab will clear the matches label on the other tabs.
1463 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
1464 GlobalPasteBoardClearMatches) {
1465 FindBar* find_bar = browser()->GetFindBarController()->find_bar();
1466 if (!find_bar->HasGlobalFindPasteboard())
1467 return;
1469 // First we navigate to any page.
1470 GURL url = GetURL(kSimple);
1471 ui_test_utils::NavigateToURL(browser(), url);
1473 // Change the match count on the first tab to "1 of 1".
1474 int ordinal = 0;
1475 WebContents* web_contents_1 =
1476 browser()->tab_strip_model()->GetActiveWebContents();
1477 EXPECT_EQ(1, FindInPageASCII(web_contents_1, "page",
1478 kFwd, kIgnoreCase, &ordinal));
1479 EnsureFindBoxOpen();
1480 EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
1482 // Next, do a search in a second tab.
1483 chrome::AddTabAt(browser(), GURL(), -1, true);
1484 ui_test_utils::NavigateToURL(browser(), url);
1485 WebContents* web_contents_2 =
1486 browser()->tab_strip_model()->GetActiveWebContents();
1487 FindInPageASCII(web_contents_2, "text", kFwd, kIgnoreCase, &ordinal);
1488 EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
1490 // Go back to the first tab and verify that the match text is cleared.
1491 // text to "text".
1492 browser()->tab_strip_model()->ActivateTabAt(0, false);
1493 EXPECT_TRUE(GetMatchCountText().empty());
1496 // Verify that Incognito window doesn't propagate find string to other widows.
1497 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, GlobalPasteboardIncognito) {
1498 Browser* browser_incognito = CreateIncognitoBrowser();
1499 WebContents* web_contents_1 =
1500 browser()->tab_strip_model()->GetActiveWebContents();
1501 FindInPageASCII(web_contents_1, "page", kFwd, kIgnoreCase, NULL);
1502 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1503 WebContents* web_contents_2 =
1504 browser_incognito->tab_strip_model()->GetActiveWebContents();
1505 FindInPageASCII(web_contents_2, "Incognito", kFwd, kIgnoreCase, NULL);
1506 EXPECT_EQ(ASCIIToUTF16("Incognito"),
1507 GetFindBarTextForBrowser(browser_incognito));
1508 EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
1511 // Find text in regular window, find different text in incognito, send
1512 // IDC_FIND_NEXT to incognito. It should search for the second phrase.
1513 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, IncognitoFindNextSecret) {
1514 WebContents* web_contents =
1515 browser()->tab_strip_model()->GetActiveWebContents();
1516 // On Mac this updates the find pboard.
1517 FindInPageASCII(web_contents, "bar", kFwd, kIgnoreCase, NULL);
1519 Browser* browser_incognito = CreateIncognitoBrowser();
1520 ui_test_utils::NavigateToURL(browser_incognito,
1521 GURL("data:text/plain,barfoofoo"));
1522 WebContents* web_contents_incognito =
1523 browser_incognito->tab_strip_model()->GetActiveWebContents();
1524 FindInPageASCII(web_contents_incognito, "foo", true, kIgnoreCase, NULL);
1525 EXPECT_EQ(ASCIIToUTF16("foo"),
1526 GetFindBarTextForBrowser(browser_incognito));
1527 EXPECT_EQ(ASCIIToUTF16("1 of 2"),
1528 GetFindBarMatchCountTextForBrowser(browser_incognito));
1530 // Close the find bar.
1531 FindTabHelper* find_tab_helper =
1532 FindTabHelper::FromWebContents(web_contents_incognito);
1533 find_tab_helper->StopFinding(FindBarController::kActivateSelectionOnPage);
1535 // Cmd + G triggers IDC_FIND_NEXT command. Thus we test FindInPage()
1536 // method from browser_commands.cc. FindInPage16() bypasses it.
1537 EXPECT_TRUE(chrome::ExecuteCommand(browser_incognito, IDC_FIND_NEXT));
1538 ui_test_utils::FindInPageNotificationObserver observer(
1539 web_contents_incognito);
1540 observer.Wait();
1541 EXPECT_EQ(ASCIIToUTF16("foo"),
1542 GetFindBarTextForBrowser(browser_incognito));
1543 EXPECT_EQ(ASCIIToUTF16("2 of 2"),
1544 GetFindBarMatchCountTextForBrowser(browser_incognito));
1547 // Find text in regular window, send IDC_FIND_NEXT to incognito. It should
1548 // search for the first phrase.
1549 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, IncognitoFindNextShared) {
1550 WebContents* web_contents =
1551 browser()->tab_strip_model()->GetActiveWebContents();
1552 // On Mac this updates the find pboard.
1553 FindInPageASCII(web_contents, "bar", kFwd, kIgnoreCase, NULL);
1555 Browser* browser_incognito = CreateIncognitoBrowser();
1556 ui_test_utils::NavigateToURL(browser_incognito,
1557 GURL("data:text/plain,bar"));
1559 EXPECT_TRUE(chrome::ExecuteCommand(browser_incognito, IDC_FIND_NEXT));
1560 WebContents* web_contents_incognito =
1561 browser_incognito->tab_strip_model()->GetActiveWebContents();
1562 ui_test_utils::FindInPageNotificationObserver observer(
1563 web_contents_incognito);
1564 observer.Wait();
1565 EXPECT_EQ(ASCIIToUTF16("bar"),
1566 GetFindBarTextForBrowser(browser_incognito));