Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / search / instant_test_utils.cc
blob14021296a582d0f2a5c458bebd289617b11db704
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"
27 namespace {
29 std::string WrapScript(const std::string& script) {
30 return "domAutomationController.send(" + script + ")";
33 } // namespace
35 // InstantTestBase -----------------------------------------------------------
37 InstantTestBase::InstantTestBase()
38 : https_test_server_(
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) {
48 browser_ = browser;
50 TemplateURLService* service =
51 TemplateURLServiceFactory::GetForProfile(browser_->profile());
52 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
54 TemplateURLData data;
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);
77 TemplateURLData data;
78 data.SetShortName(base::ASCIIToUTF16("name"));
79 data.SetURL(url);
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,
88 const GURL& ntp_url,
89 bool init_suggestions_url) {
90 instant_url_ = instant_url;
91 ntp_url_ = ntp_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);
102 } else {
103 browser_->window()->GetLocationBar()->FocusLocation(false);
107 void InstantTestBase::SetOmniboxText(const std::string& text) {
108 FocusOmnibox();
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();
117 nav_observer.Wait();
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();
125 nav_observer.Wait();
128 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
129 const std::string& script,
130 bool* result) {
131 return content::ExecuteScriptAndExtractBool(
132 contents, WrapScript(script), result);
135 bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
136 const std::string& script,
137 int* result) {
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,
150 bool expected) {
151 bool actual = !expected; // Purposely start with a mis-match.
152 // We can only use ASSERT_*() in a method that returns void, hence this
153 // convoluted check.
154 return GetBoolFromJS(contents, "!document.hidden", &actual) &&
155 actual == expected;
158 std::string InstantTestBase::GetOmniboxText() {
159 return base::UTF16ToUTF8(omnibox()->GetText());
162 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
163 const std::string& image,
164 bool* loaded) {
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);
176 if (start > end)
177 std::swap(start, end);
178 return omnibox()->GetText().substr(start, end - start);