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

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

Issue 12520005: Move desktop-specific Instant bits to c/b/ui/search. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 7 years, 9 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_match.h"
12 #include "chrome/browser/autocomplete/autocomplete_provider.h"
13 #include "chrome/browser/autocomplete/autocomplete_result.h"
14 #include "chrome/browser/extensions/extension_browsertest.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/favicon/favicon_tab_helper.h"
17 #include "chrome/browser/history/history_types.h"
18 #include "chrome/browser/history/top_sites.h"
19 #include "chrome/browser/instant/instant_commit_type.h"
20 #include "chrome/browser/instant/instant_ntp.h"
21 #include "chrome/browser/instant/instant_overlay.h"
22 #include "chrome/browser/instant/instant_service.h"
23 #include "chrome/browser/instant/instant_service_factory.h"
24 #include "chrome/browser/instant/instant_tab.h"
25 #include "chrome/browser/instant/instant_test_utils.h"
26 #include "chrome/browser/instant/search.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/themes/theme_service.h"
29 #include "chrome/browser/themes/theme_service_factory.h"
30 #include "chrome/browser/ui/omnibox/omnibox_view.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/browser/ui/webui/theme_source.h"
33 #include "chrome/common/chrome_notification_types.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/thumbnail_score.h"
36 #include "chrome/common/url_constants.h"
37 #include "chrome/test/base/in_process_browser_test.h"
38 #include "chrome/test/base/interactive_test_utils.h"
39 #include "chrome/test/base/ui_test_utils.h"
40 #include "content/public/browser/navigation_controller.h"
41 #include "content/public/browser/navigation_entry.h"
42 #include "content/public/browser/notification_service.h"
43 #include "content/public/browser/render_process_host.h"
44 #include "content/public/browser/render_view_host.h"
45 #include "content/public/browser/site_instance.h"
46 #include "content/public/browser/url_data_source.h"
47 #include "content/public/browser/web_contents.h"
48 #include "content/public/browser/web_contents_view.h"
49 #include "content/public/common/bindings_policy.h"
50 #include "content/public/test/browser_test_utils.h"
51 #include "third_party/skia/include/core/SkBitmap.h"
52
53 namespace {
54
55 // Creates a bitmap of the specified color. Caller takes ownership.
56 gfx::Image CreateBitmap(SkColor color) {
57 SkBitmap thumbnail;
58 thumbnail.setConfig(SkBitmap::kARGB_8888_Config, 4, 4);
59 thumbnail.allocPixels();
60 thumbnail.eraseColor(color);
61 return gfx::Image::CreateFrom1xBitmap(thumbnail); // adds ref.
62 }
63
64 } // namespace
65
66 class InstantExtendedTest : public InProcessBrowserTest,
67 public InstantTestBase {
68 public:
69 InstantExtendedTest()
70 : on_most_visited_change_calls_(0),
71 most_visited_items_count_(0),
72 first_most_visited_item_id_(0),
73 on_native_suggestions_calls_(0),
74 on_change_calls_(0) {
75 }
76 protected:
77 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
78 chrome::search::EnableInstantExtendedAPIForTesting();
79 ASSERT_TRUE(https_test_server().Start());
80 GURL instant_url = https_test_server().GetURL(
81 "files/instant_extended.html?strk=1&");
82 InstantTestBase::Init(instant_url);
83 }
84
85 std::string GetOmniboxText() {
86 return UTF16ToUTF8(omnibox()->GetText());
87 }
88
89 void SendDownArrow() {
90 omnibox()->model()->OnUpOrDownKeyPressed(1);
91 // Wait for JavaScript to run the key handler by executing a blank script.
92 EXPECT_TRUE(ExecuteScript(std::string()));
93 }
94
95 void SendUpArrow() {
96 omnibox()->model()->OnUpOrDownKeyPressed(-1);
97 // Wait for JavaScript to run the key handler by executing a blank script.
98 EXPECT_TRUE(ExecuteScript(std::string()));
99 }
100
101 void SendEscape() {
102 omnibox()->model()->OnEscapeKeyPressed();
103 // Wait for JavaScript to run the key handler by executing a blank script.
104 EXPECT_TRUE(ExecuteScript(std::string()));
105 }
106
107 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT {
108 return GetIntFromJS(contents, "onMostVisitedChangedCalls",
109 &on_most_visited_change_calls_) &&
110 GetIntFromJS(contents, "mostVisitedItemsCount",
111 &most_visited_items_count_) &&
112 GetIntFromJS(contents, "firstMostVisitedItemId",
113 &first_most_visited_item_id_) &&
114 GetIntFromJS(contents, "onNativeSuggestionsCalls",
115 &on_native_suggestions_calls_) &&
116 GetIntFromJS(contents, "onChangeCalls",
117 &on_change_calls_);
118 }
119
120 int on_most_visited_change_calls_;
121 int most_visited_items_count_;
122 int first_most_visited_item_id_;
123 int on_native_suggestions_calls_;
124 int on_change_calls_;
125 };
126
127 // Test class used to verify chrome-search: scheme and access policy from the
128 // Instant overlay. This is a subclass of |ExtensionBrowserTest| because it
129 // loads a theme that provides a background image.
130 class InstantPolicyTest : public ExtensionBrowserTest, public InstantTestBase {
131 public:
132 InstantPolicyTest() {}
133
134 protected:
135 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
136 chrome::search::EnableInstantExtendedAPIForTesting();
137 ASSERT_TRUE(https_test_server().Start());
138 GURL instant_url = https_test_server().GetURL(
139 "files/instant_extended.html?strk=1&");
140 InstantTestBase::Init(instant_url);
141 }
142
143 void InstallThemeSource() {
144 ThemeSource* theme = new ThemeSource(profile());
145 content::URLDataSource::Add(profile(), theme);
146 }
147
148 void InstallThemeAndVerify(const std::string& theme_dir,
149 const std::string& theme_name) {
150 const base::FilePath theme_path = test_data_dir_.AppendASCII(theme_dir);
151 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
152 theme_path, 1, ExtensionBrowserTest::browser()));
153 const extensions::Extension* theme =
154 ThemeServiceFactory::GetThemeForProfile(
155 ExtensionBrowserTest::browser()->profile());
156 ASSERT_NE(static_cast<extensions::Extension*>(NULL), theme);
157 ASSERT_EQ(theme->name(), theme_name);
158 }
159
160 private:
161 DISALLOW_COPY_AND_ASSIGN(InstantPolicyTest);
162 };
163
164 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, ExtendedModeIsOn) {
165 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
166 EXPECT_TRUE(instant()->extended_enabled_);
167 }
168
169 // Test that Instant is preloaded when the omnibox is focused.
170 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxFocusLoadsInstant) {
171 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
172
173 // Explicitly unfocus the omnibox.
174 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
175 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
176
177 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
178 EXPECT_FALSE(omnibox()->model()->has_focus());
179
180 // Delete any existing overlay.
181 instant()->overlay_.reset();
182 EXPECT_FALSE(instant()->GetOverlayContents());
183
184 // Refocus the omnibox. The InstantController should've preloaded Instant.
185 FocusOmniboxAndWaitForInstantExtendedSupport();
186
187 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
188 EXPECT_TRUE(omnibox()->model()->has_focus());
189
190 content::WebContents* overlay = instant()->GetOverlayContents();
191 EXPECT_TRUE(overlay);
192
193 // Check that the page supports Instant, but it isn't showing.
194 EXPECT_TRUE(instant()->overlay_->supports_instant());
195 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
196 EXPECT_TRUE(instant()->model()->mode().is_default());
197
198 // Adding a new tab shouldn't delete or recreate the overlay; otherwise,
199 // what's the point of preloading?
200 AddBlankTabAndShow(browser());
201 EXPECT_EQ(overlay, instant()->GetOverlayContents());
202
203 // Unfocusing and refocusing the omnibox should also preserve the overlay.
204 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
205 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
206
207 FocusOmnibox();
208 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER));
209 EXPECT_EQ(overlay, instant()->GetOverlayContents());
210 }
211
212 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, InputShowsOverlay) {
213 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
214
215 // Focus omnibox and confirm overlay isn't shown.
216 FocusOmniboxAndWaitForInstantExtendedSupport();
217 content::WebContents* overlay = instant()->GetOverlayContents();
218 EXPECT_TRUE(overlay);
219 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
220 EXPECT_TRUE(instant()->model()->mode().is_default());
221
222 // Typing in the omnibox should show the overlay.
223 SetOmniboxTextAndWaitForOverlayToShow("query");
224 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions());
225 EXPECT_EQ(overlay, instant()->GetOverlayContents());
226 }
227
228 // Test that middle clicking on a suggestion opens the result in a new tab.
229 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
230 MiddleClickOnSuggestionOpensInNewTab) {
231 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
232 FocusOmniboxAndWaitForInstantExtendedSupport();
233 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
234
235 EXPECT_EQ(1, browser()->tab_strip_model()->count());
236
237 // Typing in the omnibox should show the overlay.
238 SetOmniboxTextAndWaitForOverlayToShow("http://www.example.com/");
239
240 // Create an event listener that opens the top suggestion in a new tab.
241 EXPECT_TRUE(ExecuteScript(
242 "var rid = getApiHandle().nativeSuggestions[0].rid;"
243 "document.body.addEventListener('click', function() {"
244 "chrome.embeddedSearch.navigateContentWindow(rid, 2);"
245 "});"
246 ));
247
248 content::WindowedNotificationObserver observer(
249 chrome::NOTIFICATION_TAB_ADDED,
250 content::NotificationService::AllSources());
251
252 // Click to trigger the event listener.
253 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
254
255 // Wait for the new tab to be added.
256 observer.Wait();
257
258 // Check that the new tab URL is as expected.
259 content::WebContents* new_tab_contents =
260 browser()->tab_strip_model()->GetWebContentsAt(1);
261 EXPECT_EQ("http://www.example.com/", new_tab_contents->GetURL().spec());
262
263 // Check that there are now two tabs.
264 EXPECT_EQ(2, browser()->tab_strip_model()->count());
265 }
266
267 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
268 UnfocusingOmniboxDoesNotChangeSuggestions) {
269 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
270 FocusOmniboxAndWaitForInstantExtendedSupport();
271 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
272
273 // Get a committed tab to work with.
274 content::WebContents* instant_tab = instant()->GetOverlayContents();
275 SetOmniboxTextAndWaitForOverlayToShow("committed");
276 browser()->window()->GetLocationBar()->AcceptInput();
277
278 // Put focus back into the omnibox, type, and wait for some gray text.
279 EXPECT_TRUE(content::ExecuteScript(instant_tab,
280 "suggestion = 'santa claus';"));
281 SetOmniboxTextAndWaitForSuggestion("santa ");
282 EXPECT_EQ(ASCIIToUTF16("claus"), omnibox()->GetInstantSuggestion());
283 EXPECT_TRUE(content::ExecuteScript(instant_tab,
284 "onChangeCalls = onNativeSuggestionsCalls = 0;"));
285
286 // Now unfocus the omnibox.
287 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
288 EXPECT_TRUE(UpdateSearchState(instant_tab));
289 EXPECT_EQ(0, on_change_calls_);
290 EXPECT_EQ(0, on_native_suggestions_calls_);
291 }
292
293 // Test that omnibox text is correctly set when overlay is committed with Enter.
294 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxTextUponEnterCommit) {
295 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
296 FocusOmniboxAndWaitForInstantExtendedSupport();
297
298 // The page will autocomplete once we set the omnibox value.
299 EXPECT_TRUE(ExecuteScript("suggestion = 'santa claus';"));
300
301 // Set the text, and wait for suggestions to show up.
302 SetOmniboxTextAndWaitForOverlayToShow("santa");
303 EXPECT_EQ(ASCIIToUTF16("santa"), omnibox()->GetText());
304
305 // Test that the current suggestion is correctly set.
306 EXPECT_EQ(ASCIIToUTF16(" claus"), omnibox()->GetInstantSuggestion());
307
308 // Commit the search by pressing Enter.
309 browser()->window()->GetLocationBar()->AcceptInput();
310
311 // 'Enter' commits the query as it was typed.
312 EXPECT_EQ(ASCIIToUTF16("santa"), omnibox()->GetText());
313
314 // Suggestion should be cleared at this point.
315 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
316 }
317
318 // Test that omnibox text is correctly set when committed with focus lost.
319 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxTextUponFocusLostCommit) {
320 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
321 FocusOmniboxAndWaitForInstantExtendedSupport();
322
323 // Set autocomplete text (grey text).
324 EXPECT_TRUE(ExecuteScript("suggestion = 'johnny depp';"));
325
326 // Set the text, and wait for suggestions to show up.
327 SetOmniboxTextAndWaitForOverlayToShow("johnny");
328 EXPECT_EQ(ASCIIToUTF16("johnny"), omnibox()->GetText());
329
330 // Test that the current suggestion is correctly set.
331 EXPECT_EQ(ASCIIToUTF16(" depp"), omnibox()->GetInstantSuggestion());
332
333 // Commit the overlay by lost focus (e.g. clicking on the page).
334 instant()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST);
335
336 // Search term extraction should kick in with the autocompleted text.
337 EXPECT_EQ(ASCIIToUTF16("johnny depp"), omnibox()->GetText());
338
339 // Suggestion should be cleared at this point.
340 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
341 }
342
343 // Test that omnibox text is correctly set when clicking on committed SERP.
344 // Disabled on Mac because omnibox focus loss is not working correctly.
345 #if defined(OS_MACOSX)
346 #define MAYBE_OmniboxTextUponFocusedCommittedSERP \
347 DISABLED_OmniboxTextUponFocusedCommittedSERP
348 #else
349 #define MAYBE_OmniboxTextUponFocusedCommittedSERP \
350 OmniboxTextUponFocusedCommittedSERP
351 #endif
352 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
353 MAYBE_OmniboxTextUponFocusedCommittedSERP) {
354 // Setup Instant.
355 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
356 FocusOmniboxAndWaitForInstantExtendedSupport();
357
358 // Do a search and commit it.
359 SetOmniboxTextAndWaitForOverlayToShow("hello k");
360 EXPECT_EQ(ASCIIToUTF16("hello k"), omnibox()->GetText());
361 browser()->window()->GetLocationBar()->AcceptInput();
362
363 // With a committed results page, do a search by unfocusing the omnibox and
364 // focusing the contents.
365 SetOmniboxText("hello");
366 // Calling handleOnChange manually to make sure it is called before the
367 // Focus() call below.
368 EXPECT_TRUE(content::ExecuteScript(instant()->instant_tab()->contents(),
369 "suggestion = 'hello kitty';"
370 "handleOnChange();"));
371 instant()->instant_tab()->contents()->GetView()->Focus();
372
373 // Search term extraction should kick in with the autocompleted text.
374 EXPECT_EQ(ASCIIToUTF16("hello kitty"), omnibox()->GetText());
375
376 // Suggestion should be cleared at this point.
377 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
378 }
379
380 // This test simulates a search provider using the InstantExtended API to
381 // navigate through the suggested results and back to the original user query.
382 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NavigateSuggestionsWithArrowKeys) {
383 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
384 FocusOmniboxAndWaitForInstantExtendedSupport();
385
386 SetOmniboxTextAndWaitForOverlayToShow("hello");
387 EXPECT_EQ("hello", GetOmniboxText());
388
389 SendDownArrow();
390 EXPECT_EQ("result 1", GetOmniboxText());
391 SendDownArrow();
392 EXPECT_EQ("result 2", GetOmniboxText());
393 SendUpArrow();
394 EXPECT_EQ("result 1", GetOmniboxText());
395 SendUpArrow();
396 EXPECT_EQ("hello", GetOmniboxText());
397
398 // Ensure that the API's value is set correctly.
399 std::string result;
400 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
401 "window.chrome.searchBox.value",
402 &result));
403 EXPECT_EQ("hello", result);
404
405 EXPECT_TRUE(HasUserInputInProgress());
406 // TODO(beaudoin): Figure out why this fails.
407 // EXPECT_FALSE(HasTemporaryText());
408
409
410 // Commit the search by pressing Enter.
411 // TODO(sreeram): Enable this check once @mathp's CL lands:
412 // https://codereview.chromium.org/12179025/
413 // browser()->window()->GetLocationBar()->AcceptInput();
414 // EXPECT_EQ("hello", GetOmniboxText());
415 }
416
417 // This test simulates a search provider using the InstantExtended API to
418 // navigate through the suggested results and hitting escape to get back to the
419 // original user query.
420 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NavigateSuggestionsAndHitEscape) {
421 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
422 FocusOmniboxAndWaitForInstantExtendedSupport();
423
424 SetOmniboxTextAndWaitForOverlayToShow("hello");
425 EXPECT_EQ("hello", GetOmniboxText());
426
427 SendDownArrow();
428 EXPECT_EQ("result 1", GetOmniboxText());
429 SendDownArrow();
430 EXPECT_EQ("result 2", GetOmniboxText());
431 SendEscape();
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 EXPECT_FALSE(HasTemporaryText());
443
444 // Commit the search by pressing Enter.
445 // TODO(sreeram): Enable this check once @mathp's CL lands:
446 // https://codereview.chromium.org/12179025/
447 // browser()->window()->GetLocationBar()->AcceptInput();
448 // EXPECT_EQ("hello", GetOmniboxText());
449 }
450
451 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NTPIsPreloaded) {
452 // Setup Instant.
453 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
454 FocusOmniboxAndWaitForInstantExtendedSupport();
455
456 // NTP contents should be preloaded.
457 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
458 content::WebContents* ntp_contents = instant()->ntp_->contents();
459 EXPECT_TRUE(ntp_contents);
460 }
461
462 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, PreloadedNTPIsUsedInNewTab) {
463 // Setup Instant.
464 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
465 FocusOmniboxAndWaitForInstantExtendedSupport();
466
467 // NTP contents should be preloaded.
468 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
469 content::WebContents* ntp_contents = instant()->ntp_->contents();
470 EXPECT_TRUE(ntp_contents);
471
472 // Open new tab. Preloaded NTP contents should have been used.
473 ui_test_utils::NavigateToURLWithDisposition(
474 browser(),
475 GURL(chrome::kChromeUINewTabURL),
476 NEW_FOREGROUND_TAB,
477 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
478 content::WebContents* active_tab =
479 browser()->tab_strip_model()->GetActiveWebContents();
480 EXPECT_EQ(ntp_contents, active_tab);
481 EXPECT_TRUE(chrome::search::IsInstantNTP(active_tab));
482 }
483
484 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, PreloadedNTPIsUsedInSameTab) {
485 // Setup Instant.
486 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
487 FocusOmniboxAndWaitForInstantExtendedSupport();
488
489 // NTP contents should be preloaded.
490 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
491 content::WebContents* ntp_contents = instant()->ntp_->contents();
492 EXPECT_TRUE(ntp_contents);
493
494 // Open new tab. Preloaded NTP contents should have been used.
495 ui_test_utils::NavigateToURLWithDisposition(
496 browser(),
497 GURL(chrome::kChromeUINewTabURL),
498 CURRENT_TAB,
499 ui_test_utils::BROWSER_TEST_NONE);
500 content::WebContents* active_tab =
501 browser()->tab_strip_model()->GetActiveWebContents();
502 EXPECT_EQ(ntp_contents, active_tab);
503 EXPECT_TRUE(chrome::search::IsInstantNTP(active_tab));
504 }
505
506 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxHasFocusOnNewTab) {
507 // Setup Instant.
508 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
509 FocusOmniboxAndWaitForInstantExtendedSupport();
510
511 // Explicitly unfocus the omnibox.
512 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
513 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
514 EXPECT_FALSE(omnibox()->model()->has_focus());
515
516 // Open new tab. Preloaded NTP contents should have been used.
517 ui_test_utils::NavigateToURLWithDisposition(
518 browser(),
519 GURL(chrome::kChromeUINewTabURL),
520 NEW_FOREGROUND_TAB,
521 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
522
523 // Omnibox should have focus.
524 EXPECT_TRUE(omnibox()->model()->has_focus());
525 }
526
527 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, OmniboxEmptyOnNewTabPage) {
528 // Setup Instant.
529 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
530 FocusOmniboxAndWaitForInstantExtendedSupport();
531
532 // Open new tab. Preloaded NTP contents should have been used.
533 ui_test_utils::NavigateToURLWithDisposition(
534 browser(),
535 GURL(chrome::kChromeUINewTabURL),
536 CURRENT_TAB,
537 ui_test_utils::BROWSER_TEST_NONE);
538
539 // Omnibox should be empty.
540 EXPECT_TRUE(omnibox()->GetText().empty());
541 }
542
543 // TODO(dhollowa): Fix flakes. http://crbug.com/179930.
544 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_NoFaviconOnNewTabPage) {
545 // Setup Instant.
546 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
547 FocusOmniboxAndWaitForInstantExtendedSupport();
548
549 // Open new tab. Preloaded NTP contents should have been used.
550 ui_test_utils::NavigateToURLWithDisposition(
551 browser(),
552 GURL(chrome::kChromeUINewTabURL),
553 CURRENT_TAB,
554 ui_test_utils::BROWSER_TEST_NONE);
555
556 // No favicon should be shown.
557 content::WebContents* active_tab =
558 browser()->tab_strip_model()->GetActiveWebContents();
559 FaviconTabHelper* favicon_tab_helper =
560 FaviconTabHelper::FromWebContents(active_tab);
561 EXPECT_FALSE(favicon_tab_helper->ShouldDisplayFavicon());
562
563 // Favicon should be shown off the NTP.
564 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
565 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
566 favicon_tab_helper = FaviconTabHelper::FromWebContents(active_tab);
567 EXPECT_TRUE(favicon_tab_helper->ShouldDisplayFavicon());
568 }
569
570 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, InputOnNTPDoesntShowOverlay) {
571 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
572
573 // Focus omnibox and confirm overlay isn't shown.
574 FocusOmniboxAndWaitForInstantExtendedSupport();
575 content::WebContents* overlay = instant()->GetOverlayContents();
576 EXPECT_TRUE(overlay);
577 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
578 EXPECT_TRUE(instant()->model()->mode().is_default());
579
580 // Navigate to the NTP.
581 ui_test_utils::NavigateToURLWithDisposition(
582 browser(),
583 GURL(chrome::kChromeUINewTabURL),
584 CURRENT_TAB,
585 ui_test_utils::BROWSER_TEST_NONE);
586
587 // Typing in the omnibox should not show the overlay.
588 SetOmniboxText("query");
589 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
590 EXPECT_TRUE(instant()->model()->mode().is_default());
591 }
592
593 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, ProcessIsolation) {
594 // Prior to setup, Instant has an overlay with a failed "google.com" load in
595 // it, which is rendered in the dedicated Instant renderer process.
596 //
597 // TODO(sreeram): Fix this up when we stop doing crazy things on init.
598 InstantService* instant_service =
599 InstantServiceFactory::GetForProfile(browser()->profile());
600 ASSERT_NE(static_cast<InstantService*>(NULL), instant_service);
601 EXPECT_EQ(1, instant_service->GetInstantProcessCount());
602
603 // Setup Instant.
604 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
605 FocusOmniboxAndWaitForInstantExtendedSupport();
606
607 // The registered Instant render process should still exist.
608 EXPECT_EQ(1, instant_service->GetInstantProcessCount());
609
610 // And the Instant overlay and ntp should live inside it.
611 content::WebContents* overlay = instant()->GetOverlayContents();
612 EXPECT_TRUE(instant_service->IsInstantProcess(
613 overlay->GetRenderProcessHost()->GetID()));
614 content::WebContents* ntp_contents = instant()->ntp_->contents();
615 EXPECT_TRUE(instant_service->IsInstantProcess(
616 ntp_contents->GetRenderProcessHost()->GetID()));
617
618 // Navigating to the NTP should use the Instant render process.
619 ui_test_utils::NavigateToURLWithDisposition(
620 browser(),
621 GURL(chrome::kChromeUINewTabURL),
622 CURRENT_TAB,
623 ui_test_utils::BROWSER_TEST_NONE);
624 content::WebContents* active_tab =
625 browser()->tab_strip_model()->GetActiveWebContents();
626 EXPECT_TRUE(instant_service->IsInstantProcess(
627 active_tab->GetRenderProcessHost()->GetID()));
628
629 // Navigating elsewhere should not use the Instant render process.
630 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
631 EXPECT_FALSE(instant_service->IsInstantProcess(
632 active_tab->GetRenderProcessHost()->GetID()));
633 }
634
635 // Verification of fix for BUG=176365. Ensure that each Instant WebContents in
636 // a tab uses a new BrowsingInstance, to avoid conflicts in the
637 // NavigationController.
638 // Flaky: http://crbug.com/177516
639 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_UnrelatedSiteInstance) {
640 // Setup Instant.
641 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
642 FocusOmniboxAndWaitForInstantExtendedSupport();
643
644 // Check that the uncommited ntp page and uncommited overlay have unrelated
645 // site instances.
646 // TODO(sreeram): |ntp_| is going away, so this check can be removed in the
647 // future.
648 content::WebContents* overlay = instant()->GetOverlayContents();
649 content::WebContents* ntp_contents = instant()->ntp_->contents();
650 EXPECT_FALSE(overlay->GetSiteInstance()->IsRelatedSiteInstance(
651 ntp_contents->GetSiteInstance()));
652
653 // Type a query and hit enter to get a results page. The overlay becomes the
654 // active tab.
655 SetOmniboxTextAndWaitForOverlayToShow("hello");
656 EXPECT_EQ("hello", GetOmniboxText());
657 browser()->window()->GetLocationBar()->AcceptInput();
658 content::WebContents* first_active_tab =
659 browser()->tab_strip_model()->GetActiveWebContents();
660 EXPECT_EQ(first_active_tab, overlay);
661 scoped_refptr<content::SiteInstance> first_site_instance =
662 first_active_tab->GetSiteInstance();
663 EXPECT_FALSE(first_site_instance->IsRelatedSiteInstance(
664 ntp_contents->GetSiteInstance()));
665
666 // Navigating elsewhere gets us off of the commited page. The next
667 // query will give us a new |overlay| which we will then commit.
668 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
669
670 // Show and commit the new overlay.
671 SetOmniboxTextAndWaitForOverlayToShow("hello again");
672 EXPECT_EQ("hello again", GetOmniboxText());
673 browser()->window()->GetLocationBar()->AcceptInput();
674 content::WebContents* second_active_tab =
675 browser()->tab_strip_model()->GetActiveWebContents();
676 EXPECT_NE(first_active_tab, second_active_tab);
677 scoped_refptr<content::SiteInstance> second_site_instance =
678 second_active_tab->GetSiteInstance();
679 EXPECT_NE(first_site_instance, second_site_instance);
680 EXPECT_FALSE(first_site_instance->IsRelatedSiteInstance(
681 second_site_instance));
682 }
683
684 // Tests that suggestions are sanity checked.
685 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, ValidatesSuggestions) {
686 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
687 FocusOmniboxAndWaitForInstantExtendedSupport();
688
689 // Do not set gray text that is not a suffix of the query.
690 EXPECT_TRUE(ExecuteScript("behavior = 2"));
691 EXPECT_TRUE(ExecuteScript("suggestion = 'potato'"));
692 SetOmniboxTextAndWaitForOverlayToShow("query");
693 EXPECT_EQ(ASCIIToUTF16("query"), omnibox()->GetText());
694 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
695
696 omnibox()->RevertAll();
697
698 // Do not set blue text that is not a valid URL completion.
699 EXPECT_TRUE(ExecuteScript("behavior = 1"));
700 EXPECT_TRUE(ExecuteScript("suggestion = 'this is not a url!'"));
701 SetOmniboxTextAndWaitForOverlayToShow("this is");
702 EXPECT_EQ(ASCIIToUTF16("this is"), omnibox()->GetText());
703 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
704
705 omnibox()->RevertAll();
706
707 // Do not set gray text when blue text is already set.
708 // First set up some blue text completion.
709 EXPECT_TRUE(ExecuteScript("behavior = 1"));
710 EXPECT_TRUE(ExecuteScript("suggestion = 'www.example.com'"));
711 SetOmniboxTextAndWaitForOverlayToShow("http://www.ex");
712 string16 text = omnibox()->GetText();
713 EXPECT_EQ(ASCIIToUTF16("http://www.example.com"), text);
714 size_t start = 0, end = 0;
715 omnibox()->GetSelectionBounds(&start, &end);
716 if (start > end)
717 std::swap(start, end);
718 EXPECT_EQ(ASCIIToUTF16("ample.com"), text.substr(start, end - start));
719 // Now try to set gray text for the same query.
720 EXPECT_TRUE(ExecuteScript("behavior = 2"));
721 EXPECT_TRUE(ExecuteScript("suggestion = 'www.example.com rocks'"));
722 SetOmniboxText("http://www.ex");
723 EXPECT_EQ(ASCIIToUTF16("http://www.example.com"), omnibox()->GetText());
724 EXPECT_EQ(ASCIIToUTF16(""), omnibox()->GetInstantSuggestion());
725
726 omnibox()->RevertAll();
727
728 // Ignore an out-of-date blue text suggestion. (Simulates a laggy
729 // SetSuggestion IPC by directly calling into InstantController.)
730 SetOmniboxTextAndWaitForOverlayToShow("http://www.example.com/");
731 instant()->SetSuggestions(
732 instant()->overlay()->contents(),
733 std::vector<InstantSuggestion>(
734 1,
735 InstantSuggestion(ASCIIToUTF16("www.exa"),
736 INSTANT_COMPLETE_NOW,
737 INSTANT_SUGGESTION_URL,
738 ASCIIToUTF16("www.exa"))));
739 EXPECT_EQ(
740 "http://www.example.com/",
741 omnibox()->model()->result().default_match()->destination_url.spec());
742
743 omnibox()->RevertAll();
744
745 // TODO(samarth): uncomment after fixing crbug.com/191656.
746 // Use an out-of-date blue text suggestion, if the text typed by the user is
747 // contained in the suggestion.
748 // SetOmniboxTextAndWaitForOverlayToShow("ex");
749 // instant()->SetSuggestions(
750 // instant()->overlay()->contents(),
751 // std::vector<InstantSuggestion>(
752 // 1,
753 // InstantSuggestion(ASCIIToUTF16("www.example.com"),
754 // INSTANT_COMPLETE_NOW,
755 // INSTANT_SUGGESTION_URL,
756 // ASCIIToUTF16("e"))));
757 // EXPECT_EQ(
758 // "http://www.example.com/",
759 // omnibox()->model()->result().default_match()->destination_url.spec());
760
761 // omnibox()->RevertAll();
762
763 // When asked to suggest blue text in verbatim mode, suggest the exact
764 // omnibox text rather than using the supplied suggestion text.
765 EXPECT_TRUE(ExecuteScript("behavior = 1"));
766 EXPECT_TRUE(ExecuteScript("suggestion = 'www.example.com/q'"));
767 SetOmniboxText("www.example.com/q");
768 omnibox()->OnBeforePossibleChange();
769 SetOmniboxText("www.example.com/");
770 omnibox()->OnAfterPossibleChange();
771 EXPECT_EQ(ASCIIToUTF16("www.example.com/"), omnibox()->GetText());
772 }
773
774 // Tests that a previous navigation suggestion is not discarded if it's not
775 // stale.
776 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
777 NavigationSuggestionIsNotDiscarded) {
778 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
779 FocusOmniboxAndWaitForInstantExtendedSupport();
780
781 // Tell the page to send a URL suggestion.
782 EXPECT_TRUE(ExecuteScript("suggestion = 'http://www.example.com';"
783 "behavior = 0;"));
784 SetOmniboxTextAndWaitForOverlayToShow("exa");
785 EXPECT_EQ(ASCIIToUTF16("example.com"), omnibox()->GetText());
786 SetOmniboxText("exam");
787 EXPECT_EQ(ASCIIToUTF16("example.com"), omnibox()->GetText());
788
789 // TODO(jered): Remove this after fixing OnBlur().
790 omnibox()->RevertAll();
791 }
792
793 // TODO(dhollowa): Fix flakes. http://crbug.com/179930.
794 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_MostVisited) {
795 content::WindowedNotificationObserver observer(
796 chrome::NOTIFICATION_INSTANT_SENT_MOST_VISITED_ITEMS,
797 content::NotificationService::AllSources());
798 // Initialize Instant.
799 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
800 FocusOmniboxAndWaitForInstantExtendedSupport();
801
802 // Get a handle to the NTP and the current state of the JS.
803 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
804 content::WebContents* overlay = instant()->ntp_->contents();
805 EXPECT_TRUE(overlay);
806 EXPECT_TRUE(UpdateSearchState(overlay));
807
808 // Wait for most visited data to be ready, if necessary.
809 if (on_most_visited_change_calls_ == 0) {
810 observer.Wait();
811 EXPECT_TRUE(UpdateSearchState(overlay));
812 }
813
814 EXPECT_EQ(1, on_most_visited_change_calls_);
815
816 // Make sure we have at least two Most Visited Items and save that number.
817 // TODO(pedrosimonetti): For now, we're relying on the fact that the Top
818 // Sites will have at lease two items in it. The correct approach would be
819 // adding those items to the Top Sites manually before starting the test.
820 EXPECT_GT(most_visited_items_count_, 1);
821 int old_most_visited_items_count = most_visited_items_count_;
822
823 // Delete the fist Most Visited Item.
824 int rid = first_most_visited_item_id_;
825 std::ostringstream stream;
826 stream << "newTabPageHandle.deleteMostVisitedItem(" << rid << ")";
827 EXPECT_TRUE(ExecuteScript(stream.str()));
828 observer.Wait();
829
830 // Update Most Visited state.
831 EXPECT_TRUE(UpdateSearchState(overlay));
832
833 // Make sure we have one less item in there.
834 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count - 1);
835
836 // Undo the deletion of the fist Most Visited Item.
837 stream.str(std::string());
838 stream << "newTabPageHandle.undoMostVisitedDeletion(" << rid << ")";
839 EXPECT_TRUE(ExecuteScript(stream.str()));
840 observer.Wait();
841
842 // Update Most Visited state.
843 EXPECT_TRUE(UpdateSearchState(overlay));
844
845 // Make sure we have the same number of items as before.
846 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count);
847
848 // Delete the fist Most Visited Item.
849 rid = first_most_visited_item_id_;
850 stream.str(std::string());
851 stream << "newTabPageHandle.deleteMostVisitedItem(" << rid << ")";
852 EXPECT_TRUE(ExecuteScript(stream.str()));
853 observer.Wait();
854
855 // Update Most Visited state.
856 EXPECT_TRUE(UpdateSearchState(overlay));
857
858 // Delete the second Most Visited Item.
859 rid = first_most_visited_item_id_;
860 stream.str(std::string());
861 stream << "newTabPageHandle.deleteMostVisitedItem(" << rid << ")";
862 EXPECT_TRUE(ExecuteScript(stream.str()));
863 observer.Wait();
864
865 // Update Most Visited state.
866 EXPECT_TRUE(UpdateSearchState(overlay));
867
868 // Make sure we have two less items in there.
869 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count - 2);
870
871 // Delete the second Most Visited Item.
872 stream.str(std::string());
873 stream << "newTabPageHandle.undoAllMostVisitedDeletions()";
874 EXPECT_TRUE(ExecuteScript(stream.str()));
875 observer.Wait();
876
877 // Update Most Visited state.
878 EXPECT_TRUE(UpdateSearchState(overlay));
879
880 // Make sure we have the same number of items as before.
881 EXPECT_EQ(most_visited_items_count_, old_most_visited_items_count);
882 }
883
884 IN_PROC_BROWSER_TEST_F(InstantPolicyTest, ThemeBackgroundAccess) {
885 InstallThemeSource();
886 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
887 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
888 FocusOmniboxAndWaitForInstantExtendedSupport();
889
890 // The "Instant" New Tab should have access to chrome-search: scheme but not
891 // chrome: scheme.
892 ui_test_utils::NavigateToURLWithDisposition(
893 browser(),
894 GURL(chrome::kChromeUINewTabURL),
895 NEW_FOREGROUND_TAB,
896 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
897
898 content::RenderViewHost* rvh =
899 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
900
901 const std::string chrome_url("chrome://theme/IDR_THEME_NTP_BACKGROUND");
902 const std::string search_url(
903 "chrome-search://theme/IDR_THEME_NTP_BACKGROUND");
904 bool loaded = false;
905 ASSERT_TRUE(LoadImage(rvh, chrome_url, &loaded));
906 EXPECT_FALSE(loaded) << chrome_url;
907 ASSERT_TRUE(LoadImage(rvh, search_url, &loaded));
908 EXPECT_TRUE(loaded) << search_url;
909 }
910
911 // TODO(dhollowa): Fix flakes. http://crbug.com/179930.
912 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_FaviconAccess) {
913 // Create a favicon.
914 history::TopSites* top_sites = browser()->profile()->GetTopSites();
915 GURL url("http://www.google.com/foo.html");
916 gfx::Image thumbnail(CreateBitmap(SK_ColorWHITE));
917 ThumbnailScore high_score(0.0, true, true, base::Time::Now());
918 EXPECT_TRUE(top_sites->SetPageThumbnail(url, thumbnail, high_score));
919
920 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
921 FocusOmniboxAndWaitForInstantExtendedSupport();
922
923 // The "Instant" New Tab should have access to chrome-search: scheme but not
924 // chrome: scheme.
925 ui_test_utils::NavigateToURLWithDisposition(
926 browser(),
927 GURL(chrome::kChromeUINewTabURL),
928 NEW_FOREGROUND_TAB,
929 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
930
931 content::RenderViewHost* rvh =
932 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
933
934 // Get the favicons.
935 const std::string chrome_favicon_url(
936 "chrome://favicon/largest/http://www.google.com/foo.html");
937 const std::string search_favicon_url(
938 "chrome-search://favicon/largest/http://www.google.com/foo.html");
939 bool loaded = false;
940 ASSERT_TRUE(LoadImage(rvh, chrome_favicon_url, &loaded));
941 EXPECT_FALSE(loaded) << chrome_favicon_url;
942 ASSERT_TRUE(LoadImage(rvh, search_favicon_url, &loaded));
943 EXPECT_TRUE(loaded) << search_favicon_url;
944 }
945
946 // WebUIBindings should never be enabled on ANY Instant web contents.
947 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoWebUIBindingsOnNTP) {
948 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
949 FocusOmniboxAndWaitForInstantExtendedSupport();
950
951 ui_test_utils::NavigateToURLWithDisposition(
952 browser(),
953 GURL(chrome::kChromeUINewTabURL),
954 NEW_FOREGROUND_TAB,
955 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
956 const content::WebContents* tab =
957 browser()->tab_strip_model()->GetActiveWebContents();
958
959 // Instant-provided NTP should not have any bindings enabled.
960 EXPECT_EQ(0, tab->GetRenderViewHost()->GetEnabledBindings());
961 }
962
963 // WebUIBindings should never be enabled on ANY Instant web contents.
964 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoWebUIBindingsOnPreview) {
965 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
966 FocusOmniboxAndWaitForInstantExtendedSupport();
967
968 // Typing in the omnibox shows the overlay.
969 SetOmniboxTextAndWaitForOverlayToShow("query");
970 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions());
971 content::WebContents* preview = instant()->GetOverlayContents();
972 ASSERT_NE(static_cast<content::WebContents*>(NULL), preview);
973
974 // Instant preview should not have any bindings enabled.
975 EXPECT_EQ(0, preview->GetRenderViewHost()->GetEnabledBindings());
976 }
977
978 // WebUIBindings should never be enabled on ANY Instant web contents.
979 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoWebUIBindingsOnResults) {
980 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
981 FocusOmniboxAndWaitForInstantExtendedSupport();
982
983 // Typing in the omnibox shows the overlay.
984 SetOmniboxTextAndWaitForOverlayToShow("query");
985 content::WebContents* preview = instant()->GetOverlayContents();
986 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions());
987 // Commit the search by pressing Enter.
988 browser()->window()->GetLocationBar()->AcceptInput();
989 EXPECT_TRUE(instant()->model()->mode().is_default());
990 const content::WebContents* tab =
991 browser()->tab_strip_model()->GetActiveWebContents();
992 EXPECT_EQ(preview, tab);
993
994 // The commited Instant page should not have any bindings enabled.
995 EXPECT_EQ(0, tab->GetRenderViewHost()->GetEnabledBindings());
996 }
997
998 // Only implemented in Views and Mac currently: http://crbug.com/164723
999 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_MACOSX)
1000 #define MAYBE_HomeButtonAffectsMargin HomeButtonAffectsMargin
1001 #else
1002 #define MAYBE_HomeButtonAffectsMargin DISABLED_HomeButtonAffectsMargin
1003 #endif
1004 // Check that toggling the state of the home button changes the start-edge
1005 // margin and width.
1006 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, MAYBE_HomeButtonAffectsMargin) {
1007 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1008
1009 // Get the current value of the start-edge margin and width.
1010 int start_margin;
1011 int width;
1012 content::WebContents* overlay = instant()->GetOverlayContents();
1013 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.startMargin",
1014 &start_margin));
1015 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.width", &width));
1016
1017 // Toggle the home button visibility pref.
1018 PrefService* profile_prefs = browser()->profile()->GetPrefs();
1019 bool show_home = profile_prefs->GetBoolean(prefs::kShowHomeButton);
1020 profile_prefs->SetBoolean(prefs::kShowHomeButton, !show_home);
1021
1022 // Make sure the margin and width changed.
1023 int new_start_margin;
1024 int new_width;
1025 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.startMargin",
1026 &new_start_margin));
1027 EXPECT_TRUE(GetIntFromJS(overlay, "chrome.searchBox.width", &new_width));
1028 EXPECT_NE(start_margin, new_start_margin);
1029 EXPECT_NE(width, new_width);
1030 EXPECT_EQ(new_width - width, start_margin - new_start_margin);
1031 }
1032
1033 // Commit does not happen on Mac: http://crbug.com/178520
1034 #if defined(OS_MACOSX)
1035 #define MAYBE_CommitWhenFocusLostInFullHeight \
1036 DISABLED_CommitWhenFocusLostInFullHeight
1037 #else
1038 #define MAYBE_CommitWhenFocusLostInFullHeight CommitWhenFocusLostInFullHeight
1039 #endif
1040 // Test that the overlay is committed when the omnibox loses focus when it is
1041 // shown at 100% height.
1042 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
1043 MAYBE_CommitWhenFocusLostInFullHeight) {
1044 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1045
1046 // Focus omnibox and confirm overlay isn't shown.
1047 FocusOmniboxAndWaitForInstantExtendedSupport();
1048 content::WebContents* overlay = instant()->GetOverlayContents();
1049 EXPECT_TRUE(overlay);
1050 EXPECT_TRUE(instant()->model()->mode().is_default());
1051 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1052
1053 // Typing in the omnibox should show the overlay.
1054 SetOmniboxTextAndWaitForOverlayToShow("query");
1055 EXPECT_TRUE(instant()->IsOverlayingSearchResults());
1056 EXPECT_EQ(overlay, instant()->GetOverlayContents());
1057
1058 // Explicitly unfocus the omnibox without triggering a click. Note that this
1059 // doesn't actually change the focus state of the omnibox, only what the
1060 // Instant controller sees it as.
1061 omnibox()->model()->OnWillKillFocus(NULL);
1062 omnibox()->model()->OnKillFocus();
1063
1064 // Confirm that the overlay has been committed.
1065 content::WebContents* active_tab =
1066 browser()->tab_strip_model()->GetActiveWebContents();
1067 EXPECT_EQ(overlay, active_tab);
1068 }
1069
1070 // Test that the overlay is committed when shown at 100% height without focus
1071 // in the omnibox.
1072 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
1073 CommitWhenShownInFullHeightWithoutFocus) {
1074 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1075
1076 // Focus omnibox and confirm overlay isn't shown.
1077 FocusOmniboxAndWaitForInstantExtendedSupport();
1078 content::WebContents* overlay = instant()->GetOverlayContents();
1079 EXPECT_TRUE(overlay);
1080 EXPECT_TRUE(instant()->model()->mode().is_default());
1081 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1082
1083 // Create an observer to wait for the commit.
1084 content::WindowedNotificationObserver observer(
1085 chrome::NOTIFICATION_INSTANT_COMMITTED,
1086 content::NotificationService::AllSources());
1087
1088 // Typing in the omnibox should show the overlay. Don't wait for the overlay
1089 // to show however.
1090 SetOmniboxText("query");
1091
1092 // Explicitly unfocus the omnibox without triggering a click. Note that this
1093 // doesn't actually change the focus state of the omnibox, only what the
1094 // Instant controller sees it as.
1095 omnibox()->model()->OnWillKillFocus(NULL);
1096 omnibox()->model()->OnKillFocus();
1097
1098 // Wait for the overlay to show.
1099 observer.Wait();
1100
1101 // Confirm that the overlay has been committed.
1102 content::WebContents* active_tab =
1103 browser()->tab_strip_model()->GetActiveWebContents();
1104 EXPECT_EQ(overlay, active_tab);
1105 }
1106
1107 // Test that a transient entry is set properly when the overlay is committed
1108 // without a navigation.
1109 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, TransientEntrySet) {
1110 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1111
1112 // Focus omnibox and confirm overlay isn't shown.
1113 FocusOmniboxAndWaitForInstantSupport();
1114 content::WebContents* overlay = instant()->GetOverlayContents();
1115 EXPECT_TRUE(overlay);
1116 EXPECT_TRUE(instant()->model()->mode().is_default());
1117 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1118
1119 // Commit the overlay without triggering a navigation.
1120 content::WindowedNotificationObserver observer(
1121 chrome::NOTIFICATION_INSTANT_COMMITTED,
1122 content::NotificationService::AllSources());
1123 SetOmniboxText("query");
1124 browser()->window()->GetLocationBar()->AcceptInput();
1125 observer.Wait();
1126
1127 // Confirm that the overlay has been committed.
1128 content::WebContents* active_tab =
1129 browser()->tab_strip_model()->GetActiveWebContents();
1130 EXPECT_EQ(overlay, active_tab);
1131
1132 // The page hasn't navigated so there should be a transient entry with the
1133 // same URL but different page ID as the last committed entry.
1134 const content::NavigationEntry* transient_entry =
1135 active_tab->GetController().GetTransientEntry();
1136 const content::NavigationEntry* committed_entry =
1137 active_tab->GetController().GetLastCommittedEntry();
1138 EXPECT_EQ(transient_entry->GetURL(), committed_entry->GetURL());
1139 EXPECT_NE(transient_entry->GetPageID(), committed_entry->GetPageID());
1140 }
1141
1142 // Test that the a transient entry is cleared when the overlay is committed
1143 // with a navigation.
1144 // TODO(samarth) : this test fails, http://crbug.com/181070.
1145 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_TransientEntryRemoved) {
1146 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1147
1148 // Focus omnibox and confirm overlay isn't shown.
1149 FocusOmniboxAndWaitForInstantSupport();
1150 content::WebContents* overlay = instant()->GetOverlayContents();
1151 EXPECT_TRUE(overlay);
1152 EXPECT_TRUE(instant()->model()->mode().is_default());
1153 EXPECT_FALSE(instant()->IsOverlayingSearchResults());
1154
1155 // Create an observer to wait for the commit.
1156 content::WindowedNotificationObserver observer(
1157 chrome::NOTIFICATION_INSTANT_COMMITTED,
1158 content::NotificationService::AllSources());
1159
1160 // Trigger a navigation on commit.
1161 EXPECT_TRUE(ExecuteScript(
1162 "getApiHandle().oncancel = function() {"
1163 " location.replace(location.href + '#q=query');"
1164 "};"
1165 ));
1166
1167 // Commit the overlay.
1168 SetOmniboxText("query");
1169 browser()->window()->GetLocationBar()->AcceptInput();
1170 observer.Wait();
1171
1172 // Confirm that the overlay has been committed.
1173 content::WebContents* active_tab =
1174 browser()->tab_strip_model()->GetActiveWebContents();
1175 EXPECT_EQ(overlay, active_tab);
1176
1177 // The page has navigated so there should be no transient entry.
1178 const content::NavigationEntry* transient_entry =
1179 active_tab->GetController().GetTransientEntry();
1180 EXPECT_EQ(NULL, transient_entry);
1181
1182 // The last committed entry should be the URL the page navigated to.
1183 const content::NavigationEntry* committed_entry =
1184 active_tab->GetController().GetLastCommittedEntry();
1185 EXPECT_TRUE(EndsWith(committed_entry->GetURL().spec(), "#q=query", true));
1186 }
1187
1188 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, RestrictedItemReadback) {
1189 // Initialize Instant.
1190 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
1191 FocusOmniboxAndWaitForInstantSupport();
1192
1193 // Get a handle to the NTP and the current state of the JS.
1194 ASSERT_NE(static_cast<InstantNTP*>(NULL), instant()->ntp());
1195 content::WebContents* preview_tab = instant()->ntp()->contents();
1196 EXPECT_TRUE(preview_tab);
1197
1198 // Manufacture a few autocomplete results and get them down to the page.
1199 std::vector<InstantAutocompleteResult> autocomplete_results;
1200 for (int i = 0; i < 3; ++i) {
1201 std::string description(base::StringPrintf("Test Description %d", i));
1202 std::string url(base::StringPrintf("http://www.testurl%d.com", i));
1203
1204 InstantAutocompleteResult res;
1205 res.provider = ASCIIToUTF16(AutocompleteProvider::TypeToString(
1206 AutocompleteProvider::TYPE_BUILTIN));
1207 res.type = ASCIIToUTF16(AutocompleteMatch::TypeToString(
1208 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED)),
1209 res.description = ASCIIToUTF16(description);
1210 res.destination_url = ASCIIToUTF16(url);
1211 res.transition = content::PAGE_TRANSITION_TYPED;
1212 res.relevance = 42 + i;
1213
1214 autocomplete_results.push_back(res);
1215 }
1216 instant()->overlay()->SendAutocompleteResults(autocomplete_results);
1217
1218 // Apparently, one needs to access nativeSuggestions before
1219 // apiHandle.setRestrictedValue can work.
1220 EXPECT_TRUE(ExecuteScript("var foo = apiHandle.nativeSuggestions;"));
1221
1222 const char kQueryString[] = "Hippos go berzerk!";
1223
1224 // First set the query text to a non restricted value and ensure it can be
1225 // read back.
1226 std::ostringstream stream;
1227 stream << "apiHandle.setValue('" << kQueryString << "');";
1228 EXPECT_TRUE(ExecuteScript(stream.str()));
1229
1230 std::string result;
1231 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
1232 "apiHandle.value",
1233 &result));
1234 EXPECT_EQ(kQueryString, result);
1235
1236 // Set the query text to the first restricted autocomplete item.
1237 int rid = 0;
1238 stream.str(std::string());
1239 stream << "apiHandle.setRestrictedValue(" << rid << ")";
1240 EXPECT_TRUE(ExecuteScript(stream.str()));
1241
1242 // Expect that we now receive the empty string when reading the value back.
1243 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
1244 "apiHandle.value",
1245 &result));
1246 EXPECT_EQ("", result);
1247
1248 // Now set the query text to a non restricted value and ensure that the
1249 // visibility has been reset and the string can again be read back.
1250 stream.str(std::string());
1251 stream << "apiHandle.setValue('" << kQueryString << "');";
1252 EXPECT_TRUE(ExecuteScript(stream.str()));
1253
1254 EXPECT_TRUE(GetStringFromJS(instant()->GetOverlayContents(),
1255 "apiHandle.value",
1256 &result));
1257 EXPECT_EQ(kQueryString, result);
1258 }
OLDNEW
« no previous file with comments | « chrome/browser/instant/instant_controller.cc ('k') | chrome/browser/instant/instant_extended_manual_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698