instant_test_utils.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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.
4
5#include "chrome/browser/ui/search/instant_test_utils.h"
6
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/instant_service.h"
13#include "chrome/browser/search/instant_service_factory.h"
14#include "chrome/browser/search_engines/template_url_service.h"
15#include "chrome/browser/search_engines/template_url_service_factory.h"
16#include "chrome/browser/ui/omnibox/omnibox_view.h"
17#include "chrome/browser/ui/search/instant_ntp.h"
18#include "chrome/browser/ui/search/instant_ntp_prerenderer.h"
19#include "chrome/common/chrome_switches.h"
20#include "chrome/common/pref_names.h"
21#include "chrome/test/base/interactive_test_utils.h"
22#include "chrome/test/base/ui_test_utils.h"
23#include "content/public/browser/notification_service.h"
24#include "content/public/browser/render_process_host.h"
25#include "content/public/browser/web_contents.h"
26#include "content/public/common/result_codes.h"
27#include "content/public/test/browser_test_utils.h"
28
29namespace {
30
31std::string WrapScript(const std::string& script) {
32  return "domAutomationController.send(" + script + ")";
33}
34
35}  // namespace
36
37// InstantTestBase -----------------------------------------------------------
38
39void InstantTestBase::SetupInstant(Browser* browser) {
40  browser_ = browser;
41  TemplateURLService* service =
42      TemplateURLServiceFactory::GetForProfile(browser_->profile());
43  ui_test_utils::WaitForTemplateURLServiceToLoad(service);
44
45  TemplateURLData data;
46  // Necessary to use exact URL for both the main URL and the alternate URL for
47  // search term extraction to work in InstantExtended.
48  data.short_name = ASCIIToUTF16("name");
49  data.SetURL(instant_url_.spec() +
50              "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
51  data.instant_url = instant_url_.spec();
52  if (init_suggestions_url_)
53    data.suggestions_url = instant_url_.spec() + "#q={searchTerms}";
54  data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
55  data.search_terms_replacement_key = "strk";
56
57  TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
58  service->Add(template_url);  // Takes ownership of |template_url|.
59  service->SetDefaultSearchProvider(template_url);
60
61  InstantService* instant_service =
62      InstantServiceFactory::GetForProfile(browser_->profile());
63  ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
64  instant_service->ntp_prerenderer()->ReloadInstantNTP();
65}
66
67void InstantTestBase::SetInstantURL(const std::string& url) {
68  TemplateURLService* service =
69      TemplateURLServiceFactory::GetForProfile(browser_->profile());
70  ui_test_utils::WaitForTemplateURLServiceToLoad(service);
71
72  TemplateURLData data;
73  data.short_name = ASCIIToUTF16("name");
74  data.SetURL(url);
75  data.instant_url = url;
76
77  TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
78  service->Add(template_url);  // Takes ownership of |template_url|.
79  service->SetDefaultSearchProvider(template_url);
80}
81
82void InstantTestBase::Init(const GURL& instant_url, bool init_suggestions_url) {
83  instant_url_ = instant_url;
84  init_suggestions_url_ = init_suggestions_url;
85}
86
87void InstantTestBase::FocusOmnibox() {
88  // If the omnibox already has focus, just notify Instant.
89  if (omnibox()->model()->has_focus()) {
90    instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE,
91                                   OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL);
92  } else {
93    browser_->window()->GetLocationBar()->FocusLocation(false);
94  }
95}
96
97void InstantTestBase::FocusOmniboxAndWaitForInstantNTPSupport() {
98  content::WindowedNotificationObserver ntp_observer(
99      chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED,
100      content::NotificationService::AllSources());
101  FocusOmnibox();
102
103  InstantService* instant_service =
104      InstantServiceFactory::GetForProfile(browser_->profile());
105  ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
106  if (!instant_service->ntp_prerenderer()->ntp() ||
107      !instant_service->ntp_prerenderer()->ntp()->supports_instant())
108    ntp_observer.Wait();
109}
110
111void InstantTestBase::SetOmniboxText(const std::string& text) {
112  FocusOmnibox();
113  omnibox()->SetUserText(UTF8ToUTF16(text));
114}
115
116void InstantTestBase::PressEnterAndWaitForNavigation() {
117  content::WindowedNotificationObserver nav_observer(
118      content::NOTIFICATION_NAV_ENTRY_COMMITTED,
119      content::NotificationService::AllSources());
120  browser_->window()->GetLocationBar()->AcceptInput();
121  nav_observer.Wait();
122}
123
124bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
125                                    const std::string& script,
126                                    bool* result) {
127  return content::ExecuteScriptAndExtractBool(
128      contents, WrapScript(script), result);
129}
130
131bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
132                                   const std::string& script,
133                                   int* result) {
134  return content::ExecuteScriptAndExtractInt(
135      contents, WrapScript(script), result);
136}
137
138bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
139                                      const std::string& script,
140                                      std::string* result) {
141  return content::ExecuteScriptAndExtractString(
142      contents, WrapScript(script), result);
143}
144
145bool InstantTestBase::ExecuteScript(const std::string& script) {
146  InstantService* instant_service =
147      InstantServiceFactory::GetForProfile(browser_instant()->profile());
148  if (!instant_service)
149    return false;
150  return content::ExecuteScript(instant_service->GetNTPContents(), script);
151}
152
153bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
154                                        bool expected) {
155  bool actual = !expected;  // Purposely start with a mis-match.
156  // We can only use ASSERT_*() in a method that returns void, hence this
157  // convoluted check.
158  return GetBoolFromJS(contents, "!document.webkitHidden", &actual) &&
159      actual == expected;
160}
161
162std::string InstantTestBase::GetOmniboxText() {
163  return UTF16ToUTF8(omnibox()->GetText());
164}
165
166bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
167                                const std::string& image,
168                                bool* loaded) {
169  std::string js_chrome =
170      "var img = document.createElement('img');"
171      "img.onerror = function() { domAutomationController.send(false); };"
172      "img.onload  = function() { domAutomationController.send(true); };"
173      "img.src = '" + image + "';";
174  return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
175}
176
177string16 InstantTestBase::GetBlueText() {
178  size_t start = 0, end = 0;
179  omnibox()->GetSelectionBounds(&start, &end);
180  if (start > end)
181    std::swap(start, end);
182  return omnibox()->GetText().substr(start, end - start);
183}
184