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