NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / search / instant_test_utils.cc
blobcc3e96dea4bb323c65f93d4b2b365e47e1031b4a
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/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/interactive_test_utils.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "components/variations/entropy_provider.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/result_codes.h"
24 #include "content/public/test/browser_test_utils.h"
26 namespace {
28 std::string WrapScript(const std::string& script) {
29 return "domAutomationController.send(" + script + ")";
32 } // namespace
34 // InstantTestBase -----------------------------------------------------------
36 InstantTestBase::InstantTestBase()
37 : https_test_server_(
38 net::SpawnedTestServer::TYPE_HTTPS,
39 net::BaseTestServer::SSLOptions(),
40 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
41 init_suggestions_url_(false) {
44 InstantTestBase::~InstantTestBase() {}
46 void InstantTestBase::SetupInstant(Browser* browser) {
47 browser_ = browser;
49 TemplateURLService* service =
50 TemplateURLServiceFactory::GetForProfile(browser_->profile());
51 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
53 TemplateURLData data;
54 // Necessary to use exact URL for both the main URL and the alternate URL for
55 // search term extraction to work in InstantExtended.
56 data.short_name = base::ASCIIToUTF16("name");
57 data.SetURL(instant_url_.spec() +
58 "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
59 data.instant_url = instant_url_.spec();
60 data.new_tab_url = ntp_url_.spec();
61 if (init_suggestions_url_)
62 data.suggestions_url = instant_url_.spec() + "#q={searchTerms}";
63 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
64 data.search_terms_replacement_key = "strk";
66 TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
67 service->Add(template_url); // Takes ownership of |template_url|.
68 service->SetDefaultSearchProvider(template_url);
71 void InstantTestBase::SetInstantURL(const std::string& url) {
72 TemplateURLService* service =
73 TemplateURLServiceFactory::GetForProfile(browser_->profile());
74 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
76 TemplateURLData data;
77 data.short_name = base::ASCIIToUTF16("name");
78 data.SetURL(url);
79 data.instant_url = url;
81 TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
82 service->Add(template_url); // Takes ownership of |template_url|.
83 service->SetDefaultSearchProvider(template_url);
86 void InstantTestBase::Init(const GURL& instant_url,
87 const GURL& ntp_url,
88 bool init_suggestions_url) {
89 instant_url_ = instant_url;
90 ntp_url_ = ntp_url;
91 init_suggestions_url_ = init_suggestions_url;
94 void InstantTestBase::FocusOmnibox() {
95 // If the omnibox already has focus, just notify Instant.
96 if (omnibox()->model()->has_focus()) {
97 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE,
98 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL);
99 } else {
100 browser_->window()->GetLocationBar()->FocusLocation(false);
104 void InstantTestBase::SetOmniboxText(const std::string& text) {
105 FocusOmnibox();
106 omnibox()->SetUserText(base::UTF8ToUTF16(text));
109 void InstantTestBase::PressEnterAndWaitForNavigation() {
110 content::WindowedNotificationObserver nav_observer(
111 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
112 content::NotificationService::AllSources());
113 browser_->window()->GetLocationBar()->AcceptInput();
114 nav_observer.Wait();
117 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
118 const std::string& script,
119 bool* result) {
120 return content::ExecuteScriptAndExtractBool(
121 contents, WrapScript(script), result);
124 bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
125 const std::string& script,
126 int* result) {
127 return content::ExecuteScriptAndExtractInt(
128 contents, WrapScript(script), result);
131 bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
132 const std::string& script,
133 std::string* result) {
134 return content::ExecuteScriptAndExtractString(
135 contents, WrapScript(script), result);
138 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
139 bool expected) {
140 bool actual = !expected; // Purposely start with a mis-match.
141 // We can only use ASSERT_*() in a method that returns void, hence this
142 // convoluted check.
143 return GetBoolFromJS(contents, "!document.hidden", &actual) &&
144 actual == expected;
147 std::string InstantTestBase::GetOmniboxText() {
148 return base::UTF16ToUTF8(omnibox()->GetText());
151 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
152 const std::string& image,
153 bool* loaded) {
154 std::string js_chrome =
155 "var img = document.createElement('img');"
156 "img.onerror = function() { domAutomationController.send(false); };"
157 "img.onload = function() { domAutomationController.send(true); };"
158 "img.src = '" + image + "';";
159 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
162 base::string16 InstantTestBase::GetBlueText() {
163 size_t start = 0, end = 0;
164 omnibox()->GetSelectionBounds(&start, &end);
165 if (start > end)
166 std::swap(start, end);
167 return omnibox()->GetText().substr(start, end - start);