summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/search/instant_test_utils.cc
blob: e55c2e2b96a4d1b9b4fa030a11ebe6194106de1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/search/instant_test_utils.h"

#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/omnibox/omnibox_view.h"
#include "chrome/browser/ui/search/instant_ntp.h"
#include "chrome/browser/ui/search/instant_overlay.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/result_codes.h"
#include "content/public/test/browser_test_utils.h"

namespace {

std::string WrapScript(const std::string& script) {
  return "domAutomationController.send(" + script + ")";
}

}  // namespace

// InstantTestModelObserver --------------------------------------------------

InstantTestModelObserver::InstantTestModelObserver(
    InstantOverlayModel* model,
    SearchMode::Type expected_mode_type)
    : model_(model),
      expected_mode_type_(expected_mode_type),
      observed_mode_type_(static_cast<SearchMode::Type>(-1)) {
  model_->AddObserver(this);
}

InstantTestModelObserver::~InstantTestModelObserver() {
  model_->RemoveObserver(this);
}

SearchMode::Type InstantTestModelObserver::WaitForExpectedOverlayState() {
  run_loop_.Run();
  return observed_mode_type_;
}

void InstantTestModelObserver::OverlayStateChanged(
    const InstantOverlayModel& model) {
  observed_mode_type_ = model.mode().mode;
  run_loop_.Quit();
}

// InstantTestBase -----------------------------------------------------------

void InstantTestBase::SetupInstant(Browser* browser) {
  browser_ = browser;
  TemplateURLService* service =
      TemplateURLServiceFactory::GetForProfile(browser_->profile());
  ui_test_utils::WaitForTemplateURLServiceToLoad(service);

  TemplateURLData data;
  // Necessary to use exact URL for both the main URL and the alternate URL for
  // search term extraction to work in InstantExtended.
  data.SetURL(instant_url_.spec() +
              "q={searchTerms}&is_search&{google:omniboxStartMarginParameter}");
  data.instant_url = instant_url_.spec();
  data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}");
  data.search_terms_replacement_key = "strk";

  TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
  service->Add(template_url);  // Takes ownership of |template_url|.
  service->SetDefaultSearchProvider(template_url);

  // TODO(shishir): Fix this ugly hack.
  instant()->SetInstantEnabled(false, true);
  instant()->SetInstantEnabled(true, false);
}

void InstantTestBase::SetInstantURL(const std::string& url) {
  TemplateURLService* service =
      TemplateURLServiceFactory::GetForProfile(browser_->profile());
  ui_test_utils::WaitForTemplateURLServiceToLoad(service);

  TemplateURLData data;
  data.SetURL(url);
  data.instant_url = url;

  TemplateURL* template_url = new TemplateURL(browser_->profile(), data);
  service->Add(template_url);  // Takes ownership of |template_url|.
  service->SetDefaultSearchProvider(template_url);
}

void InstantTestBase::Init(const GURL& instant_url) {
  instant_url_ = instant_url;
}

void InstantTestBase::KillInstantRenderView() {
  base::KillProcess(
      instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(),
      content::RESULT_CODE_KILLED,
      false);
}

void InstantTestBase::FocusOmnibox() {
  // If the omnibox already has focus, just notify Instant.
  if (omnibox()->model()->has_focus()) {
    instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE,
                                   OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL);
  } else {
    browser_->window()->GetLocationBar()->FocusLocation(false);
  }
}

void InstantTestBase::FocusOmniboxAndWaitForInstantNTPSupport() {
  content::WindowedNotificationObserver ntp_observer(
      chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED,
      content::NotificationService::AllSources());
  FocusOmnibox();
  if (!instant()->ntp() || !instant()->ntp()->supports_instant())
    ntp_observer.Wait();
}

void InstantTestBase::SetOmniboxText(const std::string& text) {
  FocusOmnibox();
  omnibox()->SetUserText(UTF8ToUTF16(text));
}

