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