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/autocomplete/autocomplete_match.h"
20 #include "chrome/browser/autocomplete/autocomplete_provider.h"
21 #include "chrome/browser/autocomplete/autocomplete_result.h"
22 #include "chrome/browser/autocomplete/search_provider.h"
23 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
24 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
25 #include "chrome/browser/bookmarks/bookmark_utils.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/extensions/extension_browsertest.h"
28 #include "chrome/browser/extensions/extension_service.h"
29 #include "chrome/browser/favicon/favicon_tab_helper.h"
30 #include "chrome/browser/google/google_url_tracker.h"
31 #include "chrome/browser/history/history_db_task.h"
32 #include "chrome/browser/history/history_service.h"
33 #include "chrome/browser/history/history_service_factory.h"
34 #include "chrome/browser/history/history_types.h"
35 #include "chrome/browser/history/top_sites.h"
36 #include "chrome/browser/profiles/profile.h"
37 #include "chrome/browser/search/instant_service.h"
38 #include "chrome/browser/search/instant_service_factory.h"
39 #include "chrome/browser/search/search.h"
40 #include "chrome/browser/search_engines/template_url_service.h"
41 #include "chrome/browser/search_engines/template_url_service_factory.h"
42 #include "chrome/browser/task_manager/task_manager.h"
43 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
44 #include "chrome/browser/themes/theme_service.h"
45 #include "chrome/browser/themes/theme_service_factory.h"
46 #include "chrome/browser/ui/browser_list.h"
47 #include "chrome/browser/ui/browser_tabstrip.h"
48 #include "chrome/browser/ui/omnibox/omnibox_view.h"
49 #include "chrome/browser/ui/search/instant_tab.h"
50 #include "chrome/browser/ui/search/instant_test_utils.h"
51 #include "chrome/browser/ui/search/search_tab_helper.h"
52 #include "chrome/browser/ui/tabs/tab_strip_model.h"
53 #include "chrome/browser/ui/webui/theme_source.h"
54 #include "chrome/common/chrome_switches.h"
55 #include "chrome/common/instant_types.h"
56 #include "chrome/common/pref_names.h"
57 #include "chrome/common/thumbnail_score.h"
58 #include "chrome/common/url_constants.h"
59 #include "chrome/test/base/in_process_browser_test.h"
60 #include "chrome/test/base/interactive_test_utils.h"
61 #include "chrome/test/base/ui_test_utils.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/browser/web_contents_view.h"
72 #include "content/public/common/bindings_policy.h"
73 #include "content/public/test/browser_test_utils.h"
74 #include "content/public/test/test_utils.h"
75 #include "grit/generated_resources.h"
76 #include "net/base/network_change_notifier.h"
77 #include "net/http/http_status_code.h"
78 #include "net/url_request/test_url_fetcher_factory.h"
79 #include "net/url_request/url_fetcher_impl.h"
80 #include "net/url_request/url_request_status.h"
81 #include "testing/gmock/include/gmock/gmock.h"
82 #include "third_party/skia/include/core/SkBitmap.h"
83 #include "ui/base/l10n/l10n_util.h"
85 using base::ASCIIToUTF16
;
86 using testing::HasSubstr
;
90 // Task used to make sure history has finished processing a request. Intended
91 // for use with BlockUntilHistoryProcessesPendingRequests.
92 class QuittingHistoryDBTask
: public history::HistoryDBTask
{
94 QuittingHistoryDBTask() {}
96 virtual bool RunOnDBThread(history::HistoryBackend
* backend
,
97 history::HistoryDatabase
* db
) OVERRIDE
{
101 virtual void DoneRunOnMainThread() OVERRIDE
{
102 base::MessageLoop::current()->Quit();
106 virtual ~QuittingHistoryDBTask() {}
108 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask
);
111 class FakeNetworkChangeNotifier
: public net::NetworkChangeNotifier
{
113 FakeNetworkChangeNotifier() : connection_type_(CONNECTION_NONE
) {}
115 virtual ConnectionType
GetCurrentConnectionType() const OVERRIDE
{
116 return connection_type_
;
119 void SetConnectionType(ConnectionType type
) {
120 connection_type_
= type
;
121 NotifyObserversOfNetworkChange(type
);
122 base::RunLoop().RunUntilIdle();
125 virtual ~FakeNetworkChangeNotifier() {}
128 ConnectionType connection_type_
;
129 DISALLOW_COPY_AND_ASSIGN(FakeNetworkChangeNotifier
);
133 class InstantExtendedTest
: public InProcessBrowserTest
,
134 public InstantTestBase
{
136 InstantExtendedTest()
137 : on_most_visited_change_calls_(0),
138 most_visited_items_count_(0),
139 first_most_visited_item_id_(0),
140 on_native_suggestions_calls_(0),
143 on_esc_key_press_event_calls_(0),
144 on_focus_changed_calls_(0),
146 on_toggle_voice_search_calls_(0) {
149 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
{
150 chrome::EnableQueryExtractionForTesting();
151 ASSERT_TRUE(https_test_server().Start());
152 GURL instant_url
= https_test_server().GetURL(
153 "files/instant_extended.html?strk=1&");
154 GURL ntp_url
= https_test_server().GetURL(
155 "files/instant_extended_ntp.html?strk=1&");
156 InstantTestBase::Init(instant_url
, ntp_url
, false);
159 int64
GetHistogramCount(const char* name
) {
160 base::HistogramBase
* histogram
=
161 base::StatisticsRecorder::FindHistogram(name
);
163 // If no histogram is found, it's possible that no values have been
164 // recorded yet. Assume that the value is zero.
167 return histogram
->SnapshotSamples()->TotalCount();
170 bool UpdateSearchState(content::WebContents
* contents
) WARN_UNUSED_RESULT
{
171 return GetIntFromJS(contents
, "onMostVisitedChangedCalls",
172 &on_most_visited_change_calls_
) &&
173 GetIntFromJS(contents
, "mostVisitedItemsCount",
174 &most_visited_items_count_
) &&
175 GetIntFromJS(contents
, "firstMostVisitedItemId",
176 &first_most_visited_item_id_
) &&
177 GetIntFromJS(contents
, "onNativeSuggestionsCalls",
178 &on_native_suggestions_calls_
) &&
179 GetIntFromJS(contents
, "onChangeCalls",
180 &on_change_calls_
) &&
181 GetIntFromJS(contents
, "submitCount",
183 GetStringFromJS(contents
, "apiHandle.value",
185 GetIntFromJS(contents
, "onEscKeyPressedCalls",
186 &on_esc_key_press_event_calls_
) &&
187 GetIntFromJS(contents
, "onFocusChangedCalls",
188 &on_focus_changed_calls_
) &&
189 GetBoolFromJS(contents
, "isFocused",
191 GetIntFromJS(contents
, "onToggleVoiceSearchCalls",
192 &on_toggle_voice_search_calls_
) &&
193 GetStringFromJS(contents
, "prefetchQuery", &prefetch_query_value_
);
197 TemplateURL
* GetDefaultSearchProviderTemplateURL() {
198 TemplateURLService
* template_url_service
=
199 TemplateURLServiceFactory::GetForProfile(browser()->profile());
200 if (template_url_service
)
201 return template_url_service
->GetDefaultSearchProvider();
205 bool AddSearchToHistory(base::string16 term
, int visit_count
) {
206 TemplateURL
* template_url
= GetDefaultSearchProviderTemplateURL();
210 HistoryService
* history
= HistoryServiceFactory::GetForProfile(
211 browser()->profile(), Profile::EXPLICIT_ACCESS
);
212 GURL
search(template_url
->url_ref().ReplaceSearchTerms(
213 TemplateURLRef::SearchTermsArgs(term
)));
214 history
->AddPageWithDetails(
215 search
, base::string16(), visit_count
, visit_count
,
216 base::Time::Now(), false, history::SOURCE_BROWSED
);
217 history
->SetKeywordSearchTermsForURL(
218 search
, template_url
->id(), term
);
222 void BlockUntilHistoryProcessesPendingRequests() {
223 HistoryService
* history
= HistoryServiceFactory::GetForProfile(
224 browser()->profile(), Profile::EXPLICIT_ACCESS
);
226 DCHECK(base::MessageLoop::current());
228 CancelableRequestConsumer consumer
;
229 history
->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer
);
230 base::MessageLoop::current()->Run();
233 int CountSearchProviderSuggestions() {
234 return omnibox()->model()->autocomplete_controller()->search_provider()->
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_
;
244 int on_esc_key_press_event_calls_
;
245 std::string query_value_
;
246 int on_focus_changed_calls_
;
248 int on_toggle_voice_search_calls_
;
249 std::string prefetch_query_value_
;
252 class InstantExtendedPrefetchTest
: public InstantExtendedTest
{
254 InstantExtendedPrefetchTest()
255 : factory_(new net::URLFetcherImplFactory()),
256 fake_factory_(new net::FakeURLFetcherFactory(factory_
.get())) {
259 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
{
260 chrome::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 virtual void SetUpCommandLine(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(); }
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
{
289 virtual void SetUpOnMainThread() OVERRIDE
{
290 disable_for_test_
.reset(new net::NetworkChangeNotifier::DisableForTest
);
291 fake_network_change_notifier_
.reset(new FakeNetworkChangeNotifier
);
292 InstantExtendedTest::SetUpOnMainThread();
295 virtual void CleanUpOnMainThread() OVERRIDE
{
296 InstantExtendedTest::CleanUpOnMainThread();
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
);
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
{
315 InstantPolicyTest() {}
318 virtual 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
);
352 DISALLOW_COPY_AND_ASSIGN(InstantPolicyTest
);
355 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, SearchReusesInstantTab
) {
356 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
359 content::WindowedNotificationObserver
observer(
360 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
361 content::NotificationService::AllSources());
362 SetOmniboxText("flowers");
363 PressEnterAndWaitForNavigation();
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()));
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.
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()));
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 PressEnterAndWaitForNavigation();
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();
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 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, OmniboxMarginSetForSearchURLs
) {
435 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
438 // Create an observer to wait for the instant tab to support Instant.
439 content::WindowedNotificationObserver
observer(
440 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
441 content::NotificationService::AllSources());
443 SetOmniboxText("flowers");
444 browser()->window()->GetLocationBar()->AcceptInput();
447 const std::string
& url
=
448 browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec();
449 // Make sure we actually used search_url, not instant_url.
450 ASSERT_THAT(url
, HasSubstr("&is_search"));
451 EXPECT_THAT(url
, HasSubstr("&es_sm="));
454 // Test to verify that switching tabs should not dispatch onmostvisitedchanged
456 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, NoMostVisitedChangedOnTabSwitch
) {
457 // Initialize Instant.
458 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
461 ui_test_utils::NavigateToURLWithDisposition(
463 GURL(chrome::kChromeUINewTabURL
),
465 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
466 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
467 EXPECT_EQ(2, browser()->tab_strip_model()->count());
469 // Make sure new tab received the onmostvisitedchanged event once.
470 content::WebContents
* active_tab
=
471 browser()->tab_strip_model()->GetActiveWebContents();
472 EXPECT_TRUE(UpdateSearchState(active_tab
));
473 EXPECT_EQ(1, on_most_visited_change_calls_
);
475 // Activate the previous tab.
476 browser()->tab_strip_model()->ActivateTabAt(0, false);
478 // Switch back to new tab.
479 browser()->tab_strip_model()->ActivateTabAt(1, false);
481 // Confirm that new tab got no onmostvisitedchanged event.
482 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
483 EXPECT_TRUE(UpdateSearchState(active_tab
));
484 EXPECT_EQ(1, on_most_visited_change_calls_
);
487 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
, ThemeBackgroundAccess
) {
488 InstallThemeSource();
489 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
490 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
492 // The "Instant" New Tab should have access to chrome-search: scheme but not
494 ui_test_utils::NavigateToURLWithDisposition(
496 GURL(chrome::kChromeUINewTabURL
),
498 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
499 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
501 content::RenderViewHost
* rvh
=
502 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost();
504 const std::string
chrome_url("chrome://theme/IDR_THEME_NTP_BACKGROUND");
505 const std::string
search_url(
506 "chrome-search://theme/IDR_THEME_NTP_BACKGROUND");
508 ASSERT_TRUE(LoadImage(rvh
, chrome_url
, &loaded
));
509 EXPECT_FALSE(loaded
) << chrome_url
;
510 ASSERT_TRUE(LoadImage(rvh
, search_url
, &loaded
));
511 EXPECT_TRUE(loaded
) << search_url
;
514 // Flaky on all bots. http://crbug.com/335297.
515 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
,
516 DISABLED_NoThemeBackgroundChangeEventOnTabSwitch
) {
517 InstallThemeSource();
518 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
521 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
522 EXPECT_EQ(1, browser()->tab_strip_model()->count());
525 ui_test_utils::NavigateToURLWithDisposition(
527 GURL(chrome::kChromeUINewTabURL
),
529 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
530 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
531 EXPECT_EQ(2, browser()->tab_strip_model()->count());
533 content::WebContents
* active_tab
=
534 browser()->tab_strip_model()->GetActiveWebContents();
535 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
536 int on_theme_changed_calls
= 0;
537 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
538 &on_theme_changed_calls
));
539 EXPECT_EQ(1, on_theme_changed_calls
);
541 // Activate the previous tab.
542 browser()->tab_strip_model()->ActivateTabAt(0, false);
543 ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
545 // Switch back to new tab.
546 browser()->tab_strip_model()->ActivateTabAt(1, false);
547 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
549 // Confirm that new tab got no onthemechanged event while switching tabs.
550 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
551 on_theme_changed_calls
= 0;
552 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
553 &on_theme_changed_calls
));
554 EXPECT_EQ(1, on_theme_changed_calls
);
557 // Flaky on all bots. http://crbug.com/335297, http://crbug.com/265971.
558 IN_PROC_BROWSER_TEST_F(InstantPolicyTest
,
559 DISABLED_SendThemeBackgroundChangedEvent
) {
560 InstallThemeSource();
561 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
564 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme", "camo theme"));
567 ui_test_utils::NavigateToURLWithDisposition(
569 GURL(chrome::kChromeUINewTabURL
),
571 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
572 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
573 EXPECT_EQ(2, browser()->tab_strip_model()->count());
575 // Make sure new tab received an onthemechanged event.
576 content::WebContents
* active_tab
=
577 browser()->tab_strip_model()->GetActiveWebContents();
578 ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
579 int on_theme_changed_calls
= 0;
580 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
581 &on_theme_changed_calls
));
582 EXPECT_EQ(1, on_theme_changed_calls
);
584 // Install a new theme.
585 ASSERT_NO_FATAL_FAILURE(InstallThemeAndVerify("theme2", "snowflake theme"));
587 // Confirm that new tab is notified about the theme changed event.
588 on_theme_changed_calls
= 0;
589 EXPECT_TRUE(GetIntFromJS(active_tab
, "onThemeChangedCalls",
590 &on_theme_changed_calls
));
591 EXPECT_EQ(2, on_theme_changed_calls
);
594 // Flaky on Mac and Linux Tests bots.
595 #if defined(OS_MACOSX) || defined(OS_LINUX)
596 #define MAYBE_UpdateSearchQueryOnBackNavigation DISABLED_UpdateSearchQueryOnBackNavigation
598 #define MAYBE_UpdateSearchQueryOnBackNavigation UpdateSearchQueryOnBackNavigation
600 // Test to verify that the omnibox search query is updated on browser
601 // back button press event.
602 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
603 MAYBE_UpdateSearchQueryOnBackNavigation
) {
604 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
606 // Focus omnibox and confirm overlay isn't shown.
609 // Create an observer to wait for the instant tab to support Instant.
610 content::WindowedNotificationObserver
observer(
611 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
612 content::NotificationService::AllSources());
614 SetOmniboxText("flowers");
615 // Commit the search by pressing 'Enter'.
616 PressEnterAndWaitForNavigation();
619 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
621 // Typing in the new search query in omnibox.
622 SetOmniboxText("cattles");
623 // Commit the search by pressing 'Enter'.
624 PressEnterAndWaitForNavigation();
625 // 'Enter' commits the query as it was typed. This creates a navigation entry
627 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
629 content::WebContents
* active_tab
=
630 browser()->tab_strip_model()->GetActiveWebContents();
631 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
632 content::WindowedNotificationObserver
load_stop_observer(
633 content::NOTIFICATION_LOAD_STOP
,
634 content::Source
<content::NavigationController
>(
635 &active_tab
->GetController()));
636 active_tab
->GetController().GoBack();
637 load_stop_observer
.Wait();
639 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
640 // Commit the search by pressing 'Enter'.
642 PressEnterAndWaitForNavigation();
643 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
646 // Flaky: crbug.com/253092.
647 // Test to verify that the omnibox search query is updated on browser
648 // forward button press events.
649 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
650 DISABLED_UpdateSearchQueryOnForwardNavigation
) {
651 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
653 // Focus omnibox and confirm overlay isn't shown.
656 // Create an observer to wait for the instant tab to support Instant.
657 content::WindowedNotificationObserver
observer(
658 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
659 content::NotificationService::AllSources());
661 SetOmniboxText("flowers");
662 // Commit the search by pressing 'Enter'.
663 PressEnterAndWaitForNavigation();
666 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
668 // Typing in the new search query in omnibox.
669 SetOmniboxText("cattles");
670 // Commit the search by pressing 'Enter'.
671 PressEnterAndWaitForNavigation();
672 // 'Enter' commits the query as it was typed. This creates a navigation entry
674 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
676 content::WebContents
* active_tab
=
677 browser()->tab_strip_model()->GetActiveWebContents();
678 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
679 content::WindowedNotificationObserver
load_stop_observer(
680 content::NOTIFICATION_LOAD_STOP
,
681 content::Source
<content::NavigationController
>(
682 &active_tab
->GetController()));
683 active_tab
->GetController().GoBack();
684 load_stop_observer
.Wait();
686 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
688 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
689 EXPECT_TRUE(active_tab
->GetController().CanGoForward());
690 content::WindowedNotificationObserver
load_stop_observer_2(
691 content::NOTIFICATION_LOAD_STOP
,
692 content::Source
<content::NavigationController
>(
693 &active_tab
->GetController()));
694 active_tab
->GetController().GoForward();
695 load_stop_observer_2
.Wait();
697 // Commit the search by pressing 'Enter'.
699 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
700 PressEnterAndWaitForNavigation();
701 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
704 // Flaky on all bots since re-enabled in r208032, crbug.com/253092
705 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, DISABLED_NavigateBackToNTP
) {
706 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
709 // Open a new tab page.
710 ui_test_utils::NavigateToURLWithDisposition(
712 GURL(chrome::kChromeUINewTabURL
),
714 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
715 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
716 EXPECT_EQ(2, browser()->tab_strip_model()->count());
718 content::WindowedNotificationObserver
observer(
719 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
720 content::NotificationService::AllSources());
721 SetOmniboxText("flowers");
722 PressEnterAndWaitForNavigation();
725 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
727 // Typing in the new search query in omnibox.
728 // Commit the search by pressing 'Enter'.
729 SetOmniboxText("cattles");
730 PressEnterAndWaitForNavigation();
732 // 'Enter' commits the query as it was typed. This creates a navigation entry
734 EXPECT_EQ(ASCIIToUTF16("cattles"), omnibox()->GetText());
736 // Navigate back to "flowers" search result page.
737 content::WebContents
* active_tab
=
738 browser()->tab_strip_model()->GetActiveWebContents();
739 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
740 content::WindowedNotificationObserver
load_stop_observer(
741 content::NOTIFICATION_LOAD_STOP
,
742 content::Source
<content::NavigationController
>(
743 &active_tab
->GetController()));
744 active_tab
->GetController().GoBack();
745 load_stop_observer
.Wait();
747 EXPECT_EQ(ASCIIToUTF16("flowers"), omnibox()->GetText());
749 // Navigate back to NTP.
750 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
751 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
752 content::WindowedNotificationObserver
load_stop_observer_2(
753 content::NOTIFICATION_LOAD_STOP
,
754 content::Source
<content::NavigationController
>(
755 &active_tab
->GetController()));
756 active_tab
->GetController().GoBack();
757 load_stop_observer_2
.Wait();
759 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
760 EXPECT_TRUE(chrome::IsInstantNTP(active_tab
));
763 // Flaky: crbug.com/267119
764 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
,
765 DISABLED_DispatchMVChangeEventWhileNavigatingBackToNTP
) {
767 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
771 ui_test_utils::NavigateToURLWithDisposition(
773 GURL(chrome::kChromeUINewTabURL
),
775 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB
|
776 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
778 content::WebContents
* active_tab
=
779 browser()->tab_strip_model()->GetActiveWebContents();
780 EXPECT_TRUE(UpdateSearchState(active_tab
));
781 EXPECT_EQ(1, on_most_visited_change_calls_
);
783 content::WindowedNotificationObserver
observer(
784 content::NOTIFICATION_LOAD_STOP
,
785 content::NotificationService::AllSources());
786 // Set the text and press enter to navigate from NTP.
787 SetOmniboxText("Pen");
788 PressEnterAndWaitForNavigation();
789 EXPECT_EQ(ASCIIToUTF16("Pen"), omnibox()->GetText());
792 // Navigate back to NTP.
793 content::WindowedNotificationObserver
back_observer(
794 content::NOTIFICATION_LOAD_STOP
,
795 content::NotificationService::AllSources());
796 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
797 EXPECT_TRUE(active_tab
->GetController().CanGoBack());
798 active_tab
->GetController().GoBack();
799 back_observer
.Wait();
801 // Verify that onmostvisitedchange event is dispatched when we navigate from
803 active_tab
= browser()->tab_strip_model()->GetActiveWebContents();
804 EXPECT_TRUE(UpdateSearchState(active_tab
));
805 EXPECT_EQ(1, on_most_visited_change_calls_
);
808 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest
, SetPrefetchQuery
) {
809 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
812 content::WindowedNotificationObserver
new_tab_observer(
813 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
814 content::NotificationService::AllSources());
815 ui_test_utils::NavigateToURLWithDisposition(
817 GURL(chrome::kChromeUINewTabURL
),
819 ui_test_utils::BROWSER_TEST_NONE
);
820 new_tab_observer
.Wait();
822 omnibox()->model()->autocomplete_controller()->search_provider()->
823 kMinimumTimeBetweenSuggestQueriesMs
= 0;
825 // Set the fake response for search query.
826 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
829 net::URLRequestStatus::SUCCESS
);
831 // Navigate to a search results page.
832 content::WindowedNotificationObserver
observer(
833 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
834 content::NotificationService::AllSources());
835 SetOmniboxText("flowers");
836 PressEnterAndWaitForNavigation();
839 // Set the fake response for suggest request. Response has prefetch details.
840 // Ensure that the page received the prefetch query.
841 fake_factory()->SetFakeResponse(
842 instant_url().Resolve("#q=pupp"),
843 "[\"pupp\",[\"puppy\", \"puppies\"],[],[],"
844 "{\"google:clientdata\":{\"phi\": 0},"
845 "\"google:suggesttype\":[\"QUERY\", \"QUERY\"],"
846 "\"google:suggestrelevance\":[1400, 9]}]",
848 net::URLRequestStatus::SUCCESS
);
850 SetOmniboxText("pupp");
851 while (!omnibox()->model()->autocomplete_controller()->done()) {
852 content::WindowedNotificationObserver
ready_observer(
853 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
854 content::Source
<AutocompleteController
>(
855 omnibox()->model()->autocomplete_controller()));
856 ready_observer
.Wait();
859 ASSERT_EQ(3, CountSearchProviderSuggestions());
860 content::WebContents
* active_tab
=
861 browser()->tab_strip_model()->GetActiveWebContents();
862 ASSERT_TRUE(UpdateSearchState(active_tab
));
863 ASSERT_TRUE(SearchProvider::ShouldPrefetch(*(
864 omnibox()->model()->result().default_match())));
865 ASSERT_EQ("puppy", prefetch_query_value_
);
868 IN_PROC_BROWSER_TEST_F(InstantExtendedPrefetchTest
, ClearPrefetchedResults
) {
869 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
872 content::WindowedNotificationObserver
new_tab_observer(
873 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
874 content::NotificationService::AllSources());
875 ui_test_utils::NavigateToURLWithDisposition(
877 GURL(chrome::kChromeUINewTabURL
),
879 ui_test_utils::BROWSER_TEST_NONE
);
880 new_tab_observer
.Wait();
882 omnibox()->model()->autocomplete_controller()->search_provider()->
883 kMinimumTimeBetweenSuggestQueriesMs
= 0;
885 // Set the fake response for search query.
886 fake_factory()->SetFakeResponse(instant_url().Resolve("#q=flowers"),
889 net::URLRequestStatus::SUCCESS
);
891 // Navigate to a search results page.
892 content::WindowedNotificationObserver
observer(
893 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
894 content::NotificationService::AllSources());
895 SetOmniboxText("flowers");
896 PressEnterAndWaitForNavigation();
899 // Set the fake response for suggest request. Response has no prefetch
900 // details. Ensure that the page received a blank query to clear the
901 // prefetched results.
902 fake_factory()->SetFakeResponse(
903 instant_url().Resolve("#q=dogs"),
904 "[\"dogs\",[\"https://dogs.com\"],[],[],"
905 "{\"google:suggesttype\":[\"NAVIGATION\"],"
906 "\"google:suggestrelevance\":[2]}]",
908 net::URLRequestStatus::SUCCESS
);
910 SetOmniboxText("dogs");
911 while (!omnibox()->model()->autocomplete_controller()->done()) {
912 content::WindowedNotificationObserver
ready_observer(
913 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
914 content::Source
<AutocompleteController
>(
915 omnibox()->model()->autocomplete_controller()));
916 ready_observer
.Wait();
919 ASSERT_EQ(2, CountSearchProviderSuggestions());
920 ASSERT_FALSE(SearchProvider::ShouldPrefetch(*(
921 omnibox()->model()->result().default_match())));
922 content::WebContents
* active_tab
=
923 browser()->tab_strip_model()->GetActiveWebContents();
924 ASSERT_TRUE(UpdateSearchState(active_tab
));
925 ASSERT_EQ("", prefetch_query_value_
);
928 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, ShowURL
) {
929 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
932 // Create an observer to wait for the instant tab to support Instant.
933 content::WindowedNotificationObserver
observer(
934 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED
,
935 content::NotificationService::AllSources());
937 // Do a search and commit it. The omnibox should show the search terms.
938 SetOmniboxText("foo");
939 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
940 browser()->window()->GetLocationBar()->AcceptInput();
942 EXPECT_FALSE(omnibox()->model()->user_input_in_progress());
943 EXPECT_TRUE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
945 EXPECT_EQ(ASCIIToUTF16("foo"), omnibox()->GetText());
947 // Calling ShowURL() should disable search term replacement and show the URL.
948 omnibox()->ShowURL();
949 EXPECT_FALSE(browser()->toolbar_model()->WouldPerformSearchTermReplacement(
951 // Don't bother looking for a specific URL; ensuring we're no longer showing
952 // the search terms is sufficient.
953 EXPECT_NE(ASCIIToUTF16("foo"), omnibox()->GetText());
956 // Check that clicking on a result sends the correct referrer.
957 IN_PROC_BROWSER_TEST_F(InstantExtendedTest
, Referrer
) {
958 ASSERT_TRUE(test_server()->Start());
960 test_server()->GetURL("files/referrer_policy/referrer-policy-log.html");
961 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser()));
964 // Type a query and press enter to get results.
965 SetOmniboxText("query");
966 PressEnterAndWaitForNavigation();
968 // Simulate going to a result.
969 content::WebContents
* contents
=
970 browser()->tab_strip_model()->GetActiveWebContents();
971 std::ostringstream stream
;
972 stream
<< "var link = document.createElement('a');";
973 stream
<< "link.href = \"" << result_url
.spec() << "\";";
974 stream
<< "document.body.appendChild(link);";
975 stream
<< "link.click();";
976 EXPECT_TRUE(content::ExecuteScript(contents
, stream
.str()));
978 content::WaitForLoadStop(contents
);
979 std::string expected_title
=
980 "Referrer is " + instant_url().GetWithEmptyPath().spec();
981 EXPECT_EQ(ASCIIToUTF16(expected_title
), contents
->GetTitle());