bool InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow(
    const std::string& text) {
  // The order of events may be:
  //   { hide, show } or just { show } depending on the order things
  // flow in from GWS and Chrome's response to hiding the infobar and/or
  // bookmark bar.  Note, the GWS response is relevant because of the
  // Instant "MANUAL_*" tests.
  InstantTestModelObserver first_observer(
      instant()->model(), SearchMode::MODE_DEFAULT);
  SetOmniboxText(text);

  SearchMode::Type observed = first_observer.WaitForExpectedOverlayState();
  if (observed == SearchMode::MODE_DEFAULT) {
    InstantTestModelObserver second_observer(
        instant()->model(), SearchMode::MODE_SEARCH_SUGGESTIONS);
    observed = second_observer.WaitForExpectedOverlayState();
  }
  EXPECT_EQ(SearchMode::MODE_SEARCH_SUGGESTIONS, observed);
  return observed == SearchMode::MODE_SEARCH_SUGGESTIONS;
}

void InstantTestBase::SetOmniboxTextAndWaitForSuggestion(
    const std::string& text) {
  content::WindowedNotificationObserver observer(
      chrome::NOTIFICATION_INSTANT_SET_SUGGESTION,
      content::NotificationService::AllSources());
  SetOmniboxText(text);
  observer.Wait();
}

void InstantTestBase::PressEnterAndWaitForNavigation() {
  content::WindowedNotificationObserver nav_observer(
      content::NOTIFICATION_NAV_ENTRY_COMMITTED,
      content::NotificationService::AllSources());
  browser_->window()->GetLocationBar()->AcceptInput();
  nav_observer.Wait();
}

bool InstantTestBase::GetBoolFromJS(content::WebContents* contents,
                                    const std::string& script,
                                    bool* result) {
  return content::ExecuteScriptAndExtractBool(
      contents, WrapScript(script), result);
}

bool InstantTestBase::GetIntFromJS(content::WebContents* contents,
                                   const std::string& script,
                                   int* result) {
  return content::ExecuteScriptAndExtractInt(
      contents, WrapScript(script), result);
}

bool InstantTestBase::GetStringFromJS(content::WebContents* contents,
                                      const std::string& script,
                                      std::string* result) {
  return content::ExecuteScriptAndExtractString(
      contents, WrapScript(script), result);
}

bool InstantTestBase::ExecuteScript(const std::string& script) {
  return content::ExecuteScript(instant()->GetOverlayContents(), script);
}

bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents,
                                        bool expected) {
  bool actual = !expected;  // Purposely start with a mis-match.
  // We can only use ASSERT_*() in a method that returns void, hence this
  // convoluted check.
  return GetBoolFromJS(contents, "!document.webkitHidden", &actual) &&
      actual == expected;
}

bool InstantTestBase::HasUserInputInProgress() {
  return omnibox()->model()->user_input_in_progress_;
}

bool InstantTestBase::HasTemporaryText() {
  return omnibox()->model()->has_temporary_text_;
}

std::string InstantTestBase::GetOmniboxText() {
  return UTF16ToUTF8(omnibox()->GetText());
}

bool InstantTestBase::LoadImage(content::RenderViewHost* rvh,
                                const std::string& image,
                                bool* loaded) {
  std::string js_chrome =
      "var img = document.createElement('img');"
      "img.onerror = function() { domAutomationController.send(false); };"
      "img.onload  = function() { domAutomationController.send(true); };"
      "img.src = '" + image + "';";
  return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded);
}

string16 InstantTestBase::GetBlueText() {
  size_t start = 0, end = 0;
  omnibox()->GetSelectionBounds(&start, &end);
  if (start > end)
    std::swap(start, end);
  return omnibox()->GetText().substr(start, end - start);
}

string16 InstantTestBase::GetGrayText() {
  return omnibox()->GetInstantSuggestion();
}