Componentize AccountReconcilor.
[chromium-blink-merge.git] / chrome / browser / ui / browser_instant_controller_unittest.cc
blob81f7e8a661c71bda16209c635703b02639435bda
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 "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"
23 namespace chrome {
25 namespace {
27 class BrowserInstantControllerTest : public InstantUnitTestBase {
28 protected:
29 friend class FakeWebContentsObserver;
32 const struct TabReloadTestCase {
33 const char* description;
34 const char* start_url;
35 bool start_in_instant_process;
36 bool should_reload;
37 bool end_in_instant_process;
38 } kTabReloadTestCases[] = {
39 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl,
40 true, true, true},
41 {"Remote Embedded NTP", "https://www.google.com/instant?strk",
42 true, true, false},
43 {"Remote Embedded SERP", "https://www.google.com/url?strk&bar=search+terms",
44 true, true, false},
45 {"Other NTP", "https://bar.com/instant?strk",
46 false, false, false}
49 class FakeWebContentsObserver : public content::WebContentsObserver {
50 public:
51 explicit FakeWebContentsObserver(content::WebContents* contents)
52 : WebContentsObserver(contents),
53 contents_(contents),
54 url_(contents->GetURL()),
55 num_reloads_(0) {}
57 virtual void DidStartNavigationToPendingEntry(
58 const GURL& url,
59 content::NavigationController::ReloadType reload_type) OVERRIDE {
60 if (url_ == url)
61 num_reloads_++;
64 const GURL url() const {
65 return url_;
68 int num_reloads() const {
69 return num_reloads_;
72 protected:
73 friend class BrowserInstantControllerTest;
74 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
75 DefaultSearchProviderChanged);
76 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest,
77 GoogleBaseURLUpdated);
79 private:
80 content::WebContents* contents_;
81 const GURL& url_;
82 int num_reloads_;
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()))
98 << test.description;
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.
112 EXPECT_EQ(
113 test.end_in_instant_process,
114 chrome::ShouldAssignURLToInstantRenderer(observer->url(), profile()))
115 << test.description;
118 // Ensure only the expected tabs(contents) reloaded.
119 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
120 << test.description;
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()))
137 << test.description;
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.
151 EXPECT_EQ(
152 test.end_in_instant_process,
153 chrome::ShouldAssignURLToInstantRenderer(observer->url(), profile()))
154 << test.description;
157 // Ensure only the expected tabs(contents) reloaded.
158 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads())
159 << test.description;
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";
173 browser.reset(NULL);
174 window.reset(NULL);
175 EXPECT_FALSE(IsInstantServiceObserver(bic))
176 << "New BrowserInstantController should register as InstantServiceObserver";
179 } // namespace
181 } // namespace chrome