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