OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <string> | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/search/instant_service.h" | |
10 #include "chrome/browser/search/instant_unittest_base.h" | |
11 #include "chrome/browser/search/search.h" | |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "chrome/common/url_constants.h" | |
15 #include "content/public/browser/navigation_controller.h" | |
16 #include "content/public/browser/render_process_host.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "content/public/browser/web_contents_observer.h" | |
19 | |
20 namespace chrome { | |
21 | |
22 namespace { | |
23 | |
24 class BrowserInstantControllerTest : public InstantUnitTestBase { | |
25 protected: | |
26 friend class FakeWebContentsObserver; | |
27 }; | |
28 | |
29 const struct TabReloadTestCase { | |
30 const char* description; | |
31 const char* start_url; | |
32 bool start_in_instant_process; | |
33 bool should_reload; | |
34 bool end_in_instant_process; | |
35 } kTabReloadTestCases[] = { | |
36 {"Local Embedded NTP", chrome::kChromeSearchLocalNtpUrl, | |
37 true, true, true}, | |
38 {"Remote Embedded NTP", "https://www.google.com/instant?strk", | |
39 true, true, false}, | |
40 {"Remote Embedded SERP", "https://www.google.com/url?strk", | |
41 true, true, false}, | |
42 {"Other NTP", "https://bar.com/instant?strk", | |
43 false, false, false} | |
44 }; | |
45 | |
46 class FakeWebContentsObserver : public content::WebContentsObserver { | |
47 public: | |
48 FakeWebContentsObserver(BrowserInstantControllerTest* base_test, | |
49 content::WebContents* contents) | |
50 : WebContentsObserver(contents), | |
51 base_test_(base_test), | |
52 num_reloads_(0) {} | |
53 | |
54 virtual void NavigateToPendingEntry( | |
55 const GURL& url, | |
56 content::NavigationController::ReloadType reload_type) OVERRIDE { | |
57 reload_type_ = reload_type; | |
58 | |
59 // The tab reload event doesn't work with BrowserWithTestWindowTest. | |
60 // So we capture the NavigateToPendingEntry, and use the | |
61 // BrowserWithTestWindowTest::NavigateAndCommit to simulate the complete | |
62 // reload. Note that this will again trigger NavigateToPendingEntry, so we | |
63 // remove this as observer. | |
64 content::NavigationController* controller = | |
65 &web_contents()->GetController(); | |
66 Observe(NULL); | |
67 | |
68 if (web_contents()->GetURL() == url) | |
69 num_reloads_++; | |
70 | |
71 base_test_->NavigateAndCommit(controller, url); | |
72 } | |
73 | |
74 int num_reloads() const { | |
75 return num_reloads_; | |
76 } | |
77 | |
78 const content::NavigationController::ReloadType reload_type() { | |
79 return reload_type_; | |
80 } | |
81 | |
82 protected: | |
83 friend class BrowserInstantControllerTest; | |
84 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, | |
85 DefaultSearchProviderChanged); | |
86 FRIEND_TEST_ALL_PREFIXES(BrowserInstantControllerTest, | |
87 GoogleBaseURLUpdated); | |
88 | |
89 private: | |
90 BrowserInstantControllerTest* base_test_; | |
91 int num_reloads_; | |
92 content::NavigationController::ReloadType reload_type_; | |
93 }; | |
94 | |
95 TEST_F(BrowserInstantControllerTest, DefaultSearchProviderChanged) { | |
96 size_t numTestCases = arraysize(kTabReloadTestCases); | |
97 FakeWebContentsObserver** observers = | |
98 new FakeWebContentsObserver*[numTestCases]; | |
99 for (size_t i = 0; i < numTestCases; ++i) { | |
100 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
101 AddTab(browser(), GURL(test.start_url)); | |
102 content::WebContents* contents = | |
103 browser()->tab_strip_model()->GetActiveWebContents(); | |
104 | |
105 // Validate initial instant state. | |
106 EXPECT_EQ(test.start_in_instant_process, chrome::IsInstantNTP(contents)) | |
samarth
2013/08/14 00:24:14
This is only passing because your "Remote Embedded
Anuj
2013/08/14 06:38:57
Done.
| |
107 << test.description; | |
108 EXPECT_EQ(test.start_in_instant_process, | |
109 instant_service_->IsInstantProcess( | |
110 contents->GetRenderProcessHost()->GetID())) | |
111 << test.description; | |
112 | |
113 // Setup an observer to verify reload or absence thereof. | |
114 observers[i] = new FakeWebContentsObserver(this, contents); | |
samarth
2013/08/14 00:24:14
Just use vector<linked_ptr<FakeWebContentsObserver
Anuj
2013/08/14 06:38:57
Done.
| |
115 } | |
116 | |
117 SetSearchProvider("https://bar.com/"); | |
118 | |
119 for (size_t i = 0; i < numTestCases; ++i) { | |
120 FakeWebContentsObserver* observer = observers[i]; | |
121 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
122 content::WebContents* contents = observer->web_contents(); | |
123 | |
124 // Validate final instant state. | |
125 EXPECT_EQ(test.end_in_instant_process, chrome::IsInstantNTP(contents)) | |
samarth
2013/08/14 00:24:14
Remove this.
Anuj
2013/08/14 06:38:57
Done.
| |
126 << test.description; | |
127 EXPECT_EQ(test.end_in_instant_process, | |
128 instant_service_->IsInstantProcess( | |
129 contents->GetRenderProcessHost()->GetID())) | |
130 << test.description; | |
131 | |
132 // Ensure only the expected tabs(contents) reloaded. | |
133 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) | |
samarth
2013/08/14 00:24:14
Change num_reloads to ReloadedOnce() so this is ju
Anuj
2013/08/14 06:38:57
ReloadedOnce can technically be false with num_rel
| |
134 << test.description; | |
135 if (test.should_reload) { | |
samarth
2013/08/14 00:24:14
This is testing more detail than necessary. Just s
Anuj
2013/08/14 06:38:57
Done.
| |
136 // NO_RELOAD indicates that process reassignment may be required. | |
137 EXPECT_EQ(observer->reload_type(), | |
138 (test.start_in_instant_process == test.end_in_instant_process) | |
139 ? content::NavigationController::RELOAD | |
140 : content::NavigationController::NO_RELOAD) | |
141 << test.description; | |
142 } | |
143 } | |
144 } | |
145 | |
146 TEST_F(BrowserInstantControllerTest, GoogleBaseURLUpdated) { | |
147 size_t numTestCases = arraysize(kTabReloadTestCases); | |
148 FakeWebContentsObserver** observers = | |
149 new FakeWebContentsObserver*[numTestCases]; | |
150 for (size_t i = 0; i < numTestCases; ++i) { | |
151 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
152 AddTab(browser(), GURL(test.start_url)); | |
153 content::WebContents* contents = | |
154 browser()->tab_strip_model()->GetActiveWebContents(); | |
155 | |
156 // Validate initial instant state. | |
157 EXPECT_EQ(test.start_in_instant_process, chrome::IsInstantNTP(contents)) | |
158 << test.description; | |
159 EXPECT_EQ(test.start_in_instant_process, | |
160 instant_service_->IsInstantProcess( | |
161 contents->GetRenderProcessHost()->GetID())) | |
162 << test.description; | |
163 | |
164 // Setup an observer to verify reload or absence thereof. | |
165 observers[i] = new FakeWebContentsObserver(this, contents); | |
166 } | |
167 | |
168 NotifyGoogleBaseURLUpdate("https://www.google.es/"); | |
169 | |
170 for (size_t i = 0; i < numTestCases; ++i) { | |
171 const TabReloadTestCase& test = kTabReloadTestCases[i]; | |
172 FakeWebContentsObserver* observer = observers[i]; | |
173 content::WebContents* contents = observer->web_contents(); | |
174 | |
175 // Validate final instant state. | |
176 EXPECT_EQ(test.end_in_instant_process, chrome::IsInstantNTP(contents)) | |
177 << test.description; | |
178 EXPECT_EQ(test.end_in_instant_process, | |
179 instant_service_->IsInstantProcess( | |
180 contents->GetRenderProcessHost()->GetID())) | |
181 << test.description; | |
182 | |
183 // Ensure only the expected tabs(contents) reloaded. | |
184 EXPECT_EQ(test.should_reload ? 1 : 0, observer->num_reloads()) | |
185 << test.description; | |
186 if (test.should_reload) { | |
187 // NO_RELOAD indicates that process reassignment may be required. | |
188 EXPECT_EQ(observer->reload_type(), | |
189 (test.start_in_instant_process == test.end_in_instant_process) | |
190 ? content::NavigationController::RELOAD | |
191 : content::NavigationController::NO_RELOAD) | |
192 << test.description; | |
193 } | |
194 } | |
195 } | |
196 | |
197 } // namespace | |
198 | |
199 } // namespace chrome | |
OLD | NEW |