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.
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/autocomplete/autocomplete_controller.h"
19 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/extensions/extension_browsertest.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/history/history_service_factory.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/search/instant_service.h"
26 #include "chrome/browser/search/instant_service_factory.h"
27 #include "chrome/browser/search/search.h"
28 #include "chrome/browser/search_engines/template_url_service_factory.h"
29 #include "chrome/browser/task_manager/task_manager.h"
30 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
31 #include "chrome/browser/themes/theme_service.h"
32 #include "chrome/browser/themes/theme_service_factory.h"
33 #include "chrome/browser/ui/browser_list.h"
34 #include "chrome/browser/ui/browser_tabstrip.h"
35 #include "chrome/browser/ui/omnibox/omnibox_view.h"
36 #include "chrome/browser/ui/search/instant_tab.h"
37 #include "chrome/browser/ui/search/instant_test_utils.h"
38 #include "chrome/browser/ui/search/search_tab_helper.h"
39 #include "chrome/browser/ui/tabs/tab_strip_model.h"
40 #include "chrome/browser/ui/webui/theme_source.h"
41 #include "chrome/common/chrome_switches.h"
42 #include "chrome/common/instant_types.h"
43 #include "chrome/common/url_constants.h"
44 #include "chrome/test/base/in_process_browser_test.h"
45 #include "chrome/test/base/interactive_test_utils.h"
46 #include "chrome/test/base/ui_test_utils.h"
47 #include "components/bookmarks/browser/bookmark_utils.h"
48 #include "components/bookmarks/test/bookmark_test_helpers.h"
49 #include "components/history/core/browser/history_db_task.h"
50 #include "components/history/core/browser/history_service.h"
51 #include "components/history/core/browser/history_types.h"
52 #include "components/history/core/browser/top_sites.h"
53 #include "components/history/core/common/thumbnail_score.h"
54 #include "components/omnibox/autocomplete_match.h"
55 #include "components/omnibox/autocomplete_provider.h"
56 #include "components/omnibox/autocomplete_result.h"
57 #include "components/omnibox/omnibox_field_trial.h"
58 #include "components/omnibox/search_provider.h"
59 #include "components/search_engines/template_url_service.h"
60 #include "components/sessions/serialized_navigation_entry.h"
61 #include "content/public/browser/navigation_controller.h"
62 #include "content/public/browser/navigation_entry.h"
63 #include "content/public/browser/notification_service.h"
64 #include "content/public/browser/render_process_host.h"
65 #include "content/public/browser/render_view_host.h"
66 #include "content/public/browser/site_instance.h"
67 #include "content/public/browser/url_data_source.h"
68 #include "content/public/browser/web_contents.h"
69 #include "content/public/common/bindings_policy.h"
70 #include "content/public/test/browser_test_utils.h"
71 #include "content/public/test/test_utils.h"
72 #include "net/base/network_change_notifier.h"
73 #include "net/http/http_status_code.h"
74 #include "net/url_request/test_url_fetcher_factory.h"
75 #include "net/url_request/url_fetcher_impl.h"
76 #include "net/url_request/url_request_status.h"
77 #include "testing/gmock/include/gmock/gmock.h"
78 #include "third_party/skia/include/core/SkBitmap.h"
80 using base::ASCIIToUTF16
;
81 using testing::HasSubstr
;
85 // Task used to make sure history has finished processing a request. Intended
86 // for use with BlockUntilHistoryProcessesPendingRequests.
87 class QuittingHistoryDBTask
: public history::HistoryDBTask
{
89 QuittingHistoryDBTask() {}
91 bool RunOnDBThread(history::HistoryBackend
* backend
,
92 history::HistoryDatabase
* db
) override
{
96 void DoneRunOnMainThread() override
{ base::MessageLoop::current()->Quit(); }
99 ~QuittingHistoryDBTask() override
{}
101 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask
);
104 class FakeNetworkChangeNotifier
: public net::NetworkChangeNotifier
{
106 FakeNetworkChangeNotifier() : connection_type_(CONNECTION_NONE
) {}
108 ConnectionType
GetCurrentConnectionType() const override
{
109 return connection_type_
;
112 void SetConnectionType(ConnectionType type
) {
113 connection_type_
= type
;
114 NotifyObserversOfNetworkChange(type
);
115 base::RunLoop().RunUntilIdle();
118 ~FakeNetworkChangeNotifier() override
{}
121 ConnectionType connection_type_
;
122 DISALLOW_COPY_AND_ASSIGN(FakeNetworkChangeNotifier
);
126 class InstantExtendedTest
: public InProcessBrowserTest
,
127 public InstantTestBase
{
129 InstantExtendedTest()
130 : on_most_visited_change_calls_(0),
131 most_visited_items_count_(0),
132 first_most_visited_item_id_(0),
133 on_native_suggestions_calls_(0),
136 on_esc_key_press_event_calls_(0),
137 on_focus_changed_calls_(0),
139 on_toggle_voice_search_calls_(0) {
142 void SetUpInProcessBrowserTestFixture() override
{
143 chrome::EnableQueryExtractionForTesting();
144 ASSERT_TRUE(https_test_server().Start());
145 GURL instant_url
= https_test_server().GetURL(
146 "files/instant_extended.html?strk=1&");
147 GURL ntp_url
= https_test_server().GetURL(
148 "files/instant_extended_ntp.html?strk=1&");
149 InstantTestBase::Init(instant_url
, ntp_url
, false);
152 int64
GetHistogramCount(const char* name
) {
153 base::HistogramBase
* histogram
=
154 base::StatisticsRecorder::FindHistogram(name
);
156 // If no histogram is found, it's possible that no values have been
157 // recorded yet. Assume that the value is zero.
160 return histogram
->SnapshotSamples()->TotalCount();
163 bool UpdateSearchState(content::WebContents
* contents
) WARN_UNUSED_RESULT
{
164 return GetIntFromJS(contents
, "onMostVisitedChangedCalls",
165 &on_most_visited_change_calls_
) &&
166 GetIntFromJS(contents
, "mostVisitedItemsCount",
167 &most_visited_items_count_
) &&
168 GetIntFromJS(contents
, "firstMostVisitedItemId",
169 &first_most_visited_item_id_
) &&
170 GetIntFromJS(contents
, "onNativeSuggestionsCalls",
171 &on_native_suggestions_calls_
) &&
172 GetIntFromJS(contents
, "onChangeCalls",
173 &on_change_calls_
) &&
174 GetIntFromJS(contents
, "submitCount",
176 GetStringFromJS(contents
, "apiHandle.value",
178 GetIntFromJS(contents
, "onEscKeyPressedCalls",
179 &on_esc_key_press_event_calls_
) &&
180 GetIntFromJS(contents
, "onFocusChangedCalls",
181 &on_focus_changed_calls_
) &&
182 GetBoolFromJS(contents
, "isFocused",
184 GetIntFromJS(contents
, "onToggleVoiceSearchCalls",
185 &on_toggle_voice_search_calls_
) &&
186 GetStringFromJS(contents
, "prefetchQuery", &prefetch_query_value_
);
190 TemplateURL
* GetDefaultSearchProviderTemplateURL() {
191 TemplateURLService
* template_url_service
=
192 TemplateURLServiceFactory::GetForProfile(browser()->profile());
193 if (template_url_service
)
194 return template_url_service
->GetDefaultSearchProvider();
198 bool AddSearchToHistory(base::string16 term
, int visit_count
) {
199 TemplateURL
* template_url
= GetDefaultSearchProviderTemplateURL();
203 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
204 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS
);
205 GURL
search(template_url
->url_ref().ReplaceSearchTerms(
206 TemplateURLRef::SearchTermsArgs(term
),
207 TemplateURLServiceFactory::GetForProfile(
208 browser()->profile())->search_terms_data()));
209 history
->AddPageWithDetails(
210 search
, base::string16(), visit_count
, visit_count
,
211 base::Time::Now(), false, history::SOURCE_BROWSED
);
212 history
->SetKeywordSearchTermsForURL(
213 search
, template_url
->id(), term
);
217 void BlockUntilHistoryProcessesPendingRequests() {
218 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
219 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS
);
221 DCHECK(base::MessageLoop::current());
223 base::CancelableTaskTracker tracker
;
224 history
->ScheduleDBTask(
225 scoped_ptr
<history::HistoryDBTask
>(
226 new QuittingHistoryDBTask()),
228 base::MessageLoop::current()->Run();
231 int CountSearchProviderSuggestions() {
232 return omnibox()->model()->autocomplete_controller()->search_provider()->
236 int on_most_visited_change_calls_
;
237 int most_visited_items_count_
;
238 int first_most_visited_item_id_
;
239 int on_native_suggestions_calls_
;
240 int on_change_calls_
;
242 int on_esc_key_press_event_calls_
;
243 std::string query_value_
;
244 int on_focus_changed_calls_
;
246 int on_toggle_voice_search_calls_
;
247 std::string prefetch_query_value_
;
250 class InstantExtendedPrefetchTest
: public InstantExtendedTest
{
252 InstantExtendedPrefetchTest()
253 : factory_(new net::URLFetcherImplFactory()),
254 fake_factory_(new net::FakeURLFetcherFactory(factory_
.get())) {
257 void SetUpInProcessBrowserTestFixture() override
{
258 chrome::EnableQueryExtractionForTesting();
259 ASSERT_TRUE(https_test_server().Start());
260 GURL instant_url
= https_test_server().GetURL(
261 "files/instant_extended.html?strk=1&");
262 GURL ntp_url
= https_test_server().GetURL(
263 "files/instant_extended_ntp.html?strk=1&");
264 InstantTestBase::Init(instant_url
, ntp_url
, true);
267 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
268 command_line
->AppendSwitchASCII(
269 switches::kForceFieldTrials
,
270 "EmbeddedSearch/Group11 prefetch_results_srp:1/");
273 net::FakeURLFetcherFactory
* fake_factory() { return fake_factory_
.get(); }
276 // Used to instantiate FakeURLFetcherFactory.
277 scoped_ptr
<net::URLFetcherImplFactory
> factory_
;
279 // Used to mock default search provider suggest response.
280 scoped_ptr
<net::FakeURLFetcherFactory
> fake_factory_
;
282 DISALLOW_COPY_AND_ASSIGN(InstantExtendedPrefetchTest
);
285 class InstantExtendedNetworkTest
: public InstantExtendedTest
{
287 void SetUpOnMainThread() override
{
288 disable_for_test_
.reset(new net::NetworkChangeNotifier::DisableForTest
);
289 fake_network_change_notifier_
.reset(new FakeNetworkChangeNotifier
);
290 InstantExtendedTest::SetUpOnMainThread();
293 void TearDownOnMainThread() override
{
294 InstantExtendedTest::TearDownOnMainThread();
295 fake_network_change_notifier_
.reset();
296 disable_for_test_
.reset();
299 void SetConnectionType(net::NetworkChangeNotifier::ConnectionType type
) {
300 fake_network_change_notifier_
->SetConnectionType(type
);
304 scoped_ptr
<net::NetworkChangeNotifier::DisableForTest
> disable_for_test_
;
305 scoped_ptr
<FakeNetworkChangeNotifier
> fake_network_change_notifier_
;
308 // Test class used to verify chrome-search: scheme and access policy from the
309 // Instant overlay. This is a subclass of |ExtensionBrowserTest| because it
310 // loads a theme that provides a background image.
311 class InstantPolicyTest
: public ExtensionBrowserTest
, public InstantTestBase
{
313 InstantPolicyTest() {}
316 void SetUpInProcessBrowserTestFixture() override
{
317 ASSERT_TRUE(https_test_server().Start());
318 GURL instant_url
= https_test_server().GetURL(
319 "files/instant_extended.html?strk=1&");
320 GURL ntp_url
= https_test_server().GetURL(
321 "files/instant_extended_ntp.html?strk=1&");
322 InstantTestBase::Init(instant_url
, ntp_url
, false);
325 void InstallThemeSource() {
326 ThemeSource
* theme
= new ThemeSource(profile());
327 content::URLDataSource::Add(profile(), theme
);
330 void InstallThemeAndVerify(const std::string
& theme_dir
,
331 const std::string
& theme_name
) {
332 const extensions::Extension
* theme
=
333 ThemeServiceFactory::GetThemeForProfile(
334 ExtensionBrowserTest::browser()->profile());
335 // If there is already a theme installed, the current theme should be
336 // disabled and the new one installed + enabled.
337 int expected_change
= theme
? 0 : 1;
339 const base::FilePath theme_path
= test_data_dir_
.AppendASCII(theme_dir
);
340 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
341 theme_path
, expected_change
, ExtensionBrowserTest::browser()));
342 const extensions::Extension
* new_theme
=
343 ThemeServiceFactory::GetThemeForProfile(
344 ExtensionBrowserTest::browser()->profile());
345 ASSERT_NE(static_cast<extensions::Extension
*>(NULL
), new_theme
);
346 ASSERT_EQ(new_theme
->name(), theme_name
);
350 DISALLOW_COPY_AND_ASSIGN(InstantPolicyTest
);
353 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, SearchReusesInstantTab
) {
354 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
357 content::WindowedNotificationObserver
observer(
358 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
359 content::NotificationService::AllSources());
360 SetOmniboxText("flowers");
361 PressEnterAndWaitForFrameLoad();
364 // Just did a regular search.
365 content::WebContents
* active_tab
=
366 browser()->tab_strip_model()->GetActiveWebContents();
367 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=flowers"));
368 ASSERT_TRUE(UpdateSearchState(active_tab
));
369 ASSERT_EQ(0, submit_count_
);
371 SetOmniboxText("puppies");
372 PressEnterAndWaitForNavigation();
374 // Should have reused the tab and sent an onsubmit message.
375 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
376 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=puppies"));
377 ASSERT_TRUE(UpdateSearchState(active_tab
));
378 EXPECT_EQ(1, submit_count_
);
381 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
382 SearchDoesntReuseInstantTabWithoutSupport
) {
383 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
386 // Don't wait for the navigation to complete.
387 SetOmniboxText("flowers");
388 browser()->window()->GetLocationBar()->AcceptInput();
390 SetOmniboxText("puppies");
391 browser()->window()->GetLocationBar()->AcceptInput();
393 // Should not have reused the tab.
395 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec(),
396 HasSubstr("q=puppies"));
399 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
400 TypedSearchURLDoesntReuseInstantTab
) {
401 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
404 // Create an observer to wait for the instant tab to support Instant.
405 content::WindowedNotificationObserver
observer_1(
406 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
407 content::NotificationService::AllSources());
408 SetOmniboxText("flowers");
409 PressEnterAndWaitForFrameLoad();
412 // Just did a regular search.
413 content::WebContents
* active_tab
=
414 browser()->tab_strip_model()->GetActiveWebContents();
415 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=flowers"));
416 ASSERT_TRUE(UpdateSearchState(active_tab
));
417 ASSERT_EQ(0, submit_count_
);
419 // Typed in a search URL "by hand".
420 content::WindowedNotificationObserver
observer_2(
421 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
422 content::NotificationService::AllSources());
423 SetOmniboxText(instant_url().Resolve("#q=puppies").spec());
424 PressEnterAndWaitForNavigation();
427 // Should not have reused the tab.
428 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
429 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=puppies"));
432 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, OmniboxMarginSetForSearchURLs
) {
433 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
436 // Create an observer to wait for the instant tab to support Instant.
437 content::WindowedNotificationObserver
observer(
438 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
439 content::NotificationService::AllSources());
441 SetOmniboxText("flowers");
442 browser()->window()->GetLocationBar()->AcceptInput();
445 const std::string
& url
=
446 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec();
447 // Make sure we actually used search_url, not instant_url.
448 ASSERT_THAT(url
, HasSubstr("&is_search"));
449 EXPECT_THAT(url
, HasSubstr("&es_sm="));
452 // Test to verify that switching tabs should not dispatch onmostvisitedchanged
454 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, NoMostVisitedChangedOnTabSwitch
) {
455 // Initialize Instant.
456 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
459 ui_test_utils::NavigateToURLWithDisposition(
461 GURL(chrome::kChromeUINewTabURL
),
463 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
464 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
465 EXPECT_EQ(2, browser()->tab_strip_model()->count());
467 // Make sure new tab received the onmostvisitedchanged event once.
468 content::WebContents
* active_tab
=
469 browser()->tab_strip_model()->GetActiveWebContents();
470 EXPECT_TRUE(UpdateSearchState(active_tab
));
471 EXPECT_EQ(1, on_most_visited_change_calls_
);
473 // Activate the previous tab.
474 browser()->tab_strip_model()->ActivateTabAt(0, false);
476 // Switch back to new tab.
477 browser()->tab_strip_model()->ActivateTabAt(1, false);
479 // Confirm that new tab got no onmostvisitedchanged event.
480 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
481 EXPECT_TRUE(UpdateSearchState(active_tab
));
482 EXPECT_EQ(1, on_most_visited_change_calls_
);
485 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
, ThemeBackgroundAccess
) {
486 InstallThemeSource();
487 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
488 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
490 // The "Instant" New Tab should have access to chrome-search: scheme but not
492 ui_test_utils::NavigateToURLWithDisposition(
494 GURL(chrome::kChromeUINewTabURL
),
496 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
497 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
499 content::RenderViewHost
* rvh
=
500 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
502 const std::string
chrome_url("chrome://theme/IDR_THEME_NTP_BACKGROUND");
503 const std::string
search_url(
504 "chrome-search://theme/IDR_THEME_NTP_BACKGROUND");
506 ASSERT_TRUE(LoadImage(rvh
, chrome_url
, &loaded
));
507 EXPECT_FALSE(loaded
) << chrome_url
;
508 ASSERT_TRUE(LoadImage(rvh
, search_url
, &loaded
));
509 EXPECT_TRUE(loaded
) << search_url
;
512 // Flaky on all bots. http://crbug.com/335297.
513 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
,
514 DISABLED_NoThemeBackgroundChangeEventOnTabSwitch
) {
515 InstallThemeSource();
516 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
519 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
520 EXPECT_EQ(1, browser()->tab_strip_model()->count());
523 ui_test_utils::NavigateToURLWithDisposition(
525 GURL(chrome::kChromeUINewTabURL
),
527 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
528 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
529 EXPECT_EQ(2, browser()->tab_strip_model()->count());
531 content::WebContents
* active_tab
=
532 browser()->tab_strip_model()->GetActiveWebContents();
533 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
534 int on_theme_changed_calls
= 0;
535 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
536 &on_theme_changed_calls
));
537 EXPECT_EQ(1, on_theme_changed_calls
);
539 // Activate the previous tab.
540 browser()->tab_strip_model()->ActivateTabAt(0, false);
541 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
543 // Switch back to new tab.
544 browser()->tab_strip_model()->ActivateTabAt(1, false);
545 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
547 // Confirm that new tab got no onthemechanged event while switching tabs.
548 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
549 on_theme_changed_calls
= 0;
550 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
551 &on_theme_changed_calls
));
552 EXPECT_EQ(1, on_theme_changed_calls
);
555 // Flaky on all bots. http://crbug.com/335297, http://crbug.com/265971.
556 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
,
557 DISABLED_SendThemeBackgroundChangedEvent
) {
558 InstallThemeSource();
559 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
562 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
565 ui_test_utils::NavigateToURLWithDisposition(
567 GURL(chrome::kChromeUINewTabURL
),
569 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
570 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
571 EXPECT_EQ(2, browser()->tab_strip_model()->count());
573 // Make sure new tab received an onthemechanged event.
574 content::WebContents
* active_tab
=
575 browser()->tab_strip_model()->GetActiveWebContents();
576 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
577 int on_theme_changed_calls
= 0;
578 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
579 &on_theme_changed_calls
));
580 EXPECT_EQ(1, on_theme_changed_calls
);
582 // Install a new theme.
583 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme2", "snowflake theme"));
585 // Confirm that new tab is notified about the theme changed event.
586 on_theme_changed_calls
= 0;
587 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
588 &on_theme_changed_calls
));
589 EXPECT_EQ(2, on_theme_changed_calls
);
592 // Flaky on all bots. http://crbug.com/253092
593 // Test to verify that the omnibox search query is updated on browser
594 // back button press event.
595 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
596 DISABLED_UpdateSearchQueryOnBackNavigation
) {
597 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
599 // Focus omnibox and confirm overlay isn't shown.
602 // Create an observer to wait for the instant tab to support Instant.
603 content::WindowedNotificationObserver
observer(
604 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
605 content::NotificationService::AllSources());
607 SetOmniboxText("flowers");
608 // Commit the search by pressing 'Enter'.
609 PressEnterAndWaitForNavigation();
612 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
614 // Typing in the new search query in omnibox.
615 SetOmniboxText("cattles");
616 // Commit the search by pressing 'Enter'.
617 PressEnterAndWaitForNavigation();
618 // 'Enter' commits the query as it was typed. This creates a navigation entry
620 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
622 content::WebContents
* active_tab
=
623 browser()->tab_strip_model()->GetActiveWebContents();
624 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
625 content::WindowedNotificationObserver
load_stop_observer(
626 content::NOTIFICATION_LOAD_STOP
,
627 content::Source
<content::NavigationController
>(
628 &active_tab
->GetController()));
629 active_tab
->GetController().GoBack();
630 load_stop_observer
.Wait();
632 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
633 // Commit the search by pressing 'Enter'.
635 PressEnterAndWaitForNavigation();
636 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
639 // Flaky: crbug.com/253092.
640 // Test to verify that the omnibox search query is updated on browser
641 // forward button press events.
642 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
643 DISABLED_UpdateSearchQueryOnForwardNavigation
) {
644 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
646 // Focus omnibox and confirm overlay isn't shown.
649 // Create an observer to wait for the instant tab to support Instant.
650 content::WindowedNotificationObserver
observer(
651 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
652 content::NotificationService::AllSources());
654 SetOmniboxText("flowers");
655 // Commit the search by pressing 'Enter'.
656 PressEnterAndWaitForNavigation();
659 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
661 // Typing in the new search query in omnibox.
662 SetOmniboxText("cattles");
663 // Commit the search by pressing 'Enter'.
664 PressEnterAndWaitForNavigation();
665 // 'Enter' commits the query as it was typed. This creates a navigation entry
667 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
669 content::WebContents
* active_tab
=
670 browser()->tab_strip_model()->GetActiveWebContents();
671 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
672 content::WindowedNotificationObserver
load_stop_observer(
673 content::NOTIFICATION_LOAD_STOP
,
674 content::Source
<content::NavigationController
>(
675 &active_tab
->GetController()));
676 active_tab
->GetController().GoBack();
677 load_stop_observer
.Wait();
679 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
681 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
682 EXPECT_TRUE(active_tab
->GetController().CanGoForward());
683 content::WindowedNotificationObserver
load_stop_observer_2(
684 content::NOTIFICATION_LOAD_STOP
,
685 content::Source
<content::NavigationController
>(
686 &active_tab
->GetController()));
687 active_tab
->GetController().GoForward();
688 load_stop_observer_2
.Wait();
690 // Commit the search by pressing 'Enter'.
692 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
693 PressEnterAndWaitForNavigation();
694 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
697 // Flaky on all bots since re-enabled in r208032, crbug.com/253092
698 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, DISABLED_NavigateBackToNTP
) {
699 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
702 // Open a new tab page.
703 ui_test_utils::NavigateToURLWithDisposition(
705 GURL(chrome::kChromeUINewTabURL
),
707 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
708 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
709 EXPECT_EQ(2, browser()->tab_strip_model()->count());
711 content::WindowedNotificationObserver
observer(
712 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
713 content::NotificationService::AllSources());
714 SetOmniboxText("flowers");
715 PressEnterAndWaitForNavigation();
718 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
720 // Typing in the new search query in omnibox.
721 // Commit the search by pressing 'Enter'.
722 SetOmniboxText("cattles");
723 PressEnterAndWaitForNavigation();
725 // 'Enter' commits the query as it was typed. This creates a navigation entry
727 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
729 // Navigate back to "flowers" search result page.
730 content::WebContents
* active_tab
=
731 browser()->tab_strip_model()->GetActiveWebContents();
732 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
733 content::WindowedNotificationObserver
load_stop_observer(
734 content::NOTIFICATION_LOAD_STOP
,
735 content::Source
<content::NavigationController
>(
736 &active_tab
->GetController()));
737 active_tab
->GetController().GoBack();
738 load_stop_observer
.Wait();
740 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
742 // Navigate back to NTP.
743 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
744 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
745 content::WindowedNotificationObserver
load_stop_observer_2(
746 content::NOTIFICATION_LOAD_STOP
,
747 content::Source
<content::NavigationController
>(
748 &active_tab
->GetController()));
749 active_tab
->GetController().GoBack();
750 load_stop_observer_2
.Wait();
752 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
753 EXPECT_TRUE(chrome::IsInstantNTP(active_tab
));
756 // Flaky: crbug.com/267119
757 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
758 DISABLED_DispatchMVChangeEventWhileNavigatingBackToNTP
) {
760 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
764 ui_test_utils::NavigateToURLWithDisposition(
766 GURL(chrome::kChromeUINewTabURL
),
768 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
769 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
771 content::WebContents
* active_tab
=
772 browser()->tab_strip_model()->GetActiveWebContents();
773 EXPECT_TRUE(UpdateSearchState(active_tab
));
774 EXPECT_EQ(1, on_most_visited_change_calls_
);
776 content::WindowedNotificationObserver
observer(
777 content::NOTIFICATION_LOAD_STOP
,
778 content::NotificationService::AllSources());
779 // Set the text and press enter to navigate from NTP.
780 SetOmniboxText("Pen");
781 PressEnterAndWaitForNavigation();
782 EXPECT_EQ(ASCIIToUTF16("Pen"), omnibox()->GetText());
785 // Navigate back to NTP.
786 content::WindowedNotificationObserver
back_observer(
787 content::NOTIFICATION_LOAD_STOP
,
788 content::NotificationService::AllSources());
789 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
790 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
791 active_tab
->GetController().GoBack();
792 back_observer
.Wait();
794 // Verify that onmostvisitedchange event is dispatched when we navigate from
796 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
797 EXPECT_TRUE(UpdateSearchState(active_tab
));
798 EXPECT_EQ(1, on_most_visited_change_calls_
);
801 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest
, SetPrefetchQuery
) {
802 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
805 content::WindowedNotificationObserver
new_tab_observer(
806 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
807 content::NotificationService::AllSources());
808 ui_test_utils::NavigateToURLWithDisposition(
810 GURL(chrome::kChromeUINewTabURL
),
812 ui_test_utils::BROWSER_TEST_NONE
);
813 new_tab_observer
.Wait();
815 OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs
= 0;
817 // Set the fake response for search query.
818 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
821 net::URLRequestStatus::SUCCESS
);
823 // Navigate to a search results page.
824 content::WindowedNotificationObserver
observer(
825 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
826 content::NotificationService::AllSources());
827 SetOmniboxText("flowers");
828 PressEnterAndWaitForNavigation();
831 // Set the fake response for suggest request. Response has prefetch details.
832 // Ensure that the page received the suggest response, then add another
833 // keystroke to allow the asynchronously-received inline autocomplete
834 // suggestion to actually be inlined (which in turn triggers it to prefetch).
835 fake_factory()->SetFakeResponse(
836 instant_url().Resolve("#q=pup"),
837 "[\"pup\",[\"puppy\", \"puppies\"],[],[],"
838 "{\"google:clientdata\":{\"phi\": 0},"
839 "\"google:suggesttype\":[\"QUERY\", \"QUERY\"],"
840 "\"google:suggestrelevance\":[1400, 9]}]",
842 net::URLRequestStatus::SUCCESS
);
844 SetOmniboxText("pup");
845 while (!omnibox()->model()->autocomplete_controller()->done()) {
846 content::WindowedNotificationObserver
ready_observer(
847 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
848 content::Source
<AutocompleteController
>(
849 omnibox()->model()->autocomplete_controller()));
850 ready_observer
.Wait();
852 SetOmniboxText("pupp");
854 ASSERT_EQ(3, CountSearchProviderSuggestions());
855 content::WebContents
* active_tab
=
856 browser()->tab_strip_model()->GetActiveWebContents();
857 ASSERT_TRUE(UpdateSearchState(active_tab
));
858 ASSERT_TRUE(SearchProvider::ShouldPrefetch(*(
859 omnibox()->model()->result().default_match())));
860 ASSERT_EQ("puppy", prefetch_query_value_
);
863 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest
, ClearPrefetchedResults
) {
864 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
867 content::WindowedNotificationObserver
new_tab_observer(
868 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
869 content::NotificationService::AllSources());
870 ui_test_utils::NavigateToURLWithDisposition(
872 GURL(chrome::kChromeUINewTabURL
),
874 ui_test_utils::BROWSER_TEST_NONE
);
875 new_tab_observer
.Wait();
877 OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs
= 0;
879 // Set the fake response for search query.
880 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
883 net::URLRequestStatus::SUCCESS
);
885 // Navigate to a search results page.
886 content::WindowedNotificationObserver
observer(
887 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
888 content::NotificationService::AllSources());
889 SetOmniboxText("flowers");
890 PressEnterAndWaitForNavigation();
893 // Set the fake response for suggest request. Response has no prefetch
894 // details. Ensure that the page received a blank query to clear the
895 // prefetched results.
896 fake_factory()->SetFakeResponse(
897 instant_url().Resolve("#q=dogs"),
898 "[\"dogs\",[\"https://dogs.com\"],[],[],"
899 "{\"google:suggesttype\":[\"NAVIGATION\"],"
900 "\"google:suggestrelevance\":[2]}]",
902 net::URLRequestStatus::SUCCESS
);
904 SetOmniboxText("dogs");
905 while (!omnibox()->model()->autocomplete_controller()->done()) {
906 content::WindowedNotificationObserver
ready_observer(
907 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
908 content::Source
<AutocompleteController
>(
909 omnibox()->model()->autocomplete_controller()));
910 ready_observer
.Wait();
913 ASSERT_EQ(2, CountSearchProviderSuggestions());
914 ASSERT_FALSE(SearchProvider::ShouldPrefetch(*(
915 omnibox()->model()->result().default_match())));
916 content::WebContents
* active_tab
=
917 browser()->tab_strip_model()->GetActiveWebContents();
918 ASSERT_TRUE(UpdateSearchState(active_tab
));
919 ASSERT_EQ("", prefetch_query_value_
);
922 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, ShowURL
) {
923 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
926 // Create an observer to wait for the instant tab to support Instant.
927 content::WindowedNotificationObserver
observer(
928 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
929 content::NotificationService::AllSources());
931 // Do a search and commit it. The omnibox should show the search terms.
932 SetOmniboxText("foo");
933 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
934 browser()->window()->GetLocationBar()->AcceptInput();
936 EXPECT_FALSE(omnibox()->model()->user_input_in_progress());
937 EXPECT_TRUE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
939 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
941 // Calling ShowURL() should disable search term replacement and show the URL.
942 omnibox()->ShowURL();
943 EXPECT_FALSE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
945 // Don't bother looking for a specific URL; ensuring we're no longer showing
946 // the search terms is sufficient.
947 EXPECT_NE(ASCIIToUTF16("foo"), omnibox()->GetText());
950 // Check that clicking on a result sends the correct referrer.
951 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, Referrer
) {
952 ASSERT_TRUE(test_server()->Start());
954 test_server()->GetURL("files/referrer_policy/referrer-policy-log.html");
955 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
958 // Type a query and press enter to get results.
959 SetOmniboxText("query");
960 PressEnterAndWaitForFrameLoad();
962 // Simulate going to a result.
963 content::WebContents
* contents
=
964 browser()->tab_strip_model()->GetActiveWebContents();
965 std::ostringstream stream
;
966 stream
<< "var link = document.createElement('a');";
967 stream
<< "link.href = \"" << result_url
.spec() << "\";";
968 stream
<< "document.body.appendChild(link);";
969 stream
<< "link.click();";
970 EXPECT_TRUE(content::ExecuteScript(contents
, stream
.str()));
972 content::WaitForLoadStop(contents
);
973 std::string expected_title
=
974 "Referrer is " + instant_url().GetWithEmptyPath().spec();
975 EXPECT_EQ(ASCIIToUTF16(expected_title
), contents
->GetTitle());