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 { return url_
; }
91 const GURL
& current_url() const { return contents_
->GetURL(); }
93 int num_reloads() const {
97 bool can_go_back() const {
98 return contents_
->GetController().CanGoBack();
102 friend class BrowserInstantControllerTest
;
103 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
104 DefaultSearchProviderChanged
);
105 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
106 GoogleBaseURLUpdated
);
109 content::WebContents
* contents_
;
115 TEST_F(BrowserInstantControllerTest
, DefaultSearchProviderChanged
) {
116 size_t num_tests
= arraysize(kTabReloadTestCasesFinalProviderNotGoogle
);
117 ScopedVector
<FakeWebContentsObserver
> observers
;
118 for (size_t i
= 0; i
< num_tests
; ++i
) {
119 const TabReloadTestCase
& test
=
120 kTabReloadTestCasesFinalProviderNotGoogle
[i
];
121 AddTab(browser(), GURL(test
.start_url
));
122 content::WebContents
* contents
=
123 browser()->tab_strip_model()->GetActiveWebContents();
125 // Validate initial instant state.
126 EXPECT_EQ(test
.start_in_instant_process
,
127 instant_service_
->IsInstantProcess(
128 contents
->GetRenderProcessHost()->GetID()))
131 // Setup an observer to verify reload or absence thereof.
132 observers
.push_back(new FakeWebContentsObserver(contents
));
135 SetUserSelectedDefaultSearchProvider("https://bar.com/");
137 for (size_t i
= 0; i
< num_tests
; ++i
) {
138 FakeWebContentsObserver
* observer
= observers
[i
];
139 const TabReloadTestCase
& test
=
140 kTabReloadTestCasesFinalProviderNotGoogle
[i
];
142 if (test
.should_reload
) {
143 // Validate final instant state.
144 EXPECT_EQ(test
.end_in_instant_process
,
145 search::ShouldAssignURLToInstantRenderer(
146 observer
->current_url(), profile()))
150 // Ensure only the expected tabs(contents) reloaded.
151 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
154 if (test
.end_in_local_ntp
) {
155 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
), observer
->current_url())
161 TEST_F(BrowserInstantControllerTest
, GoogleBaseURLUpdated
) {
162 const size_t num_tests
= arraysize(kTabReloadTestCasesFinalProviderGoogle
);
163 ScopedVector
<FakeWebContentsObserver
> observers
;
164 for (size_t i
= 0; i
< num_tests
; ++i
) {
165 const TabReloadTestCase
& test
= kTabReloadTestCasesFinalProviderGoogle
[i
];
166 AddTab(browser(), GURL(test
.start_url
));
167 content::WebContents
* contents
=
168 browser()->tab_strip_model()->GetActiveWebContents();
170 // Validate initial instant state.
171 EXPECT_EQ(test
.start_in_instant_process
,
172 instant_service_
->IsInstantProcess(
173 contents
->GetRenderProcessHost()->GetID()))
176 // Setup an observer to verify reload or absence thereof.
177 observers
.push_back(new FakeWebContentsObserver(contents
));
180 NotifyGoogleBaseURLUpdate("https://www.google.es/");
182 for (size_t i
= 0; i
< num_tests
; ++i
) {
183 const TabReloadTestCase
& test
= kTabReloadTestCasesFinalProviderGoogle
[i
];
184 FakeWebContentsObserver
* observer
= observers
[i
];
186 // Validate final instant state.
187 EXPECT_EQ(test
.end_in_instant_process
,
188 search::ShouldAssignURLToInstantRenderer(observer
->current_url(),
192 // Ensure only the expected tabs(contents) reloaded.
193 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
196 if (test
.end_in_local_ntp
) {
197 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl
), observer
->current_url())
199 // The navigation to Local NTP should be definitive i.e. can't go back.
200 EXPECT_FALSE(observer
->can_go_back());
205 TEST_F(BrowserInstantControllerTest
, BrowserWindowLifecycle
) {
206 scoped_ptr
<BrowserWindow
> window(CreateBrowserWindow());
207 Browser::CreateParams
params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE
);
208 params
.window
= window
.get();
209 scoped_ptr
<Browser
> browser(new Browser(params
));
210 InstantServiceObserver
* bic
;
211 bic
= browser
->instant_controller();
212 EXPECT_TRUE(IsInstantServiceObserver(bic
))
213 << "New BrowserInstantController should register as InstantServiceObserver";
217 EXPECT_FALSE(IsInstantServiceObserver(bic
))
218 << "New BrowserInstantController should register as InstantServiceObserver";
223 } // namespace chrome