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 "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/search/instant_service.h"
11 #include "chrome/browser/search/instant_service_observer.h"
12 #include "chrome/browser/search/instant_unittest_base.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/ui/browser_instant_controller.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/pref_names.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 friend class FakeWebContentsObserver
;
32 const struct TabReloadTestCase
{
33 const char* description
;
34 const char* start_url
;
35 bool start_in_instant_process
;
37 bool end_in_instant_process
;
38 } kTabReloadTestCases
[] = {
39 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl
,
41 {"Remote Embedded NTP", "https://www.google.com/instant?strk",
43 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms",
45 {"Other NTP", "https://bar.com/instant?strk",
49 class FakeWebContentsObserver
: public content::WebContentsObserver
{
51 explicit FakeWebContentsObserver(content::WebContents
* contents
)
52 : WebContentsObserver(contents
),
54 url_(contents
->GetURL()),
57 virtual void DidStartNavigationToPendingEntry(
59 content::NavigationController::ReloadType reload_type
) OVERRIDE
{
64 const GURL
url() const {
68 int num_reloads() const {
73 friend class BrowserInstantControllerTest
;
74 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
75 DefaultSearchProviderChanged
);
76 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest
,
77 GoogleBaseURLUpdated
);
80 content::WebContents
* contents_
;
85 TEST_F(BrowserInstantControllerTest
, DefaultSearchProviderChanged
) {
86 size_t num_tests
= arraysize(kTabReloadTestCases
);
87 ScopedVector
<FakeWebContentsObserver
> observers
;
88 for (size_t i
= 0; i
< num_tests
; ++i
) {
89 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
90 AddTab(browser(), GURL(test
.start_url
));
91 content::WebContents
* contents
=
92 browser()->tab_strip_model()->GetActiveWebContents();
94 // Validate initial instant state.
95 EXPECT_EQ(test
.start_in_instant_process
,
96 instant_service_
->IsInstantProcess(
97 contents
->GetRenderProcessHost()->GetID()))
100 // Setup an observer to verify reload or absence thereof.
101 observers
.push_back(new FakeWebContentsObserver(contents
));
104 SetDefaultSearchProvider("https://bar.com/");
106 for (size_t i
= 0; i
< num_tests
; ++i
) {
107 FakeWebContentsObserver
* observer
= observers
[i
];
108 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
110 if (test
.should_reload
) {
111 // Validate final instant state.
113 test
.end_in_instant_process
,
114 chrome::ShouldAssignURLToInstantRenderer(observer
->url(), profile()))
118 // Ensure only the expected tabs(contents) reloaded.
119 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
124 TEST_F(BrowserInstantControllerTest
, GoogleBaseURLUpdated
) {
125 const size_t num_tests
= arraysize(kTabReloadTestCases
);
126 ScopedVector
<FakeWebContentsObserver
> observers
;
127 for (size_t i
= 0; i
< num_tests
; ++i
) {
128 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
129 AddTab(browser(), GURL(test
.start_url
));
130 content::WebContents
* contents
=
131 browser()->tab_strip_model()->GetActiveWebContents();
133 // Validate initial instant state.
134 EXPECT_EQ(test
.start_in_instant_process
,
135 instant_service_
->IsInstantProcess(
136 contents
->GetRenderProcessHost()->GetID()))
139 // Setup an observer to verify reload or absence thereof.
140 observers
.push_back(new FakeWebContentsObserver(contents
));
143 NotifyGoogleBaseURLUpdate("https://www.google.es/");
145 for (size_t i
= 0; i
< num_tests
; ++i
) {
146 const TabReloadTestCase
& test
= kTabReloadTestCases
[i
];
147 FakeWebContentsObserver
* observer
= observers
[i
];
149 if (test
.should_reload
) {
150 // Validate final instant state.
152 test
.end_in_instant_process
,
153 chrome::ShouldAssignURLToInstantRenderer(observer
->url(), profile()))
157 // Ensure only the expected tabs(contents) reloaded.
158 EXPECT_EQ(test
.should_reload
? 1 : 0, observer
->num_reloads())
163 TEST_F(BrowserInstantControllerTest
, BrowserWindowLifecycle
) {
164 scoped_ptr
<BrowserWindow
> window(CreateBrowserWindow());
165 Browser::CreateParams
params(profile(), chrome::HOST_DESKTOP_TYPE_NATIVE
);
166 params
.window
= window
.get();
167 scoped_ptr
<Browser
> browser(new Browser(params
));
168 InstantServiceObserver
* bic
;
169 bic
= browser
->instant_controller();
170 EXPECT_TRUE(IsInstantServiceObserver(bic
))
171 << "New BrowserInstantController should register as InstantServiceObserver";
175 EXPECT_FALSE(IsInstantServiceObserver(bic
))
176 << "New BrowserInstantController should register as InstantServiceObserver";
181 } // namespace chrome