1 // Copyright 2013 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 "chrome/browser/ui/search/instant_test_utils.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/ui/search/search_tab_helper.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/omnibox/browser/omnibox_view.h"
19 #include "components/search_engines/template_url_service.h"
20 #include "components/variations/entropy_provider.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/result_codes.h"
25 #include "content/public/test/browser_test_utils.h"
29 std::string
WrapScript(const std::string
& script
) {
30 return "domAutomationController.send(" + script
+ ")";
35 // InstantTestBase -----------------------------------------------------------
37 InstantTestBase::InstantTestBase()
39 net::SpawnedTestServer::TYPE_HTTPS
,
40 net::BaseTestServer::SSLOptions(),
41 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
42 init_suggestions_url_(false) {
45 InstantTestBase::~InstantTestBase() {}
47 void InstantTestBase::SetupInstant(Browser
* browser
) {
50 TemplateURLService
* service
=
51 TemplateURLServiceFactory::GetForProfile(browser_
->profile());
52 ui_test_utils::WaitForTemplateURLServiceToLoad(service
);
55 // Necessary to use exact URL for both the main URL and the alternate URL for
56 // search term extraction to work in InstantExtended.
57 data
.SetShortName(base::ASCIIToUTF16("name"));
58 data
.SetURL(instant_url_
.spec() +
59 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
60 data
.instant_url
= instant_url_
.spec();
61 data
.new_tab_url
= ntp_url_
.spec();
62 if (init_suggestions_url_
)
63 data
.suggestions_url
= instant_url_
.spec() + "#q={searchTerms}";
64 data
.alternate_urls
.push_back(instant_url_
.spec() + "#q={searchTerms}");
65 data
.search_terms_replacement_key
= "strk";
67 TemplateURL
* template_url
= new TemplateURL(data
);
68 service
->Add(template_url
); // Takes ownership of |template_url|.
69 service
->SetUserSelectedDefaultSearchProvider(template_url
);
72 void InstantTestBase::SetInstantURL(const std::string
& url
) {
73 TemplateURLService
* service
=
74 TemplateURLServiceFactory::GetForProfile(browser_
->profile());
75 ui_test_utils::WaitForTemplateURLServiceToLoad(service
);
78 data
.SetShortName(base::ASCIIToUTF16("name"));
80 data
.instant_url
= url
;
82 TemplateURL
* template_url
= new TemplateURL(data
);
83 service
->Add(template_url
); // Takes ownership of |template_url|.
84 service
->SetUserSelectedDefaultSearchProvider(template_url
);
87 void InstantTestBase::Init(const GURL
& instant_url
,
89 bool init_suggestions_url
) {
90 instant_url_
= instant_url
;
92 init_suggestions_url_
= init_suggestions_url
;
95 void InstantTestBase::FocusOmnibox() {
96 // If the omnibox already has focus, just notify SearchTabHelper.
97 if (omnibox()->model()->has_focus()) {
98 content::WebContents
* active_tab
=
99 browser_
->tab_strip_model()->GetActiveWebContents();
100 SearchTabHelper::FromWebContents(active_tab
)->OmniboxFocusChanged(
101 OMNIBOX_FOCUS_VISIBLE
, OMNIBOX_FOCUS_CHANGE_EXPLICIT
);
103 browser_
->window()->GetLocationBar()->FocusLocation(false);
107 void InstantTestBase::SetOmniboxText(const std::string
& text
) {
109 omnibox()->SetUserText(base::UTF8ToUTF16(text
));
112 void InstantTestBase::PressEnterAndWaitForNavigation() {
113 content::WindowedNotificationObserver
nav_observer(
114 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
115 content::NotificationService::AllSources());
116 browser_
->window()->GetLocationBar()->AcceptInput();
120 void InstantTestBase::PressEnterAndWaitForFrameLoad() {
121 content::WindowedNotificationObserver
nav_observer(
122 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
123 content::NotificationService::AllSources());
124 browser_
->window()->GetLocationBar()->AcceptInput();
128 bool InstantTestBase::GetBoolFromJS(content::WebContents
* contents
,
129 const std::string
& script
,
131 return content::ExecuteScriptAndExtractBool(
132 contents
, WrapScript(script
), result
);
135 bool InstantTestBase::GetIntFromJS(content::WebContents
* contents
,
136 const std::string
& script
,
138 return content::ExecuteScriptAndExtractInt(
139 contents
, WrapScript(script
), result
);
142 bool InstantTestBase::GetStringFromJS(content::WebContents
* contents
,
143 const std::string
& script
,
144 std::string
* result
) {
145 return content::ExecuteScriptAndExtractString(
146 contents
, WrapScript(script
), result
);
149 bool InstantTestBase::CheckVisibilityIs(content::WebContents
* contents
,
151 bool actual
= !expected
; // Purposely start with a mis-match.
152 // We can only use ASSERT_*() in a method that returns void, hence this
154 return GetBoolFromJS(contents
, "!document.hidden", &actual
) &&
158 std::string
InstantTestBase::GetOmniboxText() {
159 return base::UTF16ToUTF8(omnibox()->GetText());
162 bool InstantTestBase::LoadImage(content::RenderViewHost
* rvh
,
163 const std::string
& image
,
165 std::string js_chrome
=
166 "var img = document.createElement('img');"
167 "img.onerror = function() { domAutomationController.send(false); };"
168 "img.onload = function() { domAutomationController.send(true); };"
169 "img.src = '" + image
+ "';";
170 return content::ExecuteScriptAndExtractBool(rvh
, js_chrome
, loaded
);
173 base::string16
InstantTestBase::GetBlueText() {
174 size_t start
= 0, end
= 0;
175 omnibox()->GetSelectionBounds(&start
, &end
);
177 std::swap(start
, end
);
178 return omnibox()->GetText().substr(start
, end
- start
);