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