OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/google/core/browser/google_url_tracker.h" | 5 #include "components/google/core/browser/google_url_tracker.h" |
6 | 6 |
7 #include <set> | |
8 #include <string> | 7 #include <string> |
9 | 8 |
10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
12 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
13 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
14 #include "base/prefs/testing_pref_service.h" | 13 #include "base/prefs/testing_pref_service.h" |
15 #include "components/google/core/browser/google_pref_names.h" | 14 #include "components/google/core/browser/google_pref_names.h" |
16 #include "components/google/core/browser/google_url_tracker_client.h" | 15 #include "components/google/core/browser/google_url_tracker_client.h" |
17 #include "components/google/core/browser/google_url_tracker_infobar_delegate.h" | |
18 #include "components/google/core/browser/google_url_tracker_navigation_helper.h" | |
19 #include "components/infobars/core/infobar.h" | |
20 #include "components/infobars/core/infobar_delegate.h" | |
21 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
22 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
23 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
25 | 20 |
26 namespace { | 21 namespace { |
27 | 22 |
28 // TestCallbackListener --------------------------------------------------- | 23 // TestCallbackListener --------------------------------------------------- |
29 | 24 |
30 class TestCallbackListener { | 25 class TestCallbackListener { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 62 } |
68 | 63 |
69 | 64 |
70 // TestGoogleURLTrackerClient ------------------------------------------------- | 65 // TestGoogleURLTrackerClient ------------------------------------------------- |
71 | 66 |
72 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient { | 67 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient { |
73 public: | 68 public: |
74 explicit TestGoogleURLTrackerClient(PrefService* prefs_); | 69 explicit TestGoogleURLTrackerClient(PrefService* prefs_); |
75 ~TestGoogleURLTrackerClient() override; | 70 ~TestGoogleURLTrackerClient() override; |
76 | 71 |
77 void SetListeningForNavigationStart(bool listen) override; | |
78 bool IsListeningForNavigationStart() override; | |
79 bool IsBackgroundNetworkingEnabled() override; | 72 bool IsBackgroundNetworkingEnabled() override; |
80 PrefService* GetPrefs() override; | 73 PrefService* GetPrefs() override; |
81 net::URLRequestContextGetter* GetRequestContext() override; | 74 net::URLRequestContextGetter* GetRequestContext() override; |
82 | 75 |
83 private: | 76 private: |
84 PrefService* prefs_; | 77 PrefService* prefs_; |
85 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 78 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
86 bool observe_nav_start_; | |
87 | 79 |
88 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient); | 80 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient); |
89 }; | 81 }; |
90 | 82 |
91 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs) | 83 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs) |
92 : prefs_(prefs), | 84 : prefs_(prefs), |
93 request_context_(new net::TestURLRequestContextGetter( | 85 request_context_(new net::TestURLRequestContextGetter( |
94 base::MessageLoopProxy::current())), | 86 base::MessageLoopProxy::current())) { |
95 observe_nav_start_(false) { | |
96 } | 87 } |
97 | 88 |
98 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() { | 89 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() { |
99 } | 90 } |
100 | 91 |
101 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) { | |
102 observe_nav_start_ = listen; | |
103 } | |
104 | |
105 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() { | |
106 return observe_nav_start_; | |
107 } | |
108 | |
109 bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { | 92 bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { |
110 return true; | 93 return true; |
111 } | 94 } |
112 | 95 |
113 PrefService* TestGoogleURLTrackerClient::GetPrefs() { | 96 PrefService* TestGoogleURLTrackerClient::GetPrefs() { |
114 return prefs_; | 97 return prefs_; |
115 } | 98 } |
116 | 99 |
117 net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() { | 100 net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() { |
118 return request_context_.get(); | 101 return request_context_.get(); |
119 } | 102 } |
120 | 103 |
121 | 104 |
122 // TestGoogleURLTrackerNavigationHelper --------------------------------------- | |
123 | |
124 class TestGoogleURLTrackerNavigationHelper | |
125 : public GoogleURLTrackerNavigationHelper { | |
126 public: | |
127 explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker); | |
128 ~TestGoogleURLTrackerNavigationHelper() override; | |
129 | |
130 void SetListeningForNavigationCommit(bool listen) override; | |
131 bool IsListeningForNavigationCommit() override; | |
132 void SetListeningForTabDestruction(bool listen) override; | |
133 bool IsListeningForTabDestruction() override; | |
134 void OpenURL(GURL url, | |
135 WindowOpenDisposition disposition, | |
136 bool user_clicked_on_link) override; | |
137 | |
138 private: | |
139 bool listening_for_nav_commit_; | |
140 bool listening_for_tab_destruction_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerNavigationHelper); | |
143 }; | |
144 | |
145 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper( | |
146 GoogleURLTracker* tracker) | |
147 : GoogleURLTrackerNavigationHelper(tracker), | |
148 listening_for_nav_commit_(false), | |
149 listening_for_tab_destruction_(false) { | |
150 } | |
151 | |
152 TestGoogleURLTrackerNavigationHelper::~TestGoogleURLTrackerNavigationHelper() { | |
153 } | |
154 | |
155 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit( | |
156 bool listen) { | |
157 listening_for_nav_commit_ = listen; | |
158 } | |
159 | |
160 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit() { | |
161 return listening_for_nav_commit_; | |
162 } | |
163 | |
164 void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction( | |
165 bool listen) { | |
166 listening_for_tab_destruction_ = listen; | |
167 } | |
168 | |
169 bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() { | |
170 return listening_for_tab_destruction_; | |
171 } | |
172 | |
173 void TestGoogleURLTrackerNavigationHelper::OpenURL( | |
174 GURL url, | |
175 WindowOpenDisposition disposition, | |
176 bool user_clicked_on_link) { | |
177 } | |
178 | |
179 // TestInfoBarManager --------------------------------------------------------- | |
180 | |
181 class TestInfoBarManager : public infobars::InfoBarManager { | |
182 public: | |
183 explicit TestInfoBarManager(int unique_id); | |
184 ~TestInfoBarManager() override; | |
185 int GetActiveEntryID() override; | |
186 | |
187 private: | |
188 int unique_id_; | |
189 DISALLOW_COPY_AND_ASSIGN(TestInfoBarManager); | |
190 }; | |
191 | |
192 TestInfoBarManager::TestInfoBarManager(int unique_id) : unique_id_(unique_id) { | |
193 } | |
194 | |
195 TestInfoBarManager::~TestInfoBarManager() { | |
196 ShutDown(); | |
197 } | |
198 | |
199 int TestInfoBarManager::GetActiveEntryID() { | |
200 return unique_id_; | |
201 } | |
202 | |
203 } // namespace | 105 } // namespace |
204 | 106 |
205 // GoogleURLTrackerTest ------------------------------------------------------- | 107 // GoogleURLTrackerTest ------------------------------------------------------- |
206 | 108 |
207 class GoogleURLTrackerTest : public testing::Test { | 109 class GoogleURLTrackerTest : public testing::Test { |
208 protected: | 110 protected: |
209 GoogleURLTrackerTest(); | 111 GoogleURLTrackerTest(); |
210 ~GoogleURLTrackerTest() override; | 112 ~GoogleURLTrackerTest() override; |
211 | 113 |
212 // testing::Test | 114 // testing::Test |
213 void SetUp() override; | 115 void SetUp() override; |
214 void TearDown() override; | 116 void TearDown() override; |
215 | 117 |
216 net::TestURLFetcher* GetFetcher(); | 118 net::TestURLFetcher* GetFetcher(); |
217 void MockSearchDomainCheckResponse(const std::string& domain); | 119 void MockSearchDomainCheckResponse(const std::string& domain); |
218 void RequestServerCheck(); | 120 void RequestServerCheck(); |
219 void FinishSleep(); | 121 void FinishSleep(); |
220 void NotifyNetworkChanged(); | 122 void NotifyNetworkChanged(); |
221 GURL fetched_google_url() const { | |
222 return google_url_tracker_->fetched_google_url(); | |
223 } | |
224 void set_google_url(const GURL& url) { | 123 void set_google_url(const GURL& url) { |
225 google_url_tracker_->google_url_ = url; | 124 google_url_tracker_->google_url_ = url; |
226 } | 125 } |
227 GURL google_url() const { return google_url_tracker_->google_url(); } | 126 GURL google_url() const { return google_url_tracker_->google_url(); } |
228 void SetLastPromptedGoogleURL(const GURL& url); | |
229 GURL GetLastPromptedGoogleURL(); | |
230 void SetNavigationPending(infobars::InfoBarManager* infobar_manager, | |
231 bool is_search); | |
232 void CommitNonSearch(infobars::InfoBarManager* infobar_manager); | |
233 void CommitSearch(infobars::InfoBarManager* infobar_manager, | |
234 const GURL& search_url); | |
235 void CloseTab(infobars::InfoBarManager* infobar_manager); | |
236 GoogleURLTrackerMapEntry* GetMapEntry( | |
237 infobars::InfoBarManager* infobar_manager); | |
238 GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate( | |
239 infobars::InfoBarManager* infobar_manager); | |
240 GoogleURLTrackerNavigationHelper* GetNavigationHelper( | |
241 infobars::InfoBarManager* infobar_manager); | |
242 void ExpectDefaultURLs() const; | |
243 void ExpectListeningForCommit(infobars::InfoBarManager* infobar_manager, | |
244 bool listening); | |
245 bool listener_notified() const { return listener_.notified(); } | 127 bool listener_notified() const { return listener_.notified(); } |
246 void clear_listener_notified() { listener_.clear_notified(); } | 128 void clear_listener_notified() { listener_.clear_notified(); } |
247 | 129 |
248 private: | 130 private: |
249 base::MessageLoop message_loop_; | 131 base::MessageLoop message_loop_; |
250 TestingPrefServiceSimple prefs_; | 132 TestingPrefServiceSimple prefs_; |
251 | 133 |
252 // Creating this allows us to call | 134 // Creating this allows us to call |
253 // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(). | 135 // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(). |
254 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 136 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
255 net::TestURLFetcherFactory fetcher_factory_; | 137 net::TestURLFetcherFactory fetcher_factory_; |
256 GoogleURLTrackerClient* client_; | 138 GoogleURLTrackerClient* client_; |
257 scoped_ptr<GoogleURLTracker> google_url_tracker_; | 139 scoped_ptr<GoogleURLTracker> google_url_tracker_; |
258 TestCallbackListener listener_; | 140 TestCallbackListener listener_; |
259 // This tracks the different "tabs" a test has "opened", so we can close them | |
260 // properly before shutting down |google_url_tracker_|, which expects that. | |
261 std::set<infobars::InfoBarManager*> infobar_managers_seen_; | |
262 }; | 141 }; |
263 | 142 |
264 GoogleURLTrackerTest::GoogleURLTrackerTest() { | 143 GoogleURLTrackerTest::GoogleURLTrackerTest() { |
265 prefs_.registry()->RegisterStringPref( | 144 prefs_.registry()->RegisterStringPref( |
266 prefs::kLastKnownGoogleURL, | 145 prefs::kLastKnownGoogleURL, |
267 GoogleURLTracker::kDefaultGoogleHomepage); | 146 GoogleURLTracker::kDefaultGoogleHomepage); |
268 prefs_.registry()->RegisterStringPref( | |
269 prefs::kLastPromptedGoogleURL, | |
270 std::string()); | |
271 } | 147 } |
272 | 148 |
273 GoogleURLTrackerTest::~GoogleURLTrackerTest() { | 149 GoogleURLTrackerTest::~GoogleURLTrackerTest() { |
274 } | 150 } |
275 | 151 |
276 void GoogleURLTrackerTest::SetUp() { | 152 void GoogleURLTrackerTest::SetUp() { |
277 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | 153 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
278 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; | 154 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; |
279 // this is safe since GoogleURLTracker keeps the client for its lifetime. | 155 // this is safe since GoogleURLTracker keeps the client for its lifetime. |
280 client_ = new TestGoogleURLTrackerClient(&prefs_); | 156 client_ = new TestGoogleURLTrackerClient(&prefs_); |
281 scoped_ptr<GoogleURLTrackerClient> client(client_); | 157 scoped_ptr<GoogleURLTrackerClient> client(client_); |
282 google_url_tracker_.reset(new GoogleURLTracker( | 158 google_url_tracker_.reset(new GoogleURLTracker( |
283 client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); | 159 client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); |
284 } | 160 } |
285 | 161 |
286 void GoogleURLTrackerTest::TearDown() { | 162 void GoogleURLTrackerTest::TearDown() { |
287 while (!infobar_managers_seen_.empty()) | |
288 CloseTab(*infobar_managers_seen_.begin()); | |
289 google_url_tracker_->Shutdown(); | 163 google_url_tracker_->Shutdown(); |
290 } | 164 } |
291 | 165 |
292 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { | 166 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { |
293 // This will return the last fetcher created. If no fetchers have been | 167 // This will return the last fetcher created. If no fetchers have been |
294 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. | 168 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. |
295 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); | 169 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); |
296 } | 170 } |
297 | 171 |
298 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( | 172 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( |
(...skipping 20 matching lines...) Expand all Loading... |
319 } | 193 } |
320 | 194 |
321 void GoogleURLTrackerTest::NotifyNetworkChanged() { | 195 void GoogleURLTrackerTest::NotifyNetworkChanged() { |
322 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 196 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
323 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); | 197 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); |
324 // For thread safety, the NCN queues tasks to do the actual notifications, so | 198 // For thread safety, the NCN queues tasks to do the actual notifications, so |
325 // we need to spin the message loop so the tracker will actually be notified. | 199 // we need to spin the message loop so the tracker will actually be notified. |
326 base::MessageLoop::current()->RunUntilIdle(); | 200 base::MessageLoop::current()->RunUntilIdle(); |
327 } | 201 } |
328 | 202 |
329 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { | |
330 prefs_.SetString(prefs::kLastPromptedGoogleURL, url.spec()); | |
331 } | |
332 | |
333 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { | |
334 return GURL(prefs_.GetString(prefs::kLastPromptedGoogleURL)); | |
335 } | |
336 | |
337 void GoogleURLTrackerTest::SetNavigationPending( | |
338 infobars::InfoBarManager* infobar_manager, | |
339 bool is_search) { | |
340 if (is_search) { | |
341 google_url_tracker_->SearchCommitted(); | |
342 // Note that the call above might not have actually registered a listener | |
343 // for navigation starts if the searchdomaincheck response was bogus. | |
344 } | |
345 infobar_managers_seen_.insert(infobar_manager); | |
346 if (client_->IsListeningForNavigationStart()) { | |
347 google_url_tracker_->OnNavigationPending( | |
348 scoped_ptr<GoogleURLTrackerNavigationHelper>( | |
349 new TestGoogleURLTrackerNavigationHelper( | |
350 google_url_tracker_.get())), | |
351 infobar_manager, | |
352 infobar_manager->GetActiveEntryID()); | |
353 } | |
354 } | |
355 | |
356 void GoogleURLTrackerTest::CommitNonSearch( | |
357 infobars::InfoBarManager* infobar_manager) { | |
358 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
359 if (!map_entry) | |
360 return; | |
361 | |
362 ExpectListeningForCommit(infobar_manager, false); | |
363 | |
364 // The infobar should be showing; otherwise the pending non-search should | |
365 // have closed it. | |
366 ASSERT_TRUE(map_entry->has_infobar_delegate()); | |
367 | |
368 // The pending_id should have been reset to 0 when the non-search became | |
369 // pending. | |
370 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id()); | |
371 | |
372 // Committing the navigation would close the infobar. | |
373 map_entry->infobar_delegate()->Close(false); | |
374 } | |
375 | |
376 void GoogleURLTrackerTest::CommitSearch( | |
377 infobars::InfoBarManager* infobar_manager, | |
378 const GURL& search_url) { | |
379 DCHECK(search_url.is_valid()); | |
380 GoogleURLTrackerNavigationHelper* nav_helper = | |
381 GetNavigationHelper(infobar_manager); | |
382 if (nav_helper && nav_helper->IsListeningForNavigationCommit()) { | |
383 google_url_tracker_->OnNavigationCommitted(infobar_manager, search_url); | |
384 } | |
385 } | |
386 | |
387 void GoogleURLTrackerTest::CloseTab(infobars::InfoBarManager* infobar_manager) { | |
388 infobar_managers_seen_.erase(infobar_manager); | |
389 GoogleURLTrackerNavigationHelper* nav_helper = | |
390 GetNavigationHelper(infobar_manager); | |
391 if (nav_helper && nav_helper->IsListeningForTabDestruction()) { | |
392 google_url_tracker_->OnTabClosed(nav_helper); | |
393 } else { | |
394 // Closing a tab with an infobar showing would close the infobar. | |
395 GoogleURLTrackerInfoBarDelegate* delegate = | |
396 GetInfoBarDelegate(infobar_manager); | |
397 if (delegate) | |
398 delegate->Close(false); | |
399 } | |
400 } | |
401 | |
402 GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry( | |
403 infobars::InfoBarManager* infobar_manager) { | |
404 GoogleURLTracker::EntryMap::const_iterator i = | |
405 google_url_tracker_->entry_map_.find(infobar_manager); | |
406 return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second; | |
407 } | |
408 | |
409 GoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate( | |
410 infobars::InfoBarManager* infobar_manager) { | |
411 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
412 return map_entry ? map_entry->infobar_delegate() : NULL; | |
413 } | |
414 | |
415 GoogleURLTrackerNavigationHelper* GoogleURLTrackerTest::GetNavigationHelper( | |
416 infobars::InfoBarManager* infobar_manager) { | |
417 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
418 return map_entry ? map_entry->navigation_helper() : NULL; | |
419 } | |
420 | |
421 void GoogleURLTrackerTest::ExpectDefaultURLs() const { | |
422 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
423 EXPECT_EQ(GURL(), fetched_google_url()); | |
424 } | |
425 | |
426 void GoogleURLTrackerTest::ExpectListeningForCommit( | |
427 infobars::InfoBarManager* infobar_manager, | |
428 bool listening) { | |
429 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager); | |
430 if (map_entry) { | |
431 EXPECT_EQ(listening, | |
432 map_entry->navigation_helper()->IsListeningForNavigationCommit()); | |
433 } else { | |
434 EXPECT_FALSE(listening); | |
435 } | |
436 } | |
437 | |
438 // Tests ---------------------------------------------------------------------- | 203 // Tests ---------------------------------------------------------------------- |
439 | 204 |
440 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { | 205 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { |
441 ExpectDefaultURLs(); | 206 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); |
442 FinishSleep(); | 207 FinishSleep(); |
443 // No one called RequestServerCheck() so nothing should have happened. | 208 // No one called RequestServerCheck() so nothing should have happened. |
444 EXPECT_FALSE(GetFetcher()); | 209 EXPECT_FALSE(GetFetcher()); |
445 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 210 MockSearchDomainCheckResponse("http://www.google.co.uk/"); |
446 ExpectDefaultURLs(); | 211 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); |
447 EXPECT_FALSE(listener_notified()); | 212 EXPECT_FALSE(listener_notified()); |
448 } | 213 } |
449 | 214 |
450 TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) { | 215 TEST_F(GoogleURLTrackerTest, Update) { |
451 RequestServerCheck(); | 216 RequestServerCheck(); |
452 EXPECT_FALSE(GetFetcher()); | 217 EXPECT_FALSE(GetFetcher()); |
453 ExpectDefaultURLs(); | 218 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); |
454 EXPECT_FALSE(listener_notified()); | 219 EXPECT_FALSE(listener_notified()); |
455 | 220 |
456 FinishSleep(); | 221 FinishSleep(); |
457 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 222 MockSearchDomainCheckResponse("http://www.google.co.uk/"); |
458 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
459 // GoogleURL should be updated, becase there was no last prompted URL. | |
460 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | 223 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); |
461 EXPECT_TRUE(listener_notified()); | 224 EXPECT_TRUE(listener_notified()); |
462 } | 225 } |
463 | 226 |
464 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) { | 227 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) { |
465 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | 228 GURL original_google_url("http://www.google.co.uk/"); |
| 229 set_google_url(original_google_url); |
466 | 230 |
467 RequestServerCheck(); | 231 RequestServerCheck(); |
468 EXPECT_FALSE(GetFetcher()); | 232 EXPECT_FALSE(GetFetcher()); |
469 ExpectDefaultURLs(); | 233 EXPECT_EQ(original_google_url, google_url()); |
470 EXPECT_FALSE(listener_notified()); | 234 EXPECT_FALSE(listener_notified()); |
471 | 235 |
472 FinishSleep(); | 236 FinishSleep(); |
473 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 237 MockSearchDomainCheckResponse(original_google_url.spec()); |
474 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | 238 EXPECT_EQ(original_google_url, google_url()); |
475 // GoogleURL should not be updated, because the fetched and prompted URLs | 239 // No one should be notified, because the new URL matches the old. |
476 // match. | |
477 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
478 EXPECT_FALSE(listener_notified()); | 240 EXPECT_FALSE(listener_notified()); |
479 } | 241 } |
480 | 242 |
481 TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) { | 243 TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) { |
482 TestInfoBarManager infobar_manager(1); | 244 GURL original_google_url("http://www.google.co.uk/"); |
483 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | 245 set_google_url(original_google_url); |
484 | 246 |
485 RequestServerCheck(); | 247 RequestServerCheck(); |
486 EXPECT_FALSE(GetFetcher()); | 248 EXPECT_FALSE(GetFetcher()); |
487 ExpectDefaultURLs(); | 249 EXPECT_EQ(original_google_url, google_url()); |
488 EXPECT_FALSE(listener_notified()); | 250 EXPECT_FALSE(listener_notified()); |
489 | 251 |
490 // Old-style domain string. | 252 // Old-style domain string. |
491 FinishSleep(); | 253 FinishSleep(); |
492 MockSearchDomainCheckResponse(".google.co.in"); | 254 MockSearchDomainCheckResponse(".google.co.in"); |
493 EXPECT_EQ(GURL(), fetched_google_url()); | 255 EXPECT_EQ(original_google_url, google_url()); |
494 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
495 EXPECT_FALSE(listener_notified()); | 256 EXPECT_FALSE(listener_notified()); |
496 SetNavigationPending(&infobar_manager, true); | |
497 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
498 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
499 | 257 |
500 // Bad subdomain. | 258 // Bad subdomain. |
501 NotifyNetworkChanged(); | 259 NotifyNetworkChanged(); |
502 MockSearchDomainCheckResponse("http://mail.google.com/"); | 260 MockSearchDomainCheckResponse("http://mail.google.com/"); |
503 EXPECT_EQ(GURL(), fetched_google_url()); | 261 EXPECT_EQ(original_google_url, google_url()); |
504 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
505 EXPECT_FALSE(listener_notified()); | 262 EXPECT_FALSE(listener_notified()); |
506 SetNavigationPending(&infobar_manager, true); | |
507 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
508 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
509 | 263 |
510 // Non-empty path. | 264 // Non-empty path. |
511 NotifyNetworkChanged(); | 265 NotifyNetworkChanged(); |
512 MockSearchDomainCheckResponse("http://www.google.com/search"); | 266 MockSearchDomainCheckResponse("http://www.google.com/search"); |
513 EXPECT_EQ(GURL(), fetched_google_url()); | 267 EXPECT_EQ(original_google_url, google_url()); |
514 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
515 EXPECT_FALSE(listener_notified()); | 268 EXPECT_FALSE(listener_notified()); |
516 SetNavigationPending(&infobar_manager, true); | |
517 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
518 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
519 | 269 |
520 // Non-empty query. | 270 // Non-empty query. |
521 NotifyNetworkChanged(); | 271 NotifyNetworkChanged(); |
522 MockSearchDomainCheckResponse("http://www.google.com/?q=foo"); | 272 MockSearchDomainCheckResponse("http://www.google.com/?q=foo"); |
523 EXPECT_EQ(GURL(), fetched_google_url()); | 273 EXPECT_EQ(original_google_url, google_url()); |
524 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
525 EXPECT_FALSE(listener_notified()); | 274 EXPECT_FALSE(listener_notified()); |
526 SetNavigationPending(&infobar_manager, true); | |
527 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
528 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
529 | 275 |
530 // Non-empty ref. | 276 // Non-empty ref. |
531 NotifyNetworkChanged(); | 277 NotifyNetworkChanged(); |
532 MockSearchDomainCheckResponse("http://www.google.com/#anchor"); | 278 MockSearchDomainCheckResponse("http://www.google.com/#anchor"); |
533 EXPECT_EQ(GURL(), fetched_google_url()); | 279 EXPECT_EQ(original_google_url, google_url()); |
534 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
535 EXPECT_FALSE(listener_notified()); | 280 EXPECT_FALSE(listener_notified()); |
536 SetNavigationPending(&infobar_manager, true); | |
537 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
538 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
539 | 281 |
540 // Complete garbage. | 282 // Complete garbage. |
541 NotifyNetworkChanged(); | 283 NotifyNetworkChanged(); |
542 MockSearchDomainCheckResponse("HJ)*qF)_*&@f1"); | 284 MockSearchDomainCheckResponse("HJ)*qF)_*&@f1"); |
543 EXPECT_EQ(GURL(), fetched_google_url()); | 285 EXPECT_EQ(original_google_url, google_url()); |
544 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
545 EXPECT_FALSE(listener_notified()); | 286 EXPECT_FALSE(listener_notified()); |
546 SetNavigationPending(&infobar_manager, true); | |
547 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
548 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
549 } | |
550 | |
551 TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) { | |
552 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); | |
553 set_google_url(GURL("http://www.google.co.uk/")); | |
554 RequestServerCheck(); | |
555 FinishSleep(); | |
556 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
557 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
558 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
559 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
560 EXPECT_FALSE(listener_notified()); | |
561 } | |
562 | |
563 TEST_F(GoogleURLTrackerTest, SilentlyAcceptSchemeChange) { | |
564 // We should auto-accept changes to the current Google URL that merely change | |
565 // the scheme, regardless of what the last prompted URL was. | |
566 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); | |
567 set_google_url(GURL("http://www.google.co.uk/")); | |
568 RequestServerCheck(); | |
569 FinishSleep(); | |
570 MockSearchDomainCheckResponse("https://www.google.co.uk/"); | |
571 EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url()); | |
572 EXPECT_EQ(GURL("https://www.google.co.uk/"), google_url()); | |
573 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
574 EXPECT_TRUE(listener_notified()); | |
575 | |
576 NotifyNetworkChanged(); | |
577 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
578 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
579 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
580 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
581 EXPECT_TRUE(listener_notified()); | |
582 } | 287 } |
583 | 288 |
584 TEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) { | 289 TEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) { |
585 RequestServerCheck(); | 290 RequestServerCheck(); |
586 FinishSleep(); | 291 FinishSleep(); |
587 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 292 MockSearchDomainCheckResponse("http://www.google.co.uk/"); |
588 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
589 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | 293 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); |
590 EXPECT_TRUE(listener_notified()); | 294 EXPECT_TRUE(listener_notified()); |
591 clear_listener_notified(); | 295 clear_listener_notified(); |
592 | 296 |
593 NotifyNetworkChanged(); | 297 NotifyNetworkChanged(); |
594 MockSearchDomainCheckResponse("http://www.google.co.in/"); | 298 MockSearchDomainCheckResponse("http://www.google.co.in/"); |
595 EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url()); | 299 EXPECT_EQ(GURL("http://www.google.co.in/"), google_url()); |
596 // Just fetching a new URL shouldn't reset things without a prompt. | 300 EXPECT_TRUE(listener_notified()); |
597 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
598 EXPECT_FALSE(listener_notified()); | |
599 } | 301 } |
600 | 302 |
601 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) { | 303 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) { |
602 FinishSleep(); | 304 FinishSleep(); |
603 NotifyNetworkChanged(); | 305 NotifyNetworkChanged(); |
604 // No one called RequestServerCheck() so nothing should have happened. | 306 // No one called RequestServerCheck() so nothing should have happened. |
605 EXPECT_FALSE(GetFetcher()); | 307 EXPECT_FALSE(GetFetcher()); |
606 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 308 MockSearchDomainCheckResponse("http://www.google.co.uk/"); |
607 ExpectDefaultURLs(); | 309 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); |
608 EXPECT_FALSE(listener_notified()); | 310 EXPECT_FALSE(listener_notified()); |
609 } | 311 } |
610 | 312 |
611 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) { | 313 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) { |
612 FinishSleep(); | 314 FinishSleep(); |
613 NotifyNetworkChanged(); | 315 NotifyNetworkChanged(); |
614 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | 316 MockSearchDomainCheckResponse("http://www.google.co.jp/"); |
615 | 317 |
616 RequestServerCheck(); | 318 RequestServerCheck(); |
617 // The first request for a check should trigger a fetch if it hasn't happened | 319 // The first request for a check should trigger a fetch if it hasn't happened |
618 // already. | 320 // already. |
619 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 321 MockSearchDomainCheckResponse("http://www.google.co.uk/"); |
620 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
621 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | 322 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); |
622 EXPECT_TRUE(listener_notified()); | 323 EXPECT_TRUE(listener_notified()); |
623 } | 324 } |
624 | 325 |
625 TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) { | 326 TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) { |
626 FinishSleep(); | 327 FinishSleep(); |
627 NotifyNetworkChanged(); | 328 NotifyNetworkChanged(); |
628 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | 329 MockSearchDomainCheckResponse("http://www.google.co.jp/"); |
629 | 330 |
630 RequestServerCheck(); | 331 RequestServerCheck(); |
631 // The first request for a check should trigger a fetch if it hasn't happened | 332 // The first request for a check should trigger a fetch if it hasn't happened |
632 // already. | 333 // already. |
633 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | 334 MockSearchDomainCheckResponse("http://www.google.co.uk/"); |
634 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
635 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | 335 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); |
636 EXPECT_TRUE(listener_notified()); | 336 EXPECT_TRUE(listener_notified()); |
637 clear_listener_notified(); | 337 clear_listener_notified(); |
638 | 338 |
639 RequestServerCheck(); | 339 RequestServerCheck(); |
640 // The second request should be ignored. | 340 // The second request should be ignored. |
641 EXPECT_FALSE(GetFetcher()); | 341 EXPECT_FALSE(GetFetcher()); |
642 MockSearchDomainCheckResponse("http://www.google.co.in/"); | 342 MockSearchDomainCheckResponse("http://www.google.co.in/"); |
643 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
644 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | 343 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); |
645 EXPECT_FALSE(listener_notified()); | 344 EXPECT_FALSE(listener_notified()); |
646 } | 345 } |
647 | |
648 TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) { | |
649 TestInfoBarManager infobar_manager(1); | |
650 RequestServerCheck(); | |
651 FinishSleep(); | |
652 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
653 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
654 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
655 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
656 EXPECT_TRUE(listener_notified()); | |
657 clear_listener_notified(); | |
658 | |
659 SetNavigationPending(&infobar_manager, true); | |
660 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
661 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
662 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
663 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); | |
664 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
665 EXPECT_FALSE(listener_notified()); | |
666 } | |
667 | |
668 TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) { | |
669 TestInfoBarManager infobar_manager(1); | |
670 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
671 RequestServerCheck(); | |
672 FinishSleep(); | |
673 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
674 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
675 EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url()); | |
676 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
677 EXPECT_FALSE(listener_notified()); | |
678 | |
679 SetNavigationPending(&infobar_manager, true); | |
680 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); | |
681 ASSERT_FALSE(map_entry == NULL); | |
682 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
683 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
684 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
685 EXPECT_FALSE(listener_notified()); | |
686 | |
687 CloseTab(&infobar_manager); | |
688 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
689 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
690 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
691 EXPECT_FALSE(listener_notified()); | |
692 } | |
693 | |
694 TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) { | |
695 TestInfoBarManager infobar_manager(1); | |
696 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
697 RequestServerCheck(); | |
698 FinishSleep(); | |
699 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
700 | |
701 SetNavigationPending(&infobar_manager, true); | |
702 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
703 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
704 | |
705 CloseTab(&infobar_manager); | |
706 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
707 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
708 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
709 EXPECT_FALSE(listener_notified()); | |
710 } | |
711 | |
712 TEST_F(GoogleURLTrackerTest, InfoBarClosed) { | |
713 TestInfoBarManager infobar_manager(1); | |
714 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
715 RequestServerCheck(); | |
716 FinishSleep(); | |
717 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
718 | |
719 SetNavigationPending(&infobar_manager, true); | |
720 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
721 GoogleURLTrackerInfoBarDelegate* infobar = | |
722 GetInfoBarDelegate(&infobar_manager); | |
723 ASSERT_FALSE(infobar == NULL); | |
724 | |
725 infobar->Close(false); | |
726 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
727 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
728 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
729 EXPECT_FALSE(listener_notified()); | |
730 } | |
731 | |
732 TEST_F(GoogleURLTrackerTest, InfoBarRefused) { | |
733 TestInfoBarManager infobar_manager(1); | |
734 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
735 RequestServerCheck(); | |
736 FinishSleep(); | |
737 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
738 | |
739 SetNavigationPending(&infobar_manager, true); | |
740 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
741 GoogleURLTrackerInfoBarDelegate* infobar = | |
742 GetInfoBarDelegate(&infobar_manager); | |
743 ASSERT_FALSE(infobar == NULL); | |
744 | |
745 infobar->Cancel(); | |
746 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
747 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
748 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); | |
749 EXPECT_FALSE(listener_notified()); | |
750 } | |
751 | |
752 TEST_F(GoogleURLTrackerTest, InfoBarAccepted) { | |
753 TestInfoBarManager infobar_manager(1); | |
754 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
755 RequestServerCheck(); | |
756 FinishSleep(); | |
757 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
758 | |
759 SetNavigationPending(&infobar_manager, true); | |
760 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
761 GoogleURLTrackerInfoBarDelegate* infobar = | |
762 GetInfoBarDelegate(&infobar_manager); | |
763 ASSERT_FALSE(infobar == NULL); | |
764 | |
765 infobar->Accept(); | |
766 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
767 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); | |
768 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); | |
769 EXPECT_TRUE(listener_notified()); | |
770 } | |
771 | |
772 TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) { | |
773 TestInfoBarManager infobar_manager(1); | |
774 RequestServerCheck(); | |
775 FinishSleep(); | |
776 MockSearchDomainCheckResponse(google_url().spec()); | |
777 | |
778 // Re-fetching the accepted URL after showing an infobar for another URL | |
779 // should close the infobar. | |
780 NotifyNetworkChanged(); | |
781 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
782 SetNavigationPending(&infobar_manager, true); | |
783 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
784 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
785 NotifyNetworkChanged(); | |
786 MockSearchDomainCheckResponse(google_url().spec()); | |
787 EXPECT_EQ(google_url(), GetLastPromptedGoogleURL()); | |
788 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
789 | |
790 // As should fetching a URL that differs from the accepted only by the scheme. | |
791 NotifyNetworkChanged(); | |
792 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
793 SetNavigationPending(&infobar_manager, true); | |
794 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
795 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
796 NotifyNetworkChanged(); | |
797 url::Replacements<char> replacements; | |
798 const std::string& scheme("https"); | |
799 replacements.SetScheme(scheme.data(), url::Component(0, scheme.length())); | |
800 GURL new_google_url(google_url().ReplaceComponents(replacements)); | |
801 MockSearchDomainCheckResponse(new_google_url.spec()); | |
802 EXPECT_EQ(new_google_url, GetLastPromptedGoogleURL()); | |
803 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
804 | |
805 // As should re-fetching the last prompted URL. | |
806 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
807 NotifyNetworkChanged(); | |
808 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
809 SetNavigationPending(&infobar_manager, true); | |
810 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
811 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
812 NotifyNetworkChanged(); | |
813 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
814 EXPECT_EQ(new_google_url, google_url()); | |
815 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
816 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
817 | |
818 // And one that differs from the last prompted URL only by the scheme. | |
819 NotifyNetworkChanged(); | |
820 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
821 SetNavigationPending(&infobar_manager, true); | |
822 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
823 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
824 NotifyNetworkChanged(); | |
825 MockSearchDomainCheckResponse("https://www.google.co.uk/"); | |
826 EXPECT_EQ(new_google_url, google_url()); | |
827 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
828 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
829 | |
830 // And fetching a different URL entirely. | |
831 NotifyNetworkChanged(); | |
832 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
833 SetNavigationPending(&infobar_manager, true); | |
834 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
835 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
836 NotifyNetworkChanged(); | |
837 MockSearchDomainCheckResponse("https://www.google.co.in/"); | |
838 EXPECT_EQ(new_google_url, google_url()); | |
839 EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
840 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
841 } | |
842 | |
843 TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) { | |
844 TestInfoBarManager infobar_manager(1); | |
845 RequestServerCheck(); | |
846 FinishSleep(); | |
847 MockSearchDomainCheckResponse(google_url().spec()); | |
848 | |
849 NotifyNetworkChanged(); | |
850 MockSearchDomainCheckResponse("http://www.google.co.uk/"); | |
851 SetNavigationPending(&infobar_manager, true); | |
852 CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test")); | |
853 GoogleURLTrackerInfoBarDelegate* delegate = | |
854 GetInfoBarDelegate(&infobar_manager); | |
855 ASSERT_FALSE(delegate == NULL); | |
856 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); | |
857 | |
858 // If while an infobar is showing we fetch a new URL that differs from the | |
859 // infobar's only by scheme, the infobar should stay showing. | |
860 NotifyNetworkChanged(); | |
861 MockSearchDomainCheckResponse("https://www.google.co.uk/"); | |
862 EXPECT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
863 EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url()); | |
864 } | |
865 | |
866 TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) { | |
867 TestInfoBarManager infobar_manager(1); | |
868 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
869 RequestServerCheck(); | |
870 FinishSleep(); | |
871 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
872 | |
873 // A pending non-search after a pending search should delete the map entry. | |
874 SetNavigationPending(&infobar_manager, true); | |
875 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); | |
876 ASSERT_FALSE(map_entry == NULL); | |
877 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
878 SetNavigationPending(&infobar_manager, false); | |
879 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
880 | |
881 // A pending search after a pending search should leave the map entry alive. | |
882 SetNavigationPending(&infobar_manager, true); | |
883 map_entry = GetMapEntry(&infobar_manager); | |
884 ASSERT_FALSE(map_entry == NULL); | |
885 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
886 SetNavigationPending(&infobar_manager, true); | |
887 ASSERT_EQ(map_entry, GetMapEntry(&infobar_manager)); | |
888 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
889 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
890 | |
891 // Committing this search should show an infobar. | |
892 CommitSearch(&infobar_manager, | |
893 GURL("http://www.google.co.uk/search?q=test2")); | |
894 EXPECT_TRUE(map_entry->has_infobar_delegate()); | |
895 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
896 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
897 EXPECT_FALSE(listener_notified()); | |
898 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
899 } | |
900 | |
901 TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) { | |
902 TestInfoBarManager infobar_manager(1); | |
903 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
904 RequestServerCheck(); | |
905 FinishSleep(); | |
906 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
907 SetNavigationPending(&infobar_manager, true); | |
908 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
909 GoogleURLTrackerInfoBarDelegate* delegate = | |
910 GetInfoBarDelegate(&infobar_manager); | |
911 ASSERT_FALSE(delegate == NULL); | |
912 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
913 | |
914 // A pending non-search on a visible infobar should basically do nothing. | |
915 SetNavigationPending(&infobar_manager, false); | |
916 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
917 EXPECT_EQ(0, delegate->pending_id()); | |
918 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
919 | |
920 // As should another pending non-search after the first. | |
921 SetNavigationPending(&infobar_manager, false); | |
922 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
923 EXPECT_EQ(0, delegate->pending_id()); | |
924 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
925 | |
926 // Committing this non-search should close the infobar. The control flow in | |
927 // these tests is not really comparable to in the real browser, but at least a | |
928 // few sanity-checks will be performed. | |
929 ASSERT_NO_FATAL_FAILURE(CommitNonSearch(&infobar_manager)); | |
930 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
931 | |
932 // A pending search on a visible infobar should cause the infobar to listen | |
933 // for the search to commit. | |
934 SetNavigationPending(&infobar_manager, true); | |
935 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
936 delegate = GetInfoBarDelegate(&infobar_manager); | |
937 ASSERT_FALSE(delegate == NULL); | |
938 SetNavigationPending(&infobar_manager, true); | |
939 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
940 EXPECT_EQ(1, delegate->pending_id()); | |
941 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
942 | |
943 // But a non-search after this should cancel that state. | |
944 SetNavigationPending(&infobar_manager, false); | |
945 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
946 EXPECT_EQ(0, delegate->pending_id()); | |
947 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
948 | |
949 // Another pending search after the non-search should put us back into | |
950 // "waiting for commit" mode. | |
951 SetNavigationPending(&infobar_manager, true); | |
952 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
953 EXPECT_EQ(1, delegate->pending_id()); | |
954 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
955 | |
956 // A second pending search after the first should not really change anything. | |
957 SetNavigationPending(&infobar_manager, true); | |
958 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
959 EXPECT_EQ(1, delegate->pending_id()); | |
960 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
961 | |
962 // Committing this search should change the visible infobar's search_url. | |
963 CommitSearch(&infobar_manager, | |
964 GURL("http://www.google.co.uk/search?q=test2")); | |
965 ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager)); | |
966 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"), | |
967 delegate->search_url()); | |
968 EXPECT_EQ(0, delegate->pending_id()); | |
969 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
970 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); | |
971 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); | |
972 EXPECT_FALSE(listener_notified()); | |
973 } | |
974 | |
975 TEST_F(GoogleURLTrackerTest, MultipleMapEntries) { | |
976 TestInfoBarManager infobar_manager(1); | |
977 TestInfoBarManager infobar_manager2(2); | |
978 TestInfoBarManager infobar_manager3(3); | |
979 TestInfoBarManager infobar_manager4(4); | |
980 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
981 RequestServerCheck(); | |
982 FinishSleep(); | |
983 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
984 | |
985 SetNavigationPending(&infobar_manager, true); | |
986 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager); | |
987 ASSERT_FALSE(map_entry == NULL); | |
988 EXPECT_FALSE(map_entry->has_infobar_delegate()); | |
989 | |
990 SetNavigationPending(&infobar_manager2, true); | |
991 CommitSearch(&infobar_manager2, | |
992 GURL("http://www.google.co.uk/search?q=test2")); | |
993 GoogleURLTrackerInfoBarDelegate* delegate2 = | |
994 GetInfoBarDelegate(&infobar_manager2); | |
995 ASSERT_FALSE(delegate2 == NULL); | |
996 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"), | |
997 delegate2->search_url()); | |
998 | |
999 SetNavigationPending(&infobar_manager3, true); | |
1000 GoogleURLTrackerMapEntry* map_entry3 = GetMapEntry(&infobar_manager3); | |
1001 ASSERT_FALSE(map_entry3 == NULL); | |
1002 EXPECT_FALSE(map_entry3->has_infobar_delegate()); | |
1003 | |
1004 SetNavigationPending(&infobar_manager4, true); | |
1005 CommitSearch(&infobar_manager4, | |
1006 GURL("http://www.google.co.uk/search?q=test4")); | |
1007 GoogleURLTrackerInfoBarDelegate* delegate4 = | |
1008 GetInfoBarDelegate(&infobar_manager4); | |
1009 ASSERT_FALSE(delegate4 == NULL); | |
1010 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"), | |
1011 delegate4->search_url()); | |
1012 | |
1013 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
1014 EXPECT_TRUE(map_entry->has_infobar_delegate()); | |
1015 | |
1016 delegate2->Close(false); | |
1017 EXPECT_TRUE(GetMapEntry(&infobar_manager2) == NULL); | |
1018 EXPECT_FALSE(listener_notified()); | |
1019 | |
1020 delegate4->Accept(); | |
1021 EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL); | |
1022 EXPECT_TRUE(GetMapEntry(&infobar_manager3) == NULL); | |
1023 EXPECT_TRUE(GetMapEntry(&infobar_manager4) == NULL); | |
1024 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); | |
1025 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); | |
1026 EXPECT_TRUE(listener_notified()); | |
1027 } | |
1028 | |
1029 TEST_F(GoogleURLTrackerTest, IgnoreIrrelevantNavigation) { | |
1030 TestInfoBarManager infobar_manager(1); | |
1031 TestInfoBarManager infobar_manager2(2); | |
1032 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); | |
1033 RequestServerCheck(); | |
1034 FinishSleep(); | |
1035 MockSearchDomainCheckResponse("http://www.google.co.jp/"); | |
1036 | |
1037 // This tests a particularly gnarly sequence of events that used to cause us | |
1038 // to erroneously listen for a non-search navigation to commit. | |
1039 SetNavigationPending(&infobar_manager, true); | |
1040 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test")); | |
1041 SetNavigationPending(&infobar_manager2, true); | |
1042 CommitSearch(&infobar_manager2, | |
1043 GURL("http://www.google.co.uk/search?q=test2")); | |
1044 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); | |
1045 GoogleURLTrackerInfoBarDelegate* delegate2 = | |
1046 GetInfoBarDelegate(&infobar_manager2); | |
1047 ASSERT_FALSE(delegate2 == NULL); | |
1048 SetNavigationPending(&infobar_manager, true); | |
1049 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); | |
1050 delegate2->Close(false); | |
1051 SetNavigationPending(&infobar_manager, false); | |
1052 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); | |
1053 } | |
OLD | NEW |