NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / ui / browser_instant_controller_unittest.cc
blob077390c76bce9aa369ca23d001dc7988ce0cd16b
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.
5 #include <string>
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"
23 namespace chrome {
25 namespace {
27 class BrowserInstantControllerTest : public InstantUnitTestBase {
28 public:
29 void SetUp() override {
30 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
31 "EmbeddedSearch", "Group1 use_cacheable_ntp:1"));
32 InstantUnitTestBase::SetUp();
35 protected:
36 friend class FakeWebContentsObserver;
39 struct TabReloadTestCase {
40 const char* description;
41 const char* start_url;
42 bool start_in_instant_process;
43 bool should_reload;
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 {
74 public:
75 explicit FakeWebContentsObserver(content::WebContents* contents)
76 : WebContentsObserver(contents),
77 contents_(contents),
78 url_(contents->GetURL()),
79 num_reloads_(0) {}
81 void DidStartNavigationToPendingEntry(
82 const GURL& url,
83 content::NavigationController::ReloadType reload_type) override {
84 if (url_ == url)
85 num_reloads_++;
86 current_url_ = url;
89 const GURL url() const {
90 return url_;
93 const GURL current_url() const {
94 return contents_->GetURL();
97 int num_reloads() const {
98 return num_reloads_;
101 bool can_go_back() const {
102 return contents_->GetController().CanGoBack();
105 protected:
106 friend class BrowserInstantControllerTest;
107 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
108 DefaultSearchProviderChanged);
109 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
110 GoogleBaseURLUpdated);
112 private:
113 content::WebContents* contents_;
114 const GURL& url_;
115 GURL current_url_;
116 int num_reloads_;
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()))
133 << test.description;
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.
148 EXPECT_EQ(
149 test.end_in_instant_process,
150 chrome::ShouldAssignURLToInstantRenderer(
151 observer->current_url(), profile()))
152 << test.description;
155 // Ensure only the expected tabs(contents) reloaded.
156 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
157 << test.description;
159 if (test.end_in_local_ntp) {
160 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), observer->current_url())
161 << test.description;
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()))
179 << test.description;
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.
192 EXPECT_EQ(
193 test.end_in_instant_process,
194 chrome::ShouldAssignURLToInstantRenderer(
195 observer->current_url(), profile()))
196 << test.description;
198 // Ensure only the expected tabs(contents) reloaded.
199 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
200 << test.description;
202 if (test.end_in_local_ntp) {
203 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), observer->current_url())
204 << test.description;
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";
221 browser.reset(NULL);
222 window.reset(NULL);
223 EXPECT_FALSE(IsInstantServiceObserver(bic))
224 << "New BrowserInstantController should register as InstantServiceObserver";
227 } // namespace
229 } // namespace chrome