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