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.
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"
18 using base::WideToUTF16
;
19 using content::WebContents
;
20 using net::test_server::EmbeddedTestServer
;
24 const char kEndState
[] = "/find_in_page/end_state.html";
26 class FindInPageInteractiveTest
: public InProcessBrowserTest
{
28 FindInPageInteractiveTest() {
31 // Platform independent FindInPage that takes |const wchar_t*|
33 int FindInPageASCII(WebContents
* web_contents
,
34 const base::StringPiece
& search_str
,
38 base::string16
search_str16(ASCIIToUTF16(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
);
49 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute.
50 bool FocusedOnPage(WebContents
* web_contents
, std::string
* result
)
53 bool FocusedOnPage(WebContents
* web_contents
, std::string
* result
) {
54 return content::ExecuteScriptAndExtractString(
56 "window.domAutomationController.send(getFocusedElement());",
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
64 #if defined(OS_CHROMEOS)
65 #define MAYBE_FindInPageEndState DISABLED_FindInPageEndState
67 #define MAYBE_FindInPageEndState FindInPageEndState
69 IN_PROC_BROWSER_TEST_F(FindInPageInteractiveTest
, MAYBE_FindInPageEndState
) {
70 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
72 // Make sure Chrome is in the foreground, otherwise sending input
73 // won't do anything and the test will hang.
74 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
76 // First we navigate to our special focus tracking page.
77 GURL url
= embedded_test_server()->GetURL(kEndState
);
78 ui_test_utils::NavigateToURL(browser(), url
);
80 WebContents
* web_contents
=
81 browser()->tab_strip_model()->GetActiveWebContents();
82 ASSERT_TRUE(NULL
!= web_contents
);
83 FindTabHelper
* find_tab_helper
=
84 FindTabHelper::FromWebContents(web_contents
);
86 // Verify that nothing has focus.
88 ASSERT_TRUE(FocusedOnPage(web_contents
, &result
));
89 ASSERT_STREQ("{nothing focused}", result
.c_str());
91 // Search for a text that exists within a link on the page.
93 EXPECT_EQ(1, FindInPageASCII(web_contents
, "nk",
94 true, false, &ordinal
));
95 EXPECT_EQ(1, ordinal
);
97 // End the find session, which should set focus to the link.
98 find_tab_helper
->StopFinding(FindBarController::kKeepSelectionOnPage
);
100 // Verify that the link is focused.
101 ASSERT_TRUE(FocusedOnPage(web_contents
, &result
));
102 EXPECT_STREQ("link1", result
.c_str());
104 // Search for a text that exists within a link on the page.
105 EXPECT_EQ(1, FindInPageASCII(web_contents
, "Google",
106 true, false, &ordinal
));
107 EXPECT_EQ(1, ordinal
);
109 // Move the selection to link 1, after searching.
110 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
112 "window.domAutomationController.send(selectLink1());",
115 // End the find session.
116 find_tab_helper
->StopFinding(FindBarController::kKeepSelectionOnPage
);
118 // Verify that link2 is not focused.
119 ASSERT_TRUE(FocusedOnPage(web_contents
, &result
));
120 EXPECT_STREQ("", result
.c_str());