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.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 #include "chrome/browser/ui/search/search_tab_helper.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/interactive_test_utils.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/variations/entropy_provider.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/result_codes.h"
26 #include "content/public/test/browser_test_utils.h"
30 std::string WrapScript(const std::string& script) {
31 return "domAutomationController.send(" + script + ")";
36 // InstantTestBase -----------------------------------------------------------
38 InstantTestBase::InstantTestBase()
40 net::SpawnedTestServer::TYPE_HTTPS,
41 net::BaseTestServer::SSLOptions(),
42 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
43 init_suggestions_url_(false) {
46 InstantTestBase::~InstantTestBase() {}
48 void InstantTestBase::SetupInstant(Browser* browser) {
51 TemplateURLService* service =
52 TemplateURLServiceFactory::GetForProfile(browser_->profile());
53 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
56 // Necessary to use exact URL for both the main URL and the alternate URL for
57 // search term extraction to work in InstantExtended.
58 data.short_name = base::ASCIIToUTF16("name");
59 data.SetURL(instant_url_.spec() +
60 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
61 data.instant_url = instant_url_.spec();
62 data.new_tab_url = ntp_url_.spec();
63 if (init_suggestions_url_)
64 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}";
65 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
66 data.search_terms_replacement_key = "strk";
68 TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
69 service->Add(template_url); // Takes ownership of |template_url|.
70 service->SetUserSelectedDefaultSearchProvider(template_url);
73 void InstantTestBase::SetInstantURL(const std::string& url) {
74 TemplateURLService* service =
75 TemplateURLServiceFactory::GetForProfile(browser_->profile());
76 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
79 data.short_name = base::ASCIIToUTF16("name");
81 data.instant_url = url;
83 TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
84 service->Add(template_url); // Takes ownership of |template_url|.
85 service->SetUserSelectedDefaultSearchProvider(template_url);
88 void InstantTestBase::Init(const GURL& instant_url,
90 bool init_suggestions_url) {
91 instant_url_ = instant_url;
93 init_suggestions_url_ = init_suggestions_url;
96 void InstantTestBase::FocusOmnibox() {
97 // If the omnibox already has focus, just notify SearchTabHelper.
98 if (omnibox()->model()->has_focus()) {
99 content::WebContents* active_tab =
100 browser_->tab_strip_model()->GetActiveWebContents();
101 SearchTabHelper::FromWebContents(active_tab)->OmniboxFocusChanged(
102 OMNIBOX_FOCUS_VISIBLE, OMNIBOX_FOCUS_CHANGE_EXPLICIT);
104 browser_->window()->GetLocationBar()->FocusLocation(false);
108 void InstantTestBase::SetOmniboxText(const std::string& text) {
110 omnibox()->SetUserText(base::UTF8ToUTF16(text));
113 void InstantTestBase::PressEnterAndWaitForNavigation() {
114 content::WindowedNotificationObserver nav_observer(
115 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
116 content::NotificationService::AllSources());
117 browser_->window()->GetLocationBar()->AcceptInput();
121 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
122 const std::string& script,
124 return content::ExecuteScriptAndExtractBool(
125 contents, WrapScript(script), result);
128 bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
129 const std::string& script,
131 return content::ExecuteScriptAndExtractInt(
132 contents, WrapScript(script), result);
135 bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
136 const std::string& script,
137 std::string* result) {
138 return content::ExecuteScriptAndExtractString(
139 contents, WrapScript(script), result);
142 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
144 bool actual = !expected; // Purposely start with a mis-match.
145 // We can only use ASSERT_*() in a method that returns void, hence this
147 return GetBoolFromJS(contents, "!document.hidden", &actual) &&
151 std::string InstantTestBase::GetOmniboxText() {
152 return base::UTF16ToUTF8(omnibox()->GetText());
155 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
156 const std::string& image,
158 std::string js_chrome =
159 "var img = document.createElement('img');"
160 "img.onerror = function() { domAutomationController.send(false); };"
161 "img.onload = function() { domAutomationController.send(true); };"
162 "img.src = '" + image + "';";
163 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
166 base::string16 InstantTestBase::GetBlueText() {
167 size_t start = 0, end = 0;
168 omnibox()->GetSelectionBounds(&start, &end);
170 std::swap(start, end);
171 return omnibox()->GetText().substr(start, end - start);