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