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

Side by Side Diff: chrome/browser/ui/search/instant_extended_browsertest.cc

Issue 14235006: Rename Instant browser tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@storage_unit_test
Patch Set: Rebase to upstream to avoid AUTHORS conflict Created 7 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <sstream>
6
7 #include "base/prefs/pref_service.h"
8 #include "base/string_util.h"
9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/autocomplete/autocomplete_controller.h"
12 #include "chrome/browser/autocomplete/autocomplete_match.h"
13 #include "chrome/browser/autocomplete/autocomplete_provider.h"
14 #include "chrome/browser/autocomplete/autocomplete_result.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/bookmarks/bookmark_utils.h"
17 #include "chrome/browser/extensions/extension_browsertest.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/favicon/favicon_tab_helper.h"
20 #include "chrome/browser/history/history_types.h"
21 #include "chrome/browser/history/top_sites.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search/instant_service.h"
24 #include "chrome/browser/search/instant_service_factory.h"
25 #include "chrome/browser/search/search.h"
26 #include "chrome/browser/themes/theme_service.h"
27 #include "chrome/browser/themes/theme_service_factory.h"
28 #include "chrome/browser/ui/omnibox/omnibox_view.h"
29 #include "chrome/browser/ui/search/instant_commit_type.h"
30 #include "chrome/browser/ui/search/instant_ntp.h"
31 #include "chrome/browser/ui/search/instant_overlay.h"
32 #include "chrome/browser/ui/search/instant_tab.h"
33 #include "chrome/browser/ui/search/instant_test_utils.h"
34 #include "chrome/browser/ui/search/search_tab_helper.h"
35 #include "chrome/browser/ui/tabs/tab_strip_model.h"
36 #include "chrome/browser/ui/webui/theme_source.h"
37 #include "chrome/common/chrome_notification_types.h"
38 #include "chrome/common/pref_names.h"
39 #include "chrome/common/thumbnail_score.h"
40 #include "chrome/common/url_constants.h"
41 #include "chrome/test/base/in_process_browser_test.h"
42 #include "chrome/test/base/interactive_test_utils.h"
43 #include "chrome/test/base/ui_test_utils.h"
44 #include "content/public/browser/navigation_controller.h"
45 #include "content/public/browser/navigation_entry.h"
46 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/render_process_host.h"
48 #include "content/public/browser/render_view_host.h"
49 #include "content/public/browser/site_instance.h"
50 #include "content/public/browser/url_data_source.h"
51 #include "content/public/browser/web_contents.h"
52 #include "content/public/browser/web_contents_view.h"
53 #include "content/public/common/bindings_policy.h"
54 #include "content/public/test/browser_test_utils.h"
55 #include "third_party/skia/include/core/SkBitmap.h"
56
57 namespace {
58
59 // Creates a bitmap of the specified color. Caller takes ownership.
60 gfx::Image CreateBitmap(SkColor color) {
61 SkBitmap thumbnail;
62 thumbnail.setConfig(SkBitmap::kARGB_8888_Config, 4, 4);
63 thumbnail.allocPixels();
64 thumbnail.eraseColor(color);
65 return gfx::Image::CreateFrom1xBitmap(thumbnail); // adds ref.
66 }
67
68 } // namespace
69
70 class InstantExtendedTest : public InProcessBrowserTest,
71 public InstantTestBase {
72 public:
73 InstantExtendedTest()
74 : on_most_visited_change_calls_(0),
75 most_visited_items_count_(0),
76 first_most_visited_item_id_(0),
77 on_native_suggestions_calls_(0),
78 on_change_calls_(0),
79 submit_count_(0) {
80 }
81 protected:
82 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
83 chrome::EnableInstantExtendedAPIForTesting();
84 ASSERT_TRUE(https_test_server().Start());
85 GURL instant_url = https_test_server().GetURL(
86 "files/instant_extended.html?strk=1&");
87 InstantTestBase::Init(instant_url);
88 }
89
90 std::string GetOmniboxText() {
91 return UTF16ToUTF8(omnibox()->GetText());
92 }
93
94 void SendDownArrow() {
95 omnibox()->model()->OnUpOrDownKeyPressed(1);
96 // Wait for JavaScript to run the key handler by executing a blank script.
97 EXPECT_TRUE(ExecuteScript(std::string()));
98 }
99
100 void SendUpArrow() {
101 omnibox()->model()->OnUpOrDownKeyPressed(-1);
102 // Wait for JavaScript to run the key handler by executing a blank script.
103 EXPECT_TRUE(ExecuteScript(std::string()));
104 }
105
106 void SendEscape() {
107 omnibox()->model()->OnEscapeKeyPressed();
108 // Wait for JavaScript to run the key handler by executing a blank script.
109 EXPECT_TRUE(ExecuteScript(std::string()));
110 }
111
112 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT {
113 return GetIntFromJS(contents, "onMostVisitedChangedCalls",
114 &on_most_visited_change_calls_) &&
115 GetIntFromJS(contents, "mostVisitedItemsCount",
116 &most_visited_items_count_) &&
117 GetIntFromJS(contents, "firstMostVisitedItemId",
118 &first_most_visited_item_id_) &&
119 GetIntFromJS(contents, "onNativeSuggestionsCalls",
120 &on_native_suggestions_calls_) &&
121 GetIntFromJS(contents, "onChangeCalls",
122 &on_change_calls_) &&
123 GetIntFromJS(contents, "submitCount",
124 &submit_count_) &&
125 GetStringFromJS(contents, "apiHandle.value",
126 &query_value_);
127 }
128
129 int on_most_visited_change_calls_;
130 int most_visited_items_count_;
131 int first_most_visited_item_id_;
132 int on_native_suggestions_calls_;
133 int on_change_calls_;
134 int submit_count_;
135 std::string query_value_;
136 };
137
138 // Test class used to verify chrome-search: scheme and access policy from the
139 // Instant overlay. This is a subclass of |ExtensionBrowserTest| because it
140 // loads a theme that provides a background image.
141 class InstantPolicyTest : public ExtensionBrowserTest, public InstantTestBase {
142 public:
143 InstantPolicyTest() {}
144
145 protected:
146 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
147 chrome::EnableInstantExtendedAPIForTesting();
148 ASSERT_TRUE(https_test_server().Start());
149 GURL instant_url = https_test_server().GetURL(
150 "files/instant_extended.html?strk=1&");
151 InstantTestBase::Init(instant_url);
152 }
153
154 void InstallThemeSource() {
155 ThemeSource* theme = new ThemeSource(profile());
156 content::URLDataSource::Add(profile(), theme);
157 }
158
159 void InstallThemeAndVerify(const std::string& theme_dir,
160 const std::string& theme_name) {
161 const base::FilePath theme_path = test_data_dir_.AppendASCII(theme_dir);
162 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
163 theme_path, 1, ExtensionBrowserTest::browser()));
164 const extensions::Extension* theme =
165 ThemeServiceFactory::GetThemeForProfile(
166 ExtensionBrowserTest::browser()->profile());
167 ASSERT_NE(static_cast<extensions::Extension*>(NULL), theme);
168 ASSERT_EQ(theme->name(), theme_name);
169 }
170
171 private:
172 DISALLOW_COPY_AND_ASSIGN(InstantPolicyTest);
173 };
174
175 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, ExtendedModeIsOn) {
176 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
177 EXPECT_TRUE(instant()->extended_enabled_);
178 }
179
180 // Test that Instant is preloaded when the omnibox is focused.
181 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxFocusLoadsInstant) {
182 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
183
184 // Explicitly unfocus the omnibox.
185 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
186 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
187
188 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
189 EXPECT_FALSE(omnibox()->model()->has_focus());
190
191 // Delete any existing overlay.
192 instant()->overlay_.reset();
193 EXPECT_FALSE(instant()->GetOverlayContents());
194
195 // Refocus the omnibox. The InstantController should've preloaded Instant.
196 FocusOmniboxAndWaitForInstantExtendedSupport();
197
198 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
199 EXPECT_TRUE(omnibox()->model()->has_focus());
200
201 content::WebContents* overlay = instant()->GetOverlayContents();
202 EXPECT_TRUE(overlay);
203
204 // Check that the page supports Instant, but it isn't showing.
205 EXPECT_TRUE(instant()->overlay_->supports_instant());
206 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
207 EXPECT_TRUE(instant()->model()->mode().is_default());
208
209 // Adding a new tab shouldn't delete or recreate the overlay; otherwise,
210 // what's the point of preloading?
211 AddBlankTabAndShow(browser());
212 EXPECT_EQ(overlay, instant()->GetOverlayContents());
213
214 // Unfocusing and refocusing the omnibox should also preserve the overlay.
215 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
216 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
217
218 FocusOmnibox();
219 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
220 EXPECT_EQ(overlay, instant()->GetOverlayContents());
221 }
222
223 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, InputShowsOverlay) {
224 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
225
226 // Focus omnibox and confirm overlay isn't shown.
227 FocusOmniboxAndWaitForInstantExtendedSupport();
228 content::WebContents* overlay = instant()->GetOverlayContents();
229 EXPECT_TRUE(overlay);
230 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
231 EXPECT_TRUE(instant()->model()->mode().is_default());
232
233 // Typing in the omnibox should show the overlay.
234 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
235 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions());
236 EXPECT_EQ(overlay, instant()->GetOverlayContents());
237 }
238
239 // Test that middle clicking on a suggestion opens the result in a new tab.
240 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
241 MiddleClickOnSuggestionOpensInNewTab) {
242 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
243 FocusOmniboxAndWaitForInstantExtendedSupport();
244 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
245
246 EXPECT_EQ(1, browser()->tab_strip_model()->count());
247
248 // Typing in the omnibox should show the overlay.
249 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("http://www.example.com/"));
250
251 // Create an event listener that opens the top suggestion in a new tab.
252 EXPECT_TRUE(ExecuteScript(
253 "var rid = getApiHandle().nativeSuggestions[0].rid;"
254 "document.body.addEventListener('click', function() {"
255 "chrome.embeddedSearch.navigateContentWindow(rid, 2);"
256 "});"
257 ));
258
259 content::WindowedNotificationObserver observer(
260 chrome::NOTIFICATION_TAB_ADDED,
261 content::NotificationService::AllSources());
262
263 // Click to trigger the event listener.
264 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
265
266 // Wait for the new tab to be added.
267 observer.Wait();
268
269 // Check that the new tab URL is as expected.
270 content::WebContents* new_tab_contents =
271 browser()->tab_strip_model()->GetWebContentsAt(1);
272 EXPECT_EQ("http://www.example.com/", new_tab_contents->GetURL().spec());
273
274 // Check that there are now two tabs.
275 EXPECT_EQ(2, browser()->tab_strip_model()->count());
276 }
277
278 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
279 UnfocusingOmniboxDoesNotChangeSuggestions) {
280 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
281 FocusOmniboxAndWaitForInstantExtendedSupport();
282 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
283
284 // Get a committed tab to work with.
285 content::WebContents* instant_tab = instant()->GetOverlayContents();
286 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("committed"));
287 browser()->window()->GetLocationBar()->AcceptInput();
288
289 // Put focus back into the omnibox, type, and wait for some gray text.
290 EXPECT_TRUE(content::ExecuteScript(instant_tab,
291 "suggestion = 'santa claus';"));
292 SetOmniboxTextAndWaitForSuggestion("santa ");
293 EXPECT_EQ(ASCIIToUTF16("claus"), GetGrayText());
294 EXPECT_TRUE(content::ExecuteScript(instant_tab,
295 "onChangeCalls = onNativeSuggestionsCalls = 0;"));
296
297 // Now unfocus the omnibox.
298 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
299 EXPECT_TRUE(UpdateSearchState(instant_tab));
300 EXPECT_EQ(0, on_change_calls_);
301 EXPECT_EQ(0, on_native_suggestions_calls_);
302 }
303
304 // Test that omnibox text is correctly set when overlay is committed with Enter.
305 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxTextUponEnterCommit) {
306 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
307 FocusOmniboxAndWaitForInstantExtendedSupport();
308
309 // The page will autocomplete once we set the omnibox value.
310 EXPECT_TRUE(ExecuteScript("suggestion = 'santa claus';"));
311
312 // Set the text, and wait for suggestions to show up.
313 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("santa"));
314 EXPECT_EQ(ASCIIToUTF16("santa"), omnibox()->GetText());
315
316 // Test that the current suggestion is correctly set.
317 EXPECT_EQ(ASCIIToUTF16(" claus"), GetGrayText());
318
319 // Commit the search by pressing Enter.
320 browser()->window()->GetLocationBar()->AcceptInput();
321
322 // 'Enter' commits the query as it was typed.
323 EXPECT_EQ(ASCIIToUTF16("santa"), omnibox()->GetText());
324
325 // Suggestion should be cleared at this point.
326 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
327 }
328
329 // Test that omnibox text is correctly set when committed with focus lost.
330 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxTextUponFocusLostCommit) {
331 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
332 FocusOmniboxAndWaitForInstantExtendedSupport();
333
334 // Set autocomplete text (grey text).
335 EXPECT_TRUE(ExecuteScript("suggestion = 'johnny depp';"));
336
337 // Set the text, and wait for suggestions to show up.
338 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("johnny"));
339 EXPECT_EQ(ASCIIToUTF16("johnny"), omnibox()->GetText());
340
341 // Test that the current suggestion is correctly set.
342 EXPECT_EQ(ASCIIToUTF16(" depp"), GetGrayText());
343
344 // Commit the overlay by lost focus (e.g. clicking on the page).
345 instant()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST);
346
347 // Omnibox text and suggestion should not be changed.
348 EXPECT_EQ(ASCIIToUTF16("johnny"), omnibox()->GetText());
349 EXPECT_EQ(ASCIIToUTF16(" depp"), GetGrayText());
350 }
351
352 // Test that omnibox text is correctly set when clicking on committed SERP.
353 // Disabled on Mac because omnibox focus loss is not working correctly.
354 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
355 OmniboxTextUponFocusedCommittedSERP) {
356 // Setup Instant.
357 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
358 FocusOmniboxAndWaitForInstantExtendedSupport();
359
360 // Create an observer to wait for the instant tab to support Instant.
361 content::WindowedNotificationObserver observer(
362 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
363 content::NotificationService::AllSources());
364
365 // Do a search and commit it.
366 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("hello k"));
367 EXPECT_EQ(ASCIIToUTF16("hello k"), omnibox()->GetText());
368 browser()->window()->GetLocationBar()->AcceptInput();
369 observer.Wait();
370
371 // With a committed results page, do a search by unfocusing the omnibox and
372 // focusing the contents.
373 SetOmniboxText("hello");
374 // Calling handleOnChange manually to make sure it is called before the
375 // Focus() call below.
376 EXPECT_TRUE(content::ExecuteScript(instant()->instant_tab()->contents(),
377 "suggestion = 'hello kitty';"
378 "handleOnChange();"));
379 instant()->instant_tab()->contents()->GetView()->Focus();
380
381 // Omnibox text and suggestion should not be changed.
382 EXPECT_EQ(ASCIIToUTF16("hello"), omnibox()->GetText());
383 EXPECT_EQ(ASCIIToUTF16(" kitty"), GetGrayText());
384 }
385
386 // Checks that a previous Navigation suggestion is not re-used when a search
387 // suggestion comes in.
388 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
389 NavigationSuggestionIsDiscardedUponSearchSuggestion) {
390 // Setup Instant.
391 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
392 FocusOmniboxAndWaitForInstantExtendedSupport();
393
394 // Tell the page to send a URL suggestion.
395 EXPECT_TRUE(ExecuteScript("suggestion = 'http://www.example.com';"
396 "behavior = 1;"));
397 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("exa"));
398 EXPECT_EQ(ASCIIToUTF16("example.com"), omnibox()->GetText());
399
400 // Now send a search suggestion and see that Navigation suggestion is no
401 // longer kept.
402 EXPECT_TRUE(ExecuteScript("suggestion = 'exams are great';"
403 "behavior = 2;"));
404 SetOmniboxText("exam");
405 // Wait for JavaScript to run handleOnChange by executing a blank script.
406 EXPECT_TRUE(ExecuteScript(std::string()));
407
408 instant()->overlay()->contents()->GetView()->Focus();
409 EXPECT_EQ(ASCIIToUTF16("exam"), omnibox()->GetText());
410 EXPECT_EQ(ASCIIToUTF16("s are great"), GetGrayText());
411
412 // TODO(jered): Remove this after fixing OnBlur().
413 omnibox()->RevertAll();
414 }
415
416 // This test simulates a search provider using the InstantExtended API to
417 // navigate through the suggested results and back to the original user query.
418 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NavigateSuggestionsWithArrowKeys) {
419 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
420 FocusOmniboxAndWaitForInstantExtendedSupport();
421
422 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("hello"));
423 EXPECT_EQ("hello", GetOmniboxText());
424
425 SendDownArrow();
426 EXPECT_EQ("result 1", GetOmniboxText());
427 SendDownArrow();
428 EXPECT_EQ("result 2", GetOmniboxText());
429 SendUpArrow();
430 EXPECT_EQ("result 1", GetOmniboxText());
431 SendUpArrow();
432 EXPECT_EQ("hello", GetOmniboxText());
433
434 // Ensure that the API's value is set correctly.
435 std::string result;
436 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
437 "window.chrome.searchBox.value",
438 &result));
439 EXPECT_EQ("hello", result);
440
441 EXPECT_TRUE(HasUserInputInProgress());
442 // TODO(beaudoin): Figure out why this fails.
443 // EXPECT_FALSE(HasTemporaryText());
444
445 // Commit the search by pressing Enter.
446 browser()->window()->GetLocationBar()->AcceptInput();
447 EXPECT_EQ("hello", GetOmniboxText());
448 }
449
450 // Flaky on Linux Tests bot. See http://crbug.com/233090.
451 #if defined(OS_LINUX)
452 #define MAYBE_NavigateToURLSuggestionHitEnterAndLookForSubmit DISABLED_NavigateT oURLSuggestionHitEnterAndLookForSubmit
453 #else
454 #define MAYBE_NavigateToURLSuggestionHitEnterAndLookForSubmit NavigateToURLSugge stionHitEnterAndLookForSubmit
455 #endif
456
457 // This test simulates a search provider using the InstantExtended API to
458 // navigate through the suggested results and back to the original user query.
459 // If this test starts to flake, it may be that the second call to AcceptInput
460 // below causes instant()->instant_tab() to no longer be valid due to e.g. a
461 // navigation. In that case, see https://codereview.chromium.org/12895007/#msg28
462 // and onwards for possible alternatives.
463 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
464 MAYBE_NavigateToURLSuggestionHitEnterAndLookForSubmit) {
465 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
466 FocusOmniboxAndWaitForInstantExtendedSupport();
467
468 // Create an observer to wait for the instant tab to support Instant.
469 content::WindowedNotificationObserver observer(
470 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
471 content::NotificationService::AllSources());
472
473 // Do a search and commit it.
474 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("hello k"));
475 EXPECT_EQ(ASCIIToUTF16("hello k"), omnibox()->GetText());
476 browser()->window()->GetLocationBar()->AcceptInput();
477 observer.Wait();
478
479 SetOmniboxText("http");
480 EXPECT_EQ("http", GetOmniboxText());
481
482 SendDownArrow();
483 EXPECT_EQ("result 1", GetOmniboxText());
484 SendDownArrow();
485 EXPECT_EQ("result 2", GetOmniboxText());
486 SendDownArrow();
487 EXPECT_EQ("http://www.google.com", GetOmniboxText());
488
489 EXPECT_TRUE(HasUserInputInProgress());
490
491 EXPECT_TRUE(UpdateSearchState(instant()->instant_tab()->contents()));
492 // Note the commit count is initially 1 due to the AcceptInput() call above.
493 EXPECT_EQ(1, submit_count_);
494
495 std::string old_query_value(query_value_);
496
497 // Commit the search by pressing Enter.
498 browser()->window()->GetLocationBar()->AcceptInput();
499
500 // Make sure a submit message got sent.
501 EXPECT_TRUE(UpdateSearchState(instant()->instant_tab()->contents()));
502 EXPECT_EQ(2, submit_count_);
503 EXPECT_EQ(old_query_value, query_value_);
504 }
505
506 // This test simulates a search provider using the InstantExtended API to
507 // navigate through the suggested results and hitting escape to get back to the
508 // original user query.
509 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NavigateSuggestionsAndHitEscape) {
510 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
511 FocusOmniboxAndWaitForInstantExtendedSupport();
512
513 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("hello"));
514 EXPECT_EQ("hello", GetOmniboxText());
515
516 SendDownArrow();
517 EXPECT_EQ("result 1", GetOmniboxText());
518 SendDownArrow();
519 EXPECT_EQ("result 2", GetOmniboxText());
520 SendEscape();
521 EXPECT_EQ("hello", GetOmniboxText());
522
523 // Ensure that the API's value is set correctly.
524 std::string result;
525 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
526 "window.chrome.searchBox.value",
527 &result));
528 EXPECT_EQ("hello", result);
529
530 EXPECT_TRUE(HasUserInputInProgress());
531 EXPECT_FALSE(HasTemporaryText());
532
533 // Commit the search by pressing Enter.
534 browser()->window()->GetLocationBar()->AcceptInput();
535 EXPECT_EQ("hello", GetOmniboxText());
536 }
537
538 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, PressEscapeWithBlueText) {
539 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
540 FocusOmniboxAndWaitForInstantExtendedSupport();
541
542 // Set blue text completion.
543 EXPECT_TRUE(ExecuteScript("suggestion = 'chimichanga.com';"
544 "behavior = 1;"));
545
546 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("chimi"));
547
548 EXPECT_EQ(ASCIIToUTF16("chimichanga.com"), omnibox()->GetText());
549 EXPECT_EQ(ASCIIToUTF16("changa.com"), GetBlueText());
550 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
551
552 EXPECT_TRUE(ExecuteScript("onChangeCalls = onNativeSuggestionsCalls = 0;"));
553
554 SendDownArrow();
555
556 EXPECT_EQ(ASCIIToUTF16("result 1"), omnibox()->GetText());
557 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText());
558 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
559
560 content::WindowedNotificationObserver observer(
561 chrome::NOTIFICATION_INSTANT_SET_SUGGESTION,
562 content::NotificationService::AllSources());
563 SendEscape();
564 observer.Wait();
565
566 EXPECT_EQ(ASCIIToUTF16("chimichanga.com"), omnibox()->GetText());
567 EXPECT_EQ(ASCIIToUTF16("changa.com"), GetBlueText());
568 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
569
570 EXPECT_TRUE(UpdateSearchState(instant()->GetOverlayContents()));
571 EXPECT_EQ(0, on_native_suggestions_calls_);
572 EXPECT_EQ(0, on_change_calls_);
573 }
574
575 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, PressEscapeWithGrayText) {
576 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
577 FocusOmniboxAndWaitForInstantExtendedSupport();
578
579 // Set gray text completion.
580 EXPECT_TRUE(ExecuteScript("suggestion = 'cowabunga';"
581 "behavior = 2;"));
582
583 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("cowa"));
584
585 EXPECT_EQ(ASCIIToUTF16("cowa"), omnibox()->GetText());
586 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText());
587 EXPECT_EQ(ASCIIToUTF16("bunga"), GetGrayText());
588
589 EXPECT_TRUE(ExecuteScript("onChangeCalls = onNativeSuggestionsCalls = 0;"));
590
591 SendDownArrow();
592
593 EXPECT_EQ(ASCIIToUTF16("result 1"), omnibox()->GetText());
594 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText());
595 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
596
597 content::WindowedNotificationObserver observer(
598 chrome::NOTIFICATION_INSTANT_SET_SUGGESTION,
599 content::NotificationService::AllSources());
600 SendEscape();
601 observer.Wait();
602
603 EXPECT_EQ(ASCIIToUTF16("cowa"), omnibox()->GetText());
604 EXPECT_EQ(ASCIIToUTF16(""), GetBlueText());
605 EXPECT_EQ(ASCIIToUTF16("bunga"), GetGrayText());
606
607 EXPECT_TRUE(UpdateSearchState(instant()->GetOverlayContents()));
608 EXPECT_EQ(0, on_native_suggestions_calls_);
609 EXPECT_EQ(0, on_change_calls_);
610 }
611
612 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NTPIsPreloaded) {
613 // Setup Instant.
614 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
615 FocusOmniboxAndWaitForInstantExtendedSupport();
616
617 // NTP contents should be preloaded.
618 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
619 content::WebContents* ntp_contents = instant()->ntp_->contents();
620 EXPECT_TRUE(ntp_contents);
621 }
622
623 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, PreloadedNTPIsUsedInNewTab) {
624 // Setup Instant.
625 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
626 FocusOmniboxAndWaitForInstantExtendedSupport();
627
628 // NTP contents should be preloaded.
629 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
630 content::WebContents* ntp_contents = instant()->ntp_->contents();
631 EXPECT_TRUE(ntp_contents);
632
633 // Open new tab. Preloaded NTP contents should have been used.
634 ui_test_utils::NavigateToURLWithDisposition(
635 browser(),
636 GURL(chrome::kChromeUINewTabURL),
637 NEW_FOREGROUND_TAB,
638 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
639 content::WebContents* active_tab =
640 browser()->tab_strip_model()->GetActiveWebContents();
641 EXPECT_EQ(ntp_contents, active_tab);
642 EXPECT_TRUE(chrome::IsInstantNTP(active_tab));
643 }
644
645 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, PreloadedNTPIsUsedInSameTab) {
646 // Setup Instant.
647 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
648 FocusOmniboxAndWaitForInstantExtendedSupport();
649
650 // NTP contents should be preloaded.
651 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
652 content::WebContents* ntp_contents = instant()->ntp_->contents();
653 EXPECT_TRUE(ntp_contents);
654
655 // Open new tab. Preloaded NTP contents should have been used.
656 ui_test_utils::NavigateToURLWithDisposition(
657 browser(),
658 GURL(chrome::kChromeUINewTabURL),
659 CURRENT_TAB,
660 ui_test_utils::BROWSER_TEST_NONE);
661 content::WebContents* active_tab =
662 browser()->tab_strip_model()->GetActiveWebContents();
663 EXPECT_EQ(ntp_contents, active_tab);
664 EXPECT_TRUE(chrome::IsInstantNTP(active_tab));
665 }
666
667 // TODO(samarth): re-enable when fixing the infinite reload on shutdown.
668 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
669 DISABLED_PreloadedNTPForWrongProvider) {
670 // Setup Instant.
671 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
672 FocusOmniboxAndWaitForInstantExtendedSupport();
673
674 // NTP contents should be preloaded.
675 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
676 content::WebContents* ntp_contents = instant()->ntp_->contents();
677 EXPECT_TRUE(ntp_contents);
678 GURL ntp_url = ntp_contents->GetURL();
679
680 // Change providers.
681 SetInstantURL("chrome://blank");
682
683 // Open new tab. Preloaded NTP contents should have not been used.
684 ui_test_utils::NavigateToURLWithDisposition(
685 browser(),
686 GURL(chrome::kChromeUINewTabURL),
687 NEW_FOREGROUND_TAB,
688 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
689 content::WebContents* active_tab =
690 browser()->tab_strip_model()->GetActiveWebContents();
691 EXPECT_NE(ntp_url, active_tab->GetURL());
692 }
693
694 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxHasFocusOnNewTab) {
695 // Setup Instant.
696 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
697 FocusOmniboxAndWaitForInstantExtendedSupport();
698
699 // Explicitly unfocus the omnibox.
700 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
701 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
702 EXPECT_FALSE(omnibox()->model()->has_focus());
703
704 // Open new tab. Preloaded NTP contents should have been used.
705 ui_test_utils::NavigateToURLWithDisposition(
706 browser(),
707 GURL(chrome::kChromeUINewTabURL),
708 NEW_FOREGROUND_TAB,
709 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
710
711 // Omnibox should have focus.
712 EXPECT_TRUE(omnibox()->model()->has_focus());
713 }
714
715 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxEmptyOnNewTabPage) {
716 // Setup Instant.
717 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
718 FocusOmniboxAndWaitForInstantExtendedSupport();
719
720 // Open new tab. Preloaded NTP contents should have been used.
721 ui_test_utils::NavigateToURLWithDisposition(
722 browser(),
723 GURL(chrome::kChromeUINewTabURL),
724 CURRENT_TAB,
725 ui_test_utils::BROWSER_TEST_NONE);
726
727 // Omnibox should be empty.
728 EXPECT_TRUE(omnibox()->GetText().empty());
729 }
730
731 // TODO(dhollowa): Fix flakes. http://crbug.com/179930.
732 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_NoFaviconOnNewTabPage) {
733 // Setup Instant.
734 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
735 FocusOmniboxAndWaitForInstantExtendedSupport();
736
737 // Open new tab. Preloaded NTP contents should have been used.
738 ui_test_utils::NavigateToURLWithDisposition(
739 browser(),
740 GURL(chrome::kChromeUINewTabURL),
741 CURRENT_TAB,
742 ui_test_utils::BROWSER_TEST_NONE);
743
744 // No favicon should be shown.
745 content::WebContents* active_tab =
746 browser()->tab_strip_model()->GetActiveWebContents();
747 FaviconTabHelper* favicon_tab_helper =
748 FaviconTabHelper::FromWebContents(active_tab);
749 EXPECT_FALSE(favicon_tab_helper->ShouldDisplayFavicon());
750
751 // Favicon should be shown off the NTP.
752 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
753 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
754 favicon_tab_helper = FaviconTabHelper::FromWebContents(active_tab);
755 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon());
756 }
757
758 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, InputOnNTPDoesntShowOverlay) {
759 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
760
761 // Focus omnibox and confirm overlay isn't shown.
762 FocusOmniboxAndWaitForInstantExtendedSupport();
763 content::WebContents* overlay = instant()->GetOverlayContents();
764 EXPECT_TRUE(overlay);
765 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
766 EXPECT_TRUE(instant()->model()->mode().is_default());
767
768 // Navigate to the NTP.
769 ui_test_utils::NavigateToURLWithDisposition(
770 browser(),
771 GURL(chrome::kChromeUINewTabURL),
772 CURRENT_TAB,
773 ui_test_utils::BROWSER_TEST_NONE);
774
775 // Typing in the omnibox should not show the overlay.
776 SetOmniboxText("query");
777 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
778 EXPECT_TRUE(instant()->model()->mode().is_default());
779 }
780
781 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, ProcessIsolation) {
782 // Prior to setup, Instant has an overlay with a failed "google.com" load in
783 // it, which is rendered in the dedicated Instant renderer process.
784 //
785 // TODO(sreeram): Fix this up when we stop doing crazy things on init.
786 InstantService* instant_service =
787 InstantServiceFactory::GetForProfile(browser()->profile());
788 ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
789 EXPECT_EQ(1, instant_service->GetInstantProcessCount());
790
791 // Setup Instant.
792 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
793 FocusOmniboxAndWaitForInstantExtendedSupport();
794
795 // The registered Instant render process should still exist.
796 EXPECT_EQ(1, instant_service->GetInstantProcessCount());
797
798 // And the Instant overlay and ntp should live inside it.
799 content::WebContents* overlay = instant()->GetOverlayContents();
800 EXPECT_TRUE(instant_service->IsInstantProcess(
801 overlay->GetRenderProcessHost()->GetID()));
802 content::WebContents* ntp_contents = instant()->ntp_->contents();
803 EXPECT_TRUE(instant_service->IsInstantProcess(
804 ntp_contents->GetRenderProcessHost()->GetID()));
805
806 // Navigating to the NTP should use the Instant render process.
807 ui_test_utils::NavigateToURLWithDisposition(
808 browser(),
809 GURL(chrome::kChromeUINewTabURL),
810 CURRENT_TAB,
811 ui_test_utils::BROWSER_TEST_NONE);
812 content::WebContents* active_tab =
813 browser()->tab_strip_model()->GetActiveWebContents();
814 EXPECT_TRUE(instant_service->IsInstantProcess(
815 active_tab->GetRenderProcessHost()->GetID()));
816
817 // Navigating elsewhere should not use the Instant render process.
818 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
819 EXPECT_FALSE(instant_service->IsInstantProcess(
820 active_tab->GetRenderProcessHost()->GetID()));
821 }
822
823 // Verification of fix for BUG=176365. Ensure that each Instant WebContents in
824 // a tab uses a new BrowsingInstance, to avoid conflicts in the
825 // NavigationController.
826 // Flaky: http://crbug.com/177516
827 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_UnrelatedSiteInstance) {
828 // Setup Instant.
829 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
830 FocusOmniboxAndWaitForInstantExtendedSupport();
831
832 // Check that the uncommited ntp page and uncommited overlay have unrelated
833 // site instances.
834 // TODO(sreeram): |ntp_| is going away, so this check can be removed in the
835 // future.
836 content::WebContents* overlay = instant()->GetOverlayContents();
837 content::WebContents* ntp_contents = instant()->ntp_->contents();
838 EXPECT_FALSE(overlay->GetSiteInstance()->IsRelatedSiteInstance(
839 ntp_contents->GetSiteInstance()));
840
841 // Type a query and hit enter to get a results page. The overlay becomes the
842 // active tab.
843 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("hello"));
844 EXPECT_EQ("hello", GetOmniboxText());
845 browser()->window()->GetLocationBar()->AcceptInput();
846 content::WebContents* first_active_tab =
847 browser()->tab_strip_model()->GetActiveWebContents();
848 EXPECT_EQ(first_active_tab, overlay);
849 scoped_refptr<content::SiteInstance> first_site_instance =
850 first_active_tab->GetSiteInstance();
851 EXPECT_FALSE(first_site_instance->IsRelatedSiteInstance(
852 ntp_contents->GetSiteInstance()));
853
854 // Navigating elsewhere gets us off of the commited page. The next
855 // query will give us a new |overlay| which we will then commit.
856 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
857
858 // Show and commit the new overlay.
859 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("hello again"));
860 EXPECT_EQ("hello again", GetOmniboxText());
861 browser()->window()->GetLocationBar()->AcceptInput();
862 content::WebContents* second_active_tab =
863 browser()->tab_strip_model()->GetActiveWebContents();
864 EXPECT_NE(first_active_tab, second_active_tab);
865 scoped_refptr<content::SiteInstance> second_site_instance =
866 second_active_tab->GetSiteInstance();
867 EXPECT_NE(first_site_instance, second_site_instance);
868 EXPECT_FALSE(first_site_instance->IsRelatedSiteInstance(
869 second_site_instance));
870 }
871
872 // Tests that suggestions are sanity checked.
873 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, ValidatesSuggestions) {
874 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
875 FocusOmniboxAndWaitForInstantExtendedSupport();
876
877 // Do not set gray text that is not a suffix of the query.
878 EXPECT_TRUE(ExecuteScript("suggestion = 'potato';"
879 "behavior = 2;"));
880 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
881 EXPECT_EQ(ASCIIToUTF16("query"), omnibox()->GetText());
882 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
883
884 omnibox()->RevertAll();
885
886 // Do not set blue text that is not a valid URL completion.
887 EXPECT_TRUE(ExecuteScript("suggestion = 'this is not a url!';"
888 "behavior = 1;"));
889 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("this is"));
890 EXPECT_EQ(ASCIIToUTF16("this is"), omnibox()->GetText());
891 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
892
893 omnibox()->RevertAll();
894
895 // Do not set gray text when blue text is already set.
896 // First set up some blue text completion.
897 EXPECT_TRUE(ExecuteScript("suggestion = 'www.example.com';"
898 "behavior = 1;"));
899 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("http://www.ex"));
900 EXPECT_EQ(ASCIIToUTF16("http://www.example.com"), omnibox()->GetText());
901 EXPECT_EQ(ASCIIToUTF16("ample.com"), GetBlueText());
902
903 // Now try to set gray text for the same query.
904 EXPECT_TRUE(ExecuteScript("suggestion = 'www.example.com rocks';"
905 "behavior = 2;"));
906 SetOmniboxText("http://www.ex");
907 EXPECT_EQ(ASCIIToUTF16("http://www.example.com"), omnibox()->GetText());
908 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
909
910 omnibox()->RevertAll();
911
912 // Ignore an out-of-date blue text suggestion. (Simulates a laggy
913 // SetSuggestion IPC by directly calling into InstantController.)
914 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("http://www.example.com/"));
915 instant()->SetSuggestions(
916 instant()->overlay()->contents(),
917 std::vector<InstantSuggestion>(
918 1,
919 InstantSuggestion(ASCIIToUTF16("www.exa"),
920 INSTANT_COMPLETE_NOW,
921 INSTANT_SUGGESTION_URL,
922 ASCIIToUTF16("www.exa"))));
923 EXPECT_EQ(
924 "http://www.example.com/",
925 omnibox()->model()->result().default_match()->destination_url.spec());
926
927 omnibox()->RevertAll();
928
929 // TODO(samarth): uncomment after fixing crbug.com/191656.
930 // Use an out-of-date blue text suggestion, if the text typed by the user is
931 // contained in the suggestion.
932 // ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("ex"));
933 // instant()->SetSuggestions(
934 // instant()->overlay()->contents(),
935 // std::vector<InstantSuggestion>(
936 // 1,
937 // InstantSuggestion(ASCIIToUTF16("www.example.com"),
938 // INSTANT_COMPLETE_NOW,
939 // INSTANT_SUGGESTION_URL,
940 // ASCIIToUTF16("e"))));
941 // EXPECT_EQ(
942 // "http://www.example.com/",
943 // omnibox()->model()->result().default_match()->destination_url.spec());
944
945 // omnibox()->RevertAll();
946
947 // When asked to suggest blue text in verbatim mode, suggest the exact
948 // omnibox text rather than using the supplied suggestion text.
949 EXPECT_TRUE(ExecuteScript("suggestion = 'www.example.com/q';"
950 "behavior = 1;"));
951 SetOmniboxText("www.example.com/q");
952 omnibox()->OnBeforePossibleChange();
953 SetOmniboxText("www.example.com/");
954 omnibox()->OnAfterPossibleChange();
955 EXPECT_EQ(ASCIIToUTF16("www.example.com/"), omnibox()->GetText());
956 }
957
958 // Tests that a previous navigation suggestion is not discarded if it's not
959 // stale.
960 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
961 NavigationSuggestionIsNotDiscarded) {
962 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
963 FocusOmniboxAndWaitForInstantExtendedSupport();
964
965 // Tell the page to send a URL suggestion.
966 EXPECT_TRUE(ExecuteScript("suggestion = 'http://www.example.com';"
967 "behavior = 1;"));
968 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("exa"));
969 EXPECT_EQ(ASCIIToUTF16("example.com"), omnibox()->GetText());
970 SetOmniboxText("exam");
971 EXPECT_EQ(ASCIIToUTF16("example.com"), omnibox()->GetText());
972
973 // TODO(jered): Remove this after fixing OnBlur().
974 omnibox()->RevertAll();
975 }
976
977 // TODO(dhollowa): Fix flakes. http://crbug.com/179930.
978 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_MostVisited) {
979 content::WindowedNotificationObserver observer(
980 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
981 content::NotificationService::AllSources());
982 // Initialize Instant.
983 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
984 FocusOmniboxAndWaitForInstantExtendedSupport();
985
986 // Get a handle to the NTP and the current state of the JS.
987 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
988 content::WebContents* overlay = instant()->ntp_->contents();
989 EXPECT_TRUE(overlay);
990 EXPECT_TRUE(UpdateSearchState(overlay));
991
992 // Wait for most visited data to be ready, if necessary.
993 if (on_most_visited_change_calls_ == 0) {
994 observer.Wait();
995 EXPECT_TRUE(UpdateSearchState(overlay));
996 }
997
998 EXPECT_EQ(1, on_most_visited_change_calls_);
999
1000 // Make sure we have at least two Most Visited Items and save that number.
1001 // TODO(pedrosimonetti): For now, we're relying on the fact that the Top
1002 // Sites will have at lease two items in it. The correct approach would be
1003 // adding those items to the Top Sites manually before starting the test.
1004 EXPECT_GT(most_visited_items_count_, 1);
1005 int old_most_visited_items_count = most_visited_items_count_;
1006
1007 // Delete the fist Most Visited Item.
1008 int rid = first_most_visited_item_id_;
1009 std::ostringstream stream;
1010 stream << "newTabPageHandle.deleteMostVisitedItem(" << rid << ");";
1011 EXPECT_TRUE(ExecuteScript(stream.str()));
1012 observer.Wait();
1013
1014 // Update Most Visited state.
1015 EXPECT_TRUE(UpdateSearchState(overlay));
1016
1017 // Make sure we have one less item in there.
1018 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count - 1);
1019
1020 // Undo the deletion of the fist Most Visited Item.
1021 stream.str(std::string());
1022 stream << "newTabPageHandle.undoMostVisitedDeletion(" << rid << ");";
1023 EXPECT_TRUE(ExecuteScript(stream.str()));
1024 observer.Wait();
1025
1026 // Update Most Visited state.
1027 EXPECT_TRUE(UpdateSearchState(overlay));
1028
1029 // Make sure we have the same number of items as before.
1030 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count);
1031
1032 // Delete the fist Most Visited Item.
1033 rid = first_most_visited_item_id_;
1034 stream.str(std::string());
1035 stream << "newTabPageHandle.deleteMostVisitedItem(" << rid << ");";
1036 EXPECT_TRUE(ExecuteScript(stream.str()));
1037 observer.Wait();
1038
1039 // Update Most Visited state.
1040 EXPECT_TRUE(UpdateSearchState(overlay));
1041
1042 // Delete the second Most Visited Item.
1043 rid = first_most_visited_item_id_;
1044 stream.str(std::string());
1045 stream << "newTabPageHandle.deleteMostVisitedItem(" << rid << ");";
1046 EXPECT_TRUE(ExecuteScript(stream.str()));
1047 observer.Wait();
1048
1049 // Update Most Visited state.
1050 EXPECT_TRUE(UpdateSearchState(overlay));
1051
1052 // Make sure we have two less items in there.
1053 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count - 2);
1054
1055 // Delete the second Most Visited Item.
1056 stream.str(std::string());
1057 stream << "newTabPageHandle.undoAllMostVisitedDeletions();";
1058 EXPECT_TRUE(ExecuteScript(stream.str()));
1059 observer.Wait();
1060
1061 // Update Most Visited state.
1062 EXPECT_TRUE(UpdateSearchState(overlay));
1063
1064 // Make sure we have the same number of items as before.
1065 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count);
1066 }
1067
1068 IN_PROC_BROWSER_TEST_F(InstantPolicyTest, ThemeBackgroundAccess) {
1069 InstallThemeSource();
1070 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
1071 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1072 FocusOmniboxAndWaitForInstantExtendedSupport();
1073
1074 // The "Instant" New Tab should have access to chrome-search: scheme but not
1075 // chrome: scheme.
1076 ui_test_utils::NavigateToURLWithDisposition(
1077 browser(),
1078 GURL(chrome::kChromeUINewTabURL),
1079 NEW_FOREGROUND_TAB,
1080 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
1081
1082 content::RenderViewHost* rvh =
1083 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
1084
1085 const std::string chrome_url("chrome://theme/IDR_THEME_NTP_BACKGROUND");
1086 const std::string search_url(
1087 "chrome-search://theme/IDR_THEME_NTP_BACKGROUND");
1088 bool loaded = false;
1089 ASSERT_TRUE(LoadImage(rvh, chrome_url, &loaded));
1090 EXPECT_FALSE(loaded) << chrome_url;
1091 ASSERT_TRUE(LoadImage(rvh, search_url, &loaded));
1092 EXPECT_TRUE(loaded) << search_url;
1093 }
1094
1095 // TODO(dhollowa): Fix flakes. http://crbug.com/179930.
1096 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_FaviconAccess) {
1097 // Create a favicon.
1098 history::TopSites* top_sites = browser()->profile()->GetTopSites();
1099 GURL url("http://www.google.com/foo.html");
1100 gfx::Image thumbnail(CreateBitmap(SK_ColorWHITE));
1101 ThumbnailScore high_score(0.0, true, true, base::Time::Now());
1102 EXPECT_TRUE(top_sites->SetPageThumbnail(url, thumbnail, high_score));
1103
1104 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1105 FocusOmniboxAndWaitForInstantExtendedSupport();
1106
1107 // The "Instant" New Tab should have access to chrome-search: scheme but not
1108 // chrome: scheme.
1109 ui_test_utils::NavigateToURLWithDisposition(
1110 browser(),
1111 GURL(chrome::kChromeUINewTabURL),
1112 NEW_FOREGROUND_TAB,
1113 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
1114
1115 content::RenderViewHost* rvh =
1116 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
1117
1118 // Get the favicons.
1119 const std::string chrome_favicon_url(
1120 "chrome://favicon/largest/http://www.google.com/foo.html");
1121 const std::string search_favicon_url(
1122 "chrome-search://favicon/largest/http://www.google.com/foo.html");
1123 bool loaded = false;
1124 ASSERT_TRUE(LoadImage(rvh, chrome_favicon_url, &loaded));
1125 EXPECT_FALSE(loaded) << chrome_favicon_url;
1126 ASSERT_TRUE(LoadImage(rvh, search_favicon_url, &loaded));
1127 EXPECT_TRUE(loaded) << search_favicon_url;
1128 }
1129
1130 // WebUIBindings should never be enabled on ANY Instant web contents.
1131 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoWebUIBindingsOnNTP) {
1132 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1133 FocusOmniboxAndWaitForInstantExtendedSupport();
1134
1135 ui_test_utils::NavigateToURLWithDisposition(
1136 browser(),
1137 GURL(chrome::kChromeUINewTabURL),
1138 NEW_FOREGROUND_TAB,
1139 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
1140 const content::WebContents* tab =
1141 browser()->tab_strip_model()->GetActiveWebContents();
1142
1143 // Instant-provided NTP should not have any bindings enabled.
1144 EXPECT_EQ(0, tab->GetRenderViewHost()->GetEnabledBindings());
1145 }
1146
1147 // WebUIBindings should never be enabled on ANY Instant web contents.
1148 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoWebUIBindingsOnPreview) {
1149 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1150 FocusOmniboxAndWaitForInstantExtendedSupport();
1151
1152 // Typing in the omnibox shows the overlay.
1153 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
1154 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions());
1155 content::WebContents* preview = instant()->GetOverlayContents();
1156 ASSERT_NE(static_cast<content::WebContents*>(NULL), preview);
1157
1158 // Instant preview should not have any bindings enabled.
1159 EXPECT_EQ(0, preview->GetRenderViewHost()->GetEnabledBindings());
1160 }
1161
1162 // WebUIBindings should never be enabled on ANY Instant web contents.
1163 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoWebUIBindingsOnResults) {
1164 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1165 FocusOmniboxAndWaitForInstantExtendedSupport();
1166
1167 // Typing in the omnibox shows the overlay.
1168 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
1169 content::WebContents* preview = instant()->GetOverlayContents();
1170 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions());
1171 // Commit the search by pressing Enter.
1172 browser()->window()->GetLocationBar()->AcceptInput();
1173 EXPECT_TRUE(instant()->model()->mode().is_default());
1174 const content::WebContents* tab =
1175 browser()->tab_strip_model()->GetActiveWebContents();
1176 EXPECT_EQ(preview, tab);
1177
1178 // The commited Instant page should not have any bindings enabled.
1179 EXPECT_EQ(0, tab->GetRenderViewHost()->GetEnabledBindings());
1180 }
1181
1182 // Only implemented in Views and Mac currently: http://crbug.com/164723
1183 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1184 #define MAYBE_HomeButtonAffectsMargin HomeButtonAffectsMargin
1185 #else
1186 #define MAYBE_HomeButtonAffectsMargin DISABLED_HomeButtonAffectsMargin
1187 #endif
1188 // Check that toggling the state of the home button changes the start-edge
1189 // margin and width.
1190 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, MAYBE_HomeButtonAffectsMargin) {
1191 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1192
1193 // Get the current value of the start-edge margin and width.
1194 int start_margin;
1195 int width;
1196 content::WebContents* overlay = instant()->GetOverlayContents();
1197 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.startMargin",
1198 &start_margin));
1199 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.width", &width));
1200
1201 // Toggle the home button visibility pref.
1202 PrefService* profile_prefs = browser()->profile()->GetPrefs();
1203 bool show_home = profile_prefs->GetBoolean(prefs::kShowHomeButton);
1204 profile_prefs->SetBoolean(prefs::kShowHomeButton, !show_home);
1205
1206 // Make sure the margin and width changed.
1207 int new_start_margin;
1208 int new_width;
1209 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.startMargin",
1210 &new_start_margin));
1211 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.width", &new_width));
1212 EXPECT_NE(start_margin, new_start_margin);
1213 EXPECT_NE(width, new_width);
1214 EXPECT_EQ(new_width - width, start_margin - new_start_margin);
1215 }
1216
1217 // Commit does not happen on Mac: http://crbug.com/178520
1218 #if defined(OS_MACOSX)
1219 #define MAYBE_CommitWhenFocusLostInFullHeight \
1220 DISABLED_CommitWhenFocusLostInFullHeight
1221 #else
1222 #define MAYBE_CommitWhenFocusLostInFullHeight CommitWhenFocusLostInFullHeight
1223 #endif
1224 // Test that the overlay is committed when the omnibox loses focus when it is
1225 // shown at 100% height.
1226 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
1227 MAYBE_CommitWhenFocusLostInFullHeight) {
1228 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1229
1230 // Focus omnibox and confirm overlay isn't shown.
1231 FocusOmniboxAndWaitForInstantExtendedSupport();
1232 content::WebContents* overlay = instant()->GetOverlayContents();
1233 EXPECT_TRUE(overlay);
1234 EXPECT_TRUE(instant()->model()->mode().is_default());
1235 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1236
1237 // Typing in the omnibox should show the overlay.
1238 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
1239 EXPECT_TRUE(instant()->IsOverlayingSearchResults());
1240 EXPECT_EQ(overlay, instant()->GetOverlayContents());
1241
1242 // Explicitly unfocus the omnibox without triggering a click. Note that this
1243 // doesn't actually change the focus state of the omnibox, only what the
1244 // Instant controller sees it as.
1245 omnibox()->model()->OnWillKillFocus(NULL);
1246 omnibox()->model()->OnKillFocus();
1247
1248 // Confirm that the overlay has been committed.
1249 content::WebContents* active_tab =
1250 browser()->tab_strip_model()->GetActiveWebContents();
1251 EXPECT_EQ(overlay, active_tab);
1252 }
1253
1254 #if defined(OS_MACOSX)
1255 // http://crbug.com/227076
1256 #define MAYBE_CommitWhenShownInFullHeightWithoutFocus \
1257 DISABLED_CommitWhenShownInFullHeightWithoutFocus
1258 #else
1259 #define MAYBE_CommitWhenShownInFullHeightWithoutFocus \
1260 CommitWhenShownInFullHeightWithoutFocus
1261 #endif
1262
1263 // Test that the overlay is committed when shown at 100% height without focus
1264 // in the omnibox.
1265 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
1266 MAYBE_CommitWhenShownInFullHeightWithoutFocus) {
1267 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1268
1269 // Focus omnibox and confirm overlay isn't shown.
1270 FocusOmniboxAndWaitForInstantExtendedSupport();
1271 content::WebContents* overlay = instant()->GetOverlayContents();
1272 EXPECT_TRUE(overlay);
1273 EXPECT_TRUE(instant()->model()->mode().is_default());
1274 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1275
1276 // Create an observer to wait for the commit.
1277 content::WindowedNotificationObserver commit_observer(
1278 chrome::NOTIFICATION_INSTANT_COMMITTED,
1279 content::NotificationService::AllSources());
1280
1281 // Typing in the omnibox should show the overlay. Don't wait for the overlay
1282 // to show however.
1283 SetOmniboxText("query");
1284
1285 // The autocomplete controller isn't done until all the providers are done.
1286 // Though we don't care about the SearchProvider when we send autocomplete
1287 // results to the page, we do need to cause it to be "done" to make this test
1288 // work. Setting the suggestion calls SearchProvider::FinalizeInstantQuery(),
1289 // thus causing it to be done.
1290 omnibox()->model()->SetInstantSuggestion(
1291 InstantSuggestion(ASCIIToUTF16("query"),
1292 INSTANT_COMPLETE_NOW,
1293 INSTANT_SUGGESTION_SEARCH,
1294 ASCIIToUTF16("query")));
1295 while (!omnibox()->model()->autocomplete_controller()->done()) {
1296 content::WindowedNotificationObserver autocomplete_observer(
1297 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
1298 content::NotificationService::AllSources());
1299 autocomplete_observer.Wait();
1300 }
1301
1302 // Explicitly unfocus the omnibox without triggering a click. Note that this
1303 // doesn't actually change the focus state of the omnibox, only what the
1304 // Instant controller sees it as.
1305 omnibox()->model()->OnWillKillFocus(NULL);
1306 omnibox()->model()->OnKillFocus();
1307
1308 // Wait for the overlay to show.
1309 commit_observer.Wait();
1310
1311 // Confirm that the overlay has been committed.
1312 content::WebContents* active_tab =
1313 browser()->tab_strip_model()->GetActiveWebContents();
1314 EXPECT_EQ(overlay, active_tab);
1315 }
1316
1317 // Test that a transient entry is set properly when the overlay is committed
1318 // without a navigation.
1319 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, TransientEntrySet) {
1320 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1321
1322 // Focus omnibox and confirm overlay isn't shown.
1323 FocusOmniboxAndWaitForInstantSupport();
1324 content::WebContents* overlay = instant()->GetOverlayContents();
1325 EXPECT_TRUE(overlay);
1326 EXPECT_TRUE(instant()->model()->mode().is_default());
1327 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1328
1329 // Commit the overlay without triggering a navigation.
1330 content::WindowedNotificationObserver observer(
1331 chrome::NOTIFICATION_INSTANT_COMMITTED,
1332 content::NotificationService::AllSources());
1333 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
1334 browser()->window()->GetLocationBar()->AcceptInput();
1335 observer.Wait();
1336
1337 // Confirm that the overlay has been committed.
1338 content::WebContents* active_tab =
1339 browser()->tab_strip_model()->GetActiveWebContents();
1340 EXPECT_EQ(overlay, active_tab);
1341
1342 // The page hasn't navigated so there should be a transient entry with the
1343 // same URL but different page ID as the last committed entry.
1344 const content::NavigationEntry* transient_entry =
1345 active_tab->GetController().GetTransientEntry();
1346 const content::NavigationEntry* committed_entry =
1347 active_tab->GetController().GetLastCommittedEntry();
1348 EXPECT_EQ(transient_entry->GetURL(), committed_entry->GetURL());
1349 EXPECT_NE(transient_entry->GetPageID(), committed_entry->GetPageID());
1350 }
1351
1352 // Test that the a transient entry is cleared when the overlay is committed
1353 // with a navigation.
1354 // TODO(samarth) : this test fails, http://crbug.com/181070.
1355 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_TransientEntryRemoved) {
1356 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1357
1358 // Focus omnibox and confirm overlay isn't shown.
1359 FocusOmniboxAndWaitForInstantSupport();
1360 content::WebContents* overlay = instant()->GetOverlayContents();
1361 EXPECT_TRUE(overlay);
1362 EXPECT_TRUE(instant()->model()->mode().is_default());
1363 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1364
1365 // Create an observer to wait for the commit.
1366 content::WindowedNotificationObserver observer(
1367 chrome::NOTIFICATION_INSTANT_COMMITTED,
1368 content::NotificationService::AllSources());
1369
1370 // Trigger a navigation on commit.
1371 EXPECT_TRUE(ExecuteScript(
1372 "getApiHandle().oncancel = function() {"
1373 " location.replace(location.href + '#q=query');"
1374 "};"
1375 ));
1376
1377 // Commit the overlay.
1378 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
1379 browser()->window()->GetLocationBar()->AcceptInput();
1380 observer.Wait();
1381
1382 // Confirm that the overlay has been committed.
1383 content::WebContents* active_tab =
1384 browser()->tab_strip_model()->GetActiveWebContents();
1385 EXPECT_EQ(overlay, active_tab);
1386
1387 // The page has navigated so there should be no transient entry.
1388 const content::NavigationEntry* transient_entry =
1389 active_tab->GetController().GetTransientEntry();
1390 EXPECT_EQ(NULL, transient_entry);
1391
1392 // The last committed entry should be the URL the page navigated to.
1393 const content::NavigationEntry* committed_entry =
1394 active_tab->GetController().GetLastCommittedEntry();
1395 EXPECT_TRUE(EndsWith(committed_entry->GetURL().spec(), "#q=query", true));
1396 }
1397
1398 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, RestrictedItemReadback) {
1399 // Initialize Instant.
1400 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1401 FocusOmniboxAndWaitForInstantSupport();
1402
1403 // Get a handle to the NTP and the current state of the JS.
1404 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
1405 content::WebContents* preview_tab = instant()->ntp()->contents();
1406 EXPECT_TRUE(preview_tab);
1407
1408 // Manufacture a few autocomplete results and get them down to the page.
1409 std::vector<InstantAutocompleteResult> autocomplete_results;
1410 for (int i = 0; i < 3; ++i) {
1411 std::string description(base::StringPrintf("Test Description %d", i));
1412 std::string url(base::StringPrintf("http://www.testurl%d.com", i));
1413
1414 InstantAutocompleteResult res;
1415 res.provider = ASCIIToUTF16(AutocompleteProvider::TypeToString(
1416 AutocompleteProvider::TYPE_BUILTIN));
1417 res.type = ASCIIToUTF16(AutocompleteMatch::TypeToString(
1418 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED)),
1419 res.description = ASCIIToUTF16(description);
1420 res.destination_url = ASCIIToUTF16(url);
1421 res.transition = content::PAGE_TRANSITION_TYPED;
1422 res.relevance = 42 + i;
1423
1424 autocomplete_results.push_back(res);
1425 }
1426 instant()->overlay()->SendAutocompleteResults(autocomplete_results);
1427
1428 // Apparently, one needs to access nativeSuggestions before
1429 // apiHandle.setRestrictedValue can work.
1430 EXPECT_TRUE(ExecuteScript("var foo = apiHandle.nativeSuggestions;"));
1431
1432 const char kQueryString[] = "Hippos go berzerk!";
1433
1434 // First set the query text to a non restricted value and ensure it can be
1435 // read back.
1436 std::ostringstream stream;
1437 stream << "apiHandle.setValue('" << kQueryString << "');";
1438 EXPECT_TRUE(ExecuteScript(stream.str()));
1439
1440 std::string result;
1441 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
1442 "apiHandle.value",
1443 &result));
1444 EXPECT_EQ(kQueryString, result);
1445
1446 // Set the query text to the first restricted autocomplete item.
1447 int rid = 1;
1448 stream.str(std::string());
1449 stream << "apiHandle.setRestrictedValue(" << rid << ");";
1450 EXPECT_TRUE(ExecuteScript(stream.str()));
1451
1452 // Expect that we now receive the empty string when reading the value back.
1453 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
1454 "apiHandle.value",
1455 &result));
1456 EXPECT_EQ("", result);
1457
1458 // Now set the query text to a non restricted value and ensure that the
1459 // visibility has been reset and the string can again be read back.
1460 stream.str(std::string());
1461 stream << "apiHandle.setValue('" << kQueryString << "');";
1462 EXPECT_TRUE(ExecuteScript(stream.str()));
1463
1464 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
1465 "apiHandle.value",
1466 &result));
1467 EXPECT_EQ(kQueryString, result);
1468 }
1469
1470 // Test that autocomplete results are sent to the page only when all the
1471 // providers are done.
1472 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, AutocompleteProvidersDone) {
1473 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1474 FocusOmniboxAndWaitForInstantExtendedSupport();
1475
1476 content::WebContents* overlay = instant()->GetOverlayContents();
1477 EXPECT_TRUE(UpdateSearchState(overlay));
1478 EXPECT_EQ(0, on_native_suggestions_calls_);
1479
1480 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("railroad"));
1481
1482 EXPECT_EQ(overlay, instant()->GetOverlayContents());
1483 EXPECT_TRUE(UpdateSearchState(overlay));
1484 EXPECT_EQ(1, on_native_suggestions_calls_);
1485 }
1486
1487 // Test that the local NTP is not preloaded.
1488 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, LocalNTPIsNotPreloaded) {
1489 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1490
1491 EXPECT_EQ(instant_url(), instant()->ntp_->contents()->GetURL());
1492
1493 // The second argument says to use only the local overlay and NTP.
1494 instant()->SetInstantEnabled(false, true);
1495
1496 EXPECT_EQ(NULL, instant()->ntp());
1497 }
1498
1499 // Verify top bars visibility when searching on |DEFAULT| pages and switching
1500 // between tabs. Only implemented in Views and Mac currently.
1501 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1502 #define MAYBE_TopBarsVisibilityWhenSwitchingTabs \
1503 TopBarsVisibilityWhenSwitchingTabs
1504 #else
1505 #define MAYBE_TopBarsVisibilityWhenSwitchingTabs \
1506 DISABLED_TopBarsVisibilityWhenSwitchingTabs
1507 #endif
1508 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
1509 MAYBE_TopBarsVisibilityWhenSwitchingTabs) {
1510 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1511 FocusOmniboxAndWaitForInstantExtendedSupport();
1512
1513 // Open 2 tabs of |DFEAULT| mode.
1514 ui_test_utils::NavigateToURLWithDisposition(
1515 browser(),
1516 GURL("http://www.example.com"),
1517 CURRENT_TAB,
1518 ui_test_utils::BROWSER_TEST_NONE);
1519 ui_test_utils::NavigateToURLWithDisposition(
1520 browser(),
1521 GURL("http://www.example.com"),
1522 NEW_FOREGROUND_TAB,
1523 ui_test_utils::BROWSER_TEST_NONE);
1524
1525 // Verify there are two tabs, and their top bars are visible.
1526 EXPECT_EQ(2, browser()->tab_strip_model()->count());
1527 SearchTabHelper* tab0_helper = SearchTabHelper::FromWebContents(
1528 browser()->tab_strip_model()->GetWebContentsAt(0));
1529 SearchTabHelper* tab1_helper = SearchTabHelper::FromWebContents(
1530 browser()->tab_strip_model()->GetWebContentsAt(1));
1531 EXPECT_TRUE(tab0_helper->model()->top_bars_visible());
1532 EXPECT_TRUE(tab1_helper->model()->top_bars_visible());
1533
1534 // Select 1st tab and type in omnibox. We really want a partial-height
1535 // overlay, so that it doesn't get committed when switching away from this
1536 // tab. However, the instant_extended.html used for tests always shows at
1537 // full height, though it doesn't commit the overlay when the omnibox text is
1538 // a URL and not a search. So we specifically set a URL as the omnibox text.
1539 // The overlay showing will trigger top bars in 1st tab to be hidden, but keep
1540 // 2nd tab's visible.
1541 browser()->tab_strip_model()->ActivateTabAt(0, true);
1542 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("http://www.example.com/"));
1543 EXPECT_FALSE(tab0_helper->model()->top_bars_visible());
1544 EXPECT_TRUE(tab1_helper->model()->top_bars_visible());
1545
1546 // Select 2nd tab, top bars of 1st tab should become visible, because its
1547 // overlay has been hidden.
1548 browser()->tab_strip_model()->ActivateTabAt(1, true);
1549 EXPECT_TRUE(tab0_helper->model()->top_bars_visible());
1550 EXPECT_TRUE(tab1_helper->model()->top_bars_visible());
1551
1552 // Select back 1st tab, top bars visibility of both tabs should be the same
1553 // as before.
1554 browser()->tab_strip_model()->ActivateTabAt(0, true);
1555 EXPECT_TRUE(tab0_helper->model()->top_bars_visible());
1556 EXPECT_TRUE(tab1_helper->model()->top_bars_visible());
1557
1558 // Type in omnibox to trigger full-height overlay, which will trigger its top
1559 // bars to be hidden, but keep 2nd tab's visible.
1560 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("query"));
1561 EXPECT_FALSE(tab0_helper->model()->top_bars_visible());
1562 EXPECT_TRUE(tab1_helper->model()->top_bars_visible());
1563
1564 // Select 2nd tab, its top bars should show, but top bars for 1st tab should
1565 // remain hidden, because the committed full-height overlay is still showing
1566 // suggestions.
1567 browser()->tab_strip_model()->ActivateTabAt(1, true);
1568 EXPECT_FALSE(tab0_helper->model()->top_bars_visible());
1569 EXPECT_TRUE(tab1_helper->model()->top_bars_visible());
1570 }
1571
1572 // Test that the Bookmark provider is enabled, and returns results.
1573 // TODO(sreeram): Convert this to a unit test.
1574 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_HasBookmarkProvider) {
1575 // No need to setup Instant.
1576 set_browser(browser());
1577
1578 BookmarkModel* bookmark_model =
1579 BookmarkModelFactory::GetForProfile(browser()->profile());
1580 ASSERT_TRUE(bookmark_model);
1581 ui_test_utils::WaitForBookmarkModelToLoad(bookmark_model);
1582 bookmark_utils::AddIfNotBookmarked(bookmark_model, GURL("http://angeline/"),
1583 ASCIIToUTF16("angeline"));
1584
1585 SetOmniboxText("angeline");
1586
1587 bool found_bookmark_match = false;
1588
1589 const AutocompleteResult& result = omnibox()->model()->result();
1590 for (AutocompleteResult::const_iterator iter = result.begin();
1591 !found_bookmark_match && iter != result.end(); ++iter) {
1592 found_bookmark_match = iter->type == AutocompleteMatch::BOOKMARK_TITLE;
1593 }
1594
1595 EXPECT_TRUE(found_bookmark_match);
1596 }
1597
1598 // Test that the omnibox's temporary text is reset when the popup is closed.
1599 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, TemporaryTextResetWhenPopupClosed) {
1600 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1601 FocusOmniboxAndWaitForInstantExtendedSupport();
1602 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
1603
1604 // Show the overlay and arrow-down to a suggestion (this sets temporary text).
1605 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("juju"));
1606 SendDownArrow();
1607
1608 EXPECT_TRUE(HasTemporaryText());
1609 EXPECT_EQ("result 1", GetOmniboxText());
1610
1611 // Click outside the omnibox (but not on the overlay), to make the omnibox
1612 // lose focus. Close the popup explicitly, to workaround test/toolkit issues.
1613 ui_test_utils::ClickOnView(browser(), VIEW_ID_TOOLBAR);
1614 omnibox()->CloseOmniboxPopup();
1615
1616 // The temporary text should've been accepted as the user text.
1617 EXPECT_FALSE(HasTemporaryText());
1618 EXPECT_EQ("result 1", GetOmniboxText());
1619
1620 // Now refocus the omnibox and hit Escape. This shouldn't crash.
1621 FocusOmnibox();
1622 SendEscape();
1623
1624 // The omnibox should've reverted to the underlying permanent URL.
1625 EXPECT_FALSE(HasTemporaryText());
1626 EXPECT_EQ(std::string(chrome::kAboutBlankURL), GetOmniboxText());
1627 }
1628
1629 // Test that autocomplete results aren't sent when the popup is closed.
1630 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
1631 NoAutocompleteResultsWhenPopupClosed) {
1632 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1633 FocusOmniboxAndWaitForInstantExtendedSupport();
1634 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
1635
1636 // Show the overlay and arrow-down to a suggestion (this sets temporary text).
1637 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("thangam"));
1638 SendDownArrow();
1639 EXPECT_TRUE(HasTemporaryText());
1640
1641 EXPECT_TRUE(ExecuteScript("onChangeCalls = onNativeSuggestionsCalls = 0;"));
1642
1643 content::WebContents* overlay = instant()->GetOverlayContents();
1644 EXPECT_TRUE(UpdateSearchState(overlay));
1645 EXPECT_EQ(0, on_change_calls_);
1646 EXPECT_EQ(0, on_native_suggestions_calls_);
1647
1648 // Click outside the omnibox (but not on the overlay), to make the omnibox
1649 // lose focus. Close the popup explicitly, to workaround test/toolkit issues.
1650 ui_test_utils::ClickOnView(browser(), VIEW_ID_TOOLBAR);
1651 omnibox()->CloseOmniboxPopup();
1652 EXPECT_FALSE(HasTemporaryText());
1653
1654 EXPECT_EQ(overlay, instant()->GetOverlayContents());
1655 EXPECT_TRUE(UpdateSearchState(overlay));
1656 EXPECT_EQ(0, on_change_calls_);
1657 EXPECT_EQ(0, on_native_suggestions_calls_);
1658 }
1659
1660 // Test that suggestions are not accepted when unexpected.
1661 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DeniesUnexpectedSuggestions) {
1662 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1663 FocusOmniboxAndWaitForInstantExtendedSupport();
1664 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("chip"));
1665 SendDownArrow();
1666
1667 EXPECT_EQ("result 1", GetOmniboxText());
1668 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
1669
1670 // Make the page send an unexpected suggestion.
1671 EXPECT_TRUE(ExecuteScript("suggestion = 'chippies';"
1672 "handleOnChange();"));
1673
1674 // Verify that the suggestion is ignored.
1675 EXPECT_EQ("result 1", GetOmniboxText());
1676 EXPECT_EQ(ASCIIToUTF16(""), GetGrayText());
1677 }
1678
1679 // Test that autocomplete results are cleared when the query is cleared.
1680 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, EmptyAutocompleteResults) {
1681 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1682 FocusOmniboxAndWaitForInstantExtendedSupport();
1683
1684 // Type a URL, so that there's at least one autocomplete result (a "URL what
1685 // you typed" match).
1686 ASSERT_TRUE(SetOmniboxTextAndWaitForOverlayToShow("http://upsamina/"));
1687
1688 content::WebContents* overlay = instant()->GetOverlayContents();
1689
1690 int num_autocomplete_results = 0;
1691 EXPECT_TRUE(GetIntFromJS(
1692 overlay,
1693 "chrome.embeddedSearch.searchBox.nativeSuggestions.length",
1694 &num_autocomplete_results));
1695 EXPECT_LT(0, num_autocomplete_results);
1696
1697 // Erase the query in the omnibox.
1698 SetOmniboxText("");
1699
1700 EXPECT_TRUE(GetIntFromJS(
1701 overlay,
1702 "chrome.embeddedSearch.searchBox.nativeSuggestions.length",
1703 &num_autocomplete_results));
1704 EXPECT_EQ(0, num_autocomplete_results);
1705 }
1706
1707 // Test that hitting Esc to clear the omnibox works. http://crbug.com/231744.
1708 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, EscapeClearsOmnibox) {
1709 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1710 FocusOmniboxAndWaitForInstantExtendedSupport();
1711
1712 // Navigate to the Instant NTP, and wait for it to be recognized.
1713 content::WindowedNotificationObserver instant_tab_observer(
1714 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
1715 content::NotificationService::AllSources());
1716 ui_test_utils::NavigateToURLWithDisposition(browser(),
1717 GURL(chrome::kChromeUINewTabURL),
1718 CURRENT_TAB,
1719 ui_test_utils::BROWSER_TEST_NONE);
1720 instant_tab_observer.Wait();
1721
1722 content::WebContents* contents =
1723 browser()->tab_strip_model()->GetActiveWebContents();
1724
1725 // Type a query. Verify that the query is seen by the page.
1726 SetOmniboxText("mojo");
1727 std::string query;
1728 EXPECT_TRUE(GetStringFromJS(contents, "chrome.embeddedSearch.searchBox.value",
1729 &query));
1730 EXPECT_EQ("mojo", query);
1731
1732 EXPECT_TRUE(content::ExecuteScript(contents,
1733 "onChangeCalls = submitCount = 0;"));
1734
1735 // Hit Escape, and verify that the page sees that the query is cleared.
1736 SendEscape();
1737 EXPECT_TRUE(GetStringFromJS(contents, "chrome.embeddedSearch.searchBox.value",
1738 &query));
1739 EXPECT_EQ("", query);
1740 EXPECT_EQ("", GetOmniboxText());
1741
1742 EXPECT_TRUE(UpdateSearchState(contents));
1743 EXPECT_LT(0, on_change_calls_);
1744 EXPECT_EQ(0, submit_count_);
1745 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/search/instant_browsertest.cc ('k') | chrome/browser/ui/search/instant_extended_interactive_uitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698