Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: components/google/core/browser/google_url_tracker_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698