Home | History | Annotate | Download | only in find_bar
      1 // Copyright 2014 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/utf_string_conversions.h"
      6 #include "chrome/browser/ui/browser_finder.h"
      7 #include "chrome/browser/ui/find_bar/find_bar.h"
      8 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
      9 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
     10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     11 #include "chrome/test/base/in_process_browser_test.h"
     12 #include "chrome/test/base/interactive_test_utils.h"
     13 #include "chrome/test/base/ui_test_utils.h"
     14 #include "content/public/browser/web_contents.h"
     15 #include "content/public/test/browser_test_utils.h"
     16 #include "net/test/embedded_test_server/embedded_test_server.h"
     17 
     18 using base::WideToUTF16;
     19 using content::WebContents;
     20 using net::test_server::EmbeddedTestServer;
     21 
     22 namespace {
     23 
     24 const char kEndState[] = "/find_in_page/end_state.html";
     25 
     26 class FindInPageInteractiveTest : public InProcessBrowserTest {
     27  public:
     28   FindInPageInteractiveTest() {
     29   }
     30 
     31   // Platform independent FindInPage that takes |const wchar_t*|
     32   // as an input.
     33   int FindInPageWchar(WebContents* web_contents,
     34                       const wchar_t* search_str,
     35                       bool forward,
     36                       bool case_sensitive,
     37                       int* ordinal) {
     38     base::string16 search_str16(WideToUTF16(std::wstring(search_str)));
     39     Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
     40     browser->GetFindBarController()->find_bar()->SetFindTextAndSelectedRange(
     41         search_str16, gfx::Range());
     42     return ui_test_utils::FindInPage(
     43         web_contents, search_str16, forward, case_sensitive, ordinal, NULL);
     44   }
     45 };
     46 
     47 }  // namespace
     48 
     49 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute.
     50 bool FocusedOnPage(WebContents* web_contents, std::string* result)
     51     WARN_UNUSED_RESULT;
     52 
     53 bool FocusedOnPage(WebContents* web_contents, std::string* result) {
     54   return content::ExecuteScriptAndExtractString(
     55       web_contents,
     56       "window.domAutomationController.send(getFocusedElement());",
     57       result);
     58 }
     59 
     60 // This tests the FindInPage end-state, in other words: what is focused when you
     61 // close the Find box (ie. if you find within a link the link should be
     62 // focused).
     63 IN_PROC_BROWSER_TEST_F(FindInPageInteractiveTest, FindInPageEndState) {
     64   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     65 
     66   // Make sure Chrome is in the foreground, otherwise sending input
     67   // won't do anything and the test will hang.
     68   ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
     69 
     70   // First we navigate to our special focus tracking page.
     71   GURL url = embedded_test_server()->GetURL(kEndState);
     72   ui_test_utils::NavigateToURL(browser(), url);
     73 
     74   WebContents* web_contents =
     75       browser()->tab_strip_model()->GetActiveWebContents();
     76   ASSERT_TRUE(NULL != web_contents);
     77   FindTabHelper* find_tab_helper =
     78       FindTabHelper::FromWebContents(web_contents);
     79 
     80   // Verify that nothing has focus.
     81   std::string result;
     82   ASSERT_TRUE(FocusedOnPage(web_contents, &result));
     83   ASSERT_STREQ("{nothing focused}", result.c_str());
     84 
     85   // Search for a text that exists within a link on the page.
     86   int ordinal = 0;
     87   EXPECT_EQ(1, FindInPageWchar(web_contents, L"nk",
     88                                true, false, &ordinal));
     89   EXPECT_EQ(1, ordinal);
     90 
     91   // End the find session, which should set focus to the link.
     92   find_tab_helper->StopFinding(FindBarController::kKeepSelectionOnPage);
     93 
     94   // Verify that the link is focused.
     95   ASSERT_TRUE(FocusedOnPage(web_contents, &result));
     96   EXPECT_STREQ("link1", result.c_str());
     97 
     98   // Search for a text that exists within a link on the page.
     99   EXPECT_EQ(1, FindInPageWchar(web_contents, L"Google",
    100                                true, false, &ordinal));
    101   EXPECT_EQ(1, ordinal);
    102 
    103   // Move the selection to link 1, after searching.
    104   ASSERT_TRUE(content::ExecuteScriptAndExtractString(
    105       web_contents,
    106       "window.domAutomationController.send(selectLink1());",
    107       &result));
    108 
    109   // End the find session.
    110   find_tab_helper->StopFinding(FindBarController::kKeepSelectionOnPage);
    111 
    112   // Verify that link2 is not focused.
    113   ASSERT_TRUE(FocusedOnPage(web_contents, &result));
    114   EXPECT_STREQ("", result.c_str());
    115 }
    116