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 FindInPageWchar(WebContents
* web_contents
,
34 const wchar_t* search_str
,
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
);
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
63 IN_PROC_BROWSER_TEST_F(FindInPageInteractiveTest
, FindInPageEndState
) {
64 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
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()));
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
);
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
);
80 // Verify that nothing has focus.
82 ASSERT_TRUE(FocusedOnPage(web_contents
, &result
));
83 ASSERT_STREQ("{nothing focused}", result
.c_str());
85 // Search for a text that exists within a link on the page.
87 EXPECT_EQ(1, FindInPageWchar(web_contents
, L
"nk",
88 true, false, &ordinal
));
89 EXPECT_EQ(1, ordinal
);
91 // End the find session, which should set focus to the link.
92 find_tab_helper
->StopFinding(FindBarController::kKeepSelectionOnPage
);
94 // Verify that the link is focused.
95 ASSERT_TRUE(FocusedOnPage(web_contents
, &result
));
96 EXPECT_STREQ("link1", result
.c_str());
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
);
103 // Move the selection to link 1, after searching.
104 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
106 "window.domAutomationController.send(selectLink1());",
109 // End the find session.
110 find_tab_helper
->StopFinding(FindBarController::kKeepSelectionOnPage
);
112 // Verify that link2 is not focused.
113 ASSERT_TRUE(FocusedOnPage(web_contents
, &result
));
114 EXPECT_STREQ("", result
.c_str());