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