Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / browser_instant_controller_unittest.cc
blob975063a81d490a964879c61330fec063f5310d3b
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 { return url_; }
91 const GURL& current_url() const { return contents_->GetURL(); }
93 int num_reloads() const {
94 return num_reloads_;
97 bool can_go_back() const {
98 return contents_->GetController().CanGoBack();
101 protected:
102 friend class BrowserInstantControllerTest;
103 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
104 DefaultSearchProviderChanged);
105 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
106 GoogleBaseURLUpdated);
108 private:
109 content::WebContents* contents_;
110 const GURL& url_;
111 GURL current_url_;
112 int num_reloads_;
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()))
129 << test.description;
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()))
147 << test.description;
150 // Ensure only the expected tabs(contents) reloaded.
151 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
152 << test.description;
154 if (test.end_in_local_ntp) {
155 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), observer->current_url())
156 << test.description;
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()))
174 << test.description;
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(),
189 profile()))
190 << test.description;
192 // Ensure only the expected tabs(contents) reloaded.
193 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
194 << test.description;
196 if (test.end_in_local_ntp) {
197 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), observer->current_url())
198 << test.description;
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";
215 browser.reset(NULL);
216 window.reset(NULL);
217 EXPECT_FALSE(IsInstantServiceObserver(bic))
218 << "New BrowserInstantController should register as InstantServiceObserver";
221 } // namespace
223 } // namespace chrome