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