Home | History | Annotate | Download | only in find_bar
      1 // Copyright (c) 2011 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/message_loop.h"
      6 #include "base/string_util.h"
      7 #include "base/utf_string_conversions.h"
      8 #include "chrome/browser/profiles/profile.h"
      9 #include "chrome/browser/tabs/tab_strip_model.h"
     10 #include "chrome/browser/ui/browser.h"
     11 #include "chrome/browser/ui/browser_navigator.h"
     12 #include "chrome/browser/ui/browser_window.h"
     13 #include "chrome/browser/ui/find_bar/find_bar.h"
     14 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
     15 #include "chrome/browser/ui/find_bar/find_notification_details.h"
     16 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
     17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
     18 #include "chrome/test/in_process_browser_test.h"
     19 #include "chrome/test/ui_test_utils.h"
     20 #include "content/browser/renderer_host/render_view_host.h"
     21 #include "content/browser/tab_contents/tab_contents.h"
     22 #include "content/browser/tab_contents/tab_contents_view.h"
     23 #include "net/test/test_server.h"
     24 #include "ui/base/keycodes/keyboard_codes.h"
     25 
     26 #if defined(TOOLKIT_VIEWS)
     27 #include "chrome/browser/ui/views/find_bar_host.h"
     28 #include "views/focus/focus_manager.h"
     29 #elif defined(TOOLKIT_GTK)
     30 #include "chrome/browser/ui/gtk/slide_animator_gtk.h"
     31 #elif defined(OS_MACOSX)
     32 #include "chrome/browser/ui/cocoa/find_bar/find_bar_bridge.h"
     33 #endif
     34 
     35 const std::string kSimplePage = "404_is_enough_for_us.html";
     36 const std::string kFramePage = "files/find_in_page/frames.html";
     37 const std::string kFrameData = "files/find_in_page/framedata_general.html";
     38 const std::string kUserSelectPage = "files/find_in_page/user-select.html";
     39 const std::string kCrashPage = "files/find_in_page/crash_1341577.html";
     40 const std::string kTooFewMatchesPage = "files/find_in_page/bug_1155639.html";
     41 const std::string kLongTextareaPage = "files/find_in_page/large_textarea.html";
     42 const std::string kEndState = "files/find_in_page/end_state.html";
     43 const std::string kPrematureEnd = "files/find_in_page/premature_end.html";
     44 const std::string kMoveIfOver = "files/find_in_page/move_if_obscuring.html";
     45 const std::string kBitstackCrash = "files/find_in_page/crash_14491.html";
     46 const std::string kSelectChangesOrdinal =
     47     "files/find_in_page/select_changes_ordinal.html";
     48 const std::string kSimple = "files/find_in_page/simple.html";
     49 const std::string kLinkPage = "files/find_in_page/link.html";
     50 
     51 const bool kBack = false;
     52 const bool kFwd = true;
     53 
     54 const bool kIgnoreCase = false;
     55 const bool kCaseSensitive = true;
     56 
     57 const int kMoveIterations = 30;
     58 
     59 class FindInPageControllerTest : public InProcessBrowserTest {
     60  public:
     61   FindInPageControllerTest() {
     62     EnableDOMAutomation();
     63 
     64 #if defined(TOOLKIT_VIEWS)
     65     DropdownBarHost::disable_animations_during_testing_ = true;
     66 #elif defined(TOOLKIT_GTK)
     67     SlideAnimatorGtk::SetAnimationsForTesting(false);
     68 #elif defined(OS_MACOSX)
     69     FindBarBridge::disable_animations_during_testing_ = true;
     70 #endif
     71   }
     72 
     73  protected:
     74   bool GetFindBarWindowInfoForBrowser(
     75       Browser* browser, gfx::Point* position, bool* fully_visible) {
     76     FindBarTesting* find_bar =
     77         browser->GetFindBarController()->find_bar()->GetFindBarTesting();
     78     return find_bar->GetFindBarWindowInfo(position, fully_visible);
     79   }
     80 
     81   bool GetFindBarWindowInfo(gfx::Point* position, bool* fully_visible) {
     82     return GetFindBarWindowInfoForBrowser(browser(), position, fully_visible);
     83   }
     84 
     85   string16 GetFindBarTextForBrowser(Browser* browser) {
     86     FindBarTesting* find_bar =
     87         browser->GetFindBarController()->find_bar()->GetFindBarTesting();
     88     return find_bar->GetFindText();
     89   }
     90 
     91   string16 GetFindBarText() {
     92     return GetFindBarTextForBrowser(browser());
     93   }
     94 
     95   string16 GetFindBarMatchCountTextForBrowser(Browser* browser) {
     96     FindBarTesting* find_bar =
     97       browser->GetFindBarController()->find_bar()->GetFindBarTesting();
     98     return find_bar->GetMatchCountText();
     99   }
    100 
    101   string16 GetMatchCountText() {
    102     return GetFindBarMatchCountTextForBrowser(browser());
    103   }
    104 
    105   void EnsureFindBoxOpenForBrowser(Browser* browser) {
    106     browser->ShowFindBar();
    107     gfx::Point position;
    108     bool fully_visible = false;
    109     EXPECT_TRUE(GetFindBarWindowInfoForBrowser(
    110                     browser, &position, &fully_visible));
    111     EXPECT_TRUE(fully_visible);
    112   }
    113 
    114   void EnsureFindBoxOpen() {
    115     EnsureFindBoxOpenForBrowser(browser());
    116   }
    117 };
    118 
    119 // Platform independent FindInPage that takes |const wchar_t*|
    120 // as an input.
    121 int FindInPageWchar(TabContentsWrapper* tab,
    122                     const wchar_t* search_str,
    123                     bool forward,
    124                     bool case_sensitive,
    125                     int* ordinal) {
    126   return ui_test_utils::FindInPage(
    127       tab, WideToUTF16(std::wstring(search_str)),
    128       forward, case_sensitive, ordinal);
    129 }
    130 
    131 // This test loads a page with frames and starts FindInPage requests.
    132 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) {
    133   ASSERT_TRUE(test_server()->Start());
    134 
    135   // First we navigate to our frames page.
    136   GURL url = test_server()->GetURL(kFramePage);
    137   ui_test_utils::NavigateToURL(browser(), url);
    138 
    139   // Try incremental search (mimicking user typing in).
    140   int ordinal = 0;
    141   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    142   EXPECT_EQ(18, FindInPageWchar(tab, L"g",
    143                                 kFwd, kIgnoreCase, &ordinal));
    144   EXPECT_EQ(1, ordinal);
    145   EXPECT_EQ(11, FindInPageWchar(tab, L"go",
    146                                 kFwd, kIgnoreCase, &ordinal));
    147   EXPECT_EQ(1, ordinal);
    148   EXPECT_EQ(04, FindInPageWchar(tab, L"goo",
    149                                 kFwd, kIgnoreCase, &ordinal));
    150   EXPECT_EQ(1, ordinal);
    151   EXPECT_EQ(03, FindInPageWchar(tab, L"goog",
    152                                 kFwd, kIgnoreCase, &ordinal));
    153   EXPECT_EQ(1, ordinal);
    154   EXPECT_EQ(02, FindInPageWchar(tab, L"googl",
    155                                 kFwd, kIgnoreCase, &ordinal));
    156   EXPECT_EQ(1, ordinal);
    157   EXPECT_EQ(01, FindInPageWchar(tab, L"google",
    158                                 kFwd, kIgnoreCase, &ordinal));
    159   EXPECT_EQ(1, ordinal);
    160   EXPECT_EQ(00, FindInPageWchar(tab, L"google!",
    161                                 kFwd, kIgnoreCase, &ordinal));
    162   EXPECT_EQ(0, ordinal);
    163 
    164   // Negative test (no matches should be found).
    165   EXPECT_EQ(0, FindInPageWchar(tab, L"Non-existing string",
    166                                kFwd, kIgnoreCase, &ordinal));
    167   EXPECT_EQ(0, ordinal);
    168 
    169   // 'horse' only exists in the three right frames.
    170   EXPECT_EQ(3, FindInPageWchar(tab, L"horse",
    171                                kFwd, kIgnoreCase, &ordinal));
    172   EXPECT_EQ(1, ordinal);
    173 
    174   // 'cat' only exists in the first frame.
    175   EXPECT_EQ(1, FindInPageWchar(tab, L"cat",
    176                                kFwd, kIgnoreCase, &ordinal));
    177   EXPECT_EQ(1, ordinal);
    178 
    179   // Try searching again, should still come up with 1 match.
    180   EXPECT_EQ(1, FindInPageWchar(tab, L"cat",
    181                                kFwd, kIgnoreCase, &ordinal));
    182   EXPECT_EQ(1, ordinal);
    183 
    184   // Try searching backwards, ignoring case, should still come up with 1 match.
    185   EXPECT_EQ(1, FindInPageWchar(tab, L"CAT",
    186                                kBack, kIgnoreCase, &ordinal));
    187   EXPECT_EQ(1, ordinal);
    188 
    189   // Try case sensitive, should NOT find it.
    190   EXPECT_EQ(0, FindInPageWchar(tab, L"CAT",
    191                                kFwd, kCaseSensitive, &ordinal));
    192   EXPECT_EQ(0, ordinal);
    193 
    194   // Try again case sensitive, but this time with right case.
    195   EXPECT_EQ(1, FindInPageWchar(tab, L"dog",
    196                                kFwd, kCaseSensitive, &ordinal));
    197   EXPECT_EQ(1, ordinal);
    198 
    199   // Try non-Latin characters ('Hreggvidur' with 'eth' for 'd' in left frame).
    200   EXPECT_EQ(1, FindInPageWchar(tab, L"Hreggvi\u00F0ur",
    201                                kFwd, kIgnoreCase, &ordinal));
    202   EXPECT_EQ(1, ordinal);
    203   EXPECT_EQ(1, FindInPageWchar(tab, L"Hreggvi\u00F0ur",
    204                                kFwd, kCaseSensitive, &ordinal));
    205   EXPECT_EQ(1, ordinal);
    206   EXPECT_EQ(0, FindInPageWchar(tab, L"hreggvi\u00F0ur",
    207                                kFwd, kCaseSensitive, &ordinal));
    208   EXPECT_EQ(0, ordinal);
    209 }
    210 
    211 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute.
    212 bool FocusedOnPage(TabContents* tab_contents, std::string* result)
    213     WARN_UNUSED_RESULT;
    214 
    215 bool FocusedOnPage(TabContents* tab_contents, std::string* result) {
    216   return ui_test_utils::ExecuteJavaScriptAndExtractString(
    217       tab_contents->render_view_host(),
    218       L"",
    219       L"window.domAutomationController.send(getFocusedElement());",
    220       result);
    221 }
    222 
    223 // This tests the FindInPage end-state, in other words: what is focused when you
    224 // close the Find box (ie. if you find within a link the link should be
    225 // focused).
    226 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageEndState) {
    227   ASSERT_TRUE(test_server()->Start());
    228 
    229   // First we navigate to our special focus tracking page.
    230   GURL url = test_server()->GetURL(kEndState);
    231   ui_test_utils::NavigateToURL(browser(), url);
    232 
    233   TabContentsWrapper* tab_contents = browser()->GetSelectedTabContentsWrapper();
    234   ASSERT_TRUE(NULL != tab_contents);
    235 
    236   // Verify that nothing has focus.
    237   std::string result;
    238   ASSERT_TRUE(FocusedOnPage(tab_contents->tab_contents(), &result));
    239   ASSERT_STREQ("{nothing focused}", result.c_str());
    240 
    241   // Search for a text that exists within a link on the page.
    242   int ordinal = 0;
    243   EXPECT_EQ(1, FindInPageWchar(tab_contents, L"nk",
    244                                kFwd, kIgnoreCase, &ordinal));
    245   EXPECT_EQ(1, ordinal);
    246 
    247   // End the find session, which should set focus to the link.
    248   tab_contents->
    249       find_tab_helper()->StopFinding(FindBarController::kKeepSelection);
    250 
    251   // Verify that the link is focused.
    252   ASSERT_TRUE(FocusedOnPage(tab_contents->tab_contents(), &result));
    253   EXPECT_STREQ("link1", result.c_str());
    254 
    255   // Search for a text that exists within a link on the page.
    256   EXPECT_EQ(1, FindInPageWchar(tab_contents, L"Google",
    257                                kFwd, kIgnoreCase, &ordinal));
    258   EXPECT_EQ(1, ordinal);
    259 
    260   // Move the selection to link 1, after searching.
    261   ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
    262       tab_contents->render_view_host(),
    263       L"",
    264       L"window.domAutomationController.send(selectLink1());",
    265       &result));
    266 
    267   // End the find session.
    268   tab_contents->
    269       find_tab_helper()->StopFinding(FindBarController::kKeepSelection);
    270 
    271   // Verify that link2 is not focused.
    272   ASSERT_TRUE(FocusedOnPage(tab_contents->tab_contents(), &result));
    273   EXPECT_STREQ("", result.c_str());
    274 }
    275 
    276 // This test loads a single-frame page and makes sure the ordinal returned makes
    277 // sense as we FindNext over all the items.
    278 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageOrdinal) {
    279   ASSERT_TRUE(test_server()->Start());
    280 
    281   // First we navigate to our page.
    282   GURL url = test_server()->GetURL(kFrameData);
    283   ui_test_utils::NavigateToURL(browser(), url);
    284 
    285   // Search for 'o', which should make the first item active and return
    286   // '1 in 3' (1st ordinal of a total of 3 matches).
    287   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    288   int ordinal = 0;
    289   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    290                                kFwd, kIgnoreCase, &ordinal));
    291   EXPECT_EQ(1, ordinal);
    292   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    293                                kFwd, kIgnoreCase, &ordinal));
    294   EXPECT_EQ(2, ordinal);
    295   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    296                                kFwd, kIgnoreCase, &ordinal));
    297   EXPECT_EQ(3, ordinal);
    298   // Go back one match.
    299   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    300                                kBack, kIgnoreCase, &ordinal));
    301   EXPECT_EQ(2, ordinal);
    302   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    303                                kFwd, kIgnoreCase, &ordinal));
    304   EXPECT_EQ(3, ordinal);
    305   // This should wrap to the top.
    306   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    307                                kFwd, kIgnoreCase, &ordinal));
    308   EXPECT_EQ(1, ordinal);
    309   // This should go back to the end.
    310   EXPECT_EQ(3, FindInPageWchar(tab, L"o",
    311                                kBack, kIgnoreCase, &ordinal));
    312   EXPECT_EQ(3, ordinal);
    313 }
    314 
    315 // This tests that the ordinal is correctly adjusted after a selection
    316 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
    317                        SelectChangesOrdinal_Issue20883) {
    318   ASSERT_TRUE(test_server()->Start());
    319 
    320   // First we navigate to our test content.
    321   GURL url = test_server()->GetURL(kSelectChangesOrdinal);
    322   ui_test_utils::NavigateToURL(browser(), url);
    323 
    324   // Search for a text that exists within a link on the page.
    325   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    326   ASSERT_TRUE(NULL != tab);
    327   int ordinal = 0;
    328   EXPECT_EQ(4, FindInPageWchar(tab,
    329                                L"google",
    330                                kFwd, kIgnoreCase, &ordinal));
    331   EXPECT_EQ(1, ordinal);
    332 
    333   // Move the selection to link 1, after searching.
    334   std::string result;
    335   ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
    336       tab->render_view_host(),
    337       L"",
    338       L"window.domAutomationController.send(selectLink1());",
    339       &result));
    340 
    341   // Do a find-next after the selection.  This should move forward
    342   // from there to the 3rd instance of 'google'.
    343   EXPECT_EQ(4, FindInPageWchar(tab,
    344                                L"google",
    345                                kFwd, kIgnoreCase, &ordinal));
    346   EXPECT_EQ(3, ordinal);
    347 
    348   // End the find session.
    349   tab->find_tab_helper()->StopFinding(FindBarController::kKeepSelection);
    350 }
    351 
    352 // This test loads a page with frames and makes sure the ordinal returned makes
    353 // sense.
    354 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageMultiFramesOrdinal) {
    355   ASSERT_TRUE(test_server()->Start());
    356 
    357   // First we navigate to our page.
    358   GURL url = test_server()->GetURL(kFramePage);
    359   ui_test_utils::NavigateToURL(browser(), url);
    360 
    361   // Search for 'a', which should make the first item active and return
    362   // '1 in 7' (1st ordinal of a total of 7 matches).
    363   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    364   int ordinal = 0;
    365   EXPECT_EQ(7,
    366             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    367   EXPECT_EQ(1, ordinal);
    368   EXPECT_EQ(7,
    369             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    370   EXPECT_EQ(2, ordinal);
    371   EXPECT_EQ(7,
    372             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    373   EXPECT_EQ(3, ordinal);
    374   EXPECT_EQ(7,
    375             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    376   EXPECT_EQ(4, ordinal);
    377   // Go back one, which should go back one frame.
    378   EXPECT_EQ(7,
    379             FindInPageWchar(tab, L"a", kBack, kIgnoreCase, &ordinal));
    380   EXPECT_EQ(3, ordinal);
    381   EXPECT_EQ(7,
    382             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    383   EXPECT_EQ(4, ordinal);
    384   EXPECT_EQ(7,
    385             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    386   EXPECT_EQ(5, ordinal);
    387   EXPECT_EQ(7,
    388             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    389   EXPECT_EQ(6, ordinal);
    390   EXPECT_EQ(7,
    391             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    392   EXPECT_EQ(7, ordinal);
    393   // Now we should wrap back to frame 1.
    394   EXPECT_EQ(7,
    395             FindInPageWchar(tab, L"a", kFwd, kIgnoreCase, &ordinal));
    396   EXPECT_EQ(1, ordinal);
    397   // Now we should wrap back to frame last frame.
    398   EXPECT_EQ(7,
    399             FindInPageWchar(tab, L"a", kBack, kIgnoreCase, &ordinal));
    400   EXPECT_EQ(7, ordinal);
    401 }
    402 
    403 // We could get ordinals out of whack when restarting search in subframes.
    404 // See http://crbug.com/5132.
    405 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPage_Issue5132) {
    406   ASSERT_TRUE(test_server()->Start());
    407 
    408   // First we navigate to our page.
    409   GURL url = test_server()->GetURL(kFramePage);
    410   ui_test_utils::NavigateToURL(browser(), url);
    411 
    412   // Search for 'goa' three times (6 matches on page).
    413   int ordinal = 0;
    414   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    415   EXPECT_EQ(6, FindInPageWchar(tab, L"goa",
    416                                kFwd, kIgnoreCase, &ordinal));
    417   EXPECT_EQ(1, ordinal);
    418   EXPECT_EQ(6, FindInPageWchar(tab, L"goa",
    419                                kFwd, kIgnoreCase, &ordinal));
    420   EXPECT_EQ(2, ordinal);
    421   EXPECT_EQ(6, FindInPageWchar(tab, L"goa",
    422                                kFwd, kIgnoreCase, &ordinal));
    423   EXPECT_EQ(3, ordinal);
    424   // Add space to search (should result in no matches).
    425   EXPECT_EQ(0, FindInPageWchar(tab, L"goa ",
    426                                kFwd, kIgnoreCase, &ordinal));
    427   EXPECT_EQ(0, ordinal);
    428   // Remove the space, should be back to '3 out of 6')
    429   EXPECT_EQ(6, FindInPageWchar(tab, L"goa",
    430                                kFwd, kIgnoreCase, &ordinal));
    431   EXPECT_EQ(3, ordinal);
    432 }
    433 
    434 // Load a page with no selectable text and make sure we don't crash.
    435 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindUnselectableText) {
    436   ASSERT_TRUE(test_server()->Start());
    437 
    438   // First we navigate to our page.
    439   GURL url = test_server()->GetURL(kUserSelectPage);
    440   ui_test_utils::NavigateToURL(browser(), url);
    441 
    442   int ordinal = 0;
    443   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    444   EXPECT_EQ(1, FindInPageWchar(tab, L"text", kFwd, kIgnoreCase, &ordinal));
    445   EXPECT_EQ(1, ordinal);
    446 }
    447 
    448 // Try to reproduce the crash seen in issue 1341577.
    449 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue1341577) {
    450   ASSERT_TRUE(test_server()->Start());
    451 
    452   // First we navigate to our page.
    453   GURL url = test_server()->GetURL(kCrashPage);
    454   ui_test_utils::NavigateToURL(browser(), url);
    455 
    456   // This would crash the tab. These must be the first two find requests issued
    457   // against the frame, otherwise an active frame pointer is set and it wont
    458   // produce the crash.
    459   // We used to check the return value and |ordinal|. With ICU 4.2, FiP does
    460   // not find a stand-alone dependent vowel sign of Indic scripts. So, the
    461   // exptected values are all 0. To make this test pass regardless of
    462   // ICU version, we just call FiP and see if there's any crash.
    463   // TODO(jungshik): According to a native Malayalam speaker, it's ok not
    464   // to find U+0D4C. Still need to investigate further this issue.
    465   int ordinal = 0;
    466   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    467   FindInPageWchar(tab, L"\u0D4C", kFwd, kIgnoreCase, &ordinal);
    468   FindInPageWchar(tab, L"\u0D4C", kFwd, kIgnoreCase, &ordinal);
    469 
    470   // This should work fine.
    471   EXPECT_EQ(1, FindInPageWchar(tab, L"\u0D24\u0D46",
    472                                kFwd, kIgnoreCase, &ordinal));
    473   EXPECT_EQ(1, ordinal);
    474   EXPECT_EQ(0, FindInPageWchar(tab, L"nostring",
    475                                kFwd, kIgnoreCase, &ordinal));
    476   EXPECT_EQ(0, ordinal);
    477 }
    478 
    479 // Try to reproduce the crash seen in http://crbug.com/14491, where an assert
    480 // hits in the BitStack size comparison in WebKit.
    481 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue14491) {
    482   ASSERT_TRUE(test_server()->Start());
    483 
    484   // First we navigate to our page.
    485   GURL url = test_server()->GetURL(kBitstackCrash);
    486   ui_test_utils::NavigateToURL(browser(), url);
    487 
    488   // This used to crash the tab.
    489   int ordinal = 0;
    490   EXPECT_EQ(0, FindInPageWchar(browser()->GetSelectedTabContentsWrapper(),
    491                                L"s", kFwd, kIgnoreCase, &ordinal));
    492   EXPECT_EQ(0, ordinal);
    493 }
    494 
    495 // Test to make sure Find does the right thing when restarting from a timeout.
    496 // We used to have a problem where we'd stop finding matches when all of the
    497 // following conditions were true:
    498 // 1) The page has a lot of text to search.
    499 // 2) The page contains more than one match.
    500 // 3) It takes longer than the time-slice given to each Find operation (100
    501 //    ms) to find one or more of those matches (so Find times out and has to try
    502 //    again from where it left off).
    503 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindRestarts_Issue1155639) {
    504   ASSERT_TRUE(test_server()->Start());
    505 
    506   // First we navigate to our page.
    507   GURL url = test_server()->GetURL(kTooFewMatchesPage);
    508   ui_test_utils::NavigateToURL(browser(), url);
    509 
    510   // This string appears 5 times at the bottom of a long page. If Find restarts
    511   // properly after a timeout, it will find 5 matches, not just 1.
    512   int ordinal = 0;
    513   EXPECT_EQ(5, FindInPageWchar(browser()->GetSelectedTabContentsWrapper(),
    514                                L"008.xml",
    515                                kFwd, kIgnoreCase, &ordinal));
    516   EXPECT_EQ(1, ordinal);
    517 }
    518 
    519 // Make sure we don't get into an infinite loop when text box contains very
    520 // large amount of text.
    521 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindRestarts_Issue70505) {
    522   ASSERT_TRUE(test_server()->Start());
    523 
    524   // First we navigate to our page.
    525   GURL url = test_server()->GetURL(kLongTextareaPage);
    526   ui_test_utils::NavigateToURL(browser(), url);
    527 
    528   // If this test hangs on the FindInPage call, then it might be a regression
    529   // such as the one found in issue http://crbug.com/70505.
    530   int ordinal = 0;
    531   FindInPageWchar(browser()->GetSelectedTabContentsWrapper(),
    532                   L"a", kFwd, kIgnoreCase, &ordinal);
    533   EXPECT_EQ(1, ordinal);
    534   // TODO(finnur): We cannot reliably get the matchcount for this Find call
    535   // until we fix issue http://crbug.com/71176.
    536 }
    537 
    538 // This tests bug 11761: FindInPage terminates search prematurely.
    539 // This test is not expected to pass until bug 11761 is fixed.
    540 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
    541                        DISABLED_FindInPagePrematureEnd) {
    542   ASSERT_TRUE(test_server()->Start());
    543 
    544   // First we navigate to our special focus tracking page.
    545   GURL url = test_server()->GetURL(kPrematureEnd);
    546   ui_test_utils::NavigateToURL(browser(), url);
    547 
    548   TabContentsWrapper* tab_contents = browser()->GetSelectedTabContentsWrapper();
    549   ASSERT_TRUE(NULL != tab_contents);
    550 
    551   // Search for a text that exists within a link on the page.
    552   int ordinal = 0;
    553   EXPECT_EQ(2, FindInPageWchar(tab_contents, L"html ",
    554                                kFwd, kIgnoreCase, &ordinal));
    555   EXPECT_EQ(1, ordinal);
    556 }
    557 
    558 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindDisappearOnNavigate) {
    559   ASSERT_TRUE(test_server()->Start());
    560 
    561   // First we navigate to our special focus tracking page.
    562   GURL url = test_server()->GetURL(kSimplePage);
    563   GURL url2 = test_server()->GetURL(kFramePage);
    564   ui_test_utils::NavigateToURL(browser(), url);
    565 
    566   browser()->ShowFindBar();
    567 
    568   gfx::Point position;
    569   bool fully_visible = false;
    570 
    571   // Make sure it is open.
    572   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    573   EXPECT_TRUE(fully_visible);
    574 
    575   // Reload the tab and make sure Find window doesn't go away.
    576   browser()->Reload(CURRENT_TAB);
    577   ui_test_utils::WaitForNavigationInCurrentTab(browser());
    578 
    579   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    580   EXPECT_TRUE(fully_visible);
    581 
    582   // Navigate and make sure the Find window goes away.
    583   ui_test_utils::NavigateToURL(browser(), url2);
    584 
    585   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    586   EXPECT_FALSE(fully_visible);
    587 }
    588 
    589 #if defined(OS_MACOSX)
    590 // FindDisappearOnNewTabAndHistory is flaky, at least on Mac.
    591 // See http://crbug.com/43072
    592 #define FindDisappearOnNewTabAndHistory FLAKY_FindDisappearOnNewTabAndHistory
    593 #endif
    594 
    595 // Make sure Find box disappears when History/Downloads page is opened, and
    596 // when a New Tab is opened.
    597 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
    598                        FindDisappearOnNewTabAndHistory) {
    599   ASSERT_TRUE(test_server()->Start());
    600 
    601   // First we navigate to our special focus tracking page.
    602   GURL url = test_server()->GetURL(kSimplePage);
    603   ui_test_utils::NavigateToURL(browser(), url);
    604 
    605   browser()->ShowFindBar();
    606 
    607   gfx::Point position;
    608   bool fully_visible = false;
    609 
    610   // Make sure it is open.
    611   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    612   EXPECT_TRUE(fully_visible);
    613 
    614   // Open another tab (tab B).
    615   browser()->NewTab();
    616   ui_test_utils::NavigateToURL(browser(), url);
    617 
    618   // Make sure Find box is closed.
    619   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    620   EXPECT_FALSE(fully_visible);
    621 
    622   // Close tab B.
    623   browser()->CloseTab();
    624 
    625   // Make sure Find window appears again.
    626   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    627   EXPECT_TRUE(fully_visible);
    628 
    629   browser()->ShowHistoryTab();
    630 
    631   // Make sure Find box is closed.
    632   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    633   EXPECT_FALSE(fully_visible);
    634 }
    635 
    636 // Make sure Find box moves out of the way if it is obscuring the active match.
    637 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindMovesWhenObscuring) {
    638   ASSERT_TRUE(test_server()->Start());
    639 
    640   GURL url = test_server()->GetURL(kMoveIfOver);
    641   ui_test_utils::NavigateToURL(browser(), url);
    642 
    643   browser()->ShowFindBar();
    644 
    645   // This is needed on GTK because the reposition operation is asynchronous.
    646   MessageLoop::current()->RunAllPending();
    647 
    648   gfx::Point start_position;
    649   gfx::Point position;
    650   bool fully_visible = false;
    651 
    652   // Make sure it is open.
    653   EXPECT_TRUE(GetFindBarWindowInfo(&start_position, &fully_visible));
    654   EXPECT_TRUE(fully_visible);
    655 
    656   // Search for 'Chromium' which the Find box is obscuring.
    657   int ordinal = 0;
    658   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    659   int index = 0;
    660   for (; index < kMoveIterations; ++index) {
    661     EXPECT_EQ(kMoveIterations, FindInPageWchar(tab, L"Chromium",
    662                                                kFwd, kIgnoreCase, &ordinal));
    663 
    664     // Check the position.
    665     EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    666     EXPECT_TRUE(fully_visible);
    667 
    668     // If the Find box has moved then we are done.
    669     if (position.x() != start_position.x())
    670       break;
    671   }
    672 
    673   // We should not have reached the end.
    674   ASSERT_GT(kMoveIterations, index);
    675 
    676   // Search for something guaranteed not to be obscured by the Find box.
    677   EXPECT_EQ(1, FindInPageWchar(tab, L"Done",
    678                                kFwd, kIgnoreCase, &ordinal));
    679   // Check the position.
    680   EXPECT_TRUE(GetFindBarWindowInfo(&position, &fully_visible));
    681   EXPECT_TRUE(fully_visible);
    682 
    683   // Make sure Find box has moved back to its original location.
    684   EXPECT_EQ(position.x(), start_position.x());
    685 }
    686 
    687 #if defined(OS_MACOSX)
    688 // FindNextInNewTabUsesPrepopulate times-out, at least on Mac.
    689 // See http://crbug.com/43070
    690 #define FindNextInNewTabUsesPrepopulate DISABLED_FindNextInNewTabUsesPrepopulate
    691 #endif
    692 
    693 // Make sure F3 in a new tab works if Find has previous string to search for.
    694 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
    695                        FindNextInNewTabUsesPrepopulate) {
    696   ASSERT_TRUE(test_server()->Start());
    697 
    698   // First we navigate to any page.
    699   GURL url = test_server()->GetURL(kSimplePage);
    700   ui_test_utils::NavigateToURL(browser(), url);
    701 
    702   // Search for 'no_match'. No matches should be found.
    703   int ordinal = 0;
    704   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    705   EXPECT_EQ(0, FindInPageWchar(tab, L"no_match",
    706                                kFwd, kIgnoreCase, &ordinal));
    707   EXPECT_EQ(0, ordinal);
    708 
    709   // Open another tab (tab B).
    710   browser()->NewTab();
    711   ui_test_utils::NavigateToURL(browser(), url);
    712 
    713   // Simulate what happens when you press F3 for FindNext. We should get a
    714   // response here (a hang means search was aborted).
    715   EXPECT_EQ(0, ui_test_utils::FindInPage(tab, string16(),
    716                                          kFwd, kIgnoreCase, &ordinal));
    717   EXPECT_EQ(0, ordinal);
    718 
    719   // Open another tab (tab C).
    720   browser()->NewTab();
    721   ui_test_utils::NavigateToURL(browser(), url);
    722 
    723   // Simulate what happens when you press F3 for FindNext. We should get a
    724   // response here (a hang means search was aborted).
    725   EXPECT_EQ(0, ui_test_utils::FindInPage(tab, string16(),
    726                                          kFwd, kIgnoreCase, &ordinal));
    727   EXPECT_EQ(0, ordinal);
    728 }
    729 
    730 #if defined(TOOLKIT_VIEWS)
    731 // Make sure Find box grabs the Esc accelerator and restores it again.
    732 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, AcceleratorRestoring) {
    733   ASSERT_TRUE(test_server()->Start());
    734 
    735   // First we navigate to any page.
    736   GURL url = test_server()->GetURL(kSimplePage);
    737   ui_test_utils::NavigateToURL(browser(), url);
    738 
    739   views::FocusManager* focus_manager =
    740       views::FocusManager::GetFocusManagerForNativeWindow(
    741           browser()->window()->GetNativeHandle());
    742 
    743   // See where Escape is registered.
    744   views::Accelerator escape(ui::VKEY_ESCAPE, false, false, false);
    745   views::AcceleratorTarget* old_target =
    746       focus_manager->GetCurrentTargetForAccelerator(escape);
    747   EXPECT_TRUE(old_target != NULL);
    748 
    749   browser()->ShowFindBar();
    750 
    751   // Our Find bar should be the new target.
    752   views::AcceleratorTarget* new_target =
    753       focus_manager->GetCurrentTargetForAccelerator(escape);
    754 
    755   EXPECT_TRUE(new_target != NULL);
    756   EXPECT_NE(new_target, old_target);
    757 
    758   // Close the Find box.
    759   browser()->GetFindBarController()->EndFindSession(
    760       FindBarController::kKeepSelection);
    761 
    762   // The accelerator for Escape should be back to what it was before.
    763   EXPECT_EQ(old_target,
    764             focus_manager->GetCurrentTargetForAccelerator(escape));
    765 }
    766 #endif  // TOOLKIT_VIEWS
    767 
    768 // Make sure Find box does not become UI-inactive when no text is in the box as
    769 // we switch to a tab contents with an empty find string. See issue 13570.
    770 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, StayActive) {
    771   ASSERT_TRUE(test_server()->Start());
    772 
    773   // First we navigate to any page.
    774   GURL url = test_server()->GetURL(kSimplePage);
    775   ui_test_utils::NavigateToURL(browser(), url);
    776 
    777   browser()->ShowFindBar();
    778 
    779   // Simulate a user clearing the search string. Ideally, we should be
    780   // simulating keypresses here for searching for something and pressing
    781   // backspace, but that's been proven flaky in the past, so we go straight to
    782   // tab_contents.
    783   FindTabHelper* find_tab_helper =
    784       browser()->GetSelectedTabContentsWrapper()->find_tab_helper();
    785   // Stop the (non-existing) find operation, and clear the selection (which
    786   // signals the UI is still active).
    787   find_tab_helper->StopFinding(FindBarController::kClearSelection);
    788   // Make sure the Find UI flag hasn't been cleared, it must be so that the UI
    789   // still responds to browser window resizing.
    790   ASSERT_TRUE(find_tab_helper->find_ui_active());
    791 }
    792 
    793 // Make sure F3 works after you FindNext a couple of times and end the Find
    794 // session. See issue http://crbug.com/28306.
    795 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, RestartSearchFromF3) {
    796   ASSERT_TRUE(test_server()->Start());
    797 
    798   // First we navigate to a simple page.
    799   GURL url = test_server()->GetURL(kSimple);
    800   ui_test_utils::NavigateToURL(browser(), url);
    801 
    802   // Search for 'page'. Should have 1 match.
    803   int ordinal = 0;
    804   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
    805   EXPECT_EQ(1, FindInPageWchar(tab, L"page", kFwd, kIgnoreCase, &ordinal));
    806   EXPECT_EQ(1, ordinal);
    807 
    808   // Simulate what happens when you press F3 for FindNext. Still should show
    809   // one match. This cleared the pre-populate string at one point (see bug).
    810   EXPECT_EQ(1, ui_test_utils::FindInPage(tab, string16(),
    811                                          kFwd, kIgnoreCase, &ordinal));
    812   EXPECT_EQ(1, ordinal);
    813 
    814   // End the Find session, thereby making the next F3 start afresh.
    815   browser()->GetFindBarController()->EndFindSession(
    816       FindBarController::kKeepSelection);
    817 
    818   // Simulate F3 while Find box is closed. Should have 1 match.
    819   EXPECT_EQ(1, FindInPageWchar(tab, L"", kFwd, kIgnoreCase, &ordinal));
    820   EXPECT_EQ(1, ordinal);
    821 }
    822 
    823 // When re-opening the find bar with F3, the find bar should be re-populated
    824 // with the last search from the same tab rather than the last overall search.
    825 // http://crbug.com/30006
    826 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PreferPreviousSearch) {
    827   ASSERT_TRUE(test_server()->Start());
    828 
    829   // First we navigate to any page.
    830   GURL url = test_server()->GetURL(kSimplePage);
    831   ui_test_utils::NavigateToURL(browser(), url);
    832 
    833   // Find "Default".
    834   int ordinal = 0;
    835   TabContentsWrapper* tab1 = browser()->GetSelectedTabContentsWrapper();
    836   EXPECT_EQ(1, FindInPageWchar(tab1, L"Default", kFwd, kIgnoreCase, &ordinal));
    837 
    838   // Create a second tab.
    839   // For some reason we can't use AddSelectedTabWithURL here on ChromeOS. It
    840   // could be some delicate assumption about the tab starting off unselected or
    841   // something relating to user gesture.
    842   browser::NavigateParams params(browser(), url, PageTransition::TYPED);
    843   params.disposition = NEW_BACKGROUND_TAB;
    844   params.tabstrip_add_types = TabStripModel::ADD_NONE;
    845   browser::Navigate(&params);
    846   browser()->ActivateTabAt(1, false);
    847   TabContentsWrapper* tab2 = browser()->GetSelectedTabContentsWrapper();
    848   EXPECT_NE(tab1, tab2);
    849 
    850   // Find "given".
    851   FindInPageWchar(tab2, L"given", kFwd, kIgnoreCase, &ordinal);
    852 
    853   // Switch back to first tab.
    854   browser()->ActivateTabAt(0, false);
    855   browser()->GetFindBarController()->EndFindSession(
    856       FindBarController::kKeepSelection);
    857   // Simulate F3.
    858   ui_test_utils::FindInPage(tab1, string16(), kFwd, kIgnoreCase, &ordinal);
    859   EXPECT_EQ(tab1->find_tab_helper()->find_text(), WideToUTF16(L"Default"));
    860 }
    861 
    862 // This tests that whenever you close and reopen the Find bar, it should show
    863 // the last search entered in that tab. http://crbug.com/40121.
    864 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateSameTab) {
    865 #if defined(OS_MACOSX)
    866   // FindInPage on Mac doesn't use prepopulated values. Search there is global.
    867   return;
    868 #endif
    869 
    870   ASSERT_TRUE(test_server()->Start());
    871 
    872   // First we navigate to any page.
    873   GURL url = test_server()->GetURL(kSimple);
    874   ui_test_utils::NavigateToURL(browser(), url);
    875 
    876   // Search for the word "page".
    877   int ordinal = 0;
    878   TabContentsWrapper* tab1 = browser()->GetSelectedTabContentsWrapper();
    879   EXPECT_EQ(1, FindInPageWchar(tab1, L"page", kFwd, kIgnoreCase, &ordinal));
    880 
    881   // Open the Find box.
    882   EnsureFindBoxOpen();
    883 
    884   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
    885   EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
    886 
    887   // Close the Find box.
    888   browser()->GetFindBarController()->EndFindSession(
    889       FindBarController::kKeepSelection);
    890 
    891   // Open the Find box again.
    892   EnsureFindBoxOpen();
    893 
    894   // After the Find box has been reopened, it should have been prepopulated with
    895   // the word "page" again.
    896   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
    897   EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
    898 }
    899 
    900 // This tests that whenever you open Find in a new tab it should prepopulate
    901 // with a previous search term (in any tab), if a search has not been issued in
    902 // this tab before.
    903 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateInNewTab) {
    904 #if defined(OS_MACOSX)
    905   // FindInPage on Mac doesn't use prepopulated values. Search there is global.
    906   return;
    907 #endif
    908 
    909   ASSERT_TRUE(test_server()->Start());
    910 
    911   // First we navigate to any page.
    912   GURL url = test_server()->GetURL(kSimple);
    913   ui_test_utils::NavigateToURL(browser(), url);
    914 
    915   // Search for the word "page".
    916   int ordinal = 0;
    917   TabContentsWrapper* tab1 = browser()->GetSelectedTabContentsWrapper();
    918   EXPECT_EQ(1, FindInPageWchar(tab1, L"page", kFwd, kIgnoreCase, &ordinal));
    919   EXPECT_EQ(ASCIIToUTF16("1 of 1"), GetMatchCountText());
    920 
    921   // Now create a second tab and load the same page.
    922   browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
    923   TabContentsWrapper* tab2 = browser()->GetSelectedTabContentsWrapper();
    924   EXPECT_NE(tab1, tab2);
    925 
    926   // Open the Find box.
    927   EnsureFindBoxOpen();
    928 
    929   // The new tab should have "page" prepopulated, since that was the last search
    930   // in the first tab.
    931   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
    932   // But it should not seem like a search has been issued.
    933   EXPECT_EQ(ASCIIToUTF16(""), GetMatchCountText());
    934 }
    935 
    936 // This makes sure that we can search for A in tabA, then for B in tabB and
    937 // when we come back to tabA we should still see A (because that was the last
    938 // search in that tab).
    939 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulatePreserveLast) {
    940 #if defined(OS_MACOSX)
    941   // FindInPage on Mac doesn't use prepopulated values. Search there is global.
    942   return;
    943 #endif
    944 
    945   ASSERT_TRUE(test_server()->Start());
    946 
    947   // First we navigate to any page.
    948   GURL url = test_server()->GetURL(kSimple);
    949   ui_test_utils::NavigateToURL(browser(), url);
    950 
    951   // Search for the word "page".
    952   int ordinal = 0;
    953   TabContentsWrapper* tab1 = browser()->GetSelectedTabContentsWrapper();
    954   EXPECT_EQ(1, FindInPageWchar(tab1, L"page", kFwd, kIgnoreCase, &ordinal));
    955 
    956   // Open the Find box.
    957   EnsureFindBoxOpen();
    958 
    959   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
    960 
    961   // Close the Find box.
    962   browser()->GetFindBarController()->EndFindSession(
    963       FindBarController::kKeepSelection);
    964 
    965   // Now create a second tab and load the same page.
    966   browser::NavigateParams params(browser(), url, PageTransition::TYPED);
    967   params.disposition = NEW_BACKGROUND_TAB;
    968   params.tabstrip_add_types = TabStripModel::ADD_NONE;
    969   browser::Navigate(&params);
    970   browser()->ActivateTabAt(1, false);
    971   TabContentsWrapper* tab2 = browser()->GetSelectedTabContentsWrapper();
    972   EXPECT_NE(tab1, tab2);
    973 
    974   // Search for the word "text".
    975   FindInPageWchar(tab2, L"text", kFwd, kIgnoreCase, &ordinal);
    976 
    977   // Go back to the first tab and make sure we have NOT switched the prepopulate
    978   // text to "text".
    979   browser()->ActivateTabAt(0, false);
    980 
    981   // Open the Find box.
    982   EnsureFindBoxOpen();
    983 
    984   // After the Find box has been reopened, it should have been prepopulated with
    985   // the word "page" again, since that was the last search in that tab.
    986   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
    987 
    988   // Close the Find box.
    989   browser()->GetFindBarController()->EndFindSession(
    990       FindBarController::kKeepSelection);
    991 
    992   // Re-open the Find box.
    993   // This is a special case: previous search in TabContents used to get cleared
    994   // if you opened and closed the FindBox, which would cause the global
    995   // prepopulate value to show instead of last search in this tab.
    996   EnsureFindBoxOpen();
    997 
    998   // After the Find box has been reopened, it should have been prepopulated with
    999   // the word "page" again, since that was the last search in that tab.
   1000   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarText());
   1001 }
   1002 
   1003 // TODO(rohitrao): Searching in incognito tabs does not work in browser tests in
   1004 // Linux views.  Investigate and fix.  http://crbug.com/40948
   1005 #if defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
   1006 #define MAYBE_NoIncognitoPrepopulate DISABLED_NoIncognitoPrepopulate
   1007 #else
   1008 #define MAYBE_NoIncognitoPrepopulate NoIncognitoPrepopulate
   1009 #endif
   1010 
   1011 // This tests that search terms entered into an incognito find bar are not used
   1012 // as prepopulate terms for non-incognito windows.
   1013 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_NoIncognitoPrepopulate) {
   1014 #if defined(OS_MACOSX)
   1015   // FindInPage on Mac doesn't use prepopulated values. Search there is global.
   1016   return;
   1017 #endif
   1018 
   1019   ASSERT_TRUE(test_server()->Start());
   1020 
   1021   // First we navigate to the "simple" test page.
   1022   GURL url = test_server()->GetURL(kSimple);
   1023   ui_test_utils::NavigateToURL(browser(), url);
   1024 
   1025   // Search for the word "page" in the normal browser tab.
   1026   int ordinal = 0;
   1027   TabContentsWrapper* tab1 = browser()->GetSelectedTabContentsWrapper();
   1028   EXPECT_EQ(1, FindInPageWchar(tab1, L"page", kFwd, kIgnoreCase, &ordinal));
   1029 
   1030   // Open the Find box.
   1031   EnsureFindBoxOpenForBrowser(browser());
   1032   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarTextForBrowser(browser()));
   1033 
   1034   // Close the Find box.
   1035   browser()->GetFindBarController()->EndFindSession(
   1036       FindBarController::kKeepSelection);
   1037 
   1038   // Open a new incognito window and navigate to the same page.
   1039   Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
   1040   Browser* incognito_browser = Browser::Create(incognito_profile);
   1041   incognito_browser->AddSelectedTabWithURL(url, PageTransition::START_PAGE);
   1042   ui_test_utils::WaitForNavigation(
   1043       &incognito_browser->GetSelectedTabContents()->controller());
   1044   incognito_browser->window()->Show();
   1045 
   1046   // Open the find box and make sure that it is prepopulated with "page".
   1047   EnsureFindBoxOpenForBrowser(incognito_browser);
   1048   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarTextForBrowser(incognito_browser));
   1049 
   1050   // Search for the word "text" in the incognito tab.
   1051   TabContentsWrapper* incognito_tab =
   1052       incognito_browser->GetSelectedTabContentsWrapper();
   1053   EXPECT_EQ(1, FindInPageWchar(incognito_tab, L"text",
   1054                                kFwd, kIgnoreCase, &ordinal));
   1055   EXPECT_EQ(ASCIIToUTF16("text"), GetFindBarTextForBrowser(incognito_browser));
   1056 
   1057   // Close the Find box.
   1058   incognito_browser->GetFindBarController()->EndFindSession(
   1059       FindBarController::kKeepSelection);
   1060 
   1061   // Now open a new tab in the original (non-incognito) browser.
   1062   browser()->AddSelectedTabWithURL(url, PageTransition::TYPED);
   1063   TabContentsWrapper* tab2 = browser()->GetSelectedTabContentsWrapper();
   1064   EXPECT_NE(tab1, tab2);
   1065 
   1066   // Open the Find box and make sure it is prepopulated with the search term
   1067   // from the original browser, not the search term from the incognito window.
   1068   EnsureFindBoxOpenForBrowser(browser());
   1069   EXPECT_EQ(ASCIIToUTF16("page"), GetFindBarTextForBrowser(browser()));
   1070 }
   1071 
   1072 // This makes sure that dismissing the find bar with kActivateSelection works.
   1073 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, ActivateLinkNavigatesPage) {
   1074   ASSERT_TRUE(test_server()->Start());
   1075 
   1076   // First we navigate to our test content.
   1077   GURL url = test_server()->GetURL(kLinkPage);
   1078   ui_test_utils::NavigateToURL(browser(), url);
   1079 
   1080   TabContentsWrapper* tab = browser()->GetSelectedTabContentsWrapper();
   1081   int ordinal = 0;
   1082   FindInPageWchar(tab, L"link", kFwd, kIgnoreCase, &ordinal);
   1083   EXPECT_EQ(ordinal, 1);
   1084 
   1085   // End the find session, click on the link.
   1086   tab->find_tab_helper()->StopFinding(FindBarController::kActivateSelection);
   1087   EXPECT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser()));
   1088 }
   1089