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