Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: chrome/browser/instant/instant_browsertest.cc

Issue 10836031: Remove Instant v1 API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update history, title, favicon Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/instant/instant_commit_type.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/string_util.h"
7 #include "base/stringprintf.h" 6 #include "base/stringprintf.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/content_settings/host_content_settings_map.h" 7 #include "chrome/browser/content_settings/host_content_settings_map.h"
8 #include "chrome/browser/history/history.h"
9 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/instant/instant_controller.h" 10 #include "chrome/browser/instant/instant_controller.h"
11 #include "chrome/browser/instant/instant_loader.h" 11 #include "chrome/browser/instant/instant_loader.h"
12 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search_engines/template_url.h"
15 #include "chrome/browser/search_engines/template_url_service.h" 13 #include "chrome/browser/search_engines/template_url_service.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h" 14 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/task_manager/task_manager.h" 15 #include "chrome/browser/task_manager/task_manager.h"
18 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" 16 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
19 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser_commands.h"
20 #include "chrome/browser/ui/browser_instant_controller.h" 18 #include "chrome/browser/ui/browser_instant_controller.h"
21 #include "chrome/browser/ui/browser_tabstrip.h" 19 #include "chrome/browser/ui/browser_tabstrip.h"
22 #include "chrome/browser/ui/browser_window.h" 20 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/omnibox/location_bar.h" 21 #include "chrome/browser/ui/omnibox/location_bar.h"
24 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" 22 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
25 #include "chrome/browser/ui/omnibox/omnibox_view.h" 23 #include "chrome/browser/ui/omnibox/omnibox_view.h"
26 #include "chrome/browser/ui/tab_contents/tab_contents.h" 24 #include "chrome/browser/ui/tab_contents/tab_contents.h"
27 #include "chrome/common/chrome_notification_types.h" 25 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
30 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h" 28 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h" 29 #include "chrome/test/base/ui_test_utils.h"
33 #include "content/public/browser/navigation_controller.h"
34 #include "content/public/browser/notification_service.h" 30 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/render_widget_host_view.h"
37 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
38 #include "content/public/common/content_switches.h"
39 #include "content/public/test/browser_test_utils.h" 32 #include "content/public/test/browser_test_utils.h"
40 #include "grit/generated_resources.h" 33 #include "grit/generated_resources.h"
41 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
42 35
43 using content::WebContents;
44
45 // Tests are flaky on Linux because of http://crbug.com/80118.
46 #if defined(OS_LINUX) && !defined(USE_ASH)
47 #define MAYBE(TestName) DISABLED_ ## TestName
48 #elif defined(OS_WIN)
49 #define MAYBE(TestName) FLAKY_ ## TestName
50 #else
51 #define MAYBE(TestName) TestName
52 #endif
53
54 class InstantTest : public InProcessBrowserTest { 36 class InstantTest : public InProcessBrowserTest {
55 public: 37 protected:
56 InstantTest() {} 38 virtual void SetUpCommandLine(CommandLine* command_line) {
57 39 // Do not prelaunch the GPU process because it will show up in the task
58 void EnableInstant() { 40 // manager, but whether it appears before or after the Instant tab is not
59 InstantController::Enable(browser()->profile()); 41 // well defined. This affects the TaskManagerPrefix test below.
60 } 42 command_line->AppendSwitch(switches::kDisableGpuProcessPrelaunch);
61 43 }
62 void SetupInstantProvider(const std::string& page) { 44
63 Profile* profile = browser()->profile(); 45 void SetupInstant(const std::string& page) {
64 TemplateURLService* model = 46 ASSERT_TRUE(test_server()->Start());
65 TemplateURLServiceFactory::GetForProfile(profile); 47
66 48 TemplateURLService* service =
67 content::WindowedNotificationObserver observer( 49 TemplateURLServiceFactory::GetForProfile(browser()->profile());
68 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, 50 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
69 content::NotificationService::AllSources());
70 if (!model->loaded()) {
71 model->Load();
72 observer.Wait();
73 }
74 51
75 TemplateURLData data; 52 TemplateURLData data;
76 data.short_name = ASCIIToUTF16("foo");
77 data.SetKeyword(ASCIIToUTF16("foo"));
78 data.SetURL(base::StringPrintf("http://%s:%d/files/%s?q={searchTerms}", 53 data.SetURL(base::StringPrintf("http://%s:%d/files/%s?q={searchTerms}",
79 test_server()->host_port_pair().host().c_str(), 54 test_server()->host_port_pair().host().c_str(),
80 test_server()->host_port_pair().port(), page.c_str())); 55 test_server()->host_port_pair().port(), page.c_str()));
81 data.instant_url = data.url(); 56 data.instant_url = data.url();
82 // TemplateURLService takes ownership of this. 57
83 TemplateURL* template_url = new TemplateURL(profile, data); 58 TemplateURL* template_url = new TemplateURL(browser()->profile(), data);
84 model->Add(template_url); 59 service->Add(template_url); // Takes ownership of |template_url|.
85 model->SetDefaultSearchProvider(template_url); 60 service->SetDefaultSearchProvider(template_url);
86 } 61
87 62 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true);
88 // Type a character to get instant to trigger and determine instant support. 63 }
89 void DetermineInstantSupport() { 64
90 content::WindowedNotificationObserver observer( 65 OmniboxView* omnibox() const {
91 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, 66 return browser()->window()->GetLocationBar()->GetLocationEntry();
92 content::NotificationService::AllSources());
93 // "a" triggers the "about:" provider. "b" begins the "bing.com" keyword.
94 // "c" might someday trigger a "chrome:" provider.
95 omnibox()->SetUserText(ASCIIToUTF16("d"));
96 observer.Wait();
97 }
98
99 // Types "def" into the omnibox and waits for the preview to be shown.
100 void SearchAndWaitForPreviewToShow() {
101 content::WindowedNotificationObserver observer(
102 chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN,
103 content::NotificationService::AllSources());
104 omnibox()->SetUserText(ASCIIToUTF16("def"));
105 observer.Wait();
106 }
107
108 // Sends a message to the renderer and waits for the response to come back to
109 // the browser. Returns true on success.
110 bool WaitForMessageToBeProcessedByRenderer() {
111 bool result = false;
112 return GetBoolFromJavascript(preview()->web_contents(), "true", &result) &&
113 result;
114 } 67 }
115 68
116 InstantController* instant() const { 69 InstantController* instant() const {
117 return browser()->instant_controller()->instant(); 70 return browser()->instant_controller()->instant();
118 } 71 }
119 72
120 OmniboxView* omnibox() const { 73 void WaitFor(chrome::NotificationType notification_type) const {
121 return browser()->window()->GetLocationBar()->GetLocationEntry(); 74 content::WindowedNotificationObserver observer(
122 } 75 notification_type, content::NotificationService::AllSources());
123 76 observer.Wait();
124 TabContents* preview() const { 77 }
125 return instant()->GetPreviewContents(); 78
126 } 79 std::wstring WrapScript(const std::string& script) const {
127 80 return UTF8ToWide("domAutomationController.send(" + script + ")");
128 InstantLoader* loader() const { 81 }
129 return instant()->loader_.get(); 82
130 } 83 bool GetBoolFromJS(content::RenderViewHost* rvh,
131 84 const std::string& script,
132 std::string GetSuggestion() const { 85 bool* result) const WARN_UNUSED_RESULT {
133 return UTF16ToUTF8(loader()->complete_suggested_text_); 86 return content::ExecuteJavaScriptAndExtractBool(
134 } 87 rvh, std::wstring(), WrapScript(script), result);
135 88 }
136 bool PressEnter() { 89
137 return ui_test_utils::SendKeyPressSync( 90 bool GetIntFromJS(content::RenderViewHost* rvh,
138 browser(), ui::VKEY_RETURN, false, false, false, false); 91 const std::string& script,
139 } 92 int* result) const WARN_UNUSED_RESULT {
140 93 return content::ExecuteJavaScriptAndExtractInt(
141 bool SetSuggestionsJavascriptArgument(const std::string& argument) { 94 rvh, std::wstring(), WrapScript(script), result);
142 std::wstring script = UTF8ToWide(base::StringPrintf( 95 }
143 "window.setSuggestionsArgument = %s;", argument.c_str())); 96
144 content::RenderViewHost* rvh = 97 bool GetStringFromJS(content::RenderViewHost* rvh,
145 preview()->web_contents()->GetRenderViewHost(); 98 const std::string& script,
146 return content::ExecuteJavaScript(rvh, std::wstring(), script); 99 std::string* result) const WARN_UNUSED_RESULT {
147 }
148
149 std::wstring WrapScript(const std::string& script) {
150 return UTF8ToWide(base::StringPrintf(
151 "window.domAutomationController.send(%s)", script.c_str()));
152 }
153
154 bool GetStringFromJavascript(WebContents* tab,
155 const std::string& script,
156 std::string* result) {
157 return content::ExecuteJavaScriptAndExtractString( 100 return content::ExecuteJavaScriptAndExtractString(
158 tab->GetRenderViewHost(), std::wstring(), WrapScript(script), result); 101 rvh, std::wstring(), WrapScript(script), result);
159 } 102 }
160 103
161 bool GetIntFromJavascript(WebContents* tab, 104 bool UpdateSearchState(TabContents* tab) WARN_UNUSED_RESULT {
162 const std::string& script, 105 content::RenderViewHost* rvh = tab->web_contents()->GetRenderViewHost();
163 int* result) { 106 return GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) &&
164 return content::ExecuteJavaScriptAndExtractInt( 107 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) &&
165 tab->GetRenderViewHost(), std::wstring(), WrapScript(script), result); 108 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) &&
166 } 109 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) &&
167 110 GetStringFromJS(rvh, "value", &value_) &&
168 bool GetBoolFromJavascript(WebContents* tab, 111 GetBoolFromJS(rvh, "verbatim", &verbatim_) &&
169 const std::string& script, 112 GetIntFromJS(rvh, "height", &height_);
170 bool* result) { 113 }
171 return content::ExecuteJavaScriptAndExtractBool( 114
172 tab->GetRenderViewHost(), std::wstring(), WrapScript(script), result); 115 bool ExecuteScript(const std::string& script) const WARN_UNUSED_RESULT {
173 } 116 return content::ExecuteJavaScript(
174 117 instant()->GetPreviewContents()->web_contents()->GetRenderViewHost(),
175 bool CheckVisibilityIs(WebContents* tab, bool visible) { 118 std::wstring(), ASCIIToWide(script));
176 bool hidden = visible; 119 }
177 return GetBoolFromJavascript(tab, "document.webkitHidden", &hidden) && 120
178 hidden != visible; 121 void SetOmniboxText(const std::string& text) const {
179 } 122 omnibox()->SetUserText(ASCIIToUTF16(text));
180 123 }
181 // Returns the state of the search box as a string. This consists of the 124
182 // following: 125 bool CheckVisibilityIs(TabContents* tab,
183 // window.chrome.sv 126 bool expected) const WARN_UNUSED_RESULT {
184 // window.onsubmitcalls 127 bool actual = !expected; // Purposely start with a mis-match.
185 // window.oncancelcalls 128 // We can only use ASSERT_*() in a method that returns void, hence this
186 // window.onchangecalls 129 // convoluted check.
187 // 'true' if any window.onresize call has been sent, otherwise false. 130 return GetBoolFromJS(tab->web_contents()->GetRenderViewHost(),
188 // window.beforeLoadSearchBox.value 131 "!document.webkitHidden", &actual) &&
189 // window.beforeLoadSearchBox.verbatim 132 actual == expected;
190 // window.chrome.searchBox.value 133 }
191 // window.chrome.searchBox.verbatim 134
192 // window.chrome.searchBox.selectionStart 135 int onchangecalls_;
193 // window.chrome.searchBox.selectionEnd 136 int onsubmitcalls_;
194 // If determining any of the values fails, the value is 'fail'. 137 int oncancelcalls_;
195 // 138 int onresizecalls_;
196 // If |use_last| is true, then the last searchBox values are used instead of 139
197 // the current. Set |use_last| to true when testing OnSubmit/OnCancel. 140 std::string value_;
198 std::string GetSearchStateAsString(WebContents* tab, bool use_last) { 141 bool verbatim_;
199 bool sv = false; 142 int height_;
200 int onsubmitcalls = 0;
201 int oncancelcalls = 0;
202 int onchangecalls = 0;
203 int onresizecalls = 0;
204 int selection_start = 0;
205 int selection_end = 0;
206 std::string before_load_value;
207 bool before_load_verbatim = false;
208 std::string value;
209 bool verbatim = false;
210
211 if (!GetBoolFromJavascript(tab, "window.chrome.sv", &sv) ||
212 !GetIntFromJavascript(tab, "window.onsubmitcalls", &onsubmitcalls) ||
213 !GetIntFromJavascript(tab, "window.oncancelcalls", &oncancelcalls) ||
214 !GetIntFromJavascript(tab, "window.onchangecalls", &onchangecalls) ||
215 !GetIntFromJavascript(tab, "window.onresizecalls", &onresizecalls) ||
216 !GetStringFromJavascript(tab, "window.beforeLoadSearchBox.value",
217 &before_load_value) ||
218 !GetBoolFromJavascript(tab, "window.beforeLoadSearchBox.verbatim",
219 &before_load_verbatim)) {
220 return "fail";
221 }
222
223 if (use_last &&
224 (!GetStringFromJavascript(tab, "window.lastSearchBox.value", &value) ||
225 !GetBoolFromJavascript(tab, "window.lastSearchBox.verbatim",
226 &verbatim) ||
227 !GetIntFromJavascript(tab, "window.lastSearchBox.selectionStart",
228 &selection_start) ||
229 !GetIntFromJavascript(tab, "window.lastSearchBox.selectionEnd",
230 &selection_end))) {
231 return "fail";
232 }
233
234 if (!use_last &&
235 (!GetStringFromJavascript(tab, "window.chrome.searchBox.value",
236 &value) ||
237 !GetBoolFromJavascript(tab, "window.chrome.searchBox.verbatim",
238 &verbatim) ||
239 !GetIntFromJavascript(tab, "window.chrome.searchBox.selectionStart",
240 &selection_start) ||
241 !GetIntFromJavascript(tab, "window.chrome.searchBox.selectionEnd",
242 &selection_end))) {
243 return "fail";
244 }
245
246 return base::StringPrintf("%s %d %d %d %s %s %s %s %s %d %d",
247 sv ? "true" : "false",
248 onsubmitcalls,
249 oncancelcalls,
250 onchangecalls,
251 onresizecalls ? "true" : "false",
252 before_load_value.c_str(),
253 before_load_verbatim ? "true" : "false",
254 value.c_str(),
255 verbatim ? "true" : "false",
256 selection_start,
257 selection_end);
258 }
259
260 protected:
261 virtual void SetUpCommandLine(CommandLine* command_line) {
262 // Do not prelaunch the GPU process for these tests because it will show
263 // up in task manager but whether it appears before or after the new tab
264 // renderer process is not well defined.
265 command_line->AppendSwitch(switches::kDisableGpuProcessPrelaunch);
266 }
267 }; 143 };
268 144
269 // TODO(tonyg): Add the following tests: 145 // Test that Instant is preloaded when the omnibox is focused.
270 // - Test that the search box API is not populated for pages other than the 146 IN_PROC_BROWSER_TEST_F(InstantTest, OmniboxFocusLoadsInstant) {
271 // default search provider. 147 // The omnibox gets focus before the test begins. At this time, there's no
272 // - Test resize events. 148 // InstantController (which is only created in SetupInstant() below), so no
273 149 // preloading has happened yet.
274 // Verify that the onchange event is dispatched upon typing in the box. 150 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
275 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(OnChangeEvent)) { 151 EXPECT_FALSE(instant()->GetPreviewContents());
276 ASSERT_TRUE(test_server()->Start()); 152
277 EnableInstant(); 153 // Explicitly unfocus and refocus the omnibox. Since an InstantController now
278 SetupInstantProvider("instant.html"); 154 // exists, it will preload Instant.
279 DetermineInstantSupport(); 155 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
280 SearchAndWaitForPreviewToShow(); 156 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
281 157 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
282 EXPECT_TRUE(preview()); 158
283 EXPECT_TRUE(instant()->is_displayable()); 159 browser()->window()->GetLocationBar()->FocusLocation(false);
284 EXPECT_TRUE(instant()->IsCurrent()); 160 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
285 EXPECT_EQ("defghi", UTF16ToUTF8(omnibox()->GetText())); 161
286 162 TabContents* preview_tab = instant()->GetPreviewContents();
287 // Make sure the URL that will get committed when we press <Enter> matches
288 // that of the default search provider.
289 const TemplateURL* default_turl =
290 TemplateURLServiceFactory::GetForProfile(browser()->profile())->
291 GetDefaultSearchProvider();
292 EXPECT_TRUE(default_turl);
293 EXPECT_EQ(default_turl->url_ref().ReplaceSearchTerms(
294 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("defghi"))),
295 loader()->url().spec());
296
297 // Check that the value is reflected and onchange is called.
298 EXPECT_EQ("true 0 0 1 true d false def false 3 3",
299 GetSearchStateAsString(preview()->web_contents(), false));
300 }
301
302 // Verify that the onsubmit event is dispatched upon pressing <Enter>.
303 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(OnSubmitEvent)) {
304 ASSERT_TRUE(test_server()->Start());
305 EnableInstant();
306 SetupInstantProvider("instant.html");
307 DetermineInstantSupport();
308 SearchAndWaitForPreviewToShow();
309
310 EXPECT_TRUE(preview());
311 EXPECT_TRUE(instant()->is_displayable());
312 EXPECT_TRUE(instant()->IsCurrent());
313 EXPECT_EQ("defghi", UTF16ToUTF8(omnibox()->GetText()));
314
315 WebContents* preview_tab = preview()->web_contents();
316 EXPECT_TRUE(preview_tab); 163 EXPECT_TRUE(preview_tab);
317 164
318 ASSERT_TRUE(PressEnter()); 165 // Check that the page supports Instant, but it isn't showing.
319 166 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
320 // Check that the preview has been committed. 167 EXPECT_TRUE(instant()->loader()->supports_instant());
321 EXPECT_FALSE(preview()); 168 EXPECT_FALSE(instant()->IsCurrent());
322 EXPECT_FALSE(instant()->is_displayable()); 169 EXPECT_FALSE(instant()->is_showing());
323 EXPECT_FALSE(instant()->IsCurrent()); 170
324 EXPECT_EQ(preview_tab, chrome::GetActiveWebContents(browser())); 171 // Adding a new tab shouldn't delete or recreate the TabContents; otherwise,
325 172 // what's the point of preloading?
326 // We should have two entries. One corresponding to the page the user was 173 AddBlankTabAndShow(browser());
327 // first on, and one for the search page. 174 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
328 EXPECT_EQ(2, preview_tab->GetController().GetEntryCount()); 175
329 176 // Unfocusing and refocusing the omnibox should also preserve the preview.
330 // Check that the value is reflected and onsubmit is called. 177 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
331 EXPECT_EQ("true 1 0 1 true d false defghi true 3 3", 178 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
332 GetSearchStateAsString(preview_tab, true)); 179
333 180 browser()->window()->GetLocationBar()->FocusLocation(false);
334 // Make sure the searchbox values were reset. 181 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
335 EXPECT_EQ("true 1 0 1 true d false false 0 0", 182
336 GetSearchStateAsString(preview_tab, false)); 183 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
337 } 184
338 185 // Doing a search should also use the same preloaded page.
339 // Verify that the oncancel event is dispatched upon losing focus. 186 SetOmniboxText("query");
340 IN_PROC_BROWSER_TEST_F(InstantTest, DISABLED_OnCancelEvent) { 187 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
341 ASSERT_TRUE(test_server()->Start()); 188 EXPECT_TRUE(instant()->is_showing());
342 EnableInstant(); 189 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
343 SetupInstantProvider("instant.html"); 190 }
344 DetermineInstantSupport(); 191
345 SearchAndWaitForPreviewToShow(); 192 // Test that the onchange event is dispatched upon typing in the omnibox.
346 193 IN_PROC_BROWSER_TEST_F(InstantTest, OnChangeEvent) {
347 EXPECT_TRUE(preview()); 194 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
348 EXPECT_TRUE(instant()->is_displayable()); 195
349 EXPECT_TRUE(instant()->IsCurrent()); 196 // Typing "query" into the omnibox causes the first onchange event.
350 EXPECT_EQ("defghi", UTF16ToUTF8(omnibox()->GetText())); 197 SetOmniboxText("query");
351 198 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
352 WebContents* preview_tab = preview()->web_contents(); 199
200 // The page suggested "query suggestion" is inline autocompleted into the
201 // omnibox, causing the second onchange event.
202 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText());
203 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
204 EXPECT_EQ(2, onchangecalls_);
205
206 // Change the query and confirm that another onchange is sent. Since the new
207 // query is not a prefix of the hardcoded "query suggestion", no inline
208 // autocompletion happens, and thus, no fourth onchange event.
209 SetOmniboxText("search");
210 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
211 EXPECT_EQ(3, onchangecalls_);
212 }
213
214 // Test that the onsubmit event is dispatched upon pressing Enter.
215 IN_PROC_BROWSER_TEST_F(InstantTest, OnSubmitEvent) {
216 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
217 SetOmniboxText("search");
218 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
219
220 // Stash a reference to the preview, so we can refer to it after commit.
221 TabContents* preview_tab = instant()->GetPreviewContents();
353 EXPECT_TRUE(preview_tab); 222 EXPECT_TRUE(preview_tab);
354 223
355 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 224 // The state of the searchbox before the commit.
225 EXPECT_TRUE(UpdateSearchState(preview_tab));
226 EXPECT_EQ("search", value_);
227 EXPECT_FALSE(verbatim_);
228 EXPECT_EQ(0, onsubmitcalls_);
229
230 // Before the commit, the active tab is the NTP (i.e., not Instant).
231 TabContents* active_tab = chrome::GetActiveTabContents(browser());
232 EXPECT_NE(preview_tab, active_tab);
233 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
234 EXPECT_EQ(std::string(chrome::kAboutBlankURL),
235 omnibox()->model()->PermanentURL().spec());
236
237 // Commit the search by pressing Enter.
238 browser()->window()->GetLocationBar()->AcceptInput();
239
240 // After the commit, Instant should not be showing, or even have a preview.
241 EXPECT_FALSE(instant()->GetPreviewContents());
242 EXPECT_FALSE(instant()->IsCurrent());
243 EXPECT_FALSE(instant()->is_showing());
244
245 // Check that the current active tab is indeed what was once the preview.
246 EXPECT_EQ(preview_tab, chrome::GetActiveTabContents(browser()));
247
248 // We should have two navigation entries, one for the NTP, and one for the
249 // Instant search that was committed.
250 EXPECT_EQ(2, preview_tab->web_contents()->GetController().GetEntryCount());
251
252 // Check that the omnibox contains the Instant URL we loaded.
253 std::string instant_url = TemplateURLServiceFactory::GetForProfile(
254 browser()->profile())->GetDefaultSearchProvider()->instant_url_ref().
255 ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(string16()));
256 EXPECT_EQ(instant_url, omnibox()->model()->PermanentURL().spec());
257
258 // Check that the searchbox API values have been reset.
259 std::string value;
260 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
261 "chrome.searchBox.value", &value));
262 EXPECT_EQ("", value);
263
264 // However, the page should've correctly received the committed query.
265 EXPECT_TRUE(UpdateSearchState(preview_tab));
266 EXPECT_EQ("search", value_);
267 EXPECT_TRUE(verbatim_);
268 EXPECT_EQ(1, onsubmitcalls_);
269 }
270
271 // Test that the oncancel event is dispatched upon clicking on the preview.
272 IN_PROC_BROWSER_TEST_F(InstantTest, OnCancelEvent) {
273 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
274 SetOmniboxText("search");
275 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
276
277 // Stash a reference to the preview, so we can refer to it after commit.
278 TabContents* preview_tab = instant()->GetPreviewContents();
279 EXPECT_TRUE(preview_tab);
280
281 // The state of the searchbox before the commit.
282 EXPECT_TRUE(UpdateSearchState(preview_tab));
283 EXPECT_EQ("search", value_);
284 EXPECT_FALSE(verbatim_);
285 EXPECT_EQ(0, oncancelcalls_);
286
287 // Before the commit, the active tab is the NTP (i.e., not Instant).
288 TabContents* active_tab = chrome::GetActiveTabContents(browser());
289 EXPECT_NE(preview_tab, active_tab);
290 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
291 EXPECT_EQ(std::string(chrome::kAboutBlankURL),
292 omnibox()->model()->PermanentURL().spec());
293
294 // Commit the search by clicking on the preview.
295 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
356 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); 296 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
357 297
358 // Check that the preview has been committed. 298 // After the commit, Instant should not be showing, or even have a preview.
359 EXPECT_FALSE(preview()); 299 EXPECT_FALSE(instant()->GetPreviewContents());
360 EXPECT_FALSE(instant()->is_displayable()); 300 EXPECT_FALSE(instant()->IsCurrent());
361 EXPECT_FALSE(instant()->IsCurrent()); 301 EXPECT_FALSE(instant()->is_showing());
362 EXPECT_EQ(preview_tab, chrome::GetActiveWebContents(browser())); 302
363 303 // Check that the current active tab is indeed what was once the preview.
364 // Check that the value is reflected and oncancel is called. 304 EXPECT_EQ(preview_tab, chrome::GetActiveTabContents(browser()));
365 EXPECT_EQ("true 0 1 1 true d false def false 3 3", 305
366 GetSearchStateAsString(preview_tab, true)); 306 // We should have two navigation entries, one for the NTP, and one for the
367 307 // Instant search that was committed.
368 // Make sure the searchbox values were reset. 308 EXPECT_EQ(2, preview_tab->web_contents()->GetController().GetEntryCount());
369 EXPECT_EQ("true 0 1 1 true d false false 0 0", 309
370 GetSearchStateAsString(preview_tab, false)); 310 // Check that the omnibox contains the Instant URL we loaded.
371 } 311 std::string instant_url = TemplateURLServiceFactory::GetForProfile(
372 312 browser()->profile())->GetDefaultSearchProvider()->instant_url_ref().
373 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsArrayOfStrings)) { 313 ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(string16()));
374 ASSERT_TRUE(test_server()->Start()); 314 EXPECT_EQ(instant_url, omnibox()->model()->PermanentURL().spec());
375 EnableInstant(); 315
376 SetupInstantProvider("instant.html"); 316 // Check that the searchbox API values have been reset.
377 DetermineInstantSupport(); 317 std::string value;
378 318 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
379 ASSERT_TRUE(SetSuggestionsJavascriptArgument("['defg', 'unused']")); 319 "chrome.searchBox.value", &value));
380 SearchAndWaitForPreviewToShow(); 320 EXPECT_EQ("", value);
381 EXPECT_EQ("defg", GetSuggestion()); 321
382 } 322 // However, the page should've correctly received the committed query.
383 323 EXPECT_TRUE(UpdateSearchState(preview_tab));
384 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptyArray)) { 324 EXPECT_EQ("search", value_);
385 ASSERT_TRUE(test_server()->Start()); 325 EXPECT_TRUE(verbatim_);
386 EnableInstant(); 326 EXPECT_EQ(1, oncancelcalls_);
387 SetupInstantProvider("instant.html"); 327 }
388 DetermineInstantSupport(); 328
389 329 // Test that the onreisze event is dispatched upon typing in the omnibox.
390 ASSERT_TRUE(SetSuggestionsJavascriptArgument("[]")); 330 IN_PROC_BROWSER_TEST_F(InstantTest, OnResizeEvent) {
391 SearchAndWaitForPreviewToShow(); 331 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
392 EXPECT_EQ("", GetSuggestion()); 332
393 } 333 // This makes Instant load the preview, along with an initial onresize() (see
394 334 // SearchBoxExtension::PageSupportsInstant() for why).
395 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsValidJson)) { 335 instant()->OnAutocompleteGotFocus();
396 ASSERT_TRUE(test_server()->Start()); 336 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
397 EnableInstant(); 337
398 SetupInstantProvider("instant.html"); 338 EXPECT_TRUE(instant()->GetPreviewContents());
399 DetermineInstantSupport(); 339 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
400 340 EXPECT_EQ(1, onresizecalls_);
401 ASSERT_TRUE(SetSuggestionsJavascriptArgument( 341 EXPECT_EQ(0, height_);
402 "{suggestions:[{value:'defg'},{value:'unused'}]}")); 342
403 SearchAndWaitForPreviewToShow(); 343 // Type a query into the omnibox. This should cause an onresize() event, with
404 EXPECT_EQ("defg", GetSuggestion()); 344 // a valid (non-zero) height.
405 } 345 SetOmniboxText("search");
406 346 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
407 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsInvalidSuggestions)) { 347
408 ASSERT_TRUE(test_server()->Start()); 348 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
409 EnableInstant(); 349 EXPECT_EQ(2, onresizecalls_);
410 SetupInstantProvider("instant.html"); 350 EXPECT_LT(0, height_);
411 DetermineInstantSupport(); 351 }
412 352
413 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{suggestions:{value:'defg'}}")); 353 // Test that the INSTANT_COMPLETE_NOW behavior works as expected.
414 SearchAndWaitForPreviewToShow(); 354 IN_PROC_BROWSER_TEST_F(InstantTest, SuggestionIsCompletedNow) {
415 EXPECT_EQ("", GetSuggestion()); 355 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
416 } 356 instant()->OnAutocompleteGotFocus();
417 357 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
418 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptyJson)) { 358
419 ASSERT_TRUE(test_server()->Start()); 359 // Tell the JS to request for the given behavior.
420 EnableInstant(); 360 EXPECT_TRUE(ExecuteScript("behavior = 'now'"));
421 SetupInstantProvider("instant.html"); 361
422 DetermineInstantSupport(); 362 SetOmniboxText("query");
423 363 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
424 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{}")); 364
425 SearchAndWaitForPreviewToShow(); 365 // Get what's showing in the omnibox, and what's highlighted.
426 EXPECT_EQ("", GetSuggestion()); 366 string16 text = omnibox()->GetText();
427 } 367 size_t start = 0, end = 0;
428 368 omnibox()->GetSelectionBounds(&start, &end);
429 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptySuggestions)) { 369 if (start > end)
430 ASSERT_TRUE(test_server()->Start()); 370 std::swap(start, end);
431 EnableInstant(); 371
432 SetupInstantProvider("instant.html"); 372 EXPECT_EQ(ASCIIToUTF16("query suggestion"), text);
433 DetermineInstantSupport(); 373 EXPECT_EQ(ASCIIToUTF16(" suggestion"), text.substr(start, end - start));
434 374 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
435 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{suggestions:[]}")); 375 }
436 SearchAndWaitForPreviewToShow(); 376
437 EXPECT_EQ("", GetSuggestion()); 377 // The "delayed" completion behavior is not implemented on the Mac. Strange.
438 } 378 #if !defined(OS_MACOSX)
439 379 // Test that the INSTANT_COMPLETE_DELAYED behavior works as expected.
440 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptySuggestion)) { 380 IN_PROC_BROWSER_TEST_F(InstantTest, SuggestionIsCompletedDelayed) {
441 ASSERT_TRUE(test_server()->Start()); 381 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
442 EnableInstant(); 382 instant()->OnAutocompleteGotFocus();
443 SetupInstantProvider("instant.html"); 383 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
444 DetermineInstantSupport(); 384
445 385 // Tell the JS to request for the given behavior.
446 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{suggestions:[{}]}")); 386 EXPECT_TRUE(ExecuteScript("behavior = 'delayed'"));
447 SearchAndWaitForPreviewToShow(); 387
448 EXPECT_EQ("", GetSuggestion()); 388 SetOmniboxText("query");
449 } 389 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
450 390
451 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(InstantCompleteNever)) { 391 // Get what's showing in the omnibox, and what's highlighted.
452 ASSERT_TRUE(test_server()->Start()); 392 string16 text = omnibox()->GetText();
453 EnableInstant(); 393 size_t start = 0, end = 0;
454 SetupInstantProvider("instant.html"); 394 omnibox()->GetSelectionBounds(&start, &end);
455 DetermineInstantSupport(); 395 if (start > end)
456 396 std::swap(start, end);
457 ASSERT_TRUE(SetSuggestionsJavascriptArgument( 397
458 "{suggestions:[{value:'defg'}],complete_behavior:'never'}")); 398 EXPECT_EQ(ASCIIToUTF16("query"), text);
459 SearchAndWaitForPreviewToShow(); 399 EXPECT_EQ(ASCIIToUTF16(""), text.substr(start, end - start));
460 EXPECT_EQ("defg", GetSuggestion()); 400 EXPECT_EQ(ASCIIToUTF16(" suggestion"), omnibox()->GetInstantSuggestion());
461 401
462 EXPECT_EQ(INSTANT_COMPLETE_NEVER, 402 // Wait for the animation to complete, which causes the omnibox to update.
463 omnibox()->model()->instant_complete_behavior()); 403 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED);
464 EXPECT_EQ("def", UTF16ToUTF8(omnibox()->GetText())); 404
465 } 405 text = omnibox()->GetText();
466 406 omnibox()->GetSelectionBounds(&start, &end);
467 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(InstantCompleteDelayed)) { 407 if (start > end)
468 ASSERT_TRUE(test_server()->Start()); 408 std::swap(start, end);
469 EnableInstant(); 409
470 SetupInstantProvider("instant.html"); 410 EXPECT_EQ(ASCIIToUTF16("query suggestion"), text);
471 DetermineInstantSupport(); 411 EXPECT_EQ(ASCIIToUTF16(" suggestion"), text.substr(start, end - start));
472 412 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
473 ASSERT_TRUE(SetSuggestionsJavascriptArgument( 413 }
474 "{suggestions:[{value:'defg'}],complete_behavior:'delayed'}")); 414 #endif
475 SearchAndWaitForPreviewToShow(); 415
476 EXPECT_EQ("defg", GetSuggestion()); 416 // Test that the INSTANT_COMPLETE_NEVER behavior works as expected.
477 417 IN_PROC_BROWSER_TEST_F(InstantTest, SuggestionIsCompletedNever) {
478 EXPECT_EQ(INSTANT_COMPLETE_DELAYED, 418 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
479 omnibox()->model()->instant_complete_behavior()); 419 instant()->OnAutocompleteGotFocus();
480 EXPECT_EQ("def", UTF16ToUTF8(omnibox()->GetText())); 420 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
481 } 421
482 422 // Tell the JS to request for the given behavior.
483 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(InstantCompleteNow)) { 423 EXPECT_TRUE(ExecuteScript("behavior = 'never'"));
484 ASSERT_TRUE(test_server()->Start()); 424
485 EnableInstant(); 425 SetOmniboxText("query");
486 SetupInstantProvider("instant.html"); 426 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
487 DetermineInstantSupport(); 427
488 428 // Get what's showing in the omnibox, and what's highlighted.
489 ASSERT_TRUE(SetSuggestionsJavascriptArgument( 429 string16 text = omnibox()->GetText();
490 "{suggestions:[{value:'defg'}],complete_behavior:'now'}")); 430 size_t start = 0, end = 0;
491 SearchAndWaitForPreviewToShow(); 431 omnibox()->GetSelectionBounds(&start, &end);
492 EXPECT_EQ("defg", GetSuggestion()); 432 if (start > end)
493 433 std::swap(start, end);
494 EXPECT_EQ(INSTANT_COMPLETE_NOW, 434
495 omnibox()->model()->instant_complete_behavior()); 435 EXPECT_EQ(ASCIIToUTF16("query"), text);
496 EXPECT_EQ("defg", UTF16ToUTF8(omnibox()->GetText())); 436 EXPECT_EQ(ASCIIToUTF16(""), text.substr(start, end - start));
497 } 437 EXPECT_EQ(ASCIIToUTF16(" suggestion"), omnibox()->GetInstantSuggestion());
498 438 }
499 // Verifies that instant previews aren't shown for crash URLs. 439
500 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(CrashUrlCancelsInstant)) { 440 // Test that a valid suggestion is accepted.
501 ASSERT_TRUE(test_server()->Start()); 441 IN_PROC_BROWSER_TEST_F(InstantTest, SuggestionIsValidObject) {
502 EnableInstant(); 442 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
503 SetupInstantProvider("instant.html"); 443 instant()->OnAutocompleteGotFocus();
504 444 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
505 omnibox()->SetUserText(ASCIIToUTF16(chrome::kChromeUICrashURL)); 445
506 EXPECT_FALSE(preview()); 446 // Tell the JS to use the given suggestion.
507 } 447 EXPECT_TRUE(ExecuteScript("suggestion = [ { value: 'query completion' } ]"));
508 448
509 // Tests that instant doesn't fire for intranet paths that look like searches. 449 SetOmniboxText("query");
450 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
451
452 EXPECT_EQ(ASCIIToUTF16("query completion"), omnibox()->GetText());
453 }
454
455 // Test that an invalid suggestion is rejected.
456 IN_PROC_BROWSER_TEST_F(InstantTest, SuggestionIsInvalidObject) {
457 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
458 instant()->OnAutocompleteGotFocus();
459 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
460
461 // Tell the JS to use the given suggestion.
462 EXPECT_TRUE(ExecuteScript("suggestion = { value: 'query completion' }"));
463
464 SetOmniboxText("query");
465 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
466
467 EXPECT_EQ(ASCIIToUTF16("query"), omnibox()->GetText());
468 }
469
470 // Test that various forms of empty suggestions are rejected.
471 IN_PROC_BROWSER_TEST_F(InstantTest, SuggestionIsEmpty) {
472 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
473 instant()->OnAutocompleteGotFocus();
474 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
475
476 EXPECT_TRUE(ExecuteScript("suggestion = {}"));
477 SetOmniboxText("query1");
478 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
479 EXPECT_EQ(ASCIIToUTF16("query1"), omnibox()->GetText());
480
481 instant()->Hide();
482
483 EXPECT_TRUE(ExecuteScript("suggestion = []"));
484 SetOmniboxText("query2");
485 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
486 EXPECT_EQ(ASCIIToUTF16("query2"), omnibox()->GetText());
487
488 instant()->Hide();
489
490 EXPECT_TRUE(ExecuteScript("suggestion = [{}]"));
491 SetOmniboxText("query3");
492 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
493 EXPECT_EQ(ASCIIToUTF16("query3"), omnibox()->GetText());
494 }
495
496 // Test that Instant doesn't process URLs.
497 IN_PROC_BROWSER_TEST_F(InstantTest, RejectsURLs) {
498 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
499
500 // Note that we are not actually navigating to these URLs yet. We are just
501 // typing them into the omnibox (without pressing Enter) and checking that
502 // Instant doesn't try to process them.
503 SetOmniboxText(chrome::kChromeUICrashURL);
504 EXPECT_FALSE(instant()->GetPreviewContents());
505 EXPECT_FALSE(instant()->IsCurrent());
506 EXPECT_FALSE(instant()->is_showing());
507
508 // Let's try again after creating the preview.
509 instant()->OnAutocompleteGotFocus();
510 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
511
512 SetOmniboxText(chrome::kChromeUIHangURL);
513 EXPECT_TRUE(instant()->GetPreviewContents());
514 EXPECT_FALSE(instant()->IsCurrent());
515 EXPECT_FALSE(instant()->is_showing());
516
517 SetOmniboxText(chrome::kChromeUIKillURL);
518 EXPECT_TRUE(instant()->GetPreviewContents());
519 EXPECT_FALSE(instant()->IsCurrent());
520 EXPECT_FALSE(instant()->is_showing());
521
522 // Make sure that the URLs were never sent to the preview page.
523 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
524 EXPECT_EQ(0, onchangecalls_);
525 EXPECT_EQ("", value_);
526 }
527
528 // Test that Instant doesn't fire for intranet paths that look like searches.
510 // http://crbug.com/99836 529 // http://crbug.com/99836
511 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(IntranetPathLooksLikeSearch)) { 530 IN_PROC_BROWSER_TEST_F(InstantTest, IntranetPathLooksLikeSearch) {
512 ASSERT_TRUE(test_server()->Start()); 531 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
513 EnableInstant();
514 SetupInstantProvider("instant.html");
515
516 // Unfocus the omnibox. This should delete any existing preview contents.
517 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
518 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
519 EXPECT_FALSE(preview());
520 532
521 // Navigate to a URL that looks like a search (when the scheme is stripped). 533 // Navigate to a URL that looks like a search (when the scheme is stripped).
522 // It's okay if the host is bogus or the navigation fails, since we only care 534 // It's okay if the host is bogus or the navigation fails, since we only care
523 // that instant doesn't act on it. 535 // that Instant doesn't act on it.
524 ui_test_utils::NavigateToURL(browser(), GURL("http://baby/beluga")); 536 ui_test_utils::NavigateToURL(browser(), GURL("http://baby/beluga"));
525 EXPECT_EQ("baby/beluga", UTF16ToUTF8(omnibox()->GetText())); 537 EXPECT_EQ(ASCIIToUTF16("baby/beluga"), omnibox()->GetText());
526 EXPECT_FALSE(preview()); 538 EXPECT_FALSE(instant()->GetPreviewContents());
527 } 539 }
528 540
529 // Verifies that instant previews aren't shown for non-search URLs. 541 // Test that transitions between searches and non-searches work as expected.
530 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(ShowPreviewNonSearch)) { 542 IN_PROC_BROWSER_TEST_F(InstantTest, TransitionsBetweenSearchAndURL) {
531 ASSERT_TRUE(test_server()->Start()); 543 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
532 EnableInstant(); 544
533 SetupInstantProvider("instant.html"); 545 // Type a search, but without waiting for the page to load, type a URL.
534 546 SetOmniboxText("query");
535 GURL url(test_server()->GetURL("files/empty.html")); 547 SetOmniboxText("http://monstrous/nightmare");
536 omnibox()->SetUserText(UTF8ToUTF16(url.spec())); 548
537 EXPECT_FALSE(preview()); 549 // Wait for the page to load.
538 } 550 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
539 551 EXPECT_TRUE(instant()->GetPreviewContents());
540 // Transition from non-search to search and make sure everything works. 552 EXPECT_FALSE(instant()->IsCurrent());
541 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(NonSearchToSearch)) { 553 EXPECT_FALSE(instant()->is_showing());
542 ASSERT_TRUE(test_server()->Start()); 554 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
543 EnableInstant(); 555 EXPECT_EQ(1, onchangecalls_);
544 SetupInstantProvider("instant.html"); 556 EXPECT_EQ("query", value_);
545 557
546 // Load a non-search URL. 558 // Type a search. Instant should show.
547 GURL url(test_server()->GetURL("files/empty.html")); 559 SetOmniboxText("search");
548 omnibox()->SetUserText(UTF8ToUTF16(url.spec())); 560 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
549 EXPECT_FALSE(preview()); 561 EXPECT_TRUE(instant()->GetPreviewContents());
550
551 // Now type in some search text.
552 DetermineInstantSupport();
553
554 // We should now have a preview, but it shouldn't be showing yet, because we
555 // haven't gotten back suggestions.
556 EXPECT_TRUE(preview());
557 EXPECT_FALSE(loader()->ready());
558 EXPECT_FALSE(instant()->is_displayable());
559 EXPECT_FALSE(instant()->IsCurrent());
560
561 // Reset the user text so that the page is told the text changed.
562 //
563 // Typing into the omnibox sends onchange() to the page, which responds with
564 // suggestions, which causes the preview to be shown. However, when we called
565 // DetermineInstantSupport(), the resulting onchange was dropped on the floor
566 // because the page wasn't loaded yet. This is fine (the user may type before
567 // the page loads too). To handle this, we explicitly call onchange after the
568 // page loads (see initScript in searchbox_extension.cc). The search provider
569 // used in this test (instant.html) doesn't support initScript, so we have to
570 // trigger an onchange ourselves.
571 SearchAndWaitForPreviewToShow();
572
573 // We should now be showing the preview.
574 EXPECT_TRUE(preview());
575 EXPECT_TRUE(loader()->ready());
576 EXPECT_TRUE(instant()->is_displayable());
577 EXPECT_TRUE(instant()->IsCurrent()); 562 EXPECT_TRUE(instant()->IsCurrent());
578 563 EXPECT_TRUE(instant()->is_showing());
579 content::RenderWidgetHostView* rwhv = 564 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
580 preview()->web_contents()->GetRenderWidgetHostView(); 565 EXPECT_EQ(2, onchangecalls_);
581 EXPECT_TRUE(rwhv); 566
582 EXPECT_TRUE(rwhv->IsShowing()); 567 // Type another URL. The preview should be hidden.
583 } 568 SetOmniboxText("http://terrible/terror");
584 569 EXPECT_TRUE(instant()->GetPreviewContents());
585 // Transition from search to non-search and make sure instant isn't displayable. 570 EXPECT_FALSE(instant()->IsCurrent());
586 // See bug http://crbug.com/100368 for details. 571 EXPECT_FALSE(instant()->is_showing());
587 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SearchToNonSearch)) { 572 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
588 ASSERT_TRUE(test_server()->Start()); 573 EXPECT_EQ(2, onchangecalls_);
589 EnableInstant(); 574
590 SetupInstantProvider("instant.html"); 575 // Type the same search as before. The preview should show, but no onchange()
591 576 // is sent, since query hasn't changed.
592 content::WindowedNotificationObserver instant_support_observer( 577 SetOmniboxText("search");
593 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, 578 EXPECT_TRUE(instant()->GetPreviewContents());
594 content::NotificationService::AllSources()); 579 EXPECT_TRUE(instant()->IsCurrent());
595 580 EXPECT_TRUE(instant()->is_showing());
596 // Type in some search text. 581 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
597 omnibox()->SetUserText(ASCIIToUTF16("def")); 582 EXPECT_EQ(2, onchangecalls_);
598 583 }
599 // Load a non search URL. Don't wait for the preview to navigate. It'll still 584
600 // end up loading in the background. 585 // Test that Instant can't be fooled into committing a URL.
601 GURL url(test_server()->GetURL("files/empty.html")); 586 IN_PROC_BROWSER_TEST_F(InstantTest, DoesNotCommitURLs) {
602 omnibox()->SetUserText(UTF8ToUTF16(url.spec())); 587 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
603 588 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
604 instant_support_observer.Wait(); 589
605 590 // Type a URL. No Instant.
606 // We should now have a preview, but it shouldn't be showing yet. 591 SetOmniboxText("http://deadly/nadder");
607 EXPECT_TRUE(preview()); 592 EXPECT_FALSE(instant()->GetPreviewContents());
608 EXPECT_FALSE(loader()->ready()); 593
609 EXPECT_FALSE(instant()->is_displayable()); 594 // Unfocus and refocus the omnibox.
610 EXPECT_FALSE(instant()->IsCurrent()); 595 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
611 596 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
612 // Send onchange so that the page sends up suggestions. See the comments in 597 browser()->window()->GetLocationBar()->FocusLocation(false);
613 // NonSearchToSearch for why this is needed. 598
614 ASSERT_TRUE(content::ExecuteJavaScript( 599 // The omnibox text hasn't changed, but the preview should've preloaded due
615 preview()->web_contents()->GetRenderViewHost(), std::wstring(), 600 // to the omnibox getting focus. It still shouldn't be showing.
616 L"window.chrome.searchBox.onchange();")); 601 EXPECT_EQ(ASCIIToUTF16("http://deadly/nadder"), omnibox()->GetText());
617 ASSERT_TRUE(WaitForMessageToBeProcessedByRenderer()); 602 TabContents* preview_tab = instant()->GetPreviewContents();
618 603 EXPECT_TRUE(preview_tab);
619 // Instant should be active, but not displaying. 604 EXPECT_FALSE(instant()->IsCurrent());
620 EXPECT_TRUE(preview()); 605 EXPECT_FALSE(instant()->is_showing());
621 EXPECT_TRUE(loader()->ready()); 606
622 EXPECT_FALSE(instant()->is_displayable()); 607 // Commit the URL. The omnibox should reflect the URL minus the scheme.
623 EXPECT_FALSE(instant()->IsCurrent()); 608 browser()->window()->GetLocationBar()->AcceptInput();
624 } 609 TabContents* active_tab = chrome::GetActiveTabContents(browser());
625 610 EXPECT_NE(preview_tab, active_tab);
626 // Makes sure that if the server doesn't support the instant API we don't show 611 EXPECT_EQ(ASCIIToUTF16("deadly/nadder"), omnibox()->GetText());
627 // anything. 612
628 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SearchServerDoesntSupportInstant)) { 613 // Instant shouldn't have done anything.
629 ASSERT_TRUE(test_server()->Start()); 614 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
630 EnableInstant(); 615 EXPECT_FALSE(instant()->IsCurrent());
631 SetupInstantProvider("empty.html"); 616 EXPECT_FALSE(instant()->is_showing());
632 617
633 content::WindowedNotificationObserver tab_closed_observer( 618 // Let's try again, using a slightly different approach.
634 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 619
635 content::NotificationService::AllSources()); 620 // Type a query. This causes the preview to be shown.
636 621 browser()->window()->GetLocationBar()->FocusLocation(false);
637 omnibox()->SetUserText(ASCIIToUTF16("d")); 622 SetOmniboxText("query");
638 EXPECT_TRUE(preview()); 623 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
639 624
640 // When the response comes back that the page doesn't support instant the tab 625 // Type a URL. This causes the preview to be hidden.
641 // should be closed. 626 SetOmniboxText("http://hideous/zippleback");
642 tab_closed_observer.Wait(); 627 EXPECT_FALSE(instant()->is_showing());
643 EXPECT_FALSE(preview()); 628
644 } 629 // Pretend the omnibox got focus. It already had focus, so we are just trying
645 630 // to tickle a different code path.
646 // Verifies transitioning from loading a non-search string to a search string 631 instant()->OnAutocompleteGotFocus();
647 // with the provider not supporting instant works (meaning we don't display 632
648 // anything). 633 // Commit the URL. As before, check that Instant wasn't committed.
649 IN_PROC_BROWSER_TEST_F(InstantTest, 634 browser()->window()->GetLocationBar()->AcceptInput();
650 MAYBE(NonSearchToSearchDoesntSupportInstant)) { 635 EXPECT_EQ(active_tab, chrome::GetActiveTabContents(browser()));
651 ASSERT_TRUE(test_server()->Start()); 636 EXPECT_NE(preview_tab, active_tab);
652 EnableInstant(); 637 EXPECT_EQ(ASCIIToUTF16("hideous/zippleback"), omnibox()->GetText());
653 SetupInstantProvider("empty.html"); 638
654 639 // As before, Instant shouldn't have done anything.
655 GURL url(test_server()->GetURL("files/empty.html")); 640 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
656 omnibox()->SetUserText(UTF8ToUTF16(url.spec())); 641 EXPECT_FALSE(instant()->IsCurrent());
657 EXPECT_FALSE(preview()); 642 EXPECT_FALSE(instant()->is_showing());
658 643 }
659 content::WindowedNotificationObserver tab_closed_observer( 644
660 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 645 // Test that a non-Instant search provider shows no previews.
661 content::NotificationService::AllSources()); 646 IN_PROC_BROWSER_TEST_F(InstantTest, NonInstantSearchProvider) {
662 647 ASSERT_NO_FATAL_FAILURE(SetupInstant("empty.html"));
663 // Now type in some search text. 648
664 omnibox()->SetUserText(ASCIIToUTF16("d")); 649 // Type a query. Instant will load the search provider.
665 EXPECT_TRUE(preview()); 650 SetOmniboxText("query");
666 651 EXPECT_TRUE(instant()->GetPreviewContents());
667 // When the response comes back that the page doesn't support instant the tab 652
668 // should be closed. 653 // When the response comes back, Instant will destroy the non-Instant page.
669 tab_closed_observer.Wait(); 654 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
670 EXPECT_FALSE(preview()); 655 EXPECT_FALSE(instant()->GetPreviewContents());
671 } 656 }
672 657
673 // Verifies the page was told a non-zero height. 658 // Test that the renderer doesn't crash if JavaScript is blocked.
674 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(ValidHeight)) { 659 IN_PROC_BROWSER_TEST_F(InstantTest, NoCrashOnBlockedJS) {
675 ASSERT_TRUE(test_server()->Start());
676 EnableInstant();
677 SetupInstantProvider("instant.html");
678 DetermineInstantSupport();
679 SearchAndWaitForPreviewToShow();
680
681 int height = -1;
682
683 // searchBox height is not yet set during initial load.
684 ASSERT_TRUE(GetIntFromJavascript(preview()->web_contents(),
685 "window.beforeLoadSearchBox.height", &height));
686 EXPECT_EQ(0, height);
687
688 // searchBox height is available by the time the page loads.
689 ASSERT_TRUE(GetIntFromJavascript(preview()->web_contents(),
690 "window.chrome.searchBox.height", &height));
691 EXPECT_GT(height, 0);
692 }
693
694 // Make sure the renderer doesn't crash if javascript is blocked.
695 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(DontCrashOnBlockedJS)) {
696 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( 660 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
697 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); 661 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
698 ASSERT_TRUE(test_server()->Start()); 662 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
699 EnableInstant(); 663
700 SetupInstantProvider("instant.html"); 664 // Wait for notification that the Instant API has been determined. As long as
701
702 // Wait for notification that the instant API has been determined. As long as
703 // we get the notification we're good (the renderer didn't crash). 665 // we get the notification we're good (the renderer didn't crash).
704 DetermineInstantSupport(); 666 instant()->OnAutocompleteGotFocus();
705 } 667 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
706 668 }
707 // Makes sure window.chrome.searchbox doesn't persist when a new page is loaded. 669
708 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(DontPersistSearchbox)) { 670 // Test that the preview and active tab's visibility states are set correctly.
709 ASSERT_TRUE(test_server()->Start()); 671 IN_PROC_BROWSER_TEST_F(InstantTest, PageVisibility) {
710 EnableInstant(); 672 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
711 SetupInstantProvider("instant.html"); 673 instant()->OnAutocompleteGotFocus();
712 DetermineInstantSupport(); 674 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
713 SearchAndWaitForPreviewToShow(); 675
714 676 TabContents* active_tab = chrome::GetActiveTabContents(browser());
715 std::string value; 677 TabContents* preview_tab = instant()->GetPreviewContents();
716 ASSERT_TRUE(GetStringFromJavascript(preview()->web_contents(), 678
717 "window.chrome.searchBox.value", &value)); 679 // Inititally, the active tab is showing; the preview is not.
718 EXPECT_EQ("def", value); 680 EXPECT_TRUE(CheckVisibilityIs(active_tab, true));
681 EXPECT_TRUE(CheckVisibilityIs(preview_tab, false));
682
683 // Type a query and wait for Instant to show.
684 SetOmniboxText("search");
685 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
686 EXPECT_TRUE(CheckVisibilityIs(active_tab, false));
687 EXPECT_TRUE(CheckVisibilityIs(preview_tab, true));
688
689 // Deleting the omnibox text should hide the preview.
690 SetOmniboxText("");
691 EXPECT_TRUE(CheckVisibilityIs(active_tab, true));
692 EXPECT_TRUE(CheckVisibilityIs(preview_tab, false));
693
694 // Typing a query should show the preview again.
695 SetOmniboxText("search");
696 EXPECT_TRUE(CheckVisibilityIs(active_tab, false));
697 EXPECT_TRUE(CheckVisibilityIs(preview_tab, true));
719 698
720 // Commit the preview. 699 // Commit the preview.
721 ASSERT_TRUE(PressEnter()); 700 browser()->window()->GetLocationBar()->AcceptInput();
722 EXPECT_FALSE(preview()); 701 EXPECT_EQ(preview_tab, chrome::GetActiveTabContents(browser()));
723 702 EXPECT_TRUE(CheckVisibilityIs(preview_tab, true));
724 // The searchBox actually gets cleared on commit. 703 }
725 ASSERT_TRUE(GetStringFromJavascript(chrome::GetActiveWebContents(browser()), 704
726 "window.chrome.searchBox.value", &value)); 705 // Test that the task manager identifies Instant's preview tab correctly.
727 EXPECT_EQ("", value); 706 IN_PROC_BROWSER_TEST_F(InstantTest, TaskManagerPrefix) {
728
729 // Navigate to a new URL. The searchBox values should stay cleared.
730 ui_test_utils::NavigateToURL(
731 browser(), test_server()->GetURL("files/empty.html"));
732
733 ASSERT_TRUE(GetStringFromJavascript(chrome::GetActiveWebContents(browser()),
734 "window.chrome.searchBox.value", &value));
735 EXPECT_EQ("", value);
736 }
737
738 // Tests that instant search is preloaded whenever the omnibox gets focus.
739 // PreloadsInstant fails on linux_chromeos trybots all the time, possibly
740 // because of http://crbug.com/80118.
741 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
742 IN_PROC_BROWSER_TEST_F(InstantTest, DISABLED_PreloadsInstant) {
743 #else
744 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(PreloadsInstant)) {
745 #endif
746 ASSERT_TRUE(test_server()->Start());
747 EnableInstant();
748 SetupInstantProvider("instant.html");
749
750 // The omnibox gets focus before the test begins. At that time, there was no
751 // instant controller (which was only created after EnableInstant()), so no
752 // preloading happened. Unfocus the omnibox with ClickOnView(), so that when
753 // we focus it again, the controller will preload instant search.
754 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
755 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
756
757 // Verify that there is no preview.
758 EXPECT_FALSE(preview());
759
760 // Focusing the omnibox should cause instant to be preloaded.
761 content::WindowedNotificationObserver instant_support_observer(
762 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED,
763 content::NotificationService::AllSources());
764 browser()->window()->GetLocationBar()->FocusLocation(false);
765 instant_support_observer.Wait();
766
767 // Instant should have a preview, but not display it.
768 EXPECT_TRUE(preview());
769 EXPECT_FALSE(instant()->is_displayable());
770 EXPECT_FALSE(instant()->IsCurrent());
771 ASSERT_TRUE(CheckVisibilityIs(preview()->web_contents(), false));
772
773 // Adding a new tab shouldn't delete (or recreate) the TabContents.
774 TabContents* preview_tab = preview();
775 AddBlankTabAndShow(browser());
776 EXPECT_EQ(preview_tab, preview());
777
778 // Doing a search should still use the same loader for the preview.
779 SearchAndWaitForPreviewToShow();
780 EXPECT_EQ(preview_tab, preview());
781
782 // Verify that the preview is in fact showing instant search.
783 EXPECT_TRUE(instant()->is_displayable());
784 EXPECT_TRUE(instant()->IsCurrent());
785 ASSERT_TRUE(CheckVisibilityIs(preview()->web_contents(), true));
786 }
787
788 // Tests that the instant search page's visibility is set correctly.
789 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(PageVisibilityTest)) {
790 ASSERT_TRUE(test_server()->Start());
791 EnableInstant();
792 SetupInstantProvider("instant.html");
793
794 // Initially navigate to the empty page which should be visible.
795 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(""));
796 WebContents* initial_contents = chrome::GetActiveWebContents(browser());
797
798 ASSERT_TRUE(CheckVisibilityIs(initial_contents, true));
799
800 // Type a search term and wait for the preview to appear.
801 browser()->window()->GetLocationBar()->FocusLocation(false);
802 DetermineInstantSupport();
803 SearchAndWaitForPreviewToShow();
804 WebContents* preview_contents = preview()->web_contents();
805
806 ASSERT_TRUE(CheckVisibilityIs(preview_contents, true));
807 ASSERT_TRUE(CheckVisibilityIs(initial_contents, false));
808
809 // Deleting the user text should hide the preview.
810 omnibox()->SetUserText(string16());
811 ASSERT_TRUE(CheckVisibilityIs(preview_contents, false));
812 ASSERT_TRUE(CheckVisibilityIs(initial_contents, true));
813
814 // Set the user text back and we should see the preview again.
815 omnibox()->SetUserText(ASCIIToUTF16("def"));
816 ASSERT_TRUE(CheckVisibilityIs(preview_contents, true));
817 ASSERT_TRUE(CheckVisibilityIs(initial_contents, false));
818
819 // Commit the preview.
820 ASSERT_TRUE(PressEnter());
821 EXPECT_EQ(preview_contents, chrome::GetActiveWebContents(browser()));
822 ASSERT_TRUE(CheckVisibilityIs(preview_contents, true));
823 }
824
825 // Tests that the task manager identifies instant's preview tab correctly.
826 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(TaskManagerPrefix)) {
827 // The browser starts with one new tab, so the task manager should have two 707 // The browser starts with one new tab, so the task manager should have two
828 // rows initially, one for the browser process and one for tab's renderer. 708 // rows initially, one for the browser process and one for tab's renderer.
829 TaskManagerModel* task_manager = TaskManager::GetInstance()->model(); 709 TaskManagerModel* task_manager = TaskManager::GetInstance()->model();
830 task_manager->StartUpdating(); 710 task_manager->StartUpdating();
831 TaskManagerBrowserTestUtil::WaitForResourceChange(2); 711 TaskManagerBrowserTestUtil::WaitForResourceChange(2);
832 712
833 ASSERT_TRUE(test_server()->Start()); 713 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
834 EnableInstant(); 714 instant()->OnAutocompleteGotFocus();
835 SetupInstantProvider("instant.html"); 715 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
836 DetermineInstantSupport(); 716
837 SearchAndWaitForPreviewToShow(); 717 // Now there should be three rows, the third being the Instant preview.
838
839 // Now there should be three rows, the third being the instant preview.
840 TaskManagerBrowserTestUtil::WaitForResourceChange(3); 718 TaskManagerBrowserTestUtil::WaitForResourceChange(3);
841 string16 prefix = l10n_util::GetStringFUTF16( 719 string16 prefix = l10n_util::GetStringFUTF16(
842 IDS_TASK_MANAGER_INSTANT_PREVIEW_PREFIX, string16()); 720 IDS_TASK_MANAGER_INSTANT_PREVIEW_PREFIX, string16());
843 string16 title = task_manager->GetResourceTitle(2); 721 string16 title = task_manager->GetResourceTitle(2);
844 EXPECT_TRUE(StartsWith(title, prefix, true)) << title << " vs " << prefix; 722 EXPECT_TRUE(StartsWith(title, prefix, true)) << title << " vs " << prefix;
845 } 723 }
724
725 void HistoryQueryDone(base::RunLoop* run_loop,
726 bool* result,
727 HistoryService::Handle /* handle */,
728 bool success,
729 const history::URLRow* /* urlrow */,
730 history::VisitVector* /* visitvector */) {
731 *result = success;
732 run_loop->Quit();
733 }
734
735 void KeywordQueryDone(base::RunLoop* run_loop,
736 std::vector<string16>* result,
737 HistoryService::Handle /* handle */,
738 std::vector<history::KeywordSearchTermVisit>* terms) {
739 for (size_t i = 0; i < terms->size(); ++i)
740 result->push_back((*terms)[i].term);
741 run_loop->Quit();
742 }
743
744 // Test that the Instant page load is not added to history.
745 IN_PROC_BROWSER_TEST_F(InstantTest, History) {
746 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
747 instant()->OnAutocompleteGotFocus();
748 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
749
750 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
751 browser()->profile())->GetDefaultSearchProvider();
752
753 // |instant_url| is the URL Instant loads. |search_url| is the fake URL we
754 // enter into history for search terms extraction to work correctly.
755 std::string search_url = template_url->url_ref().ReplaceSearchTerms(
756 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("search")));
757 std::string instant_url = template_url->instant_url_ref().ReplaceSearchTerms(
758 TemplateURLRef::SearchTermsArgs(string16()));
759
760 HistoryService* history = HistoryServiceFactory::GetForProfile(
761 browser()->profile(), Profile::EXPLICIT_ACCESS);
762 ui_test_utils::WaitForHistoryToLoad(history);
763
764 // Perform a search.
765 SetOmniboxText("search");
766 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
767 EXPECT_EQ(instant_url, instant()->loader()->instant_url());
768
769 // Commit the search.
770 browser()->window()->GetLocationBar()->AcceptInput();
771
772 bool found = false;
773 CancelableRequestConsumer consumer;
774
775 // The fake search URL should be in history.
776 base::RunLoop run_loop1;
777 history->QueryURL(GURL(search_url), false, &consumer,
778 base::Bind(&HistoryQueryDone, &run_loop1, &found));
779 run_loop1.Run();
780 EXPECT_TRUE(found);
781
782 // The Instant URL should not be in history.
783 base::RunLoop run_loop2;
784 history->QueryURL(GURL(instant_url), false, &consumer,
785 base::Bind(&HistoryQueryDone, &run_loop2, &found));
786 run_loop2.Run();
787 EXPECT_FALSE(found);
788
789 // The search terms should have been extracted into history.
790 base::RunLoop run_loop3;
791 std::vector<string16> queries;
792 history->GetMostRecentKeywordSearchTerms(template_url->id(),
793 ASCIIToUTF16("s"), 1, &consumer,
794 base::Bind(&KeywordQueryDone, &run_loop3, &queries));
795 run_loop3.Run();
796 ASSERT_TRUE(queries.size());
797 EXPECT_EQ(ASCIIToUTF16("search"), queries[0]);
798 }
799
800 // On Windows, the call to NewEmptyWindow() fails the "GetBackingStore called
801 // while hidden" DCHECK(). It's not clear why; maybe because the active tab is
802 // in a hidden state when the Instant preview is showing, and somebody's trying
803 // to get its backing store?
804 #if !defined(OS_WIN)
805 // Test that creating a new window hides any currently showing Instant preview.
806 IN_PROC_BROWSER_TEST_F(InstantTest, NewWindowDismissesInstant) {
807 ASSERT_NO_FATAL_FAILURE(SetupInstant("instant.html"));
808 instant()->OnAutocompleteGotFocus();
809 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
810
811 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
812 SetOmniboxText("search");
813 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
814 EXPECT_TRUE(instant()->GetPreviewContents());
815 EXPECT_TRUE(instant()->IsCurrent());
816 EXPECT_TRUE(instant()->is_showing());
817
818 content::WindowedNotificationObserver observer(
819 chrome::NOTIFICATION_INSTANT_CONTROLLER_HIDDEN,
820 content::NotificationService::AllSources());
821 chrome::NewEmptyWindow(browser()->profile());
822 observer.Wait();
823 EXPECT_TRUE(instant()->GetPreviewContents());
824 EXPECT_FALSE(instant()->IsCurrent());
825 EXPECT_FALSE(instant()->is_showing());
826 }
827 #endif
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/instant/instant_commit_type.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698