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

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: 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
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"
6 #include "base/string_util.h"
7 #include "base/stringprintf.h" 5 #include "base/stringprintf.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/content_settings/host_content_settings_map.h"
10 #include "chrome/browser/instant/instant_controller.h" 6 #include "chrome/browser/instant/instant_controller.h"
11 #include "chrome/browser/instant/instant_loader.h" 7 #include "chrome/browser/instant/instant_loader.h"
12 #include "chrome/browser/prefs/pref_service.h" 8 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h" 9 #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" 10 #include "chrome/browser/search_engines/template_url_service.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h" 11 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/browser/task_manager/task_manager.h"
18 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
19 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_instant_controller.h" 13 #include "chrome/browser/ui/browser_instant_controller.h"
21 #include "chrome/browser/ui/browser_tabstrip.h" 14 #include "chrome/browser/ui/browser_tabstrip.h"
22 #include "chrome/browser/ui/browser_window.h" 15 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/omnibox/location_bar.h" 16 #include "chrome/browser/ui/omnibox/location_bar.h"
24 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h" 17 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
25 #include "chrome/browser/ui/omnibox/omnibox_view.h" 18 #include "chrome/browser/ui/omnibox/omnibox_view.h"
26 #include "chrome/browser/ui/tab_contents/tab_contents.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents.h"
27 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
30 #include "chrome/common/url_constants.h"
31 #include "chrome/test/base/in_process_browser_test.h" 22 #include "chrome/test/base/in_process_browser_test.h"
32 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
33 #include "content/public/browser/navigation_controller.h"
34 #include "content/public/browser/notification_service.h" 24 #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" 25 #include "content/public/browser/web_contents.h"
38 #include "content/public/common/content_switches.h"
39 #include "content/public/test/browser_test_utils.h" 26 #include "content/public/test/browser_test_utils.h"
40 #include "grit/generated_resources.h"
41 #include "ui/base/l10n/l10n_util.h"
42
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 27
54 class InstantTest : public InProcessBrowserTest { 28 class InstantTest : public InProcessBrowserTest {
55 public: 29 protected:
56 InstantTest() {} 30 void SetupInstant(const std::string& page, InstantController::Mode mode) {
57 31 ASSERT_TRUE(test_server()->Start());
58 void EnableInstant() { 32
59 InstantController::Enable(browser()->profile()); 33 TemplateURLService* service =
60 } 34 TemplateURLServiceFactory::GetForProfile(browser()->profile());
61 35 ui_test_utils::WaitForTemplateURLServiceToLoad(service);
62 void SetupInstantProvider(const std::string& page) {
63 Profile* profile = browser()->profile();
64 TemplateURLService* model =
65 TemplateURLServiceFactory::GetForProfile(profile);
66
67 content::WindowedNotificationObserver observer(
68 chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED,
69 content::NotificationService::AllSources());
70 if (!model->loaded()) {
71 model->Load();
72 observer.Wait();
73 }
74 36
75 TemplateURLData data; 37 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}", 38 data.SetURL(base::StringPrintf("http://%s:%d/files/%s?q={searchTerms}",
79 test_server()->host_port_pair().host().c_str(), 39 test_server()->host_port_pair().host().c_str(),
80 test_server()->host_port_pair().port(), page.c_str())); 40 test_server()->host_port_pair().port(), page.c_str()));
81 data.instant_url = data.url(); 41 data.instant_url = data.url();
82 // TemplateURLService takes ownership of this. 42
83 TemplateURL* template_url = new TemplateURL(profile, data); 43 TemplateURL* template_url = new TemplateURL(browser()->profile(), data);
84 model->Add(template_url); 44 service->Add(template_url); // Takes ownership of |template_url|.
85 model->SetDefaultSearchProvider(template_url); 45 service->SetDefaultSearchProvider(template_url);
86 } 46
87 47 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true);
88 // Type a character to get instant to trigger and determine instant support. 48 }
89 void DetermineInstantSupport() { 49
90 content::WindowedNotificationObserver observer( 50 OmniboxView* omnibox() const {
91 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, 51 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 } 52 }
115 53
116 InstantController* instant() const { 54 InstantController* instant() const {
117 return browser()->instant_controller()->instant(); 55 return browser()->instant_controller()->instant();
118 } 56 }
119 57
120 OmniboxView* omnibox() const {
121 return browser()->window()->GetLocationBar()->GetLocationEntry();
122 }
123
124 TabContents* preview() const {
125 return instant()->GetPreviewContents();
126 }
127
128 InstantLoader* loader() const { 58 InstantLoader* loader() const {
129 return instant()->loader_.get(); 59 return instant()->loader_.get();
130 } 60 }
131 61
132 std::string GetSuggestion() const { 62 bool is_showing() const {
133 return UTF16ToUTF8(loader()->complete_suggested_text_); 63 return instant()->is_showing_;
134 } 64 }
135 65
136 bool PressEnter() { 66 void WaitFor(chrome::NotificationType notification_type) const {
137 return ui_test_utils::SendKeyPressSync( 67 content::WindowedNotificationObserver observer(
138 browser(), ui::VKEY_RETURN, false, false, false, false); 68 notification_type, content::NotificationService::AllSources());
139 } 69 observer.Wait();
140
141 bool SetSuggestionsJavascriptArgument(const std::string& argument) {
142 std::wstring script = UTF8ToWide(base::StringPrintf(
143 "window.setSuggestionsArgument = %s;", argument.c_str()));
144 content::RenderViewHost* rvh =
145 preview()->web_contents()->GetRenderViewHost();
146 return content::ExecuteJavaScript(rvh, std::wstring(), script);
147 } 70 }
148 71
149 std::wstring WrapScript(const std::string& script) { 72 std::wstring WrapScript(const std::string& script) {
150 return UTF8ToWide(base::StringPrintf( 73 return UTF8ToWide("domAutomationController.send(" + script + ")");
151 "window.domAutomationController.send(%s)", script.c_str())); 74 }
152 } 75
153 76 bool GetBoolFromJS(content::RenderViewHost* rvh,
154 bool GetStringFromJavascript(WebContents* tab, 77 const std::string& script,
155 const std::string& script, 78 bool* result) WARN_UNUSED_RESULT {
156 std::string* result) { 79 return content::ExecuteJavaScriptAndExtractBool(
80 rvh, std::wstring(), WrapScript(script), result);
81 }
82
83 bool GetIntFromJS(content::RenderViewHost* rvh,
84 const std::string& script,
85 int* result) WARN_UNUSED_RESULT {
86 return content::ExecuteJavaScriptAndExtractInt(
87 rvh, std::wstring(), WrapScript(script), result);
88 }
89
90 bool GetStringFromJS(content::RenderViewHost* rvh,
91 const std::string& script,
92 std::string* result) WARN_UNUSED_RESULT {
157 return content::ExecuteJavaScriptAndExtractString( 93 return content::ExecuteJavaScriptAndExtractString(
158 tab->GetRenderViewHost(), std::wstring(), WrapScript(script), result); 94 rvh, std::wstring(), WrapScript(script), result);
159 } 95 }
160 96
161 bool GetIntFromJavascript(WebContents* tab, 97 bool UpdateSearchState(TabContents* tab) WARN_UNUSED_RESULT {
162 const std::string& script, 98 content::RenderViewHost* rvh = tab->web_contents()->GetRenderViewHost();
163 int* result) { 99 return GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) &&
164 return content::ExecuteJavaScriptAndExtractInt( 100 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) &&
165 tab->GetRenderViewHost(), std::wstring(), WrapScript(script), result); 101 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) &&
166 } 102 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) &&
167 103 GetStringFromJS(rvh, "value", &value_) &&
168 bool GetBoolFromJavascript(WebContents* tab, 104 GetBoolFromJS(rvh, "verbatim", &verbatim_);
169 const std::string& script, 105 }
170 bool* result) { 106
171 return content::ExecuteJavaScriptAndExtractBool( 107 int onchangecalls_;
172 tab->GetRenderViewHost(), std::wstring(), WrapScript(script), result); 108 int onsubmitcalls_;
173 } 109 int oncancelcalls_;
174 110 int onresizecalls_;
175 bool CheckVisibilityIs(WebContents* tab, bool visible) { 111
176 bool hidden = visible; 112 std::string value_;
177 return GetBoolFromJavascript(tab, "document.webkitHidden", &hidden) && 113 bool verbatim_;
178 hidden != visible;
179 }
180
181 // Returns the state of the search box as a string. This consists of the
182 // following:
183 // window.chrome.sv
184 // window.onsubmitcalls
185 // window.oncancelcalls
186 // window.onchangecalls
187 // 'true' if any window.onresize call has been sent, otherwise false.
188 // window.beforeLoadSearchBox.value
189 // window.beforeLoadSearchBox.verbatim
190 // window.chrome.searchBox.value
191 // window.chrome.searchBox.verbatim
192 // window.chrome.searchBox.selectionStart
193 // window.chrome.searchBox.selectionEnd
194 // If determining any of the values fails, the value is 'fail'.
195 //
196 // If |use_last| is true, then the last searchBox values are used instead of
197 // the current. Set |use_last| to true when testing OnSubmit/OnCancel.
198 std::string GetSearchStateAsString(WebContents* tab, bool use_last) {
199 bool sv = false;
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 }; 114 };
268 115
269 // TODO(tonyg): Add the following tests: 116 // 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 117 IN_PROC_BROWSER_TEST_F(InstantTest, OmniboxFocusLoadsInstant) {
271 // default search provider. 118 // The omnibox gets focus before the test begins. At this time, there's no
272 // - Test resize events. 119 // InstantController (which is only created in SetupInstant() below), so no
273 120 // preloading has happened yet.
274 // Verify that the onchange event is dispatched upon typing in the box. 121 SetupInstant("instant.html", InstantController::INSTANT);
275 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(OnChangeEvent)) { 122 EXPECT_FALSE(instant()->GetPreviewContents());
276 ASSERT_TRUE(test_server()->Start()); 123
277 EnableInstant(); 124 // Explicitly unfocus and refocus the omnibox. Since an InstantController now
278 SetupInstantProvider("instant.html"); 125 // exists, it will preload Instant.
279 DetermineInstantSupport(); 126 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
280 SearchAndWaitForPreviewToShow(); 127 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
281 128 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
282 EXPECT_TRUE(preview()); 129 omnibox()->SetFocus();
283 EXPECT_TRUE(instant()->is_displayable()); 130 TabContents* preview_tab = instant()->GetPreviewContents();
284 EXPECT_TRUE(instant()->IsCurrent());
285 EXPECT_EQ("defghi", UTF16ToUTF8(omnibox()->GetText()));
286
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); 131 EXPECT_TRUE(preview_tab);
317 132
318 ASSERT_TRUE(PressEnter()); 133 // Check that the page supports Instant, but it isn't showing.
319 134 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
320 // Check that the preview has been committed. 135 EXPECT_TRUE(loader()->supports_instant());
321 EXPECT_FALSE(preview());
322 EXPECT_FALSE(instant()->is_displayable());
323 EXPECT_FALSE(instant()->IsCurrent()); 136 EXPECT_FALSE(instant()->IsCurrent());
324 EXPECT_EQ(preview_tab, chrome::GetActiveWebContents(browser())); 137 EXPECT_FALSE(is_showing());
325 138
326 // We should have two entries. One corresponding to the page the user was 139 // Adding a new tab shouldn't delete or recreate the TabContents; otherwise,
327 // first on, and one for the search page. 140 // what's the point of preloading?
328 EXPECT_EQ(2, preview_tab->GetController().GetEntryCount()); 141 AddBlankTabAndShow(browser());
329 142 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
330 // Check that the value is reflected and onsubmit is called. 143
331 EXPECT_EQ("true 1 0 1 true d false defghi true 3 3", 144 // Unfocusing and refocusing the omnibox should also preserve the preview.
332 GetSearchStateAsString(preview_tab, true)); 145 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
333 146 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
334 // Make sure the searchbox values were reset. 147 omnibox()->SetFocus();
335 EXPECT_EQ("true 1 0 1 true d false false 0 0", 148 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
336 GetSearchStateAsString(preview_tab, false)); 149
337 } 150 // Doing a search should also use the same preloaded page.
338 151 omnibox()->SetUserText(ASCIIToUTF16("query"));
339 // Verify that the oncancel event is dispatched upon losing focus. 152 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
340 IN_PROC_BROWSER_TEST_F(InstantTest, DISABLED_OnCancelEvent) { 153 EXPECT_TRUE(is_showing());
341 ASSERT_TRUE(test_server()->Start()); 154 EXPECT_EQ(preview_tab, instant()->GetPreviewContents());
342 EnableInstant(); 155 }
343 SetupInstantProvider("instant.html"); 156
344 DetermineInstantSupport(); 157 // Test that the onchange event is dispatched upon typing in the omnibox.
345 SearchAndWaitForPreviewToShow(); 158 IN_PROC_BROWSER_TEST_F(InstantTest, OnChangeEvent) {
346 159 SetupInstant("instant.html", InstantController::INSTANT);
347 EXPECT_TRUE(preview()); 160
348 EXPECT_TRUE(instant()->is_displayable()); 161 // Typing "query" into the omnibox causes the first onchange event.
349 EXPECT_TRUE(instant()->IsCurrent()); 162 omnibox()->SetUserText(ASCIIToUTF16("query"));
350 EXPECT_EQ("defghi", UTF16ToUTF8(omnibox()->GetText())); 163 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
351 164
352 WebContents* preview_tab = preview()->web_contents(); 165 // The page suggested "query suggestion" is inline autocompleted into the
166 // omnibox, causing the second onchange event.
167 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText());
168 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
169 EXPECT_EQ(2, onchangecalls_);
170
171 // Change the query and confirm that another onchange is sent. Since the new
172 // query is not a prefix of the hardcoded "query suggestion", no inline
173 // autocompletion happens, and thus, no fourth onchange event.
174 omnibox()->SetUserText(ASCIIToUTF16("search"));
175 EXPECT_TRUE(UpdateSearchState(instant()->GetPreviewContents()));
176 EXPECT_EQ(3, onchangecalls_);
177 }
178
179 // Test that the onsubmit event is dispatched upon pressing Enter.
180 IN_PROC_BROWSER_TEST_F(InstantTest, OnSubmitEvent) {
181 SetupInstant("instant.html", InstantController::INSTANT);
182 omnibox()->SetUserText(ASCIIToUTF16("search"));
183 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
184
185 // Stash a reference to the preview, so we can refer to it after commit.
186 TabContents* preview_tab = instant()->GetPreviewContents();
353 EXPECT_TRUE(preview_tab); 187 EXPECT_TRUE(preview_tab);
354 188
355 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 189 // The state of the searchbox before the commit.
190 EXPECT_TRUE(UpdateSearchState(preview_tab));
191 EXPECT_EQ("search", value_);
192 EXPECT_FALSE(verbatim_);
193 EXPECT_EQ(0, onsubmitcalls_);
194
195 // Before the commit, the active tab is the NTP (i.e., not Instant).
196 TabContents* active_tab = chrome::GetActiveTabContents(browser());
197 EXPECT_NE(preview_tab, active_tab);
198 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
199 EXPECT_EQ(chrome::kAboutBlankURL, omnibox()->model()->PermanentURL().spec());
200
201 // Commit the search by pressing Enter.
202 browser()->window()->GetLocationBar()->AcceptInput();
203
204 // After the commit, Instant should not be showing, or even have a preview.
205 EXPECT_FALSE(instant()->GetPreviewContents());
206 EXPECT_FALSE(instant()->IsCurrent());
207 EXPECT_FALSE(is_showing());
208
209 // Check that the current active tab is indeed what was once the preview.
210 EXPECT_EQ(preview_tab, chrome::GetActiveTabContents(browser()));
211
212 // We should have two navigation entries, one for the NTP, and one for the
213 // Instant search that was committed.
214 EXPECT_EQ(2, preview_tab->web_contents()->GetController().GetEntryCount());
215
216 // Check that the omnibox contains the Instant URL we loaded.
217 const std::string& instant_url = TemplateURLServiceFactory::GetForProfile(
218 browser()->profile())->GetDefaultSearchProvider()->instant_url_ref().
219 ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(string16()));
220 EXPECT_EQ(instant_url, omnibox()->model()->PermanentURL().spec());
221
222 // Check that the searchbox API values have been reset.
223 std::string value;
224 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
225 "chrome.searchBox.value", &value));
226 EXPECT_EQ("", value);
227
228 // However, the page should've correctly received the committed query.
229 EXPECT_TRUE(UpdateSearchState(preview_tab));
230 EXPECT_EQ("search", value_);
231 EXPECT_TRUE(verbatim_);
232 EXPECT_EQ(1, onsubmitcalls_);
233 }
234
235 // Test that the oncancel event is dispatched upon clicking on the preview.
236 IN_PROC_BROWSER_TEST_F(InstantTest, OnCancelEvent) {
237 SetupInstant("instant.html", InstantController::INSTANT);
238 omnibox()->SetUserText(ASCIIToUTF16("search"));
239 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
240
241 // Stash a reference to the preview, so we can refer to it after commit.
242 TabContents* preview_tab = instant()->GetPreviewContents();
243 EXPECT_TRUE(preview_tab);
244
245 // The state of the searchbox before the commit.
246 EXPECT_TRUE(UpdateSearchState(preview_tab));
247 EXPECT_EQ("search", value_);
248 EXPECT_FALSE(verbatim_);
249 EXPECT_EQ(0, oncancelcalls_);
250
251 // Before the commit, the active tab is the NTP (i.e., not Instant).
252 TabContents* active_tab = chrome::GetActiveTabContents(browser());
253 EXPECT_NE(preview_tab, active_tab);
254 EXPECT_EQ(1, active_tab->web_contents()->GetController().GetEntryCount());
255 EXPECT_EQ(chrome::kAboutBlankURL, omnibox()->model()->PermanentURL().spec());
256
257 // Commit the search by clicking on the preview.
258 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
356 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); 259 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
357 260
358 // Check that the preview has been committed. 261 // After the commit, Instant should not be showing, or even have a preview.
359 EXPECT_FALSE(preview()); 262 EXPECT_FALSE(instant()->GetPreviewContents());
360 EXPECT_FALSE(instant()->is_displayable());
361 EXPECT_FALSE(instant()->IsCurrent()); 263 EXPECT_FALSE(instant()->IsCurrent());
362 EXPECT_EQ(preview_tab, chrome::GetActiveWebContents(browser())); 264 EXPECT_FALSE(is_showing());
363 265
364 // Check that the value is reflected and oncancel is called. 266 // Check that the current active tab is indeed what was once the preview.
365 EXPECT_EQ("true 0 1 1 true d false def false 3 3", 267 EXPECT_EQ(preview_tab, chrome::GetActiveTabContents(browser()));
366 GetSearchStateAsString(preview_tab, true)); 268
367 269 // We should have two navigation entries, one for the NTP, and one for the
368 // Make sure the searchbox values were reset. 270 // Instant search that was committed.
369 EXPECT_EQ("true 0 1 1 true d false false 0 0", 271 EXPECT_EQ(2, preview_tab->web_contents()->GetController().GetEntryCount());
370 GetSearchStateAsString(preview_tab, false)); 272
371 } 273 // Check that the omnibox contains the Instant URL we loaded.
372 274 const std::string& instant_url = TemplateURLServiceFactory::GetForProfile(
373 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsArrayOfStrings)) { 275 browser()->profile())->GetDefaultSearchProvider()->instant_url_ref().
374 ASSERT_TRUE(test_server()->Start()); 276 ReplaceSearchTerms(TemplateURLRef::SearchTermsArgs(string16()));
375 EnableInstant(); 277 EXPECT_EQ(instant_url, omnibox()->model()->PermanentURL().spec());
376 SetupInstantProvider("instant.html"); 278
377 DetermineInstantSupport(); 279 // Check that the searchbox API values have been reset.
378
379 ASSERT_TRUE(SetSuggestionsJavascriptArgument("['defg', 'unused']"));
380 SearchAndWaitForPreviewToShow();
381 EXPECT_EQ("defg", GetSuggestion());
382 }
383
384 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptyArray)) {
385 ASSERT_TRUE(test_server()->Start());
386 EnableInstant();
387 SetupInstantProvider("instant.html");
388 DetermineInstantSupport();
389
390 ASSERT_TRUE(SetSuggestionsJavascriptArgument("[]"));
391 SearchAndWaitForPreviewToShow();
392 EXPECT_EQ("", GetSuggestion());
393 }
394
395 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsValidJson)) {
396 ASSERT_TRUE(test_server()->Start());
397 EnableInstant();
398 SetupInstantProvider("instant.html");
399 DetermineInstantSupport();
400
401 ASSERT_TRUE(SetSuggestionsJavascriptArgument(
402 "{suggestions:[{value:'defg'},{value:'unused'}]}"));
403 SearchAndWaitForPreviewToShow();
404 EXPECT_EQ("defg", GetSuggestion());
405 }
406
407 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsInvalidSuggestions)) {
408 ASSERT_TRUE(test_server()->Start());
409 EnableInstant();
410 SetupInstantProvider("instant.html");
411 DetermineInstantSupport();
412
413 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{suggestions:{value:'defg'}}"));
414 SearchAndWaitForPreviewToShow();
415 EXPECT_EQ("", GetSuggestion());
416 }
417
418 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptyJson)) {
419 ASSERT_TRUE(test_server()->Start());
420 EnableInstant();
421 SetupInstantProvider("instant.html");
422 DetermineInstantSupport();
423
424 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{}"));
425 SearchAndWaitForPreviewToShow();
426 EXPECT_EQ("", GetSuggestion());
427 }
428
429 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptySuggestions)) {
430 ASSERT_TRUE(test_server()->Start());
431 EnableInstant();
432 SetupInstantProvider("instant.html");
433 DetermineInstantSupport();
434
435 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{suggestions:[]}"));
436 SearchAndWaitForPreviewToShow();
437 EXPECT_EQ("", GetSuggestion());
438 }
439
440 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SetSuggestionsEmptySuggestion)) {
441 ASSERT_TRUE(test_server()->Start());
442 EnableInstant();
443 SetupInstantProvider("instant.html");
444 DetermineInstantSupport();
445
446 ASSERT_TRUE(SetSuggestionsJavascriptArgument("{suggestions:[{}]}"));
447 SearchAndWaitForPreviewToShow();
448 EXPECT_EQ("", GetSuggestion());
449 }
450
451 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(InstantCompleteNever)) {
452 ASSERT_TRUE(test_server()->Start());
453 EnableInstant();
454 SetupInstantProvider("instant.html");
455 DetermineInstantSupport();
456
457 ASSERT_TRUE(SetSuggestionsJavascriptArgument(
458 "{suggestions:[{value:'defg'}],complete_behavior:'never'}"));
459 SearchAndWaitForPreviewToShow();
460 EXPECT_EQ("defg", GetSuggestion());
461
462 EXPECT_EQ(INSTANT_COMPLETE_NEVER,
463 omnibox()->model()->instant_complete_behavior());
464 EXPECT_EQ("def", UTF16ToUTF8(omnibox()->GetText()));
465 }
466
467 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(InstantCompleteDelayed)) {
468 ASSERT_TRUE(test_server()->Start());
469 EnableInstant();
470 SetupInstantProvider("instant.html");
471 DetermineInstantSupport();
472
473 ASSERT_TRUE(SetSuggestionsJavascriptArgument(
474 "{suggestions:[{value:'defg'}],complete_behavior:'delayed'}"));
475 SearchAndWaitForPreviewToShow();
476 EXPECT_EQ("defg", GetSuggestion());
477
478 EXPECT_EQ(INSTANT_COMPLETE_DELAYED,
479 omnibox()->model()->instant_complete_behavior());
480 EXPECT_EQ("def", UTF16ToUTF8(omnibox()->GetText()));
481 }
482
483 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(InstantCompleteNow)) {
484 ASSERT_TRUE(test_server()->Start());
485 EnableInstant();
486 SetupInstantProvider("instant.html");
487 DetermineInstantSupport();
488
489 ASSERT_TRUE(SetSuggestionsJavascriptArgument(
490 "{suggestions:[{value:'defg'}],complete_behavior:'now'}"));
491 SearchAndWaitForPreviewToShow();
492 EXPECT_EQ("defg", GetSuggestion());
493
494 EXPECT_EQ(INSTANT_COMPLETE_NOW,
495 omnibox()->model()->instant_complete_behavior());
496 EXPECT_EQ("defg", UTF16ToUTF8(omnibox()->GetText()));
497 }
498
499 // Verifies that instant previews aren't shown for crash URLs.
500 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(CrashUrlCancelsInstant)) {
501 ASSERT_TRUE(test_server()->Start());
502 EnableInstant();
503 SetupInstantProvider("instant.html");
504
505 omnibox()->SetUserText(ASCIIToUTF16(chrome::kChromeUICrashURL));
506 EXPECT_FALSE(preview());
507 }
508
509 // Tests that instant doesn't fire for intranet paths that look like searches.
510 // http://crbug.com/99836
511 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(IntranetPathLooksLikeSearch)) {
512 ASSERT_TRUE(test_server()->Start());
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
521 // 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
523 // that instant doesn't act on it.
524 ui_test_utils::NavigateToURL(browser(), GURL("http://baby/beluga"));
525 EXPECT_EQ("baby/beluga", UTF16ToUTF8(omnibox()->GetText()));
526 EXPECT_FALSE(preview());
527 }
528
529 // Verifies that instant previews aren't shown for non-search URLs.
530 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(ShowPreviewNonSearch)) {
531 ASSERT_TRUE(test_server()->Start());
532 EnableInstant();
533 SetupInstantProvider("instant.html");
534
535 GURL url(test_server()->GetURL("files/empty.html"));
536 omnibox()->SetUserText(UTF8ToUTF16(url.spec()));
537 EXPECT_FALSE(preview());
538 }
539
540 // Transition from non-search to search and make sure everything works.
541 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(NonSearchToSearch)) {
542 ASSERT_TRUE(test_server()->Start());
543 EnableInstant();
544 SetupInstantProvider("instant.html");
545
546 // Load a non-search URL.
547 GURL url(test_server()->GetURL("files/empty.html"));
548 omnibox()->SetUserText(UTF8ToUTF16(url.spec()));
549 EXPECT_FALSE(preview());
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());
578
579 content::RenderWidgetHostView* rwhv =
580 preview()->web_contents()->GetRenderWidgetHostView();
581 EXPECT_TRUE(rwhv);
582 EXPECT_TRUE(rwhv->IsShowing());
583 }
584
585 // Transition from search to non-search and make sure instant isn't displayable.
586 // See bug http://crbug.com/100368 for details.
587 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SearchToNonSearch)) {
588 ASSERT_TRUE(test_server()->Start());
589 EnableInstant();
590 SetupInstantProvider("instant.html");
591
592 content::WindowedNotificationObserver instant_support_observer(
593 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED,
594 content::NotificationService::AllSources());
595
596 // Type in some search text.
597 omnibox()->SetUserText(ASCIIToUTF16("def"));
598
599 // Load a non search URL. Don't wait for the preview to navigate. It'll still
600 // end up loading in the background.
601 GURL url(test_server()->GetURL("files/empty.html"));
602 omnibox()->SetUserText(UTF8ToUTF16(url.spec()));
603
604 instant_support_observer.Wait();
605
606 // We should now have a preview, but it shouldn't be showing yet.
607 EXPECT_TRUE(preview());
608 EXPECT_FALSE(loader()->ready());
609 EXPECT_FALSE(instant()->is_displayable());
610 EXPECT_FALSE(instant()->IsCurrent());
611
612 // Send onchange so that the page sends up suggestions. See the comments in
613 // NonSearchToSearch for why this is needed.
614 ASSERT_TRUE(content::ExecuteJavaScript(
615 preview()->web_contents()->GetRenderViewHost(), std::wstring(),
616 L"window.chrome.searchBox.onchange();"));
617 ASSERT_TRUE(WaitForMessageToBeProcessedByRenderer());
618
619 // Instant should be active, but not displaying.
620 EXPECT_TRUE(preview());
621 EXPECT_TRUE(loader()->ready());
622 EXPECT_FALSE(instant()->is_displayable());
623 EXPECT_FALSE(instant()->IsCurrent());
624 }
625
626 // Makes sure that if the server doesn't support the instant API we don't show
627 // anything.
628 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(SearchServerDoesntSupportInstant)) {
629 ASSERT_TRUE(test_server()->Start());
630 EnableInstant();
631 SetupInstantProvider("empty.html");
632
633 content::WindowedNotificationObserver tab_closed_observer(
634 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
635 content::NotificationService::AllSources());
636
637 omnibox()->SetUserText(ASCIIToUTF16("d"));
638 EXPECT_TRUE(preview());
639
640 // When the response comes back that the page doesn't support instant the tab
641 // should be closed.
642 tab_closed_observer.Wait();
643 EXPECT_FALSE(preview());
644 }
645
646 // Verifies transitioning from loading a non-search string to a search string
647 // with the provider not supporting instant works (meaning we don't display
648 // anything).
649 IN_PROC_BROWSER_TEST_F(InstantTest,
650 MAYBE(NonSearchToSearchDoesntSupportInstant)) {
651 ASSERT_TRUE(test_server()->Start());
652 EnableInstant();
653 SetupInstantProvider("empty.html");
654
655 GURL url(test_server()->GetURL("files/empty.html"));
656 omnibox()->SetUserText(UTF8ToUTF16(url.spec()));
657 EXPECT_FALSE(preview());
658
659 content::WindowedNotificationObserver tab_closed_observer(
660 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
661 content::NotificationService::AllSources());
662
663 // Now type in some search text.
664 omnibox()->SetUserText(ASCIIToUTF16("d"));
665 EXPECT_TRUE(preview());
666
667 // When the response comes back that the page doesn't support instant the tab
668 // should be closed.
669 tab_closed_observer.Wait();
670 EXPECT_FALSE(preview());
671 }
672
673 // Verifies the page was told a non-zero height.
674 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(ValidHeight)) {
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(
697 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
698 ASSERT_TRUE(test_server()->Start());
699 EnableInstant();
700 SetupInstantProvider("instant.html");
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).
704 DetermineInstantSupport();
705 }
706
707 // Makes sure window.chrome.searchbox doesn't persist when a new page is loaded.
708 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(DontPersistSearchbox)) {
709 ASSERT_TRUE(test_server()->Start());
710 EnableInstant();
711 SetupInstantProvider("instant.html");
712 DetermineInstantSupport();
713 SearchAndWaitForPreviewToShow();
714
715 std::string value; 280 std::string value;
716 ASSERT_TRUE(GetStringFromJavascript(preview()->web_contents(), 281 EXPECT_TRUE(GetStringFromJS(preview_tab->web_contents()->GetRenderViewHost(),
717 "window.chrome.searchBox.value", &value)); 282 "chrome.searchBox.value", &value));
718 EXPECT_EQ("def", value);
719
720 // Commit the preview.
721 ASSERT_TRUE(PressEnter());
722 EXPECT_FALSE(preview());
723
724 // The searchBox actually gets cleared on commit.
725 ASSERT_TRUE(GetStringFromJavascript(chrome::GetActiveWebContents(browser()),
726 "window.chrome.searchBox.value", &value));
727 EXPECT_EQ("", value); 283 EXPECT_EQ("", value);
728 284
729 // Navigate to a new URL. The searchBox values should stay cleared. 285 // However, the page should've correctly received the committed query.
730 ui_test_utils::NavigateToURL( 286 EXPECT_TRUE(UpdateSearchState(preview_tab));
731 browser(), test_server()->GetURL("files/empty.html")); 287 EXPECT_EQ("search", value_);
732 288 EXPECT_TRUE(verbatim_);
733 ASSERT_TRUE(GetStringFromJavascript(chrome::GetActiveWebContents(browser()), 289 EXPECT_EQ(1, oncancelcalls_);
734 "window.chrome.searchBox.value", &value)); 290 }
735 EXPECT_EQ("", value); 291
736 } 292 // Test that the INSTANT_COMPLETE_NOW behavior works as expected.
737 293 IN_PROC_BROWSER_TEST_F(InstantTest, InstantCompleteNow) {
738 // Tests that instant search is preloaded whenever the omnibox gets focus. 294 SetupInstant("instant.html", InstantController::INSTANT);
739 // PreloadsInstant fails on linux_chromeos trybots all the time, possibly 295 instant()->OnAutocompleteGotFocus();
740 // because of http://crbug.com/80118. 296 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
741 #if defined(OS_CHROMEOS) || defined(OS_MACOSX) 297
742 IN_PROC_BROWSER_TEST_F(InstantTest, DISABLED_PreloadsInstant) { 298 // Tell the JS to request for the given behavior.
743 #else 299 EXPECT_TRUE(content::ExecuteJavaScript(
744 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(PreloadsInstant)) { 300 instant()->GetPreviewContents()->web_contents()->GetRenderViewHost(),
745 #endif 301 std::wstring(), ASCIIToWide("behavior='now'")));
746 ASSERT_TRUE(test_server()->Start()); 302
747 EnableInstant(); 303 omnibox()->SetUserText(ASCIIToUTF16("query"));
748 SetupInstantProvider("instant.html"); 304 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
749 305
750 // The omnibox gets focus before the test begins. At that time, there was no 306 string16 text = omnibox()->GetText();
751 // instant controller (which was only created after EnableInstant()), so no 307 size_t start = 0, end = 0;
752 // preloading happened. Unfocus the omnibox with ClickOnView(), so that when 308 omnibox()->GetSelectionBounds(&start, &end);
753 // we focus it again, the controller will preload instant search. 309 if (start > end)
754 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 310 std::swap(start, end);
755 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); 311
756 312 EXPECT_EQ(ASCIIToUTF16("query suggestion"), text);
757 // Verify that there is no preview. 313 EXPECT_EQ(ASCIIToUTF16(" suggestion"), text.substr(start, end - start));
758 EXPECT_FALSE(preview()); 314 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
759 315 }
760 // Focusing the omnibox should cause instant to be preloaded. 316
761 content::WindowedNotificationObserver instant_support_observer( 317 // Test that the INSTANT_COMPLETE_DELAYED behavior works as expected.
762 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, 318 IN_PROC_BROWSER_TEST_F(InstantTest, InstantCompleteDelayed) {
763 content::NotificationService::AllSources()); 319 SetupInstant("instant.html", InstantController::INSTANT);
764 browser()->window()->GetLocationBar()->FocusLocation(false); 320 instant()->OnAutocompleteGotFocus();
765 instant_support_observer.Wait(); 321 WaitFor(chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED);
766 322
767 // Instant should have a preview, but not display it. 323 // Tell the JS to request for the given behavior.
768 EXPECT_TRUE(preview()); 324 EXPECT_TRUE(content::ExecuteJavaScript(
769 EXPECT_FALSE(instant()->is_displayable()); 325 instant()->GetPreviewContents()->web_contents()->GetRenderViewHost(),
770 EXPECT_FALSE(instant()->IsCurrent()); 326 std::wstring(), ASCIIToWide("behavior='delayed'")));
771 ASSERT_TRUE(CheckVisibilityIs(preview()->web_contents(), false)); 327
772 328 omnibox()->SetUserText(ASCIIToUTF16("query"));
773 // Adding a new tab shouldn't delete (or recreate) the TabContents. 329 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_SHOWN);
774 TabContents* preview_tab = preview(); 330
775 AddBlankTabAndShow(browser()); 331 string16 text = omnibox()->GetText();
776 EXPECT_EQ(preview_tab, preview()); 332 size_t start = 0, end = 0;
777 333 omnibox()->GetSelectionBounds(&start, &end);
778 // Doing a search should still use the same loader for the preview. 334
779 SearchAndWaitForPreviewToShow(); 335 EXPECT_EQ(ASCIIToUTF16("query"), text);
780 EXPECT_EQ(preview_tab, preview()); 336 EXPECT_EQ(ASCIIToUTF16(""), text.substr(start, end - start));
781 337 EXPECT_EQ(ASCIIToUTF16(" suggestion"), omnibox()->GetInstantSuggestion());
782 // Verify that the preview is in fact showing instant search. 338
783 EXPECT_TRUE(instant()->is_displayable()); 339 // Wait for the animation to complete, which causes the omnibox to update.
784 EXPECT_TRUE(instant()->IsCurrent()); 340 WaitFor(chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED);
785 ASSERT_TRUE(CheckVisibilityIs(preview()->web_contents(), true)); 341
786 } 342 text = omnibox()->GetText();
787 343 omnibox()->GetSelectionBounds(&start, &end);
788 // Tests that the instant search page's visibility is set correctly. 344 if (start > end)
789 IN_PROC_BROWSER_TEST_F(InstantTest, MAYBE(PageVisibilityTest)) { 345 std::swap(start, end);
790 ASSERT_TRUE(test_server()->Start()); 346
791 EnableInstant(); 347 EXPECT_EQ(ASCIIToUTF16("query suggestion"), text);
792 SetupInstantProvider("instant.html"); 348 EXPECT_EQ(ASCIIToUTF16(" suggestion"), text.substr(start, end - start));
793 349 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
794 // Initially navigate to the empty page which should be visible. 350 }
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
828 // rows initially, one for the browser process and one for tab's renderer.
829 TaskManagerModel* task_manager = TaskManager::GetInstance()->model();
830 task_manager->StartUpdating();
831 TaskManagerBrowserTestUtil::WaitForResourceChange(2);
832
833 ASSERT_TRUE(test_server()->Start());
834 EnableInstant();
835 SetupInstantProvider("instant.html");
836 DetermineInstantSupport();
837 SearchAndWaitForPreviewToShow();
838
839 // Now there should be three rows, the third being the instant preview.
840 TaskManagerBrowserTestUtil::WaitForResourceChange(3);
841 string16 prefix = l10n_util::GetStringFUTF16(
842 IDS_TASK_MANAGER_INSTANT_PREVIEW_PREFIX, string16());
843 string16 title = task_manager->GetResourceTitle(2);
844 EXPECT_TRUE(StartsWith(title, prefix, true)) << title << " vs " << prefix;
845 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/instant/instant_commit_type.h » ('j') | chrome/browser/instant/instant_controller.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698