Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / search / instant_extended_interactive_uitest.cc
blob25cde0989d857865796039666001ecf101a4f032
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.
5 #include <sstream>
7 #include "base/command_line.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/histogram_samples.h"
10 #include "base/metrics/statistics_recorder.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/extensions/extension_browsertest.h"
21 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/history/history_service_factory.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/search/instant_service.h"
25 #include "chrome/browser/search/instant_service_factory.h"
26 #include "chrome/browser/search/search.h"
27 #include "chrome/browser/search_engines/template_url_service_factory.h"
28 #include "chrome/browser/task_manager/task_manager.h"
29 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
30 #include "chrome/browser/themes/theme_service.h"
31 #include "chrome/browser/themes/theme_service_factory.h"
32 #include "chrome/browser/ui/browser_list.h"
33 #include "chrome/browser/ui/browser_tabstrip.h"
34 #include "chrome/browser/ui/search/instant_tab.h"
35 #include "chrome/browser/ui/search/instant_test_utils.h"
36 #include "chrome/browser/ui/search/search_tab_helper.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #include "chrome/browser/ui/webui/theme_source.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "chrome/common/instant_types.h"
41 #include "chrome/common/pref_names.h"
42 #include "chrome/common/url_constants.h"
43 #include "chrome/test/base/in_process_browser_test.h"
44 #include "chrome/test/base/interactive_test_utils.h"
45 #include "chrome/test/base/ui_test_utils.h"
46 #include "components/bookmarks/browser/bookmark_utils.h"
47 #include "components/bookmarks/test/bookmark_test_helpers.h"
48 #include "components/history/core/browser/history_db_task.h"
49 #include "components/history/core/browser/history_service.h"
50 #include "components/history/core/browser/history_types.h"
51 #include "components/history/core/browser/top_sites.h"
52 #include "components/history/core/common/thumbnail_score.h"
53 #include "components/omnibox/browser/autocomplete_controller.h"
54 #include "components/omnibox/browser/autocomplete_match.h"
55 #include "components/omnibox/browser/autocomplete_provider.h"
56 #include "components/omnibox/browser/autocomplete_result.h"
57 #include "components/omnibox/browser/omnibox_field_trial.h"
58 #include "components/omnibox/browser/omnibox_view.h"
59 #include "components/omnibox/browser/search_provider.h"
60 #include "components/search/search.h"
61 #include "components/search_engines/template_url_service.h"
62 #include "components/sessions/serialized_navigation_entry.h"
63 #include "content/public/browser/navigation_controller.h"
64 #include "content/public/browser/navigation_entry.h"
65 #include "content/public/browser/notification_service.h"
66 #include "content/public/browser/render_process_host.h"
67 #include "content/public/browser/render_view_host.h"
68 #include "content/public/browser/site_instance.h"
69 #include "content/public/browser/url_data_source.h"
70 #include "content/public/browser/web_contents.h"
71 #include "content/public/common/bindings_policy.h"
72 #include "content/public/test/browser_test_utils.h"
73 #include "content/public/test/test_utils.h"
74 #include "net/base/network_change_notifier.h"
75 #include "net/http/http_status_code.h"
76 #include "net/url_request/test_url_fetcher_factory.h"
77 #include "net/url_request/url_fetcher_impl.h"
78 #include "net/url_request/url_request_status.h"
79 #include "testing/gmock/include/gmock/gmock.h"
80 #include "third_party/skia/include/core/SkBitmap.h"
82 using base::ASCIIToUTF16;
83 using testing::HasSubstr;
85 namespace {
87 // Task used to make sure history has finished processing a request. Intended
88 // for use with BlockUntilHistoryProcessesPendingRequests.
89 class QuittingHistoryDBTask : public history::HistoryDBTask {
90 public:
91 QuittingHistoryDBTask() {}
93 bool RunOnDBThread(history::HistoryBackend* backend,
94 history::HistoryDatabase* db) override {
95 return true;
98 void DoneRunOnMainThread() override { base::MessageLoop::current()->Quit(); }
100 private:
101 ~QuittingHistoryDBTask() override {}
103 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
106 class FakeNetworkChangeNotifier : public net::NetworkChangeNotifier {
107 public:
108 FakeNetworkChangeNotifier() : connection_type_(CONNECTION_NONE) {}
110 ConnectionType GetCurrentConnectionType() const override {
111 return connection_type_;
114 void SetConnectionType(ConnectionType type) {
115 connection_type_ = type;
116 NotifyObserversOfNetworkChange(type);
117 base::RunLoop().RunUntilIdle();
120 ~FakeNetworkChangeNotifier() override {}
122 private:
123 ConnectionType connection_type_;
124 DISALLOW_COPY_AND_ASSIGN(FakeNetworkChangeNotifier);
126 } // namespace
128 class InstantExtendedTest : public InProcessBrowserTest,
129 public InstantTestBase {
130 public:
131 InstantExtendedTest()
132 : on_most_visited_change_calls_(0),
133 most_visited_items_count_(0),
134 first_most_visited_item_id_(0),
135 on_native_suggestions_calls_(0),
136 on_change_calls_(0),
137 submit_count_(0),
138 on_esc_key_press_event_calls_(0),
139 on_focus_changed_calls_(0),
140 is_focused_(false),
141 on_toggle_voice_search_calls_(0) {
143 protected:
144 void SetUpInProcessBrowserTestFixture() override {
145 search::EnableQueryExtractionForTesting();
146 ASSERT_TRUE(https_test_server().Start());
147 GURL instant_url = https_test_server().GetURL(
148 "files/instant_extended.html?strk=1&");
149 GURL ntp_url = https_test_server().GetURL(
150 "files/instant_extended_ntp.html?strk=1&");
151 InstantTestBase::Init(instant_url, ntp_url, false);
154 int64 GetHistogramCount(const char* name) {
155 base::HistogramBase* histogram =
156 base::StatisticsRecorder::FindHistogram(name);
157 if (!histogram) {
158 // If no histogram is found, it's possible that no values have been
159 // recorded yet. Assume that the value is zero.
160 return 0;
162 return histogram->SnapshotSamples()->TotalCount();
165 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT {
166 return GetIntFromJS(contents, "onMostVisitedChangedCalls",
167 &on_most_visited_change_calls_) &&
168 GetIntFromJS(contents, "mostVisitedItemsCount",
169 &most_visited_items_count_) &&
170 GetIntFromJS(contents, "firstMostVisitedItemId",
171 &first_most_visited_item_id_) &&
172 GetIntFromJS(contents, "onNativeSuggestionsCalls",
173 &on_native_suggestions_calls_) &&
174 GetIntFromJS(contents, "onChangeCalls",
175 &on_change_calls_) &&
176 GetIntFromJS(contents, "submitCount",
177 &submit_count_) &&
178 GetStringFromJS(contents, "apiHandle.value",
179 &query_value_) &&
180 GetIntFromJS(contents, "onEscKeyPressedCalls",
181 &on_esc_key_press_event_calls_) &&
182 GetIntFromJS(contents, "onFocusChangedCalls",
183 &on_focus_changed_calls_) &&
184 GetBoolFromJS(contents, "isFocused",
185 &is_focused_) &&
186 GetIntFromJS(contents, "onToggleVoiceSearchCalls",
187 &on_toggle_voice_search_calls_) &&
188 GetStringFromJS(contents, "prefetchQuery", &prefetch_query_value_);
192 TemplateURL* GetDefaultSearchProviderTemplateURL() {
193 TemplateURLService* template_url_service =
194 TemplateURLServiceFactory::GetForProfile(browser()->profile());
195 if (template_url_service)
196 return template_url_service->GetDefaultSearchProvider();
197 return NULL;
200 bool AddSearchToHistory(base::string16 term, int visit_count) {
201 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL();
202 if (!template_url)
203 return false;
205 history::HistoryService* history = HistoryServiceFactory::GetForProfile(
206 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS);
207 GURL search(template_url->url_ref().ReplaceSearchTerms(
208 TemplateURLRef::SearchTermsArgs(term),
209 TemplateURLServiceFactory::GetForProfile(
210 browser()->profile())->search_terms_data()));
211 history->AddPageWithDetails(
212 search, base::string16(), visit_count, visit_count,
213 base::Time::Now(), false, history::SOURCE_BROWSED);
214 history->SetKeywordSearchTermsForURL(
215 search, template_url->id(), term);
216 return true;
219 void BlockUntilHistoryProcessesPendingRequests() {
220 history::HistoryService* history = HistoryServiceFactory::GetForProfile(
221 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS);
222 DCHECK(history);
223 DCHECK(base::MessageLoop::current());
225 base::CancelableTaskTracker tracker;
226 history->ScheduleDBTask(
227 scoped_ptr<history::HistoryDBTask>(
228 new QuittingHistoryDBTask()),
229 &tracker);
230 base::MessageLoop::current()->Run();
233 int CountSearchProviderSuggestions() {
234 return omnibox()->model()->autocomplete_controller()->search_provider()->
235 matches().size();
238 int on_most_visited_change_calls_;
239 int most_visited_items_count_;
240 int first_most_visited_item_id_;
241 int on_native_suggestions_calls_;
242 int on_change_calls_;
243 int submit_count_;
244 int on_esc_key_press_event_calls_;
245 std::string query_value_;
246 int on_focus_changed_calls_;
247 bool is_focused_;
248 int on_toggle_voice_search_calls_;
249 std::string prefetch_query_value_;
252 class InstantExtendedPrefetchTest : public InstantExtendedTest {
253 public:
254 InstantExtendedPrefetchTest()
255 : factory_(new net::URLFetcherImplFactory()),
256 fake_factory_(new net::FakeURLFetcherFactory(factory_.get())) {
259 void SetUpInProcessBrowserTestFixture() override {
260 search::EnableQueryExtractionForTesting();
261 ASSERT_TRUE(https_test_server().Start());
262 GURL instant_url = https_test_server().GetURL(
263 "files/instant_extended.html?strk=1&");
264 GURL ntp_url = https_test_server().GetURL(
265 "files/instant_extended_ntp.html?strk=1&");
266 InstantTestBase::Init(instant_url, ntp_url, true);
269 void SetUpCommandLine(base::CommandLine* command_line) override {
270 command_line->AppendSwitchASCII(
271 switches::kForceFieldTrials,
272 "EmbeddedSearch/Group11 prefetch_results_srp:1/");
275 net::FakeURLFetcherFactory* fake_factory() { return fake_factory_.get(); }
277 private:
278 // Used to instantiate FakeURLFetcherFactory.
279 scoped_ptr<net::URLFetcherImplFactory> factory_;
281 // Used to mock default search provider suggest response.
282 scoped_ptr<net::FakeURLFetcherFactory> fake_factory_;
284 DISALLOW_COPY_AND_ASSIGN(InstantExtendedPrefetchTest);
287 class InstantExtendedNetworkTest : public InstantExtendedTest {
288 protected:
289 void SetUpOnMainThread() override {
290 disable_for_test_.reset(new net::NetworkChangeNotifier::DisableForTest);
291 fake_network_change_notifier_.reset(new FakeNetworkChangeNotifier);
292 InstantExtendedTest::SetUpOnMainThread();
295 void TearDownOnMainThread() override {
296 InstantExtendedTest::TearDownOnMainThread();
297 fake_network_change_notifier_.reset();
298 disable_for_test_.reset();
301 void SetConnectionType(net::NetworkChangeNotifier::ConnectionType type) {
302 fake_network_change_notifier_->SetConnectionType(type);
305 private:
306 scoped_ptr<net::NetworkChangeNotifier::DisableForTest> disable_for_test_;
307 scoped_ptr<FakeNetworkChangeNotifier> fake_network_change_notifier_;
310 // Test class used to verify chrome-search: scheme and access policy from the
311 // Instant overlay. This is a subclass of |ExtensionBrowserTest| because it
312 // loads a theme that provides a background image.
313 class InstantPolicyTest : public ExtensionBrowserTest, public InstantTestBase {
314 public:
315 InstantPolicyTest() {}
317 protected:
318 void SetUpInProcessBrowserTestFixture() override {
319 ASSERT_TRUE(https_test_server().Start());
320 GURL instant_url = https_test_server().GetURL(
321 "files/instant_extended.html?strk=1&");
322 GURL ntp_url = https_test_server().GetURL(
323 "files/instant_extended_ntp.html?strk=1&");
324 InstantTestBase::Init(instant_url, ntp_url, false);
327 void InstallThemeSource() {
328 ThemeSource* theme = new ThemeSource(profile());
329 content::URLDataSource::Add(profile(), theme);
332 void InstallThemeAndVerify(const std::string& theme_dir,
333 const std::string& theme_name) {
334 const extensions::Extension* theme =
335 ThemeServiceFactory::GetThemeForProfile(
336 ExtensionBrowserTest::browser()->profile());
337 // If there is already a theme installed, the current theme should be
338 // disabled and the new one installed + enabled.
339 int expected_change = theme ? 0 : 1;
341 const base::FilePath theme_path = test_data_dir_.AppendASCII(theme_dir);
342 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
343 theme_path, expected_change, ExtensionBrowserTest::browser()));
344 const extensions::Extension* new_theme =
345 ThemeServiceFactory::GetThemeForProfile(
346 ExtensionBrowserTest::browser()->profile());
347 ASSERT_NE(static_cast<extensions::Extension*>(NULL), new_theme);
348 ASSERT_EQ(new_theme->name(), theme_name);
351 private:
352 DISALLOW_COPY_AND_ASSIGN(InstantPolicyTest);
355 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, SearchReusesInstantTab) {
356 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
357 FocusOmnibox();
359 content::WindowedNotificationObserver observer(
360 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
361 content::NotificationService::AllSources());
362 SetOmniboxText("flowers");
363 PressEnterAndWaitForFrameLoad();
364 observer.Wait();
366 // Just did a regular search.
367 content::WebContents* active_tab =
368 browser()->tab_strip_model()->GetActiveWebContents();
369 ASSERT_THAT(active_tab->GetURL().spec(), HasSubstr("q=flowers"));
370 ASSERT_TRUE(UpdateSearchState(active_tab));
371 ASSERT_EQ(0, submit_count_);
373 SetOmniboxText("puppies");
374 PressEnterAndWaitForNavigation();
376 // Should have reused the tab and sent an onsubmit message.
377 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
378 ASSERT_THAT(active_tab->GetURL().spec(), HasSubstr("q=puppies"));
379 ASSERT_TRUE(UpdateSearchState(active_tab));
380 EXPECT_EQ(1, submit_count_);
383 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
384 SearchDoesntReuseInstantTabWithoutSupport) {
385 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
386 FocusOmnibox();
388 // Don't wait for the navigation to complete.
389 SetOmniboxText("flowers");
390 browser()->window()->GetLocationBar()->AcceptInput();
392 SetOmniboxText("puppies");
393 browser()->window()->GetLocationBar()->AcceptInput();
395 // Should not have reused the tab.
396 ASSERT_THAT(
397 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec(),
398 HasSubstr("q=puppies"));
401 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
402 TypedSearchURLDoesntReuseInstantTab) {
403 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
404 FocusOmnibox();
406 // Create an observer to wait for the instant tab to support Instant.
407 content::WindowedNotificationObserver observer_1(
408 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
409 content::NotificationService::AllSources());
410 SetOmniboxText("flowers");
411 PressEnterAndWaitForFrameLoad();
412 observer_1.Wait();
414 // Just did a regular search.
415 content::WebContents* active_tab =
416 browser()->tab_strip_model()->GetActiveWebContents();
417 ASSERT_THAT(active_tab->GetURL().spec(), HasSubstr("q=flowers"));
418 ASSERT_TRUE(UpdateSearchState(active_tab));
419 ASSERT_EQ(0, submit_count_);
421 // Typed in a search URL "by hand".
422 content::WindowedNotificationObserver observer_2(
423 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
424 content::NotificationService::AllSources());
425 SetOmniboxText(instant_url().Resolve("#q=puppies").spec());
426 PressEnterAndWaitForNavigation();
427 observer_2.Wait();
429 // Should not have reused the tab.
430 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
431 ASSERT_THAT(active_tab->GetURL().spec(), HasSubstr("q=puppies"));
434 #if defined(OS_LINUX) && defined(ADDRESS_SANITIZER)
435 // Flaky crashes at shutdown on Linux Asan; http://crbug.com/517886.
436 #define MAYBE_OmniboxMarginSetForSearchURLs \
437 DISABLED_OmniboxMarginSetForSearchURLs
438 #else
439 #define MAYBE_OmniboxMarginSetForSearchURLs OmniboxMarginSetForSearchURLs
440 #endif
441 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
442 MAYBE_OmniboxMarginSetForSearchURLs) {
443 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
444 FocusOmnibox();
446 // Create an observer to wait for the instant tab to support Instant.
447 content::WindowedNotificationObserver observer(
448 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
449 content::NotificationService::AllSources());
451 SetOmniboxText("flowers");
452 browser()->window()->GetLocationBar()->AcceptInput();
453 observer.Wait();
455 const std::string& url =
456 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec();
457 // Make sure we actually used search_url, not instant_url.
458 ASSERT_THAT(url, HasSubstr("&is_search"));
459 EXPECT_THAT(url, HasSubstr("&es_sm="));
462 // Test to verify that switching tabs should not dispatch onmostvisitedchanged
463 // events.
464 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, NoMostVisitedChangedOnTabSwitch) {
465 // Initialize Instant.
466 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
468 // Open new tab.
469 ui_test_utils::NavigateToURLWithDisposition(
470 browser(),
471 GURL(chrome::kChromeUINewTabURL),
472 NEW_FOREGROUND_TAB,
473 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
474 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
475 EXPECT_EQ(2, browser()->tab_strip_model()->count());
477 // Make sure new tab received the onmostvisitedchanged event once.
478 content::WebContents* active_tab =
479 browser()->tab_strip_model()->GetActiveWebContents();
480 EXPECT_TRUE(UpdateSearchState(active_tab));
481 EXPECT_EQ(1, on_most_visited_change_calls_);
483 // Activate the previous tab.
484 browser()->tab_strip_model()->ActivateTabAt(0, false);
486 // Switch back to new tab.
487 browser()->tab_strip_model()->ActivateTabAt(1, false);
489 // Confirm that new tab got no onmostvisitedchanged event.
490 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
491 EXPECT_TRUE(UpdateSearchState(active_tab));
492 EXPECT_EQ(1, on_most_visited_change_calls_);
495 IN_PROC_BROWSER_TEST_F(InstantPolicyTest, ThemeBackgroundAccess) {
496 InstallThemeSource();
497 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
498 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
500 // The "Instant" New Tab should have access to chrome-search: scheme but not
501 // chrome: scheme.
502 ui_test_utils::NavigateToURLWithDisposition(
503 browser(),
504 GURL(chrome::kChromeUINewTabURL),
505 NEW_FOREGROUND_TAB,
506 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
507 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
509 content::RenderViewHost* rvh =
510 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
512 const std::string chrome_url("chrome://theme/IDR_THEME_NTP_BACKGROUND");
513 const std::string search_url(
514 "chrome-search://theme/IDR_THEME_NTP_BACKGROUND");
515 bool loaded = false;
516 ASSERT_TRUE(LoadImage(rvh, chrome_url, &loaded));
517 EXPECT_FALSE(loaded) << chrome_url;
518 ASSERT_TRUE(LoadImage(rvh, search_url, &loaded));
519 EXPECT_TRUE(loaded) << search_url;
522 // Flaky on all bots. http://crbug.com/335297.
523 IN_PROC_BROWSER_TEST_F(InstantPolicyTest,
524 DISABLED_NoThemeBackgroundChangeEventOnTabSwitch) {
525 InstallThemeSource();
526 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
528 // Install a theme.
529 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
530 EXPECT_EQ(1, browser()->tab_strip_model()->count());
532 // Open new tab.
533 ui_test_utils::NavigateToURLWithDisposition(
534 browser(),
535 GURL(chrome::kChromeUINewTabURL),
536 NEW_FOREGROUND_TAB,
537 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
538 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
539 EXPECT_EQ(2, browser()->tab_strip_model()->count());
541 content::WebContents* active_tab =
542 browser()->tab_strip_model()->GetActiveWebContents();
543 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
544 int on_theme_changed_calls = 0;
545 EXPECT_TRUE(GetIntFromJS(active_tab, "onThemeChangedCalls",
546 &on_theme_changed_calls));
547 EXPECT_EQ(1, on_theme_changed_calls);
549 // Activate the previous tab.
550 browser()->tab_strip_model()->ActivateTabAt(0, false);
551 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
553 // Switch back to new tab.
554 browser()->tab_strip_model()->ActivateTabAt(1, false);
555 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
557 // Confirm that new tab got no onthemechanged event while switching tabs.
558 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
559 on_theme_changed_calls = 0;
560 EXPECT_TRUE(GetIntFromJS(active_tab, "onThemeChangedCalls",
561 &on_theme_changed_calls));
562 EXPECT_EQ(1, on_theme_changed_calls);
565 // Flaky on all bots. http://crbug.com/335297, http://crbug.com/265971.
566 IN_PROC_BROWSER_TEST_F(InstantPolicyTest,
567 DISABLED_SendThemeBackgroundChangedEvent) {
568 InstallThemeSource();
569 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
571 // Install a theme.
572 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
574 // Open new tab.
575 ui_test_utils::NavigateToURLWithDisposition(
576 browser(),
577 GURL(chrome::kChromeUINewTabURL),
578 NEW_FOREGROUND_TAB,
579 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
580 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
581 EXPECT_EQ(2, browser()->tab_strip_model()->count());
583 // Make sure new tab received an onthemechanged event.
584 content::WebContents* active_tab =
585 browser()->tab_strip_model()->GetActiveWebContents();
586 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
587 int on_theme_changed_calls = 0;
588 EXPECT_TRUE(GetIntFromJS(active_tab, "onThemeChangedCalls",
589 &on_theme_changed_calls));
590 EXPECT_EQ(1, on_theme_changed_calls);
592 // Install a new theme.
593 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme2", "snowflake theme"));
595 // Confirm that new tab is notified about the theme changed event.
596 on_theme_changed_calls = 0;
597 EXPECT_TRUE(GetIntFromJS(active_tab, "onThemeChangedCalls",
598 &on_theme_changed_calls));
599 EXPECT_EQ(2, on_theme_changed_calls);
602 // Flaky on all bots. http://crbug.com/253092
603 // Test to verify that the omnibox search query is updated on browser
604 // back button press event.
605 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
606 DISABLED_UpdateSearchQueryOnBackNavigation) {
607 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
609 // Focus omnibox and confirm overlay isn't shown.
610 FocusOmnibox();
612 // Create an observer to wait for the instant tab to support Instant.
613 content::WindowedNotificationObserver observer(
614 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
615 content::NotificationService::AllSources());
617 SetOmniboxText("flowers");
618 // Commit the search by pressing 'Enter'.
619 PressEnterAndWaitForNavigation();
620 observer.Wait();
622 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
624 // Typing in the new search query in omnibox.
625 SetOmniboxText("cattles");
626 // Commit the search by pressing 'Enter'.
627 PressEnterAndWaitForNavigation();
628 // 'Enter' commits the query as it was typed. This creates a navigation entry
629 // in the history.
630 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
632 content::WebContents* active_tab =
633 browser()->tab_strip_model()->GetActiveWebContents();
634 EXPECT_TRUE(active_tab->GetController().CanGoBack());
635 content::WindowedNotificationObserver load_stop_observer(
636 content::NOTIFICATION_LOAD_STOP,
637 content::Source<content::NavigationController>(
638 &active_tab->GetController()));
639 active_tab->GetController().GoBack();
640 load_stop_observer.Wait();
642 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
643 // Commit the search by pressing 'Enter'.
644 FocusOmnibox();
645 PressEnterAndWaitForNavigation();
646 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
649 // Flaky: crbug.com/253092.
650 // Test to verify that the omnibox search query is updated on browser
651 // forward button press events.
652 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
653 DISABLED_UpdateSearchQueryOnForwardNavigation) {
654 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
656 // Focus omnibox and confirm overlay isn't shown.
657 FocusOmnibox();
659 // Create an observer to wait for the instant tab to support Instant.
660 content::WindowedNotificationObserver observer(
661 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
662 content::NotificationService::AllSources());
664 SetOmniboxText("flowers");
665 // Commit the search by pressing 'Enter'.
666 PressEnterAndWaitForNavigation();
667 observer.Wait();
669 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
671 // Typing in the new search query in omnibox.
672 SetOmniboxText("cattles");
673 // Commit the search by pressing 'Enter'.
674 PressEnterAndWaitForNavigation();
675 // 'Enter' commits the query as it was typed. This creates a navigation entry
676 // in the history.
677 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
679 content::WebContents* active_tab =
680 browser()->tab_strip_model()->GetActiveWebContents();
681 EXPECT_TRUE(active_tab->GetController().CanGoBack());
682 content::WindowedNotificationObserver load_stop_observer(
683 content::NOTIFICATION_LOAD_STOP,
684 content::Source<content::NavigationController>(
685 &active_tab->GetController()));
686 active_tab->GetController().GoBack();
687 load_stop_observer.Wait();
689 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
691 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
692 EXPECT_TRUE(active_tab->GetController().CanGoForward());
693 content::WindowedNotificationObserver load_stop_observer_2(
694 content::NOTIFICATION_LOAD_STOP,
695 content::Source<content::NavigationController>(
696 &active_tab->GetController()));
697 active_tab->GetController().GoForward();
698 load_stop_observer_2.Wait();
700 // Commit the search by pressing 'Enter'.
701 FocusOmnibox();
702 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
703 PressEnterAndWaitForNavigation();
704 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
707 // Flaky on all bots since re-enabled in r208032, crbug.com/253092
708 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, DISABLED_NavigateBackToNTP) {
709 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
710 FocusOmnibox();
712 // Open a new tab page.
713 ui_test_utils::NavigateToURLWithDisposition(
714 browser(),
715 GURL(chrome::kChromeUINewTabURL),
716 NEW_FOREGROUND_TAB,
717 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
718 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
719 EXPECT_EQ(2, browser()->tab_strip_model()->count());
721 content::WindowedNotificationObserver observer(
722 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
723 content::NotificationService::AllSources());
724 SetOmniboxText("flowers");
725 PressEnterAndWaitForNavigation();
726 observer.Wait();
728 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
730 // Typing in the new search query in omnibox.
731 // Commit the search by pressing 'Enter'.
732 SetOmniboxText("cattles");
733 PressEnterAndWaitForNavigation();
735 // 'Enter' commits the query as it was typed. This creates a navigation entry
736 // in the history.
737 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
739 // Navigate back to "flowers" search result page.
740 content::WebContents* active_tab =
741 browser()->tab_strip_model()->GetActiveWebContents();
742 EXPECT_TRUE(active_tab->GetController().CanGoBack());
743 content::WindowedNotificationObserver load_stop_observer(
744 content::NOTIFICATION_LOAD_STOP,
745 content::Source<content::NavigationController>(
746 &active_tab->GetController()));
747 active_tab->GetController().GoBack();
748 load_stop_observer.Wait();
750 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
752 // Navigate back to NTP.
753 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
754 EXPECT_TRUE(active_tab->GetController().CanGoBack());
755 content::WindowedNotificationObserver load_stop_observer_2(
756 content::NOTIFICATION_LOAD_STOP,
757 content::Source<content::NavigationController>(
758 &active_tab->GetController()));
759 active_tab->GetController().GoBack();
760 load_stop_observer_2.Wait();
762 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
763 EXPECT_TRUE(search::IsInstantNTP(active_tab));
766 // Flaky: crbug.com/267119
767 IN_PROC_BROWSER_TEST_F(InstantExtendedTest,
768 DISABLED_DispatchMVChangeEventWhileNavigatingBackToNTP) {
769 // Setup Instant.
770 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
771 FocusOmnibox();
773 // Open new tab.
774 ui_test_utils::NavigateToURLWithDisposition(
775 browser(),
776 GURL(chrome::kChromeUINewTabURL),
777 NEW_FOREGROUND_TAB,
778 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
779 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
781 content::WebContents* active_tab =
782 browser()->tab_strip_model()->GetActiveWebContents();
783 EXPECT_TRUE(UpdateSearchState(active_tab));
784 EXPECT_EQ(1, on_most_visited_change_calls_);
786 content::WindowedNotificationObserver observer(
787 content::NOTIFICATION_LOAD_STOP,
788 content::NotificationService::AllSources());
789 // Set the text and press enter to navigate from NTP.
790 SetOmniboxText("Pen");
791 PressEnterAndWaitForNavigation();
792 EXPECT_EQ(ASCIIToUTF16("Pen"), omnibox()->GetText());
793 observer.Wait();
795 // Navigate back to NTP.
796 content::WindowedNotificationObserver back_observer(
797 content::NOTIFICATION_LOAD_STOP,
798 content::NotificationService::AllSources());
799 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
800 EXPECT_TRUE(active_tab->GetController().CanGoBack());
801 active_tab->GetController().GoBack();
802 back_observer.Wait();
804 // Verify that onmostvisitedchange event is dispatched when we navigate from
805 // SRP to NTP.
806 active_tab = browser()->tab_strip_model()->GetActiveWebContents();
807 EXPECT_TRUE(UpdateSearchState(active_tab));
808 EXPECT_EQ(1, on_most_visited_change_calls_);
811 // http://crbug.com/518106
812 #if defined(OS_WIN)
813 #define MAYBE_SetPrefetchQuery DISABLED_SetPrefetchQuery
814 #else
815 #define MAYBE_SetPrefetchQuery SetPrefetchQuery
816 #endif
817 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest, MAYBE_SetPrefetchQuery) {
818 // Skip the test if suggest support is disabled, since this is generally due
819 // to policy and can't be overridden.
820 if (!browser()->profile()->GetPrefs()->GetBoolean(
821 prefs::kSearchSuggestEnabled)) {
822 return;
824 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
825 FocusOmnibox();
827 content::WindowedNotificationObserver new_tab_observer(
828 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
829 content::NotificationService::AllSources());
830 ui_test_utils::NavigateToURLWithDisposition(
831 browser(),
832 GURL(chrome::kChromeUINewTabURL),
833 CURRENT_TAB,
834 ui_test_utils::BROWSER_TEST_NONE);
835 new_tab_observer.Wait();
837 OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs = 0;
839 // Set the fake response for search query.
840 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
842 net::HTTP_OK,
843 net::URLRequestStatus::SUCCESS);
845 // Navigate to a search results page.
846 content::WindowedNotificationObserver observer(
847 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
848 content::NotificationService::AllSources());
849 SetOmniboxText("flowers");
850 PressEnterAndWaitForNavigation();
851 observer.Wait();
853 // Set the fake response for suggest request. Response has prefetch details.
854 // Ensure that the page received the suggest response, then add another
855 // keystroke to allow the asynchronously-received inline autocomplete
856 // suggestion to actually be inlined (which in turn triggers it to prefetch).
857 fake_factory()->SetFakeResponse(
858 instant_url().Resolve("#q=pup"),
859 "[\"pup\",[\"puppy\", \"puppies\"],[],[],"
860 "{\"google:clientdata\":{\"phi\": 0},"
861 "\"google:suggesttype\":[\"QUERY\", \"QUERY\"],"
862 "\"google:suggestrelevance\":[1400, 9]}]",
863 net::HTTP_OK,
864 net::URLRequestStatus::SUCCESS);
866 SetOmniboxText("pup");
867 while (!omnibox()->model()->autocomplete_controller()->done()) {
868 content::WindowedNotificationObserver ready_observer(
869 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
870 content::Source<AutocompleteController>(
871 omnibox()->model()->autocomplete_controller()));
872 ready_observer.Wait();
874 SetOmniboxText("pupp");
876 ASSERT_EQ(3, CountSearchProviderSuggestions());
877 content::WebContents* active_tab =
878 browser()->tab_strip_model()->GetActiveWebContents();
879 ASSERT_TRUE(UpdateSearchState(active_tab));
880 ASSERT_TRUE(SearchProvider::ShouldPrefetch(*(
881 omnibox()->model()->result().default_match())));
882 ASSERT_EQ("puppy", prefetch_query_value_);
885 // http://crbug.com/518106
886 #if defined(OS_WIN)
887 #define MAYBE_ClearPrefetchedResults DISABLED_ClearPrefetchedResults
888 #else
889 #define MAYBE_ClearPrefetchedResults ClearPrefetchedResults
890 #endif
891 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest,
892 MAYBE_ClearPrefetchedResults) {
893 // Skip the test if suggest support is disabled, since this is generally due
894 // to policy and can't be overridden.
895 if (!browser()->profile()->GetPrefs()->GetBoolean(
896 prefs::kSearchSuggestEnabled)) {
897 return;
899 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
900 FocusOmnibox();
902 content::WindowedNotificationObserver new_tab_observer(
903 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
904 content::NotificationService::AllSources());
905 ui_test_utils::NavigateToURLWithDisposition(
906 browser(),
907 GURL(chrome::kChromeUINewTabURL),
908 CURRENT_TAB,
909 ui_test_utils::BROWSER_TEST_NONE);
910 new_tab_observer.Wait();
912 OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs = 0;
914 // Set the fake response for search query.
915 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
917 net::HTTP_OK,
918 net::URLRequestStatus::SUCCESS);
920 // Navigate to a search results page.
921 content::WindowedNotificationObserver observer(
922 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
923 content::NotificationService::AllSources());
924 SetOmniboxText("flowers");
925 PressEnterAndWaitForNavigation();
926 observer.Wait();
928 // Set the fake response for suggest request. Response has no prefetch
929 // details. Ensure that the page received a blank query to clear the
930 // prefetched results.
931 fake_factory()->SetFakeResponse(
932 instant_url().Resolve("#q=dogs"),
933 "[\"dogs\",[\"https://dogs.com\"],[],[],"
934 "{\"google:suggesttype\":[\"NAVIGATION\"],"
935 "\"google:suggestrelevance\":[2]}]",
936 net::HTTP_OK,
937 net::URLRequestStatus::SUCCESS);
939 SetOmniboxText("dogs");
940 while (!omnibox()->model()->autocomplete_controller()->done()) {
941 content::WindowedNotificationObserver ready_observer(
942 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY,
943 content::Source<AutocompleteController>(
944 omnibox()->model()->autocomplete_controller()));
945 ready_observer.Wait();
948 ASSERT_EQ(2, CountSearchProviderSuggestions());
949 ASSERT_FALSE(SearchProvider::ShouldPrefetch(*(
950 omnibox()->model()->result().default_match())));
951 content::WebContents* active_tab =
952 browser()->tab_strip_model()->GetActiveWebContents();
953 ASSERT_TRUE(UpdateSearchState(active_tab));
954 ASSERT_EQ("", prefetch_query_value_);
957 #if defined(OS_LINUX) && defined(ADDRESS_SANITIZER)
958 // Flaky timeouts at shutdown on Linux ASan; http://crbug.com/505478.
959 #define MAYBE_ShowURL DISABLED_ShowURL
960 #else
961 #define MAYBE_ShowURL ShowURL
962 #endif
963 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, MAYBE_ShowURL) {
964 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
965 FocusOmnibox();
967 // Create an observer to wait for the instant tab to support Instant.
968 content::WindowedNotificationObserver observer(
969 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
970 content::NotificationService::AllSources());
972 // Do a search and commit it. The omnibox should show the search terms.
973 SetOmniboxText("foo");
974 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
975 browser()->window()->GetLocationBar()->AcceptInput();
976 observer.Wait();
977 EXPECT_FALSE(omnibox()->model()->user_input_in_progress());
978 EXPECT_TRUE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
979 false));
980 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
982 // Calling ShowURL() should disable search term replacement and show the URL.
983 omnibox()->ShowURL();
984 EXPECT_FALSE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
985 false));
986 // Don't bother looking for a specific URL; ensuring we're no longer showing
987 // the search terms is sufficient.
988 EXPECT_NE(ASCIIToUTF16("foo"), omnibox()->GetText());
991 // Check that clicking on a result sends the correct referrer.
992 IN_PROC_BROWSER_TEST_F(InstantExtendedTest, Referrer) {
993 ASSERT_TRUE(test_server()->Start());
994 GURL result_url =
995 test_server()->GetURL("files/referrer_policy/referrer-policy-log.html");
996 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
997 FocusOmnibox();
999 // Type a query and press enter to get results.
1000 SetOmniboxText("query");
1001 PressEnterAndWaitForFrameLoad();
1003 // Simulate going to a result.
1004 content::WebContents* contents =
1005 browser()->tab_strip_model()->GetActiveWebContents();
1006 std::ostringstream stream;
1007 stream << "var link = document.createElement('a');";
1008 stream << "link.href = \"" << result_url.spec() << "\";";
1009 stream << "document.body.appendChild(link);";
1010 stream << "link.click();";
1011 EXPECT_TRUE(content::ExecuteScript(contents, stream.str()));
1013 content::WaitForLoadStop(contents);
1014 std::string expected_title =
1015 "Referrer is " + instant_url().GetWithEmptyPath().spec();
1016 EXPECT_EQ(ASCIIToUTF16(expected_title), contents->GetTitle());