146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
119ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/prefs/pref_registry_simple.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/prefs/testing_pref_service.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/google/core/browser/google_pref_names.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker_client.h"
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker_infobar_delegate.h"
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker_navigation_helper.h"
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar.h"
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar_delegate.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher.h"
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/url_request/url_request_test_util.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// TestCallbackListener ---------------------------------------------------
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class TestCallbackListener {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestCallbackListener();
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~TestCallbackListener();
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool HasRegisteredCallback();
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void RegisterCallback(GoogleURLTracker* google_url_tracker);
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool notified() const { return notified_; }
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void clear_notified() { notified_ = false; }
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnGoogleURLUpdated();
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool notified_;
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TestCallbackListener::TestCallbackListener() : notified_(false) {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TestCallbackListener::~TestCallbackListener() {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestCallbackListener::OnGoogleURLUpdated() {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notified_ = true;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool TestCallbackListener::HasRegisteredCallback() {
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return google_url_updated_subscription_.get();
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void TestCallbackListener::RegisterCallback(
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    GoogleURLTracker* google_url_tracker) {
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  google_url_updated_subscription_ =
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      google_url_tracker->RegisterCallback(base::Bind(
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this)));
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// TestGoogleURLTrackerClient -------------------------------------------------
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  explicit TestGoogleURLTrackerClient(PrefService* prefs_);
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~TestGoogleURLTrackerClient();
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool IsListeningForNavigationStart() OVERRIDE;
7946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual bool IsBackgroundNetworkingEnabled() OVERRIDE;
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual PrefService* GetPrefs() OVERRIDE;
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PrefService* prefs_;
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_refptr<net::TestURLRequestContextGetter> request_context_;
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool observe_nav_start_;
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs)
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : prefs_(prefs),
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      request_context_(new net::TestURLRequestContextGetter(
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::MessageLoopProxy::current())),
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      observe_nav_start_(false) {
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  observe_nav_start_ = listen;
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return observe_nav_start_;
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() {
11046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return true;
11146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
11246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
11346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)PrefService* TestGoogleURLTrackerClient::GetPrefs() {
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return prefs_;
11546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
11646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
11746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() {
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return request_context_.get();
11946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// TestGoogleURLTrackerNavigationHelper ---------------------------------------
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class TestGoogleURLTrackerNavigationHelper
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : public GoogleURLTrackerNavigationHelper {
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker);
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~TestGoogleURLTrackerNavigationHelper();
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetListeningForNavigationCommit(bool listen) OVERRIDE;
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool IsListeningForNavigationCommit() OVERRIDE;
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetListeningForTabDestruction(bool listen) OVERRIDE;
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool IsListeningForTabDestruction() OVERRIDE;
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OpenURL(GURL url,
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       WindowOpenDisposition disposition,
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       bool user_clicked_on_link) OVERRIDE;
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool listening_for_nav_commit_;
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool listening_for_tab_destruction_;
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerNavigationHelper);
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper(
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    GoogleURLTracker* tracker)
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : GoogleURLTrackerNavigationHelper(tracker),
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      listening_for_nav_commit_(false),
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      listening_for_tab_destruction_(false) {
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TestGoogleURLTrackerNavigationHelper::~TestGoogleURLTrackerNavigationHelper() {
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool listen) {
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  listening_for_nav_commit_ = listen;
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit() {
161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return listening_for_nav_commit_;
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TestGoogleURLTrackerNavigationHelper::SetListeningForTabDestruction(
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool listen) {
166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  listening_for_tab_destruction_ = listen;
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool TestGoogleURLTrackerNavigationHelper::IsListeningForTabDestruction() {
170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return listening_for_tab_destruction_;
171cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void TestGoogleURLTrackerNavigationHelper::OpenURL(
174cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    GURL url,
175cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    WindowOpenDisposition disposition,
176cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool user_clicked_on_link) {
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// TestInfoBarManager ---------------------------------------------------------
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class TestInfoBarManager : public infobars::InfoBarManager {
18246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
18346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  explicit TestInfoBarManager(int unique_id);
18446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual ~TestInfoBarManager();
18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int GetActiveEntryID() OVERRIDE;
18646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
18746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
18846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int unique_id_;
18946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestInfoBarManager);
19046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
19146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TestInfoBarManager::TestInfoBarManager(int unique_id) : unique_id_(unique_id) {
19346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
19446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TestInfoBarManager::~TestInfoBarManager() {
19646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ShutDown();
19746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
19846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)int TestInfoBarManager::GetActiveEntryID() {
20046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return unique_id_;
20146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
20246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
20346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}  // namespace
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// GoogleURLTrackerTest -------------------------------------------------------
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GoogleURLTrackerTest : public testing::Test {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GoogleURLTrackerTest();
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~GoogleURLTrackerTest();
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // testing::Test
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::TestURLFetcher* GetFetcher();
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void MockSearchDomainCheckResponse(const std::string& domain);
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RequestServerCheck();
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void FinishSleep();
2201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void NotifyNetworkChanged();
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL fetched_google_url() const {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return google_url_tracker_->fetched_google_url();
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_google_url(const GURL& url) {
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    google_url_tracker_->google_url_ = url;
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL google_url() const { return google_url_tracker_->google_url(); }
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetLastPromptedGoogleURL(const GURL& url);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL GetLastPromptedGoogleURL();
23046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void SetNavigationPending(infobars::InfoBarManager* infobar_manager,
23146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            bool is_search);
23246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void CommitNonSearch(infobars::InfoBarManager* infobar_manager);
23346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void CommitSearch(infobars::InfoBarManager* infobar_manager,
23446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                    const GURL& search_url);
23546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void CloseTab(infobars::InfoBarManager* infobar_manager);
23646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* GetMapEntry(
23746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      infobars::InfoBarManager* infobar_manager);
23846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* GetInfoBarDelegate(
23946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      infobars::InfoBarManager* infobar_manager);
24046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerNavigationHelper* GetNavigationHelper(
24146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      infobars::InfoBarManager* infobar_manager);
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ExpectDefaultURLs() const;
24346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void ExpectListeningForCommit(infobars::InfoBarManager* infobar_manager,
24446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                bool listening);
245cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool listener_notified() const { return listener_.notified(); }
246cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void clear_listener_notified() { listener_.clear_notified(); }
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::MessageLoop message_loop_;
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  TestingPrefServiceSimple prefs_;
25146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creating this allows us to call
2531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests().
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::TestURLFetcherFactory fetcher_factory_;
256cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GoogleURLTrackerClient* client_;
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<GoogleURLTracker> google_url_tracker_;
258cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  TestCallbackListener listener_;
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This tracks the different "tabs" a test has "opened", so we can close them
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // properly before shutting down |google_url_tracker_|, which expects that.
26146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::set<infobars::InfoBarManager*> infobar_managers_seen_;
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciGoogleURLTrackerTest::GoogleURLTrackerTest() {
2651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  prefs_.registry()->RegisterStringPref(
2661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      prefs::kLastKnownGoogleURL,
2671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      GoogleURLTracker::kDefaultGoogleHomepage);
2681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  prefs_.registry()->RegisterStringPref(
2691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      prefs::kLastPromptedGoogleURL,
2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      std::string());
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GoogleURLTrackerTest::~GoogleURLTrackerTest() {
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::SetUp() {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Ownership is passed to google_url_tracker_, but a weak pointer is kept;
279cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // this is safe since GoogleURLTracker keeps the client for its lifetime.
2801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  client_ = new TestGoogleURLTrackerClient(&prefs_);
281cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<GoogleURLTrackerClient> client(client_);
282cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  google_url_tracker_.reset(new GoogleURLTracker(
28346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      client.Pass(), GoogleURLTracker::UNIT_TEST_MODE));
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::TearDown() {
28746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  while (!infobar_managers_seen_.empty())
28846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    CloseTab(*infobar_managers_seen_.begin());
289cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  google_url_tracker_->Shutdown();
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This will return the last fetcher created.  If no fetchers have been
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& domain) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::TestURLFetcher* fetcher = GetFetcher();
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!fetcher)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetcher_factory_.RemoveFetcherFromMap(fetcher->id());
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL));
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetcher->set_response_code(200);
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetcher->SetResponseString(domain);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fetcher->delegate()->OnURLFetchComplete(fetcher);
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // At this point, |fetcher| is deleted.
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::RequestServerCheck() {
312cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!listener_.HasRegisteredCallback())
313cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    listener_.RegisterCallback(google_url_tracker_.get());
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  google_url_tracker_->SetNeedToFetch();
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::FinishSleep() {
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  google_url_tracker_->FinishSleep();
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid GoogleURLTrackerTest::NotifyNetworkChanged() {
3221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
3231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      net::NetworkChangeNotifier::CONNECTION_UNKNOWN);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For thread safety, the NCN queues tasks to do the actual notifications, so
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we need to spin the message loop so the tracker will actually be notified.
32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->RunUntilIdle();
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
3301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  prefs_.SetString(prefs::kLastPromptedGoogleURL, url.spec());
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
3341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return GURL(prefs_.GetString(prefs::kLastPromptedGoogleURL));
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
33746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void GoogleURLTrackerTest::SetNavigationPending(
33846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager,
33946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bool is_search) {
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (is_search) {
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    google_url_tracker_->SearchCommitted();
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Note that the call above might not have actually registered a listener
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // for navigation starts if the searchdomaincheck response was bogus.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
34546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  infobar_managers_seen_.insert(infobar_manager);
346cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (client_->IsListeningForNavigationStart()) {
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    google_url_tracker_->OnNavigationPending(
348cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        scoped_ptr<GoogleURLTrackerNavigationHelper>(
349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            new TestGoogleURLTrackerNavigationHelper(
350cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                google_url_tracker_.get())),
35146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        infobar_manager,
35246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        infobar_manager->GetActiveEntryID());
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void GoogleURLTrackerTest::CommitNonSearch(
35746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager) {
35846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager);
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!map_entry)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ExpectListeningForCommit(infobar_manager, false);
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The infobar should be showing; otherwise the pending non-search should
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // have closed it.
3667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_TRUE(map_entry->has_infobar_delegate());
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The pending_id should have been reset to 0 when the non-search became
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pending.
3707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(0, map_entry->infobar_delegate()->pending_id());
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Committing the navigation would close the infobar.
3737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  map_entry->infobar_delegate()->Close(false);
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
37646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void GoogleURLTrackerTest::CommitSearch(
37746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager,
37846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    const GURL& search_url) {
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(search_url.is_valid());
38046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerNavigationHelper* nav_helper =
38146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetNavigationHelper(infobar_manager);
382cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (nav_helper && nav_helper->IsListeningForNavigationCommit()) {
38346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    google_url_tracker_->OnNavigationCommitted(infobar_manager, search_url);
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void GoogleURLTrackerTest::CloseTab(infobars::InfoBarManager* infobar_manager) {
38846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  infobar_managers_seen_.erase(infobar_manager);
38946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerNavigationHelper* nav_helper =
39046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetNavigationHelper(infobar_manager);
391cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (nav_helper && nav_helper->IsListeningForTabDestruction()) {
392cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    google_url_tracker_->OnTabClosed(nav_helper);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Closing a tab with an infobar showing would close the infobar.
39546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    GoogleURLTrackerInfoBarDelegate* delegate =
39646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        GetInfoBarDelegate(infobar_manager);
3977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (delegate)
3987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      delegate->Close(false);
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GoogleURLTrackerMapEntry* GoogleURLTrackerTest::GetMapEntry(
40346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager) {
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GoogleURLTracker::EntryMap::const_iterator i =
40546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      google_url_tracker_->entry_map_.find(infobar_manager);
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (i == google_url_tracker_->entry_map_.end()) ? NULL : i->second;
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4097dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochGoogleURLTrackerInfoBarDelegate* GoogleURLTrackerTest::GetInfoBarDelegate(
41046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager) {
41146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager);
4127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return map_entry ? map_entry->infobar_delegate() : NULL;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)GoogleURLTrackerNavigationHelper* GoogleURLTrackerTest::GetNavigationHelper(
41646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager) {
41746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager);
418cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return map_entry ? map_entry->navigation_helper() : NULL;
419cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
420cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GoogleURLTrackerTest::ExpectDefaultURLs() const {
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
42646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void GoogleURLTrackerTest::ExpectListeningForCommit(
42746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    infobars::InfoBarManager* infobar_manager,
42846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bool listening) {
42946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(infobar_manager);
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (map_entry) {
431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(listening,
432cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              map_entry->navigation_helper()->IsListeningForNavigationCommit());
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_FALSE(listening);
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests ----------------------------------------------------------------------
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) {
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectDefaultURLs();
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No one called RequestServerCheck() so nothing should have happened.
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(GetFetcher());
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectDefaultURLs();
447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) {
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(GetFetcher());
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectDefaultURLs();
454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GoogleURL should be updated, becase there was no last prompted URL.
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) {
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(GetFetcher());
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectDefaultURLs();
470cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GoogleURL should not be updated, because the fetched and prompted URLs
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // match.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
478cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, DontPromptOnBadReplies) {
48246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(GetFetcher());
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectDefaultURLs();
488cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Old-style domain string.
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse(".google.co.in");
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
495cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
49646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
49746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
49846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Bad subdomain.
5011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://mail.google.com/");
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
505cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
50646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
50746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
50846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Non-empty path.
5111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.com/search");
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
515cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
51646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
51746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
51846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Non-empty query.
5211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.com/?q=foo");
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
525cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
52646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
52746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
52846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Non-empty ref.
5311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.com/#anchor");
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
535cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
53646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
53746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
53846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Complete garbage.
5411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("HJ)*qF)_*&@f1");
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(), fetched_google_url());
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
545cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
54646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
54746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
54846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) {
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_google_url(GURL("http://www.google.co.uk/"));
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
560cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, SilentlyAcceptSchemeChange) {
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We should auto-accept changes to the current Google URL that merely change
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the scheme, regardless of what the last prompted URL was.
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_google_url(GURL("http://www.google.co.uk/"));
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("https://www.google.co.uk/");
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url());
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("https://www.google.co.uk/"), google_url());
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
574cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
581cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciTEST_F(GoogleURLTrackerTest, RefetchOnNetworkChange) {
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
590cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
591cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  clear_listener_notified();
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.in/");
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url());
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Just fetching a new URL shouldn't reset things without a prompt.
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
598cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) {
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
6031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No one called RequestServerCheck() so nothing should have happened.
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(GetFetcher());
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExpectDefaultURLs();
608cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) {
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
6131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The first request for a check should trigger a fetch if it hasn't happened
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // already.
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
622cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) {
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
6271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The first request for a check should trigger a fetch if it hasn't happened
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // already.
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
636cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
637cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  clear_listener_notified();
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The second request should be ignored.
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(GetFetcher());
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.in/");
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
645cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) {
64946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
656cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
657cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  clear_listener_notified();
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
66046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
66146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
665cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) {
66946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url());
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
677cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
67946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
68046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager);
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(map_entry == NULL);
6827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(map_entry->has_infobar_delegate());
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
685cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
68746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CloseTab(&infobar_manager);
68846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
691cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) {
69546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
70146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
70246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
70346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
70546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CloseTab(&infobar_manager);
70646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
709cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, InfoBarClosed) {
71346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
71946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
72046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
72146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* infobar =
72246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager);
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(infobar == NULL);
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  infobar->Close(false);
72646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
729cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, InfoBarRefused) {
73346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
73946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
74046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
74146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* infobar =
74246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager);
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(infobar == NULL);
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  infobar->Cancel();
74646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
749cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, InfoBarAccepted) {
75346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
76046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
76146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* infobar =
76246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager);
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(infobar == NULL);
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  infobar->Accept();
76646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
769cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, FetchesCanAutomaticallyCloseInfoBars) {
77346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse(google_url().spec());
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Re-fetching the accepted URL after showing an infobar for another URL
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should close the infobar.
7801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
78246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
78346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test"));
78446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
7851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse(google_url().spec());
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(google_url(), GetLastPromptedGoogleURL());
78846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // As should fetching a URL that differs from the accepted only by the scheme.
7911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
79346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
79446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test"));
79546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
7961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
7975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  url::Replacements<char> replacements;
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& scheme("https");
7995c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  replacements.SetScheme(scheme.data(), url::Component(0, scheme.length()));
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL new_google_url(google_url().ReplaceComponents(replacements));
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse(new_google_url.spec());
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_google_url, GetLastPromptedGoogleURL());
80346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // As should re-fetching the last prompted URL.
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
8071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
80946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
81046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test"));
81146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
8121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_google_url, google_url());
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
81646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // And one that differs from the last prompted URL only by the scheme.
8191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
82146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
82246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test"));
82346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
8241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("https://www.google.co.uk/");
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_google_url, google_url());
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
82846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // And fetching a different URL entirely.
8311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
83346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
83446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test"));
83546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
8361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("https://www.google.co.in/");
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(new_google_url, google_url());
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("https://www.google.co.uk/"), GetLastPromptedGoogleURL());
84046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, ResetInfoBarGoogleURLs) {
84446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse(google_url().spec());
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.uk/");
85146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
85246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.com/search?q=test"));
85346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* delegate =
85446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager);
8557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(delegate == NULL);
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If while an infobar is showing we fetch a new URL that differs from the
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // infobar's only by scheme, the infobar should stay showing.
8601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  NotifyNetworkChanged();
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("https://www.google.co.uk/");
86246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("https://www.google.co.uk/"), fetched_google_url());
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, NavigationsAfterPendingSearch) {
86746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A pending non-search after a pending search should delete the map entry.
87446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
87546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager);
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(map_entry == NULL);
8777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(map_entry->has_infobar_delegate());
87846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, false);
87946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A pending search after a pending search should leave the map entry alive.
88246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
88346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  map_entry = GetMapEntry(&infobar_manager);
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(map_entry == NULL);
8857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(map_entry->has_infobar_delegate());
88646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
88746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(map_entry, GetMapEntry(&infobar_manager));
8887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(map_entry->has_infobar_delegate());
88946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Committing this search should show an infobar.
89246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager,
89346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)               GURL("http://www.google.co.uk/search?q=test2"));
8947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(map_entry->has_infobar_delegate());
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
897cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
89846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, NavigationsAfterCommittedSearch) {
90246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
90746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
90846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
90946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* delegate =
91046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager);
9117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(delegate == NULL);
91246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A pending non-search on a visible infobar should basically do nothing.
91546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, false);
91646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(0, delegate->pending_id());
91846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // As should another pending non-search after the first.
92146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, false);
92246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(0, delegate->pending_id());
92446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Committing this non-search should close the infobar.  The control flow in
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // these tests is not really comparable to in the real browser, but at least a
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // few sanity-checks will be performed.
92946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(CommitNonSearch(&infobar_manager));
93046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A pending search on a visible infobar should cause the infobar to listen
9335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for the search to commit.
93446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
93546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
93646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  delegate = GetInfoBarDelegate(&infobar_manager);
9377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(delegate == NULL);
93846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
93946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(1, delegate->pending_id());
94146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // But a non-search after this should cancel that state.
94446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, false);
94546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(0, delegate->pending_id());
94746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Another pending search after the non-search should put us back into
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "waiting for commit" mode.
95146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
95246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(1, delegate->pending_id());
95446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A second pending search after the first should not really change anything.
95746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
95846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(1, delegate->pending_id());
96046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Committing this search should change the visible infobar's search_url.
96346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager,
96446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)               GURL("http://www.google.co.uk/search?q=test2"));
96546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_EQ(delegate, GetInfoBarDelegate(&infobar_manager));
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
9677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            delegate->search_url());
9687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_EQ(0, delegate->pending_id());
96946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
972cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, MultipleMapEntries) {
97646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
97746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager2(2);
97846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager3(3);
97946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager4(4);
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
9835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
98546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
98646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry = GetMapEntry(&infobar_manager);
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(map_entry == NULL);
9887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(map_entry->has_infobar_delegate());
9895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
99046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager2, true);
99146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager2,
99246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)               GURL("http://www.google.co.uk/search?q=test2"));
99346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* delegate2 =
99446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager2);
9957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(delegate2 == NULL);
9965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
9977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            delegate2->search_url());
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
99946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager3, true);
100046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerMapEntry* map_entry3 = GetMapEntry(&infobar_manager3);
10015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(map_entry3 == NULL);
10027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_FALSE(map_entry3->has_infobar_delegate());
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
100446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager4, true);
100546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager4,
100646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)               GURL("http://www.google.co.uk/search?q=test4"));
100746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* delegate4 =
100846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager4);
10097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(delegate4 == NULL);
10105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"),
10117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            delegate4->search_url());
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
10147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(map_entry->has_infobar_delegate());
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  delegate2->Close(false);
101746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager2) == NULL);
1018cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(listener_notified());
10195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  delegate4->Accept();
102146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager) == NULL);
102246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager3) == NULL);
102346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(GetMapEntry(&infobar_manager4) == NULL);
10245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
1026cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(listener_notified());
10275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(GoogleURLTrackerTest, IgnoreIrrelevantNavigation) {
103046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager(1);
103146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestInfoBarManager infobar_manager2(2);
10325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RequestServerCheck();
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FinishSleep();
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockSearchDomainCheckResponse("http://www.google.co.jp/");
10365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This tests a particularly gnarly sequence of events that used to cause us
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to erroneously listen for a non-search navigation to commit.
103946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
104046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager, GURL("http://www.google.co.uk/search?q=test"));
104146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager2, true);
104246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  CommitSearch(&infobar_manager2,
104346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)               GURL("http://www.google.co.uk/search?q=test2"));
104446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
104546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GoogleURLTrackerInfoBarDelegate* delegate2 =
104646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GetInfoBarDelegate(&infobar_manager2);
10477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ASSERT_FALSE(delegate2 == NULL);
104846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, true);
104946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
10507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  delegate2->Close(false);
105146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  SetNavigationPending(&infobar_manager, false);
105246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
10535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1054