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