Home | History | Annotate | Download | only in views
      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/strings/string_util.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "chrome/browser/chrome_notification_types.h"
      8 #include "chrome/browser/ui/browser.h"
      9 #include "chrome/browser/ui/browser_commands.h"
     10 #include "chrome/browser/ui/browser_tabstrip.h"
     11 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
     12 #include "chrome/browser/ui/find_bar/find_notification_details.h"
     13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     14 #include "chrome/browser/ui/view_ids.h"
     15 #include "chrome/browser/ui/views/find_bar_host.h"
     16 #include "chrome/browser/ui/views/frame/browser_view.h"
     17 #include "chrome/test/base/in_process_browser_test.h"
     18 #include "chrome/test/base/interactive_test_utils.h"
     19 #include "chrome/test/base/ui_test_utils.h"
     20 #include "content/public/browser/notification_service.h"
     21 #include "content/public/browser/web_contents.h"
     22 #include "net/test/spawned_test_server/spawned_test_server.h"
     23 #include "ui/base/clipboard/clipboard.h"
     24 #include "ui/events/keycodes/keyboard_codes.h"
     25 #include "ui/views/focus/focus_manager.h"
     26 #include "ui/views/view.h"
     27 #include "ui/views/views_delegate.h"
     28 
     29 using base::ASCIIToUTF16;
     30 using content::WebContents;
     31 
     32 namespace {
     33 
     34 static const char kSimplePage[] = "files/find_in_page/simple.html";
     35 
     36 class FindInPageTest : public InProcessBrowserTest {
     37  public:
     38   FindInPageTest() {
     39     FindBarHost::disable_animations_during_testing_ = true;
     40   }
     41 
     42   base::string16 GetFindBarText() {
     43     FindBar* find_bar = browser()->GetFindBarController()->find_bar();
     44     return find_bar->GetFindText();
     45   }
     46 
     47   base::string16 GetFindBarSelectedText() {
     48     FindBarTesting* find_bar =
     49         browser()->GetFindBarController()->find_bar()->GetFindBarTesting();
     50     return find_bar->GetFindSelectedText();
     51   }
     52 
     53  private:
     54   DISALLOW_COPY_AND_ASSIGN(FindInPageTest);
     55 };
     56 
     57 }  // namespace
     58 
     59 // Flaky because the test server fails to start? See: http://crbug.com/96594.
     60 IN_PROC_BROWSER_TEST_F(FindInPageTest, CrashEscHandlers) {
     61   ASSERT_TRUE(test_server()->Start());
     62 
     63   // First we navigate to our test page (tab A).
     64   GURL url = test_server()->GetURL(kSimplePage);
     65   ui_test_utils::NavigateToURL(browser(), url);
     66 
     67   chrome::Find(browser());
     68 
     69   // Open another tab (tab B).
     70   chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
     71 
     72   chrome::Find(browser());
     73   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
     74                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
     75 
     76   // Select tab A.
     77   browser()->tab_strip_model()->ActivateTabAt(0, true);
     78 
     79   // Close tab B.
     80   browser()->tab_strip_model()->CloseWebContentsAt(1,
     81                                                    TabStripModel::CLOSE_NONE);
     82 
     83   // Click on the location bar so that Find box loses focus.
     84   ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(),
     85                                                      VIEW_ID_OMNIBOX));
     86   // Check the location bar is focused.
     87   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
     88 
     89   // This used to crash until bug 1303709 was fixed.
     90   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
     91       browser(), ui::VKEY_ESCAPE, false, false, false, false));
     92 }
     93 
     94 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
     95 // TODO(erg): linux_aura bringup: http://crbug.com/163931
     96 #define MAYBE_FocusRestore DISABLED_FocusRestore
     97 #else
     98 #define MAYBE_FocusRestore FocusRestore
     99 #endif
    100 
    101 // Flaky because the test server fails to start? See: http://crbug.com/96594.
    102 IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_FocusRestore) {
    103   ASSERT_TRUE(test_server()->Start());
    104 
    105   GURL url = test_server()->GetURL("title1.html");
    106   ui_test_utils::NavigateToURL(browser(), url);
    107 
    108   // Focus the location bar, open and close the find-in-page, focus should
    109   // return to the location bar.
    110   chrome::FocusLocationBar(browser());
    111   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
    112   // Ensure the creation of the find bar controller.
    113   browser()->GetFindBarController()->Show();
    114   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    115                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    116   browser()->GetFindBarController()->EndFindSession(
    117       FindBarController::kKeepSelectionOnPage,
    118       FindBarController::kKeepResultsInFindBox);
    119   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
    120 
    121   // Focus the location bar, find something on the page, close the find box,
    122   // focus should go to the page.
    123   chrome::FocusLocationBar(browser());
    124   chrome::Find(browser());
    125   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    126                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    127   ui_test_utils::FindInPage(
    128       browser()->tab_strip_model()->GetActiveWebContents(),
    129       ASCIIToUTF16("a"), true, false, NULL, NULL);
    130   browser()->GetFindBarController()->EndFindSession(
    131       FindBarController::kKeepSelectionOnPage,
    132       FindBarController::kKeepResultsInFindBox);
    133   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
    134 
    135   // Focus the location bar, open and close the find box, focus should return to
    136   // the location bar (same as before, just checking that http://crbug.com/23599
    137   // is fixed).
    138   chrome::FocusLocationBar(browser());
    139   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
    140   browser()->GetFindBarController()->Show();
    141   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    142                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    143   browser()->GetFindBarController()->EndFindSession(
    144       FindBarController::kKeepSelectionOnPage,
    145       FindBarController::kKeepResultsInFindBox);
    146   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
    147 }
    148 
    149 
    150 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
    151 // interactive_ui_tests.
    152 // http://crbug.com/311363
    153 IN_PROC_BROWSER_TEST_F(FindInPageTest, DISABLED_SelectionRestoreOnTabSwitch) {
    154   ASSERT_TRUE(test_server()->Start());
    155 
    156   // Make sure Chrome is in the foreground, otherwise sending input
    157   // won't do anything and the test will hang.
    158   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
    159 
    160   // First we navigate to any page in the current tab (tab A).
    161   GURL url = test_server()->GetURL(kSimplePage);
    162   ui_test_utils::NavigateToURL(browser(), url);
    163 
    164   // Show the Find bar.
    165   browser()->GetFindBarController()->Show();
    166 
    167   // Search for "abc".
    168   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    169       browser(), ui::VKEY_A, false, false, false, false));
    170   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    171       browser(), ui::VKEY_B, false, false, false, false));
    172   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    173       browser(), ui::VKEY_C, false, false, false, false));
    174   EXPECT_EQ(ASCIIToUTF16("abc"), GetFindBarText());
    175 
    176   // Select "bc".
    177   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    178       browser(), ui::VKEY_LEFT, false, true, false, false));
    179   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    180       browser(), ui::VKEY_LEFT, false, true, false, false));
    181   EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
    182 
    183   // Open another tab (tab B).
    184   content::WindowedNotificationObserver observer(
    185       content::NOTIFICATION_LOAD_STOP,
    186       content::NotificationService::AllSources());
    187   chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
    188   observer.Wait();
    189 
    190   // Show the Find bar.
    191   browser()->GetFindBarController()->Show();
    192 
    193   // Search for "def".
    194   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    195       browser(), ui::VKEY_D, false, false, false, false));
    196   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    197       browser(), ui::VKEY_E, false, false, false, false));
    198   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    199       browser(), ui::VKEY_F, false, false, false, false));
    200   EXPECT_EQ(ASCIIToUTF16("def"), GetFindBarText());
    201 
    202   // Select "de".
    203   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    204       browser(), ui::VKEY_HOME, false, false, false, false));
    205   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    206       browser(), ui::VKEY_RIGHT, false, true, false, false));
    207   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    208       browser(), ui::VKEY_RIGHT, false, true, false, false));
    209   EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
    210 
    211   // Select tab A. Find bar should select "bc".
    212   browser()->tab_strip_model()->ActivateTabAt(0, true);
    213   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    214                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    215   EXPECT_EQ(ASCIIToUTF16("bc"), GetFindBarSelectedText());
    216 
    217   // Select tab B. Find bar should select "de".
    218   browser()->tab_strip_model()->ActivateTabAt(1, true);
    219   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    220                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    221   EXPECT_EQ(ASCIIToUTF16("de"), GetFindBarSelectedText());
    222 }
    223 
    224 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
    225 // TODO(erg): linux_aura bringup: http://crbug.com/163931
    226 #define MAYBE_FocusRestoreOnTabSwitch DISABLED_FocusRestoreOnTabSwitch
    227 #else
    228 #define MAYBE_FocusRestoreOnTabSwitch FocusRestoreOnTabSwitch
    229 #endif
    230 
    231 // Flaky because the test server fails to start? See: http://crbug.com/96594.
    232 IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_FocusRestoreOnTabSwitch) {
    233   ASSERT_TRUE(test_server()->Start());
    234 
    235   // First we navigate to our test page (tab A).
    236   GURL url = test_server()->GetURL(kSimplePage);
    237   ui_test_utils::NavigateToURL(browser(), url);
    238 
    239   chrome::Find(browser());
    240   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    241                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    242 
    243   // Search for 'a'.
    244   ui_test_utils::FindInPage(
    245       browser()->tab_strip_model()->GetActiveWebContents(),
    246       ASCIIToUTF16("a"), true, false, NULL, NULL);
    247   EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
    248 
    249   // Open another tab (tab B).
    250   content::WindowedNotificationObserver observer(
    251       content::NOTIFICATION_LOAD_STOP,
    252       content::NotificationService::AllSources());
    253   chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
    254   observer.Wait();
    255 
    256   // Make sure Find box is open.
    257   chrome::Find(browser());
    258   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    259                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    260 
    261   // Search for 'b'.
    262   ui_test_utils::FindInPage(
    263       browser()->tab_strip_model()->GetActiveWebContents(),
    264       ASCIIToUTF16("b"), true, false, NULL, NULL);
    265   EXPECT_EQ(ASCIIToUTF16("b"), GetFindBarSelectedText());
    266 
    267   // Set focus away from the Find bar (to the Location bar).
    268   chrome::FocusLocationBar(browser());
    269   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
    270 
    271   // Select tab A. Find bar should get focus.
    272   browser()->tab_strip_model()->ActivateTabAt(0, true);
    273   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    274                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    275   EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
    276 
    277   // Select tab B. Location bar should get focus.
    278   browser()->tab_strip_model()->ActivateTabAt(1, true);
    279   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
    280 }
    281 
    282 // FindInPage on Mac doesn't use prepopulated values. Search there is global.
    283 #if !defined(OS_MACOSX) && !defined(USE_AURA)
    284 // Flaky because the test server fails to start? See: http://crbug.com/96594.
    285 // This tests that whenever you clear values from the Find box and close it that
    286 // it respects that and doesn't show you the last search, as reported in bug:
    287 // http://crbug.com/40121. For Aura see bug http://crbug.com/292299.
    288 IN_PROC_BROWSER_TEST_F(FindInPageTest, PrepopulateRespectBlank) {
    289   ASSERT_TRUE(test_server()->Start());
    290 
    291   // Make sure Chrome is in the foreground, otherwise sending input
    292   // won't do anything and the test will hang.
    293   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
    294 
    295   // First we navigate to any page.
    296   GURL url = test_server()->GetURL(kSimplePage);
    297   ui_test_utils::NavigateToURL(browser(), url);
    298 
    299   // Show the Find bar.
    300   browser()->GetFindBarController()->Show();
    301 
    302   // Search for "a".
    303   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    304       browser(), ui::VKEY_A, false, false, false, false));
    305 
    306   // We should find "a" here.
    307   EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
    308 
    309   // Delete "a".
    310   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    311       browser(), ui::VKEY_BACK, false, false, false, false));
    312 
    313   // Validate we have cleared the text.
    314   EXPECT_EQ(base::string16(), GetFindBarText());
    315 
    316   // Close the Find box.
    317   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    318       browser(), ui::VKEY_ESCAPE, false, false, false, false));
    319 
    320   // Show the Find bar.
    321   browser()->GetFindBarController()->Show();
    322 
    323   // After the Find box has been reopened, it should not have been prepopulated
    324   // with "a" again.
    325   EXPECT_EQ(base::string16(), GetFindBarText());
    326 
    327   // Close the Find box.
    328   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    329       browser(), ui::VKEY_ESCAPE, false, false, false, false));
    330 
    331   // Press F3 to trigger FindNext.
    332   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    333       browser(), ui::VKEY_F3, false, false, false, false));
    334 
    335   // After the Find box has been reopened, it should still have no prepopulate
    336   // value.
    337   EXPECT_EQ(base::string16(), GetFindBarText());
    338 }
    339 #endif
    340 
    341 // Flaky on Win. http://crbug.com/92467
    342 // Flaky on ChromeOS. http://crbug.com/118216
    343 // Flaky on linux aura. http://crbug.com/163931
    344 #if defined(TOOLKIT_VIEWS)
    345 #define MAYBE_PasteWithoutTextChange DISABLED_PasteWithoutTextChange
    346 #else
    347 #define MAYBE_PasteWithoutTextChange PasteWithoutTextChange
    348 #endif
    349 
    350 IN_PROC_BROWSER_TEST_F(FindInPageTest, MAYBE_PasteWithoutTextChange) {
    351   ASSERT_TRUE(test_server()->Start());
    352 
    353   // Make sure Chrome is in the foreground, otherwise sending input
    354   // won't do anything and the test will hang.
    355   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
    356 
    357   // First we navigate to any page.
    358   GURL url = test_server()->GetURL(kSimplePage);
    359   ui_test_utils::NavigateToURL(browser(), url);
    360 
    361   // Show the Find bar.
    362   browser()->GetFindBarController()->Show();
    363 
    364   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    365                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    366 
    367   // Search for "a".
    368   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    369       browser(), ui::VKEY_A, false, false, false, false));
    370 
    371   // We should find "a" here.
    372   EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarText());
    373 
    374   // Reload the page to clear the matching result.
    375   chrome::Reload(browser(), CURRENT_TAB);
    376 
    377   // Focus the Find bar again to make sure the text is selected.
    378   browser()->GetFindBarController()->Show();
    379 
    380   EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(),
    381                                            VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
    382 
    383   // "a" should be selected.
    384   EXPECT_EQ(ASCIIToUTF16("a"), GetFindBarSelectedText());
    385 
    386   // Press Ctrl-C to copy the content.
    387   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    388       browser(), ui::VKEY_C, true, false, false, false));
    389 
    390   base::string16 str;
    391   ui::Clipboard::GetForCurrentThread()->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE,
    392                                                  &str);
    393 
    394   // Make sure the text is copied successfully.
    395   EXPECT_EQ(ASCIIToUTF16("a"), str);
    396 
    397   // Press Ctrl-V to paste the content back, it should start finding even if the
    398   // content is not changed.
    399   content::Source<WebContents> notification_source(
    400       browser()->tab_strip_model()->GetActiveWebContents());
    401   ui_test_utils::WindowedNotificationObserverWithDetails
    402       <FindNotificationDetails> observer(
    403           chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, notification_source);
    404 
    405   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    406       browser(), ui::VKEY_V, true, false, false, false));
    407 
    408   ASSERT_NO_FATAL_FAILURE(observer.Wait());
    409   FindNotificationDetails details;
    410   ASSERT_TRUE(observer.GetDetailsFor(notification_source.map_key(), &details));
    411   EXPECT_TRUE(details.number_of_matches() > 0);
    412 }
    413 
    414 #if defined(OS_WIN)
    415 // TODO(phajdan.jr): Disabling due to possible timing issues on XP
    416 // interactive_ui_tests.
    417 // http://crbug.com/311363
    418 IN_PROC_BROWSER_TEST_F(FindInPageTest, DISABLED_CtrlEnter) {
    419   ui_test_utils::NavigateToURL(browser(),
    420                                GURL("data:text/html,This is some text with a "
    421                                     "<a href=\"about:blank\">link</a>."));
    422 
    423   browser()->GetFindBarController()->Show();
    424 
    425   // Search for "link".
    426   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    427       browser(), ui::VKEY_L, false, false, false, false));
    428   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    429       browser(), ui::VKEY_I, false, false, false, false));
    430   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    431       browser(), ui::VKEY_N, false, false, false, false));
    432   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    433       browser(), ui::VKEY_K, false, false, false, false));
    434   EXPECT_EQ(ASCIIToUTF16("link"), GetFindBarText());
    435 
    436   ui_test_utils::UrlLoadObserver observer(
    437       GURL("about:blank"), content::NotificationService::AllSources());
    438 
    439   // Send Ctrl-Enter, should cause navigation to about:blank.
    440   ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
    441       browser(), ui::VKEY_RETURN, true, false, false, false));
    442 
    443   observer.Wait();
    444 }
    445 #endif
    446