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/url_constants.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_contents_observer.h"
27 class BrowserInstantControllerTest
: public InstantUnitTestBase
{
29 void SetUp() override
{
30 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
31 "EmbeddedSearch", "Group1 use_cacheable_ntp:1"));
32 InstantUnitTestBase::SetUp();
36 friend class FakeWebContentsObserver
;
39 struct TabReloadTestCase
{
40 const char* description
;
41 const char* start_url
;
42 bool start_in_instant_process
;
44 bool end_in_local_ntp
;
45 bool end_in_instant_process
;
48 // Test cases for when Google is the initial, but not final provider.
49 const TabReloadTestCase kTabReloadTestCasesFinalProviderNotGoogle
[] = {
50 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl
,
51 true, true, true, true},
52 {"Remote Embedded NTP", "https://www.google.com/newtab",
53 true, true, false, false},
54 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms",
55 true, true, false, false},
56 {"Other NTP", "https://bar.com/newtab",
57 false, false, false, false}
60 // Test cases for when Google is both the initial and final provider.
61 const TabReloadTestCase kTabReloadTestCasesFinalProviderGoogle
[] = {
62 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl
,
63 true, true, true, true},
64 {"Remote Embedded NTP", "https://www.google.com/newtab",
65 true, false, true, true},
66 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms",
67 true, true, false, false},
68 {"Other NTP", "https://bar.com/newtab",
69 false, false, false, false}
73 class FakeWebContentsObserver
: public content::WebContentsObserver
{
75 explicit FakeWebContentsObserver(content::WebContents
* contents
)
76 : WebContentsObserver(contents
),
78 url_(contents
->GetURL()),
81 void DidStartNavigationToPendingEntry(
83 content::NavigationController::ReloadType reload_type
) override
{
89 const GURL
url() const {
93 const GURL
current_url() const {
94 return contents_
->GetURL();
97 int num_reloads() const {
101 bool can_go_back() const {
102 return contents_
->GetController().CanGoBack();
106 friend class BrowserInstantControllerTest
;
107 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
108 DefaultSearchProviderChanged
);
109 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
110 GoogleBaseURLUpdated
);
113 content::WebContents
* contents_
;
119 TEST_F(BrowserInstantControllerTest
, DefaultSearchProviderChanged
) {
120 size_t num_tests
= arraysize(kTabReloadTestCasesFinalProviderNotGoogle
);
121 ScopedVector
<FakeWebContentsObserver
> observers
;
122 for (size_t i
= 0; i
< num_tests
; ++i
) {
123 const TabReloadTestCase
& test
=
124 kTabReloadTestCasesFinalProviderNotGoogle
[i
];
125 AddTab(browser(), GURL(test
.start_url
));
126 content::WebContents
* contents
=
127 browser()->tab_strip_model()->GetActiveWebContents();
129 // Validate initial instant state.
130 EXPECT_EQ(test
.start_in_instant_process
,
131 instant_service_
->IsInstantProcess(
132 contents
->GetRenderProcessHost()->GetID()))
135 // Setup an observer to verify reload or absence thereof.
136 observers
.push_back(new FakeWebContentsObserver(contents
));
139 SetUserSelectedDefaultSearchProvider("https://bar.com/");
141 for (size_t i
= 0; i
< num_tests
; ++i
) {
142 FakeWebContentsObserver
* observer
= observers
[i
];
143 const TabReloadTestCase
& test
=
144 kTabReloadTestCasesFinalProviderNotGoogle
[i
];
146 if (test
.should_reload
) {
147 // Validate final instant state.
149 test
.end_in_instant_process
,
150 chrome::ShouldAssignURLToInstantRenderer(
151 observer
->current_url(), profile()))
155 // Ensure only the expected tabs(contents) reloaded.
156 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
159 if (test
.end_in_local_ntp
) {
160 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
), observer
->current_url())
166 TEST_F(BrowserInstantControllerTest
, GoogleBaseURLUpdated
) {
167 const size_t num_tests
= arraysize(kTabReloadTestCasesFinalProviderGoogle
);
168 ScopedVector
<FakeWebContentsObserver
> observers
;
169 for (size_t i
= 0; i
< num_tests
; ++i
) {
170 const TabReloadTestCase
& test
= kTabReloadTestCasesFinalProviderGoogle
[i
];
171 AddTab(browser(), GURL(test
.start_url
));
172 content::WebContents
* contents
=
173 browser()->tab_strip_model()->GetActiveWebContents();
175 // Validate initial instant state.
176 EXPECT_EQ(test
.start_in_instant_process
,
177 instant_service_
->IsInstantProcess(
178 contents
->GetRenderProcessHost()->GetID()))
181 // Setup an observer to verify reload or absence thereof.
182 observers
.push_back(new FakeWebContentsObserver(contents
));
185 NotifyGoogleBaseURLUpdate("https://www.google.es/");
187 for (size_t i
= 0; i
< num_tests
; ++i
) {
188 const TabReloadTestCase
& test
= kTabReloadTestCasesFinalProviderGoogle
[i
];
189 FakeWebContentsObserver
* observer
= observers
[i
];
191 // Validate final instant state.
193 test
.end_in_instant_process
,
194 chrome::ShouldAssignURLToInstantRenderer(
195 observer
->current_url(), profile()))
198 // Ensure only the expected tabs(contents) reloaded.
199 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
202 if (test
.end_in_local_ntp
) {
203 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
), observer
->current_url())
205 // The navigation to Local NTP should be definitive i.e. can't go back.
206 EXPECT_FALSE(observer
->can_go_back());
211 TEST_F(BrowserInstantControllerTest
, BrowserWindowLifecycle
) {
212 scoped_ptr
<BrowserWindow
> window(CreateBrowserWindow());
213 Browser::CreateParams
params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE
);
214 params
.window
= window
.get();
215 scoped_ptr
<Browser
> browser(new Browser(params
));
216 InstantServiceObserver
* bic
;
217 bic
= browser
->instant_controller();
218 EXPECT_TRUE(IsInstantServiceObserver(bic
))
219 << "New BrowserInstantController should register as InstantServiceObserver";
223 EXPECT_FALSE(IsInstantServiceObserver(bic
))
224 << "New BrowserInstantController should register as InstantServiceObserver";
229 } // namespace chrome