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
3 // found in the LICENSE file.
5 #include "components/google/core/browser/google_url_tracker.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/testing_pref_service.h"
15 #include "components/google/core/browser/google_pref_names.h"
16 #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"
22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 // TestCallbackListener ---------------------------------------------------
30 class TestCallbackListener {
32 TestCallbackListener();
33 virtual ~TestCallbackListener();
35 bool HasRegisteredCallback();
36 void RegisterCallback(GoogleURLTracker* google_url_tracker);
38 bool notified() const { return notified_; }
39 void clear_notified() { notified_ = false; }
42 void OnGoogleURLUpdated();
45 scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_;
48 TestCallbackListener::TestCallbackListener() : notified_(false) {
51 TestCallbackListener::~TestCallbackListener() {
54 void TestCallbackListener::OnGoogleURLUpdated() {
58 bool TestCallbackListener::HasRegisteredCallback() {
59 return google_url_updated_subscription_.get();
62 void TestCallbackListener::RegisterCallback(
63 GoogleURLTracker* google_url_tracker) {
64 google_url_updated_subscription_ =
65 google_url_tracker->RegisterCallback(base::Bind(
66 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this)));
70 // TestGoogleURLTrackerClient -------------------------------------------------
72 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
74 explicit TestGoogleURLTrackerClient(PrefService* prefs_);
75 virtual ~TestGoogleURLTrackerClient();
77 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
78 virtual bool IsListeningForNavigationStart() OVERRIDE;
79 virtual bool IsBackgroundNetworkingEnabled() OVERRIDE;
80 virtual PrefService* GetPrefs() OVERRIDE;
81 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
85 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
86 bool observe_nav_start_;
88 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
91 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs)
93 request_context_(new net::TestURLRequestContextGetter(
94 base::MessageLoopProxy::current())),
95 observe_nav_start_(false) {
98 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
101 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
102 observe_nav_start_ = listen;
105 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
106 return observe_nav_start_;
109 bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() {
113 PrefService* TestGoogleURLTrackerClient::GetPrefs() {
117 net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() {
118 return request_context_.get();
122 // TestGoogleURLTrackerNavigationHelper ---------------------------------------
124 class TestGoogleURLTrackerNavigationHelper
125 : public GoogleURLTrackerNavigationHelper {
127 explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker);
128 virtual ~TestGoogleURLTrackerNavigationHelper();
130 virtual void SetListeningForNavigationCommit(bool listen) OVERRIDE;
131 virtual bool IsListeningForNavigationCommit() OVERRIDE;
132 virtual void SetListeningForTabDestruction(bool listen) OVERRIDE;
133 virtual bool IsListeningForTabDestruction() OVERRIDE;
134 virtual void OpenURL(GURL url,
135 WindowOpenDisposition disposition,
136 bool user_clicked_on_link) OVERRIDE;
139 bool listening_for_nav_commit_;
140 bool listening_for_tab_destruction_;
142 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerNavigationHelper);
145 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper(
146 GoogleURLTracker* tracker)
147 : GoogleURLTrackerNavigationHelper(tracker),
148 listening_for_nav_commit_(false),
149 listening_for_tab_destruction_(false) {
152 TestGoogleURLTrackerNavigationHelper::~TestGoogleURLTrackerNavigationHelper() {
155 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
157 listening_for_nav_commit_ = listen;
160 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit() {
161 return listening_for_nav_commit_;
164 void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction(
166 listening_for_tab_destruction_ = listen;
169 bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() {
170 return listening_for_tab_destruction_;
173 void TestGoogleURLTrackerNavigationHelper::OpenURL(
175 WindowOpenDisposition disposition,
176 bool user_clicked_on_link) {
179 // TestInfoBarManager ---------------------------------------------------------
181 class TestInfoBarManager : public infobars::InfoBarManager {
183 explicit TestInfoBarManager(int unique_id);
184 virtual ~TestInfoBarManager();
185 virtual int GetActiveEntryID() OVERRIDE;
189 DISALLOW_COPY_AND_ASSIGN(TestInfoBarManager);
192 TestInfoBarManager::TestInfoBarManager(int unique_id) : unique_id_(unique_id) {
195 TestInfoBarManager::~TestInfoBarManager() {
199 int TestInfoBarManager::GetActiveEntryID() {
205 // GoogleURLTrackerTest -------------------------------------------------------
207 class GoogleURLTrackerTest : public testing::Test {
209 GoogleURLTrackerTest();
210 virtual ~GoogleURLTrackerTest();
213 virtual void SetUp() OVERRIDE;
214 virtual void TearDown() OVERRIDE;
216 net::TestURLFetcher* GetFetcher();
217 void MockSearchDomainCheckResponse(const std::string& domain);
218 void RequestServerCheck();
220 void NotifyNetworkChanged();
221 GURL fetched_google_url() const {
222 return google_url_tracker_->fetched_google_url();
224 void set_google_url(const GURL& url) {
225 google_url_tracker_->google_url_ = url;
227 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,
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,
245 bool listener_notified() const { return listener_.notified(); }
246 void clear_listener_notified() { listener_.clear_notified(); }
249 base::MessageLoop message_loop_;
250 TestingPrefServiceSimple prefs_;
252 // Creating this allows us to call
253 // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests().
254 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
255 net::TestURLFetcherFactory fetcher_factory_;
256 GoogleURLTrackerClient* client_;
257 scoped_ptr<GoogleURLTracker> google_url_tracker_;
258 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_;
264 GoogleURLTrackerTest::GoogleURLTrackerTest() {
265 prefs_.registry()->RegisterStringPref(
266 prefs::kLastKnownGoogleURL,
267 GoogleURLTracker::kDefaultGoogleHomepage);
268 prefs_.registry()->RegisterStringPref(
269 prefs::kLastPromptedGoogleURL,
273 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
276 void GoogleURLTrackerTest::SetUp() {
277 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
278 // 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.
280 client_ = new TestGoogleURLTrackerClient(&prefs_);
281 scoped_ptr<GoogleURLTrackerClient> client(client_);
282 google_url_tracker_.reset(new GoogleURLTracker(
283 client.Pass(), GoogleURLTracker::UNIT_TEST_MODE));
286 void GoogleURLTrackerTest::TearDown() {
287 while (!infobar_managers_seen_.empty())
288 CloseTab(*infobar_managers_seen_.begin());
289 google_url_tracker_->Shutdown();
292 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
293 // This will return the last fetcher created. If no fetchers have been
294 // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
295 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
298 void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
299 const std::string& domain) {
300 net::TestURLFetcher* fetcher = GetFetcher();
303 fetcher_factory_.RemoveFetcherFromMap(fetcher->id());
304 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL));
305 fetcher->set_response_code(200);
306 fetcher->SetResponseString(domain);
307 fetcher->delegate()->OnURLFetchComplete(fetcher);
308 // At this point, |fetcher| is deleted.
311 void GoogleURLTrackerTest::RequestServerCheck() {
312 if (!listener_.HasRegisteredCallback())
313 listener_.RegisterCallback(google_url_tracker_.get());
314 google_url_tracker_->SetNeedToFetch();
317 void GoogleURLTrackerTest::FinishSleep() {
318 google_url_tracker_->FinishSleep();
321 void GoogleURLTrackerTest::NotifyNetworkChanged() {
322 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
323 net::NetworkChangeNotifier::CONNECTION_UNKNOWN);
324 // 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.
326 base::MessageLoop::current()->RunUntilIdle();
329 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
330 prefs_.SetString(prefs::kLastPromptedGoogleURL, url.spec());
333 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
334 return GURL(prefs_.GetString(prefs::kLastPromptedGoogleURL));
337 void GoogleURLTrackerTest::SetNavigationPending(
338 infobars::InfoBarManager* infobar_manager,
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.
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())),
352 infobar_manager->GetActiveEntryID());
356 void GoogleURLTrackerTest::CommitNonSearch(
357 infobars::InfoBarManager* infobar_manager) {
358 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager);
362 ExpectListeningForCommit(infobar_manager, false);
364 // The infobar should be showing; otherwise the pending non-search should
366 ASSERT_TRUE(map_entry->has_infobar_delegate());
368 // The pending_id should have been reset to 0 when the non-search became
370 EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id());
372 // Committing the navigation would close the infobar.
373 map_entry->infobar_delegate()->Close(false);
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);
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);
394 // Closing a tab with an infobar showing would close the infobar.
395 GoogleURLTrackerInfoBarDelegate* delegate =
396 GetInfoBarDelegate(infobar_manager);
398 delegate->Close(false);
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;
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;
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;
421 void GoogleURLTrackerTest::ExpectDefaultURLs() const {
422 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
423 EXPECT_EQ(GURL(), fetched_google_url());
426 void GoogleURLTrackerTest::ExpectListeningForCommit(
427 infobars::InfoBarManager* infobar_manager,
429 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager);
432 map_entry->navigation_helper()->IsListeningForNavigationCommit());
434 EXPECT_FALSE(listening);
438 // Tests ----------------------------------------------------------------------
440 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) {
443 // No one called RequestServerCheck() so nothing should have happened.
444 EXPECT_FALSE(GetFetcher());
445 MockSearchDomainCheckResponse("http://www.google.co.uk/");
447 EXPECT_FALSE(listener_notified());
450 TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) {
451 RequestServerCheck();
452 EXPECT_FALSE(GetFetcher());
454 EXPECT_FALSE(listener_notified());
457 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());
461 EXPECT_TRUE(listener_notified());
464 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) {
465 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
467 RequestServerCheck();
468 EXPECT_FALSE(GetFetcher());
470 EXPECT_FALSE(listener_notified());
473 MockSearchDomainCheckResponse("http://www.google.co.uk/");
474 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
475 // GoogleURL should not be updated, because the fetched and prompted URLs
477 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
478 EXPECT_FALSE(listener_notified());
481 TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) {
482 TestInfoBarManager infobar_manager(1);
483 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
485 RequestServerCheck();
486 EXPECT_FALSE(GetFetcher());
488 EXPECT_FALSE(listener_notified());
490 // Old-style domain string.
492 MockSearchDomainCheckResponse(".google.co.in");
493 EXPECT_EQ(GURL(), fetched_google_url());
494 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
495 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);
501 NotifyNetworkChanged();
502 MockSearchDomainCheckResponse("http://mail.google.com/");
503 EXPECT_EQ(GURL(), fetched_google_url());
504 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
505 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);
511 NotifyNetworkChanged();
512 MockSearchDomainCheckResponse("http://www.google.com/search");
513 EXPECT_EQ(GURL(), fetched_google_url());
514 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
515 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);
521 NotifyNetworkChanged();
522 MockSearchDomainCheckResponse("http://www.google.com/?q=foo");
523 EXPECT_EQ(GURL(), fetched_google_url());
524 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
525 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);
531 NotifyNetworkChanged();
532 MockSearchDomainCheckResponse("http://www.google.com/#anchor");
533 EXPECT_EQ(GURL(), fetched_google_url());
534 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
535 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);
541 NotifyNetworkChanged();
542 MockSearchDomainCheckResponse("HJ)*qF)_*&@f1");
543 EXPECT_EQ(GURL(), fetched_google_url());
544 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
545 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);
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();
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());
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();
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());
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());
584 TEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) {
585 RequestServerCheck();
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_TRUE(listener_notified());
591 clear_listener_notified();
593 NotifyNetworkChanged();
594 MockSearchDomainCheckResponse("http://www.google.co.in/");
595 EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url());
596 // Just fetching a new URL shouldn't reset things without a prompt.
597 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
598 EXPECT_FALSE(listener_notified());
601 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) {
603 NotifyNetworkChanged();
604 // No one called RequestServerCheck() so nothing should have happened.
605 EXPECT_FALSE(GetFetcher());
606 MockSearchDomainCheckResponse("http://www.google.co.uk/");
608 EXPECT_FALSE(listener_notified());
611 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) {
613 NotifyNetworkChanged();
614 MockSearchDomainCheckResponse("http://www.google.co.jp/");
616 RequestServerCheck();
617 // The first request for a check should trigger a fetch if it hasn't happened
619 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());
622 EXPECT_TRUE(listener_notified());
625 TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) {
627 NotifyNetworkChanged();
628 MockSearchDomainCheckResponse("http://www.google.co.jp/");
630 RequestServerCheck();
631 // The first request for a check should trigger a fetch if it hasn't happened
633 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());
636 EXPECT_TRUE(listener_notified());
637 clear_listener_notified();
639 RequestServerCheck();
640 // The second request should be ignored.
641 EXPECT_FALSE(GetFetcher());
642 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());
645 EXPECT_FALSE(listener_notified());
648 TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) {
649 TestInfoBarManager infobar_manager(1);
650 RequestServerCheck();
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();
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());
668 TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) {
669 TestInfoBarManager infobar_manager(1);
670 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
671 RequestServerCheck();
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());
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());
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());
694 TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) {
695 TestInfoBarManager infobar_manager(1);
696 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
697 RequestServerCheck();
699 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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);
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());
712 TEST_F(GoogleURLTrackerTest, InfoBarClosed) {
713 TestInfoBarManager infobar_manager(1);
714 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
715 RequestServerCheck();
717 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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);
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());
732 TEST_F(GoogleURLTrackerTest, InfoBarRefused) {
733 TestInfoBarManager infobar_manager(1);
734 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
735 RequestServerCheck();
737 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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);
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());
752 TEST_F(GoogleURLTrackerTest, InfoBarAccepted) {
753 TestInfoBarManager infobar_manager(1);
754 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
755 RequestServerCheck();
757 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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);
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());
772 TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) {
773 TestInfoBarManager infobar_manager(1);
774 RequestServerCheck();
776 MockSearchDomainCheckResponse(google_url().spec());
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);
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);
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);
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);
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);
843 TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) {
844 TestInfoBarManager infobar_manager(1);
845 RequestServerCheck();
847 MockSearchDomainCheckResponse(google_url().spec());
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());
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());
866 TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) {
867 TestInfoBarManager infobar_manager(1);
868 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
869 RequestServerCheck();
871 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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);
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));
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));
901 TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) {
902 TestInfoBarManager infobar_manager(1);
903 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
904 RequestServerCheck();
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));
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));
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));
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);
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));
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));
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));
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));
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());
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();
983 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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());
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());
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());
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());
1013 CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
1014 EXPECT_TRUE(map_entry->has_infobar_delegate());
1016 delegate2->Close(false);
1017 EXPECT_TRUE(GetMapEntry(&infobar_manager2) == NULL);
1018 EXPECT_FALSE(listener_notified());
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());
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();
1035 MockSearchDomainCheckResponse("http://www.google.co.jp/");
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));