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/basictypes.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/metrics/field_trial.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/search/instant_service.h"
12 #include "chrome/browser/search/instant_service_observer.h"
13 #include "chrome/browser/search/instant_unittest_base.h"
14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/ui/browser_instant_controller.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_observer.h"
28 class BrowserInstantControllerTest
: public InstantUnitTestBase
{
30 virtual void SetUp() override
{
31 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
32 "EmbeddedSearch", "Group1 use_cacheable_ntp:1"));
33 InstantUnitTestBase::SetUp();
37 friend class FakeWebContentsObserver
;
40 const struct TabReloadTestCase
{
41 const char* description
;
42 const char* start_url
;
43 bool start_in_instant_process
;
45 bool end_in_instant_process
;
46 } kTabReloadTestCases
[] = {
47 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl
,
49 {"Remote Embedded NTP", "https://www.google.com/instant?strk",
51 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms",
53 {"Other NTP", "https://bar.com/instant?strk",
57 class FakeWebContentsObserver
: public content::WebContentsObserver
{
59 explicit FakeWebContentsObserver(content::WebContents
* contents
)
60 : WebContentsObserver(contents
),
62 url_(contents
->GetURL()),
65 virtual void DidStartNavigationToPendingEntry(
67 content::NavigationController::ReloadType reload_type
) override
{
72 const GURL
url() const {
76 int num_reloads() const {
81 friend class BrowserInstantControllerTest
;
82 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
83 DefaultSearchProviderChanged
);
84 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
85 GoogleBaseURLUpdated
);
88 content::WebContents
* contents_
;
93 TEST_F(BrowserInstantControllerTest
, DefaultSearchProviderChanged
) {
94 size_t num_tests
= arraysize(kTabReloadTestCases
);
95 ScopedVector
<FakeWebContentsObserver
> observers
;
96 for (size_t i
= 0; i
< num_tests
; ++i
) {
97 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
98 AddTab(browser(), GURL(test
.start_url
));
99 content::WebContents
* contents
=
100 browser()->tab_strip_model()->GetActiveWebContents();
102 // Validate initial instant state.
103 EXPECT_EQ(test
.start_in_instant_process
,
104 instant_service_
->IsInstantProcess(
105 contents
->GetRenderProcessHost()->GetID()))
108 // Setup an observer to verify reload or absence thereof.
109 observers
.push_back(new FakeWebContentsObserver(contents
));
112 SetUserSelectedDefaultSearchProvider("https://bar.com/");
114 for (size_t i
= 0; i
< num_tests
; ++i
) {
115 FakeWebContentsObserver
* observer
= observers
[i
];
116 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
118 if (test
.should_reload
) {
119 // Validate final instant state.
121 test
.end_in_instant_process
,
122 chrome::ShouldAssignURLToInstantRenderer(observer
->url(), profile()))
126 // Ensure only the expected tabs(contents) reloaded.
127 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
132 TEST_F(BrowserInstantControllerTest
, GoogleBaseURLUpdated
) {
133 const size_t num_tests
= arraysize(kTabReloadTestCases
);
134 ScopedVector
<FakeWebContentsObserver
> observers
;
135 for (size_t i
= 0; i
< num_tests
; ++i
) {
136 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
137 AddTab(browser(), GURL(test
.start_url
));
138 content::WebContents
* contents
=
139 browser()->tab_strip_model()->GetActiveWebContents();
141 // Validate initial instant state.
142 EXPECT_EQ(test
.start_in_instant_process
,
143 instant_service_
->IsInstantProcess(
144 contents
->GetRenderProcessHost()->GetID()))
147 // Setup an observer to verify reload or absence thereof.
148 observers
.push_back(new FakeWebContentsObserver(contents
));
151 NotifyGoogleBaseURLUpdate("https://www.google.es/");
153 for (size_t i
= 0; i
< num_tests
; ++i
) {
154 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
155 FakeWebContentsObserver
* observer
= observers
[i
];
157 if (test
.should_reload
) {
158 // Validate final instant state.
160 test
.end_in_instant_process
,
161 chrome::ShouldAssignURLToInstantRenderer(observer
->url(), profile()))
165 // Ensure only the expected tabs(contents) reloaded.
166 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
171 TEST_F(BrowserInstantControllerTest
, BrowserWindowLifecycle
) {
172 scoped_ptr
<BrowserWindow
> window(CreateBrowserWindow());
173 Browser::CreateParams
params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE
);
174 params
.window
= window
.get();
175 scoped_ptr
<Browser
> browser(new Browser(params
));
176 InstantServiceObserver
* bic
;
177 bic
= browser
->instant_controller();
178 EXPECT_TRUE(IsInstantServiceObserver(bic
))
179 << "New BrowserInstantController should register as InstantServiceObserver";
183 EXPECT_FALSE(IsInstantServiceObserver(bic
))
184 << "New BrowserInstantController should register as InstantServiceObserver";
189 } // namespace chrome