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/favicon/favicon_tab_helper.h"
24 #include "chrome/browser/history/history_service_factory.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/search/instant_service.h"
27 #include "chrome/browser/search/instant_service_factory.h"
28 #include "chrome/browser/search/search.h"
29 #include "chrome/browser/search_engines/template_url_service_factory.h"
30 #include "chrome/browser/task_manager/task_manager.h"
31 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
32 #include "chrome/browser/themes/theme_service.h"
33 #include "chrome/browser/themes/theme_service_factory.h"
34 #include "chrome/browser/ui/browser_list.h"
35 #include "chrome/browser/ui/browser_tabstrip.h"
36 #include "chrome/browser/ui/omnibox/omnibox_view.h"
37 #include "chrome/browser/ui/search/instant_tab.h"
38 #include "chrome/browser/ui/search/instant_test_utils.h"
39 #include "chrome/browser/ui/search/search_tab_helper.h"
40 #include "chrome/browser/ui/tabs/tab_strip_model.h"
41 #include "chrome/browser/ui/webui/theme_source.h"
42 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/instant_types.h"
44 #include "chrome/common/url_constants.h"
45 #include "chrome/test/base/in_process_browser_test.h"
46 #include "chrome/test/base/interactive_test_utils.h"
47 #include "chrome/test/base/ui_test_utils.h"
48 #include "components/bookmarks/browser/bookmark_utils.h"
49 #include "components/bookmarks/test/bookmark_test_helpers.h"
50 #include "components/history/core/browser/history_db_task.h"
51 #include "components/history/core/browser/history_service.h"
52 #include "components/history/core/browser/history_types.h"
53 #include "components/history/core/browser/top_sites.h"
54 #include "components/history/core/common/thumbnail_score.h"
55 #include "components/omnibox/autocomplete_match.h"
56 #include "components/omnibox/autocomplete_provider.h"
57 #include "components/omnibox/autocomplete_result.h"
58 #include "components/omnibox/omnibox_field_trial.h"
59 #include "components/omnibox/search_provider.h"
60 #include "components/search_engines/template_url_service.h"
61 #include "components/sessions/serialized_navigation_entry.h"
62 #include "content/public/browser/navigation_controller.h"
63 #include "content/public/browser/navigation_entry.h"
64 #include "content/public/browser/notification_service.h"
65 #include "content/public/browser/render_process_host.h"
66 #include "content/public/browser/render_view_host.h"
67 #include "content/public/browser/site_instance.h"
68 #include "content/public/browser/url_data_source.h"
69 #include "content/public/browser/web_contents.h"
70 #include "content/public/common/bindings_policy.h"
71 #include "content/public/test/browser_test_utils.h"
72 #include "content/public/test/test_utils.h"
73 #include "net/base/network_change_notifier.h"
74 #include "net/http/http_status_code.h"
75 #include "net/url_request/test_url_fetcher_factory.h"
76 #include "net/url_request/url_fetcher_impl.h"
77 #include "net/url_request/url_request_status.h"
78 #include "testing/gmock/include/gmock/gmock.h"
79 #include "third_party/skia/include/core/SkBitmap.h"
81 using base::ASCIIToUTF16
;
82 using testing::HasSubstr
;
86 // Task used to make sure history has finished processing a request. Intended
87 // for use with BlockUntilHistoryProcessesPendingRequests.
88 class QuittingHistoryDBTask
: public history::HistoryDBTask
{
90 QuittingHistoryDBTask() {}
92 bool RunOnDBThread(history::HistoryBackend
* backend
,
93 history::HistoryDatabase
* db
) override
{
97 void DoneRunOnMainThread() override
{ base::MessageLoop::current()->Quit(); }
100 ~QuittingHistoryDBTask() override
{}
102 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask
);
105 class FakeNetworkChangeNotifier
: public net::NetworkChangeNotifier
{
107 FakeNetworkChangeNotifier() : connection_type_(CONNECTION_NONE
) {}
109 ConnectionType
GetCurrentConnectionType() const override
{
110 return connection_type_
;
113 void SetConnectionType(ConnectionType type
) {
114 connection_type_
= type
;
115 NotifyObserversOfNetworkChange(type
);
116 base::RunLoop().RunUntilIdle();
119 ~FakeNetworkChangeNotifier() override
{}
122 ConnectionType connection_type_
;
123 DISALLOW_COPY_AND_ASSIGN(FakeNetworkChangeNotifier
);
127 class InstantExtendedTest
: public InProcessBrowserTest
,
128 public InstantTestBase
{
130 InstantExtendedTest()
131 : on_most_visited_change_calls_(0),
132 most_visited_items_count_(0),
133 first_most_visited_item_id_(0),
134 on_native_suggestions_calls_(0),
137 on_esc_key_press_event_calls_(0),
138 on_focus_changed_calls_(0),
140 on_toggle_voice_search_calls_(0) {
143 void SetUpInProcessBrowserTestFixture() override
{
144 chrome::EnableQueryExtractionForTesting();
145 ASSERT_TRUE(https_test_server().Start());
146 GURL instant_url
= https_test_server().GetURL(
147 "files/instant_extended.html?strk=1&");
148 GURL ntp_url
= https_test_server().GetURL(
149 "files/instant_extended_ntp.html?strk=1&");
150 InstantTestBase::Init(instant_url
, ntp_url
, false);
153 int64
GetHistogramCount(const char* name
) {
154 base::HistogramBase
* histogram
=
155 base::StatisticsRecorder::FindHistogram(name
);
157 // If no histogram is found, it's possible that no values have been
158 // recorded yet. Assume that the value is zero.
161 return histogram
->SnapshotSamples()->TotalCount();
164 bool UpdateSearchState(content::WebContents
* contents
) WARN_UNUSED_RESULT
{
165 return GetIntFromJS(contents
, "onMostVisitedChangedCalls",
166 &on_most_visited_change_calls_
) &&
167 GetIntFromJS(contents
, "mostVisitedItemsCount",
168 &most_visited_items_count_
) &&
169 GetIntFromJS(contents
, "firstMostVisitedItemId",
170 &first_most_visited_item_id_
) &&
171 GetIntFromJS(contents
, "onNativeSuggestionsCalls",
172 &on_native_suggestions_calls_
) &&
173 GetIntFromJS(contents
, "onChangeCalls",
174 &on_change_calls_
) &&
175 GetIntFromJS(contents
, "submitCount",
177 GetStringFromJS(contents
, "apiHandle.value",
179 GetIntFromJS(contents
, "onEscKeyPressedCalls",
180 &on_esc_key_press_event_calls_
) &&
181 GetIntFromJS(contents
, "onFocusChangedCalls",
182 &on_focus_changed_calls_
) &&
183 GetBoolFromJS(contents
, "isFocused",
185 GetIntFromJS(contents
, "onToggleVoiceSearchCalls",
186 &on_toggle_voice_search_calls_
) &&
187 GetStringFromJS(contents
, "prefetchQuery", &prefetch_query_value_
);
191 TemplateURL
* GetDefaultSearchProviderTemplateURL() {
192 TemplateURLService
* template_url_service
=
193 TemplateURLServiceFactory::GetForProfile(browser()->profile());
194 if (template_url_service
)
195 return template_url_service
->GetDefaultSearchProvider();
199 bool AddSearchToHistory(base::string16 term
, int visit_count
) {
200 TemplateURL
* template_url
= GetDefaultSearchProviderTemplateURL();
204 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
205 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS
);
206 GURL
search(template_url
->url_ref().ReplaceSearchTerms(
207 TemplateURLRef::SearchTermsArgs(term
),
208 TemplateURLServiceFactory::GetForProfile(
209 browser()->profile())->search_terms_data()));
210 history
->AddPageWithDetails(
211 search
, base::string16(), visit_count
, visit_count
,
212 base::Time::Now(), false, history::SOURCE_BROWSED
);
213 history
->SetKeywordSearchTermsForURL(
214 search
, template_url
->id(), term
);
218 void BlockUntilHistoryProcessesPendingRequests() {
219 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
220 browser()->profile(), ServiceAccessType::EXPLICIT_ACCESS
);
222 DCHECK(base::MessageLoop::current());
224 base::CancelableTaskTracker tracker
;
225 history
->ScheduleDBTask(
226 scoped_ptr
<history::HistoryDBTask
>(
227 new QuittingHistoryDBTask()),
229 base::MessageLoop::current()->Run();
232 int CountSearchProviderSuggestions() {
233 return omnibox()->model()->autocomplete_controller()->search_provider()->
237 int on_most_visited_change_calls_
;
238 int most_visited_items_count_
;
239 int first_most_visited_item_id_
;
240 int on_native_suggestions_calls_
;
241 int on_change_calls_
;
243 int on_esc_key_press_event_calls_
;
244 std::string query_value_
;
245 int on_focus_changed_calls_
;
247 int on_toggle_voice_search_calls_
;
248 std::string prefetch_query_value_
;
251 class InstantExtendedPrefetchTest
: public InstantExtendedTest
{
253 InstantExtendedPrefetchTest()
254 : factory_(new net::URLFetcherImplFactory()),
255 fake_factory_(new net::FakeURLFetcherFactory(factory_
.get())) {
258 void SetUpInProcessBrowserTestFixture() override
{
259 chrome::EnableQueryExtractionForTesting();
260 ASSERT_TRUE(https_test_server().Start());
261 GURL instant_url
= https_test_server().GetURL(
262 "files/instant_extended.html?strk=1&");
263 GURL ntp_url
= https_test_server().GetURL(
264 "files/instant_extended_ntp.html?strk=1&");
265 InstantTestBase::Init(instant_url
, ntp_url
, true);
268 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
269 command_line
->AppendSwitchASCII(
270 switches::kForceFieldTrials
,
271 "EmbeddedSearch/Group11 prefetch_results_srp:1/");
274 net::FakeURLFetcherFactory
* fake_factory() { return fake_factory_
.get(); }
277 // Used to instantiate FakeURLFetcherFactory.
278 scoped_ptr
<net::URLFetcherImplFactory
> factory_
;
280 // Used to mock default search provider suggest response.
281 scoped_ptr
<net::FakeURLFetcherFactory
> fake_factory_
;
283 DISALLOW_COPY_AND_ASSIGN(InstantExtendedPrefetchTest
);
286 class InstantExtendedNetworkTest
: public InstantExtendedTest
{
288 void SetUpOnMainThread() override
{
289 disable_for_test_
.reset(new net::NetworkChangeNotifier::DisableForTest
);
290 fake_network_change_notifier_
.reset(new FakeNetworkChangeNotifier
);
291 InstantExtendedTest::SetUpOnMainThread();
294 void TearDownOnMainThread() override
{
295 InstantExtendedTest::TearDownOnMainThread();
296 fake_network_change_notifier_
.reset();
297 disable_for_test_
.reset();
300 void SetConnectionType(net::NetworkChangeNotifier::ConnectionType type
) {
301 fake_network_change_notifier_
->SetConnectionType(type
);
305 scoped_ptr
<net::NetworkChangeNotifier::DisableForTest
> disable_for_test_
;
306 scoped_ptr
<FakeNetworkChangeNotifier
> fake_network_change_notifier_
;
309 // Test class used to verify chrome-search: scheme and access policy from the
310 // Instant overlay. This is a subclass of |ExtensionBrowserTest| because it
311 // loads a theme that provides a background image.
312 class InstantPolicyTest
: public ExtensionBrowserTest
, public InstantTestBase
{
314 InstantPolicyTest() {}
317 void SetUpInProcessBrowserTestFixture() override
{
318 ASSERT_TRUE(https_test_server().Start());
319 GURL instant_url
= https_test_server().GetURL(
320 "files/instant_extended.html?strk=1&");
321 GURL ntp_url
= https_test_server().GetURL(
322 "files/instant_extended_ntp.html?strk=1&");
323 InstantTestBase::Init(instant_url
, ntp_url
, false);
326 void InstallThemeSource() {
327 ThemeSource
* theme
= new ThemeSource(profile());
328 content::URLDataSource::Add(profile(), theme
);
331 void InstallThemeAndVerify(const std::string
& theme_dir
,
332 const std::string
& theme_name
) {
333 const extensions::Extension
* theme
=
334 ThemeServiceFactory::GetThemeForProfile(
335 ExtensionBrowserTest::browser()->profile());
336 // If there is already a theme installed, the current theme should be
337 // disabled and the new one installed + enabled.
338 int expected_change
= theme
? 0 : 1;
340 const base::FilePath theme_path
= test_data_dir_
.AppendASCII(theme_dir
);
341 ASSERT_TRUE(InstallExtensionWithUIAutoConfirm(
342 theme_path
, expected_change
, ExtensionBrowserTest::browser()));
343 const extensions::Extension
* new_theme
=
344 ThemeServiceFactory::GetThemeForProfile(
345 ExtensionBrowserTest::browser()->profile());
346 ASSERT_NE(static_cast<extensions::Extension
*>(NULL
), new_theme
);
347 ASSERT_EQ(new_theme
->name(), theme_name
);
351 DISALLOW_COPY_AND_ASSIGN(InstantPolicyTest
);
354 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, SearchReusesInstantTab
) {
355 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
358 content::WindowedNotificationObserver
observer(
359 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
360 content::NotificationService::AllSources());
361 SetOmniboxText("flowers");
362 PressEnterAndWaitForFrameLoad();
365 // Just did a regular search.
366 content::WebContents
* active_tab
=
367 browser()->tab_strip_model()->GetActiveWebContents();
368 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=flowers"));
369 ASSERT_TRUE(UpdateSearchState(active_tab
));
370 ASSERT_EQ(0, submit_count_
);
372 SetOmniboxText("puppies");
373 PressEnterAndWaitForNavigation();
375 // Should have reused the tab and sent an onsubmit message.
376 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
377 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=puppies"));
378 ASSERT_TRUE(UpdateSearchState(active_tab
));
379 EXPECT_EQ(1, submit_count_
);
382 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
383 SearchDoesntReuseInstantTabWithoutSupport
) {
384 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
387 // Don't wait for the navigation to complete.
388 SetOmniboxText("flowers");
389 browser()->window()->GetLocationBar()->AcceptInput();
391 SetOmniboxText("puppies");
392 browser()->window()->GetLocationBar()->AcceptInput();
394 // Should not have reused the tab.
396 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec(),
397 HasSubstr("q=puppies"));
400 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
401 TypedSearchURLDoesntReuseInstantTab
) {
402 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
405 // Create an observer to wait for the instant tab to support Instant.
406 content::WindowedNotificationObserver
observer_1(
407 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
408 content::NotificationService::AllSources());
409 SetOmniboxText("flowers");
410 PressEnterAndWaitForFrameLoad();
413 // Just did a regular search.
414 content::WebContents
* active_tab
=
415 browser()->tab_strip_model()->GetActiveWebContents();
416 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=flowers"));
417 ASSERT_TRUE(UpdateSearchState(active_tab
));
418 ASSERT_EQ(0, submit_count_
);
420 // Typed in a search URL "by hand".
421 content::WindowedNotificationObserver
observer_2(
422 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
423 content::NotificationService::AllSources());
424 SetOmniboxText(instant_url().Resolve("#q=puppies").spec());
425 PressEnterAndWaitForNavigation();
428 // Should not have reused the tab.
429 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
430 ASSERT_THAT(active_tab
->GetURL().spec(), HasSubstr("q=puppies"));
433 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, OmniboxMarginSetForSearchURLs
) {
434 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
437 // Create an observer to wait for the instant tab to support Instant.
438 content::WindowedNotificationObserver
observer(
439 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
440 content::NotificationService::AllSources());
442 SetOmniboxText("flowers");
443 browser()->window()->GetLocationBar()->AcceptInput();
446 const std::string
& url
=
447 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec();
448 // Make sure we actually used search_url, not instant_url.
449 ASSERT_THAT(url
, HasSubstr("&is_search"));
450 EXPECT_THAT(url
, HasSubstr("&es_sm="));
453 // Test to verify that switching tabs should not dispatch onmostvisitedchanged
455 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, NoMostVisitedChangedOnTabSwitch
) {
456 // Initialize Instant.
457 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
460 ui_test_utils::NavigateToURLWithDisposition(
462 GURL(chrome::kChromeUINewTabURL
),
464 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
465 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
466 EXPECT_EQ(2, browser()->tab_strip_model()->count());
468 // Make sure new tab received the onmostvisitedchanged event once.
469 content::WebContents
* active_tab
=
470 browser()->tab_strip_model()->GetActiveWebContents();
471 EXPECT_TRUE(UpdateSearchState(active_tab
));
472 EXPECT_EQ(1, on_most_visited_change_calls_
);
474 // Activate the previous tab.
475 browser()->tab_strip_model()->ActivateTabAt(0, false);
477 // Switch back to new tab.
478 browser()->tab_strip_model()->ActivateTabAt(1, false);
480 // Confirm that new tab got no onmostvisitedchanged event.
481 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
482 EXPECT_TRUE(UpdateSearchState(active_tab
));
483 EXPECT_EQ(1, on_most_visited_change_calls_
);
486 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
, ThemeBackgroundAccess
) {
487 InstallThemeSource();
488 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
489 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
491 // The "Instant" New Tab should have access to chrome-search: scheme but not
493 ui_test_utils::NavigateToURLWithDisposition(
495 GURL(chrome::kChromeUINewTabURL
),
497 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
498 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
500 content::RenderViewHost
* rvh
=
501 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
503 const std::string
chrome_url("chrome://theme/IDR_THEME_NTP_BACKGROUND");
504 const std::string
search_url(
505 "chrome-search://theme/IDR_THEME_NTP_BACKGROUND");
507 ASSERT_TRUE(LoadImage(rvh
, chrome_url
, &loaded
));
508 EXPECT_FALSE(loaded
) << chrome_url
;
509 ASSERT_TRUE(LoadImage(rvh
, search_url
, &loaded
));
510 EXPECT_TRUE(loaded
) << search_url
;
513 // Flaky on all bots. http://crbug.com/335297.
514 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
,
515 DISABLED_NoThemeBackgroundChangeEventOnTabSwitch
) {
516 InstallThemeSource();
517 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
520 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
521 EXPECT_EQ(1, browser()->tab_strip_model()->count());
524 ui_test_utils::NavigateToURLWithDisposition(
526 GURL(chrome::kChromeUINewTabURL
),
528 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
529 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
530 EXPECT_EQ(2, browser()->tab_strip_model()->count());
532 content::WebContents
* active_tab
=
533 browser()->tab_strip_model()->GetActiveWebContents();
534 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
535 int on_theme_changed_calls
= 0;
536 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
537 &on_theme_changed_calls
));
538 EXPECT_EQ(1, on_theme_changed_calls
);
540 // Activate the previous tab.
541 browser()->tab_strip_model()->ActivateTabAt(0, false);
542 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
544 // Switch back to new tab.
545 browser()->tab_strip_model()->ActivateTabAt(1, false);
546 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
548 // Confirm that new tab got no onthemechanged event while switching tabs.
549 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
550 on_theme_changed_calls
= 0;
551 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
552 &on_theme_changed_calls
));
553 EXPECT_EQ(1, on_theme_changed_calls
);
556 // Flaky on all bots. http://crbug.com/335297, http://crbug.com/265971.
557 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
,
558 DISABLED_SendThemeBackgroundChangedEvent
) {
559 InstallThemeSource();
560 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
563 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
566 ui_test_utils::NavigateToURLWithDisposition(
568 GURL(chrome::kChromeUINewTabURL
),
570 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
571 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
572 EXPECT_EQ(2, browser()->tab_strip_model()->count());
574 // Make sure new tab received an onthemechanged event.
575 content::WebContents
* active_tab
=
576 browser()->tab_strip_model()->GetActiveWebContents();
577 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
578 int on_theme_changed_calls
= 0;
579 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
580 &on_theme_changed_calls
));
581 EXPECT_EQ(1, on_theme_changed_calls
);
583 // Install a new theme.
584 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme2", "snowflake theme"));
586 // Confirm that new tab is notified about the theme changed event.
587 on_theme_changed_calls
= 0;
588 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
589 &on_theme_changed_calls
));
590 EXPECT_EQ(2, on_theme_changed_calls
);
593 // Flaky on all bots. http://crbug.com/253092
594 // Test to verify that the omnibox search query is updated on browser
595 // back button press event.
596 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
597 DISABLED_UpdateSearchQueryOnBackNavigation
) {
598 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
600 // Focus omnibox and confirm overlay isn't shown.
603 // Create an observer to wait for the instant tab to support Instant.
604 content::WindowedNotificationObserver
observer(
605 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
606 content::NotificationService::AllSources());
608 SetOmniboxText("flowers");
609 // Commit the search by pressing 'Enter'.
610 PressEnterAndWaitForNavigation();
613 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
615 // Typing in the new search query in omnibox.
616 SetOmniboxText("cattles");
617 // Commit the search by pressing 'Enter'.
618 PressEnterAndWaitForNavigation();
619 // 'Enter' commits the query as it was typed. This creates a navigation entry
621 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
623 content::WebContents
* active_tab
=
624 browser()->tab_strip_model()->GetActiveWebContents();
625 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
626 content::WindowedNotificationObserver
load_stop_observer(
627 content::NOTIFICATION_LOAD_STOP
,
628 content::Source
<content::NavigationController
>(
629 &active_tab
->GetController()));
630 active_tab
->GetController().GoBack();
631 load_stop_observer
.Wait();
633 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
634 // Commit the search by pressing 'Enter'.
636 PressEnterAndWaitForNavigation();
637 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
640 // Flaky: crbug.com/253092.
641 // Test to verify that the omnibox search query is updated on browser
642 // forward button press events.
643 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
644 DISABLED_UpdateSearchQueryOnForwardNavigation
) {
645 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
647 // Focus omnibox and confirm overlay isn't shown.
650 // Create an observer to wait for the instant tab to support Instant.
651 content::WindowedNotificationObserver
observer(
652 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
653 content::NotificationService::AllSources());
655 SetOmniboxText("flowers");
656 // Commit the search by pressing 'Enter'.
657 PressEnterAndWaitForNavigation();
660 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
662 // Typing in the new search query in omnibox.
663 SetOmniboxText("cattles");
664 // Commit the search by pressing 'Enter'.
665 PressEnterAndWaitForNavigation();
666 // 'Enter' commits the query as it was typed. This creates a navigation entry
668 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
670 content::WebContents
* active_tab
=
671 browser()->tab_strip_model()->GetActiveWebContents();
672 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
673 content::WindowedNotificationObserver
load_stop_observer(
674 content::NOTIFICATION_LOAD_STOP
,
675 content::Source
<content::NavigationController
>(
676 &active_tab
->GetController()));
677 active_tab
->GetController().GoBack();
678 load_stop_observer
.Wait();
680 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
682 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
683 EXPECT_TRUE(active_tab
->GetController().CanGoForward());
684 content::WindowedNotificationObserver
load_stop_observer_2(
685 content::NOTIFICATION_LOAD_STOP
,
686 content::Source
<content::NavigationController
>(
687 &active_tab
->GetController()));
688 active_tab
->GetController().GoForward();
689 load_stop_observer_2
.Wait();
691 // Commit the search by pressing 'Enter'.
693 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
694 PressEnterAndWaitForNavigation();
695 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
698 // Flaky on all bots since re-enabled in r208032, crbug.com/253092
699 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, DISABLED_NavigateBackToNTP
) {
700 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
703 // Open a new tab page.
704 ui_test_utils::NavigateToURLWithDisposition(
706 GURL(chrome::kChromeUINewTabURL
),
708 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
709 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
710 EXPECT_EQ(2, browser()->tab_strip_model()->count());
712 content::WindowedNotificationObserver
observer(
713 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
714 content::NotificationService::AllSources());
715 SetOmniboxText("flowers");
716 PressEnterAndWaitForNavigation();
719 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
721 // Typing in the new search query in omnibox.
722 // Commit the search by pressing 'Enter'.
723 SetOmniboxText("cattles");
724 PressEnterAndWaitForNavigation();
726 // 'Enter' commits the query as it was typed. This creates a navigation entry
728 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
730 // Navigate back to "flowers" search result page.
731 content::WebContents
* active_tab
=
732 browser()->tab_strip_model()->GetActiveWebContents();
733 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
734 content::WindowedNotificationObserver
load_stop_observer(
735 content::NOTIFICATION_LOAD_STOP
,
736 content::Source
<content::NavigationController
>(
737 &active_tab
->GetController()));
738 active_tab
->GetController().GoBack();
739 load_stop_observer
.Wait();
741 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
743 // Navigate back to NTP.
744 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
745 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
746 content::WindowedNotificationObserver
load_stop_observer_2(
747 content::NOTIFICATION_LOAD_STOP
,
748 content::Source
<content::NavigationController
>(
749 &active_tab
->GetController()));
750 active_tab
->GetController().GoBack();
751 load_stop_observer_2
.Wait();
753 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
754 EXPECT_TRUE(chrome::IsInstantNTP(active_tab
));
757 // Flaky: crbug.com/267119
758 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
759 DISABLED_DispatchMVChangeEventWhileNavigatingBackToNTP
) {
761 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
765 ui_test_utils::NavigateToURLWithDisposition(
767 GURL(chrome::kChromeUINewTabURL
),
769 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
770 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
772 content::WebContents
* active_tab
=
773 browser()->tab_strip_model()->GetActiveWebContents();
774 EXPECT_TRUE(UpdateSearchState(active_tab
));
775 EXPECT_EQ(1, on_most_visited_change_calls_
);
777 content::WindowedNotificationObserver
observer(
778 content::NOTIFICATION_LOAD_STOP
,
779 content::NotificationService::AllSources());
780 // Set the text and press enter to navigate from NTP.
781 SetOmniboxText("Pen");
782 PressEnterAndWaitForNavigation();
783 EXPECT_EQ(ASCIIToUTF16("Pen"), omnibox()->GetText());
786 // Navigate back to NTP.
787 content::WindowedNotificationObserver
back_observer(
788 content::NOTIFICATION_LOAD_STOP
,
789 content::NotificationService::AllSources());
790 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
791 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
792 active_tab
->GetController().GoBack();
793 back_observer
.Wait();
795 // Verify that onmostvisitedchange event is dispatched when we navigate from
797 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
798 EXPECT_TRUE(UpdateSearchState(active_tab
));
799 EXPECT_EQ(1, on_most_visited_change_calls_
);
802 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest
, SetPrefetchQuery
) {
803 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
806 content::WindowedNotificationObserver
new_tab_observer(
807 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
808 content::NotificationService::AllSources());
809 ui_test_utils::NavigateToURLWithDisposition(
811 GURL(chrome::kChromeUINewTabURL
),
813 ui_test_utils::BROWSER_TEST_NONE
);
814 new_tab_observer
.Wait();
816 OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs
= 0;
818 // Set the fake response for search query.
819 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
822 net::URLRequestStatus::SUCCESS
);
824 // Navigate to a search results page.
825 content::WindowedNotificationObserver
observer(
826 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
827 content::NotificationService::AllSources());
828 SetOmniboxText("flowers");
829 PressEnterAndWaitForNavigation();
832 // Set the fake response for suggest request. Response has prefetch details.
833 // Ensure that the page received the suggest response, then add another
834 // keystroke to allow the asynchronously-received inline autocomplete
835 // suggestion to actually be inlined (which in turn triggers it to prefetch).
836 fake_factory()->SetFakeResponse(
837 instant_url().Resolve("#q=pup"),
838 "[\"pup\",[\"puppy\", \"puppies\"],[],[],"
839 "{\"google:clientdata\":{\"phi\": 0},"
840 "\"google:suggesttype\":[\"QUERY\", \"QUERY\"],"
841 "\"google:suggestrelevance\":[1400, 9]}]",
843 net::URLRequestStatus::SUCCESS
);
845 SetOmniboxText("pup");
846 while (!omnibox()->model()->autocomplete_controller()->done()) {
847 content::WindowedNotificationObserver
ready_observer(
848 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
849 content::Source
<AutocompleteController
>(
850 omnibox()->model()->autocomplete_controller()));
851 ready_observer
.Wait();
853 SetOmniboxText("pupp");
855 ASSERT_EQ(3, CountSearchProviderSuggestions());
856 content::WebContents
* active_tab
=
857 browser()->tab_strip_model()->GetActiveWebContents();
858 ASSERT_TRUE(UpdateSearchState(active_tab
));
859 ASSERT_TRUE(SearchProvider::ShouldPrefetch(*(
860 omnibox()->model()->result().default_match())));
861 ASSERT_EQ("puppy", prefetch_query_value_
);
864 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest
, ClearPrefetchedResults
) {
865 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
868 content::WindowedNotificationObserver
new_tab_observer(
869 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
870 content::NotificationService::AllSources());
871 ui_test_utils::NavigateToURLWithDisposition(
873 GURL(chrome::kChromeUINewTabURL
),
875 ui_test_utils::BROWSER_TEST_NONE
);
876 new_tab_observer
.Wait();
878 OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs
= 0;
880 // Set the fake response for search query.
881 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
884 net::URLRequestStatus::SUCCESS
);
886 // Navigate to a search results page.
887 content::WindowedNotificationObserver
observer(
888 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
889 content::NotificationService::AllSources());
890 SetOmniboxText("flowers");
891 PressEnterAndWaitForNavigation();
894 // Set the fake response for suggest request. Response has no prefetch
895 // details. Ensure that the page received a blank query to clear the
896 // prefetched results.
897 fake_factory()->SetFakeResponse(
898 instant_url().Resolve("#q=dogs"),
899 "[\"dogs\",[\"https://dogs.com\"],[],[],"
900 "{\"google:suggesttype\":[\"NAVIGATION\"],"
901 "\"google:suggestrelevance\":[2]}]",
903 net::URLRequestStatus::SUCCESS
);
905 SetOmniboxText("dogs");
906 while (!omnibox()->model()->autocomplete_controller()->done()) {
907 content::WindowedNotificationObserver
ready_observer(
908 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
909 content::Source
<AutocompleteController
>(
910 omnibox()->model()->autocomplete_controller()));
911 ready_observer
.Wait();
914 ASSERT_EQ(2, CountSearchProviderSuggestions());
915 ASSERT_FALSE(SearchProvider::ShouldPrefetch(*(
916 omnibox()->model()->result().default_match())));
917 content::WebContents
* active_tab
=
918 browser()->tab_strip_model()->GetActiveWebContents();
919 ASSERT_TRUE(UpdateSearchState(active_tab
));
920 ASSERT_EQ("", prefetch_query_value_
);
923 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, ShowURL
) {
924 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
927 // Create an observer to wait for the instant tab to support Instant.
928 content::WindowedNotificationObserver
observer(
929 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
930 content::NotificationService::AllSources());
932 // Do a search and commit it. The omnibox should show the search terms.
933 SetOmniboxText("foo");
934 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
935 browser()->window()->GetLocationBar()->AcceptInput();
937 EXPECT_FALSE(omnibox()->model()->user_input_in_progress());
938 EXPECT_TRUE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
940 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
942 // Calling ShowURL() should disable search term replacement and show the URL.
943 omnibox()->ShowURL();
944 EXPECT_FALSE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
946 // Don't bother looking for a specific URL; ensuring we're no longer showing
947 // the search terms is sufficient.
948 EXPECT_NE(ASCIIToUTF16("foo"), omnibox()->GetText());
951 // Check that clicking on a result sends the correct referrer.
952 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, Referrer
) {
953 ASSERT_TRUE(test_server()->Start());
955 test_server()->GetURL("files/referrer_policy/referrer-policy-log.html");
956 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
959 // Type a query and press enter to get results.
960 SetOmniboxText("query");
961 PressEnterAndWaitForFrameLoad();
963 // Simulate going to a result.
964 content::WebContents
* contents
=
965 browser()->tab_strip_model()->GetActiveWebContents();
966 std::ostringstream stream
;
967 stream
<< "var link = document.createElement('a');";
968 stream
<< "link.href = \"" << result_url
.spec() << "\";";
969 stream
<< "document.body.appendChild(link);";
970 stream
<< "link.click();";
971 EXPECT_TRUE(content::ExecuteScript(contents
, stream
.str()));
973 content::WaitForLoadStop(contents
);
974 std::string expected_title
=
975 "Referrer is " + instant_url().GetWithEmptyPath().spec();
976 EXPECT_EQ(ASCIIToUTF16(expected_title
), contents
->GetTitle());