Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: chrome/browser/google/google_url_tracker_unittest.cc

Issue 4880003: Fix a number of problems with the GoogleURLTracker (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_loop.h" 5 #include "base/message_loop.h"
6 #include "chrome/browser/google/google_url_tracker.h" 6 #include "chrome/browser/google/google_url_tracker.h"
7 #include "chrome/browser/prefs/browser_prefs.h" 7 #include "chrome/browser/prefs/browser_prefs.h"
8 #include "chrome/common/chrome_notification_types.h" 8 #include "chrome/common/chrome_notification_types.h"
9 #include "chrome/common/pref_names.h" 9 #include "chrome/common/pref_names.h"
10 #include "chrome/test/base/testing_browser_process.h" 10 #include "chrome/test/base/testing_browser_process.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 void TestNotificationObserver::Observe( 43 void TestNotificationObserver::Observe(
44 int type, 44 int type,
45 const content::NotificationSource& source, 45 const content::NotificationSource& source,
46 const content::NotificationDetails& details) { 46 const content::NotificationDetails& details) {
47 notified_ = true; 47 notified_ = true;
48 } 48 }
49 49
50 50
51 // TestInfoBarDelegate -------------------------------------------------------- 51 // TestInfoBarDelegate --------------------------------------------------------
52 52
53 class TestInfoBarDelegate : public InfoBarDelegate { 53 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate {
54 public: 54 public:
55 TestInfoBarDelegate(GoogleURLTracker* google_url_tracker, 55 TestInfoBarDelegate(InfoBarTabHelper* infobar_helper,
56 const GURL& search_url,
57 GoogleURLTracker* google_url_tracker,
56 const GURL& new_google_url); 58 const GURL& new_google_url);
57 virtual ~TestInfoBarDelegate(); 59 virtual ~TestInfoBarDelegate();
58 60
59 GoogleURLTracker* google_url_tracker() const { return google_url_tracker_; } 61 GURL search_url() const { return search_url_; }
60 GURL new_google_url() const { return new_google_url_; } 62 GURL new_google_url() const { return new_google_url_; }
63 bool showing() const { return showing_; }
61 64
62 private: 65 private:
63 // InfoBarDelegate: 66 // GoogleURLTrackerInfoBarDelegate:
64 virtual InfoBar* CreateInfoBar(InfoBarTabHelper* infobar_helper) OVERRIDE; 67 virtual void Show() OVERRIDE;
65 68 virtual void Close(bool redo_search) OVERRIDE;
66 GoogleURLTracker* google_url_tracker_;
67 GURL new_google_url_;
68 }; 69 };
69 70
70 TestInfoBarDelegate::TestInfoBarDelegate(GoogleURLTracker* google_url_tracker, 71 TestInfoBarDelegate::TestInfoBarDelegate(InfoBarTabHelper* infobar_helper,
72 const GURL& search_url,
73 GoogleURLTracker* google_url_tracker,
71 const GURL& new_google_url) 74 const GURL& new_google_url)
72 : InfoBarDelegate(NULL), 75 : GoogleURLTrackerInfoBarDelegate(NULL, search_url, google_url_tracker,
73 google_url_tracker_(google_url_tracker), 76 new_google_url) {
74 new_google_url_(new_google_url) { 77 // We set |map_key_| here instead of in the superclass constructor so that the
78 // InfoBarDelegate base class will not try to dereference it, which would fail
79 // since this is really a magic number and not an actual pointer.
80 map_key_ = infobar_helper;
81 }
82
83 void TestInfoBarDelegate::Show() {
84 showing_ = true;
85 }
86
87 void TestInfoBarDelegate::Close(bool redo_search) {
88 InfoBarClosed();
75 } 89 }
76 90
77 TestInfoBarDelegate::~TestInfoBarDelegate() { 91 TestInfoBarDelegate::~TestInfoBarDelegate() {
78 } 92 }
79 93
80 InfoBar* TestInfoBarDelegate::CreateInfoBar(InfoBarTabHelper* infobar_helper) { 94 GoogleURLTrackerInfoBarDelegate* CreateTestInfobar(
81 return NULL;
82 }
83
84 InfoBarDelegate* CreateTestInfobar(
85 InfoBarTabHelper* infobar_helper, 95 InfoBarTabHelper* infobar_helper,
96 const GURL& search_url,
86 GoogleURLTracker* google_url_tracker, 97 GoogleURLTracker* google_url_tracker,
87 const GURL& new_google_url) { 98 const GURL& new_google_url) {
88 return new TestInfoBarDelegate(google_url_tracker, new_google_url); 99 return new TestInfoBarDelegate(infobar_helper, search_url, google_url_tracker,
100 new_google_url);
89 } 101 }
90 102
91 } // namespace 103 } // namespace
92 104
93 105
94 // GoogleURLTrackerTest ------------------------------------------------------- 106 // GoogleURLTrackerTest -------------------------------------------------------
95 107
96 class GoogleURLTrackerTest : public testing::Test { 108 class GoogleURLTrackerTest : public testing::Test {
97 protected: 109 protected:
98 GoogleURLTrackerTest(); 110 GoogleURLTrackerTest();
99 virtual ~GoogleURLTrackerTest(); 111 virtual ~GoogleURLTrackerTest();
100 112
101 // testing::Test 113 // testing::Test
102 virtual void SetUp(); 114 virtual void SetUp();
103 virtual void TearDown(); 115 virtual void TearDown();
104 116
105 TestURLFetcher* GetFetcherByID(int expected_id); 117 TestURLFetcher* GetFetcher();
106 void MockSearchDomainCheckResponse(int expected_id, 118 void MockSearchDomainCheckResponse(const std::string& domain);
107 const std::string& domain);
108 void RequestServerCheck(); 119 void RequestServerCheck();
109 void FinishSleep(); 120 void FinishSleep();
110 void NotifyIPAddressChanged(); 121 void NotifyIPAddressChanged();
111 GURL fetched_google_url() const { 122 GURL fetched_google_url() const {
112 return google_url_tracker_->fetched_google_url_; 123 return google_url_tracker_->fetched_google_url_;
113 } 124 }
114 void set_google_url(const GURL& url) { 125 void set_google_url(const GURL& url) {
115 google_url_tracker_->google_url_ = url; 126 google_url_tracker_->google_url_ = url;
116 } 127 }
117 GURL google_url() const { return google_url_tracker_->google_url_; } 128 GURL google_url() const { return google_url_tracker_->google_url_; }
118 void SetLastPromptedGoogleURL(const GURL& url); 129 void SetLastPromptedGoogleURL(const GURL& url);
119 GURL GetLastPromptedGoogleURL(); 130 GURL GetLastPromptedGoogleURL();
120 void SearchCommitted(const GURL& search_url); 131 void SetSearchPending(const GURL& search_url, int unique_id);
121 void NavEntryCommitted(); 132 void CommitSearch(int unique_id);
122 bool infobar_showing() const { 133 void CloseTab(int unique_id);
123 return (google_url_tracker_->infobar_ != NULL); 134 TestInfoBarDelegate* GetInfoBar(int unique_id);
124 }
125 GURL infobar_url() const {
126 return static_cast<TestInfoBarDelegate*>(google_url_tracker_->infobar_)->
127 new_google_url();
128 }
129 void AcceptGoogleURL();
130 void CancelGoogleURL();
131 void InfoBarClosed();
132 void ExpectDefaultURLs(); 135 void ExpectDefaultURLs();
133 136
134 scoped_ptr<TestNotificationObserver> observer_; 137 scoped_ptr<TestNotificationObserver> observer_;
135 138
136 private: 139 private:
137 // These are required by the TestURLFetchers GoogleURLTracker will create (see 140 // These are required by the TestURLFetchers GoogleURLTracker will create (see
138 // test_url_fetcher_factory.h). 141 // test_url_fetcher_factory.h).
139 MessageLoop message_loop_; 142 MessageLoop message_loop_;
140 content::TestBrowserThread io_thread_; 143 content::TestBrowserThread io_thread_;
141 // Creating this allows us to call 144 // Creating this allows us to call
(...skipping 20 matching lines...) Expand all
162 google_url_tracker_.reset( 165 google_url_tracker_.reset(
163 new GoogleURLTracker(GoogleURLTracker::UNIT_TEST_MODE)); 166 new GoogleURLTracker(GoogleURLTracker::UNIT_TEST_MODE));
164 google_url_tracker_->infobar_creator_ = &CreateTestInfobar; 167 google_url_tracker_->infobar_creator_ = &CreateTestInfobar;
165 } 168 }
166 169
167 void GoogleURLTrackerTest::TearDown() { 170 void GoogleURLTrackerTest::TearDown() {
168 google_url_tracker_.reset(); 171 google_url_tracker_.reset();
169 network_change_notifier_.reset(); 172 network_change_notifier_.reset();
170 } 173 }
171 174
172 TestURLFetcher* GoogleURLTrackerTest::GetFetcherByID(int expected_id) { 175 TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
173 return fetcher_factory_.GetFetcherByID(expected_id); 176 // This will return the last fetcher created. If no fetchers have been
177 // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
178 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
174 } 179 }
175 180
176 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( 181 void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
177 int expected_id,
178 const std::string& domain) { 182 const std::string& domain) {
179 TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(expected_id); 183 TestURLFetcher* fetcher = GetFetcher();
180 if (!fetcher) 184 if (!fetcher)
181 return; 185 return;
186 fetcher_factory_.RemoveFetcherFromMap(fetcher->id());
182 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL)); 187 fetcher->set_url(GURL(GoogleURLTracker::kSearchDomainCheckURL));
183 fetcher->set_response_code(200); 188 fetcher->set_response_code(200);
184 fetcher->SetResponseString(domain); 189 fetcher->SetResponseString(domain);
185 fetcher->delegate()->OnURLFetchComplete(fetcher); 190 fetcher->delegate()->OnURLFetchComplete(fetcher);
186 // At this point, |fetcher| is deleted. 191 // At this point, |fetcher| is deleted.
187 } 192 }
188 193
189 void GoogleURLTrackerTest::RequestServerCheck() { 194 void GoogleURLTrackerTest::RequestServerCheck() {
190 if (!registrar_.IsRegistered(observer_.get(), 195 if (!registrar_.IsRegistered(observer_.get(),
191 chrome::NOTIFICATION_GOOGLE_URL_UPDATED, 196 chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
(...skipping 18 matching lines...) Expand all
210 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { 215 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
211 g_browser_process->local_state()->SetString( 216 g_browser_process->local_state()->SetString(
212 prefs::kLastPromptedGoogleURL, url.spec()); 217 prefs::kLastPromptedGoogleURL, url.spec());
213 } 218 }
214 219
215 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { 220 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
216 return GURL(g_browser_process->local_state()->GetString( 221 return GURL(g_browser_process->local_state()->GetString(
217 prefs::kLastPromptedGoogleURL)); 222 prefs::kLastPromptedGoogleURL));
218 } 223 }
219 224
220 void GoogleURLTrackerTest::SearchCommitted(const GURL& search_url) { 225 void GoogleURLTrackerTest::SetSearchPending(const GURL& search_url,
226 int unique_id) {
221 google_url_tracker_->SearchCommitted(); 227 google_url_tracker_->SearchCommitted();
222 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(), 228 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(),
223 content::NOTIFICATION_NAV_ENTRY_PENDING, 229 content::NOTIFICATION_NAV_ENTRY_PENDING,
224 content::NotificationService::AllBrowserContextsAndSources())) 230 content::NotificationService::AllBrowserContextsAndSources())) {
225 google_url_tracker_->search_url_ = search_url; 231 google_url_tracker_->OnNavigationPending(
232 content::Source<content::NavigationController>(
233 reinterpret_cast<content::NavigationController*>(unique_id)),
234 content::Source<content::WebContents>(
235 reinterpret_cast<content::WebContents*>(unique_id)),
236 reinterpret_cast<InfoBarTabHelper*>(unique_id), search_url);
237 }
226 } 238 }
227 239
228 void GoogleURLTrackerTest::NavEntryCommitted() { 240 void GoogleURLTrackerTest::CommitSearch(int unique_id) {
229 google_url_tracker_->ShowGoogleURLInfoBarIfNecessary(NULL); 241 content::Source<content::NavigationController> source(
242 reinterpret_cast<content::NavigationController*>(unique_id));
243 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(),
244 content::NOTIFICATION_NAV_ENTRY_COMMITTED, source)) {
245 google_url_tracker_->OnNavigationCommittedOrTabClosed(source,
246 content::Source<content::WebContents>(
247 reinterpret_cast<content::WebContents*>(unique_id)),
248 reinterpret_cast<InfoBarTabHelper*>(unique_id),
249 content::NOTIFICATION_NAV_ENTRY_COMMITTED);
250 }
230 } 251 }
231 252
232 void GoogleURLTrackerTest::AcceptGoogleURL() { 253 void GoogleURLTrackerTest::CloseTab(int unique_id) {
233 TestInfoBarDelegate* infobar = 254 content::Source<content::WebContents> source(
234 static_cast<TestInfoBarDelegate*>(google_url_tracker_->infobar_); 255 reinterpret_cast<content::WebContents*>(unique_id));
235 ASSERT_TRUE(infobar); 256 InfoBarTabHelper* infobar_helper =
236 ASSERT_TRUE(infobar->google_url_tracker()); 257 reinterpret_cast<InfoBarTabHelper*>(unique_id);
237 ASSERT_EQ(google_url_tracker_, infobar->google_url_tracker()); 258 if (google_url_tracker_->registrar_.IsRegistered(google_url_tracker_.get(),
238 google_url_tracker_->AcceptGoogleURL(infobar->new_google_url()); 259 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, source)) {
260 google_url_tracker_->OnNavigationCommittedOrTabClosed(
261 content::Source<content::NavigationController>(
262 reinterpret_cast<content::NavigationController*>(unique_id)),
263 source, infobar_helper, content::NOTIFICATION_WEB_CONTENTS_DESTROYED);
264 } else {
265 // Normally, closing a tab with an infobar showing will close the infobar.
266 // Since we don't have real tabs and are just faking things with magic
267 // numbers, we have to manually close the infobar, if any.
268 GoogleURLTracker::InfoBars::iterator i =
269 google_url_tracker_->infobars_.find(infobar_helper);
270 if (i != google_url_tracker_->infobars_.end()) {
271 TestInfoBarDelegate* infobar =
272 static_cast<TestInfoBarDelegate*>(i->second);
273 DCHECK(infobar->showing());
274 infobar->InfoBarClosed();
275 }
276 }
239 } 277 }
240 278
241 void GoogleURLTrackerTest::CancelGoogleURL() { 279 TestInfoBarDelegate* GoogleURLTrackerTest::GetInfoBar(int unique_id) {
242 TestInfoBarDelegate* infobar = 280 const GoogleURLTracker::InfoBars& infobars = google_url_tracker_->infobars_;
243 static_cast<TestInfoBarDelegate*>(google_url_tracker_->infobar_); 281 GoogleURLTracker::InfoBars::const_iterator i =
244 ASSERT_TRUE(infobar); 282 infobars.find(reinterpret_cast<InfoBarTabHelper*>(unique_id));
245 ASSERT_TRUE(infobar->google_url_tracker()); 283 return (i == infobars.end()) ?
246 ASSERT_EQ(google_url_tracker_, infobar->google_url_tracker()); 284 NULL : static_cast<TestInfoBarDelegate*>(i->second);
247 google_url_tracker_->CancelGoogleURL(infobar->new_google_url());
248 }
249
250 void GoogleURLTrackerTest::InfoBarClosed() {
251 TestInfoBarDelegate* infobar =
252 static_cast<TestInfoBarDelegate*>(google_url_tracker_->infobar_);
253 ASSERT_TRUE(infobar);
254 ASSERT_TRUE(infobar->google_url_tracker());
255 ASSERT_EQ(google_url_tracker_, infobar->google_url_tracker());
256 google_url_tracker_->InfoBarClosed();
257 delete infobar;
258 } 285 }
259 286
260 void GoogleURLTrackerTest::ExpectDefaultURLs() { 287 void GoogleURLTrackerTest::ExpectDefaultURLs() {
261 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); 288 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
262 EXPECT_EQ(GURL(), fetched_google_url()); 289 EXPECT_EQ(GURL(), fetched_google_url());
263 } 290 }
264 291
265 292
266 // Tests ---------------------------------------------------------------------- 293 // Tests ----------------------------------------------------------------------
267 294
268 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { 295 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) {
269 ExpectDefaultURLs(); 296 ExpectDefaultURLs();
270 FinishSleep(); 297 FinishSleep();
271 // No one called RequestServerCheck() so nothing should have happened. 298 // No one called RequestServerCheck() so nothing should have happened.
272 EXPECT_FALSE(GetFetcherByID(0)); 299 EXPECT_FALSE(GetFetcher());
300 MockSearchDomainCheckResponse(".google.co.uk");
273 ExpectDefaultURLs(); 301 ExpectDefaultURLs();
274 EXPECT_FALSE(observer_->notified()); 302 EXPECT_FALSE(observer_->notified());
275 } 303 }
276 304
277 TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) { 305 TEST_F(GoogleURLTrackerTest, UpdateOnFirstRun) {
278 RequestServerCheck(); 306 RequestServerCheck();
279 EXPECT_FALSE(GetFetcherByID(0)); 307 EXPECT_FALSE(GetFetcher());
280 ExpectDefaultURLs(); 308 ExpectDefaultURLs();
281 EXPECT_FALSE(observer_->notified()); 309 EXPECT_FALSE(observer_->notified());
282 310
283 FinishSleep(); 311 FinishSleep();
284 MockSearchDomainCheckResponse(0, ".google.co.uk"); 312 MockSearchDomainCheckResponse(".google.co.uk");
285 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 313 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
286 // GoogleURL should be updated, becase there was no last prompted URL. 314 // GoogleURL should be updated, becase there was no last prompted URL.
287 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 315 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
288 EXPECT_TRUE(observer_->notified()); 316 EXPECT_TRUE(observer_->notified());
289 } 317 }
290 318
291 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) { 319 TEST_F(GoogleURLTrackerTest, DontUpdateWhenUnchanged) {
292 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); 320 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
293 321
294 RequestServerCheck(); 322 RequestServerCheck();
295 EXPECT_FALSE(GetFetcherByID(0)); 323 EXPECT_FALSE(GetFetcher());
296 ExpectDefaultURLs(); 324 ExpectDefaultURLs();
297 EXPECT_FALSE(observer_->notified()); 325 EXPECT_FALSE(observer_->notified());
298 326
299 FinishSleep(); 327 FinishSleep();
300 MockSearchDomainCheckResponse(0, ".google.co.uk"); 328 MockSearchDomainCheckResponse(".google.co.uk");
301 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 329 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
302 // GoogleURL should not be updated, because the fetched and prompted URLs 330 // GoogleURL should not be updated, because the fetched and prompted URLs
303 // match. 331 // match.
304 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); 332 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
305 EXPECT_FALSE(observer_->notified()); 333 EXPECT_FALSE(observer_->notified());
306 } 334 }
307 335
308 TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) { 336 TEST_F(GoogleURLTrackerTest, UpdatePromptedURLOnReturnToPreviousLocation) {
309 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/")); 337 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
310 set_google_url(GURL("http://www.google.co.uk/")); 338 set_google_url(GURL("http://www.google.co.uk/"));
311 RequestServerCheck(); 339 RequestServerCheck();
312 FinishSleep(); 340 FinishSleep();
313 MockSearchDomainCheckResponse(0, ".google.co.uk"); 341 MockSearchDomainCheckResponse(".google.co.uk");
314 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 342 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
315 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 343 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
316 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); 344 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
317 EXPECT_FALSE(observer_->notified()); 345 EXPECT_FALSE(observer_->notified());
318 } 346 }
319 347
320 TEST_F(GoogleURLTrackerTest, RefetchOnIPAddressChange) { 348 TEST_F(GoogleURLTrackerTest, RefetchOnIPAddressChange) {
321 RequestServerCheck(); 349 RequestServerCheck();
322 FinishSleep(); 350 FinishSleep();
323 MockSearchDomainCheckResponse(0, ".google.co.uk"); 351 MockSearchDomainCheckResponse(".google.co.uk");
324 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 352 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
325 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 353 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
326 EXPECT_TRUE(observer_->notified()); 354 EXPECT_TRUE(observer_->notified());
327 observer_->clear_notified(); 355 observer_->clear_notified();
328 356
329 NotifyIPAddressChanged(); 357 NotifyIPAddressChanged();
330 MockSearchDomainCheckResponse(1, ".google.co.in"); 358 MockSearchDomainCheckResponse(".google.co.in");
331 EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url()); 359 EXPECT_EQ(GURL("http://www.google.co.in/"), fetched_google_url());
332 // Just fetching a new URL shouldn't reset things without a prompt. 360 // Just fetching a new URL shouldn't reset things without a prompt.
333 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 361 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
334 EXPECT_FALSE(observer_->notified()); 362 EXPECT_FALSE(observer_->notified());
335 } 363 }
336 364
337 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) { 365 TEST_F(GoogleURLTrackerTest, DontRefetchWhenNoOneRequestsCheck) {
338 FinishSleep(); 366 FinishSleep();
339 NotifyIPAddressChanged(); 367 NotifyIPAddressChanged();
340 // No one called RequestServerCheck() so nothing should have happened. 368 // No one called RequestServerCheck() so nothing should have happened.
341 EXPECT_FALSE(GetFetcherByID(0)); 369 EXPECT_FALSE(GetFetcher());
370 MockSearchDomainCheckResponse(".google.co.uk");
342 ExpectDefaultURLs(); 371 ExpectDefaultURLs();
343 EXPECT_FALSE(observer_->notified()); 372 EXPECT_FALSE(observer_->notified());
344 } 373 }
345 374
346 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) { 375 TEST_F(GoogleURLTrackerTest, FetchOnLateRequest) {
347 FinishSleep(); 376 FinishSleep();
348 NotifyIPAddressChanged(); 377 NotifyIPAddressChanged();
378 MockSearchDomainCheckResponse(".google.co.jp");
349 379
350 RequestServerCheck(); 380 RequestServerCheck();
351 // The first request for a check should trigger a fetch if it hasn't happened 381 // The first request for a check should trigger a fetch if it hasn't happened
352 // already. 382 // already.
353 MockSearchDomainCheckResponse(0, ".google.co.uk"); 383 MockSearchDomainCheckResponse(".google.co.uk");
354 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 384 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
355 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 385 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
356 EXPECT_TRUE(observer_->notified()); 386 EXPECT_TRUE(observer_->notified());
357 } 387 }
358 388
389 TEST_F(GoogleURLTrackerTest, DontFetchTwiceOnLateRequests) {
390 FinishSleep();
391 NotifyIPAddressChanged();
392 MockSearchDomainCheckResponse(".google.co.jp");
393
394 RequestServerCheck();
395 // The first request for a check should trigger a fetch if it hasn't happened
396 // already.
397 MockSearchDomainCheckResponse(".google.co.uk");
398 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
399 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
400 EXPECT_TRUE(observer_->notified());
401 observer_->clear_notified();
402
403 RequestServerCheck();
404 // The second request should be ignored.
405 EXPECT_FALSE(GetFetcher());
406 MockSearchDomainCheckResponse(".google.co.in");
407 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
408 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
409 EXPECT_FALSE(observer_->notified());
410 }
411
359 TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) { 412 TEST_F(GoogleURLTrackerTest, SearchingDoesNothingIfNoNeedToPrompt) {
360 RequestServerCheck(); 413 RequestServerCheck();
361 FinishSleep(); 414 FinishSleep();
362 MockSearchDomainCheckResponse(0, ".google.co.uk"); 415 MockSearchDomainCheckResponse(".google.co.uk");
363 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 416 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
364 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 417 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
365 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); 418 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
366 EXPECT_TRUE(observer_->notified()); 419 EXPECT_TRUE(observer_->notified());
367 observer_->clear_notified(); 420 observer_->clear_notified();
368 421
369 SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); 422 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
370 NavEntryCommitted(); 423 CommitSearch(1);
371 EXPECT_FALSE(infobar_showing()); 424 TestInfoBarDelegate* infobar = GetInfoBar(1);
425 ASSERT_TRUE(infobar == NULL);
372 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url()); 426 EXPECT_EQ(GURL("http://www.google.co.uk/"), fetched_google_url());
373 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url()); 427 EXPECT_EQ(GURL("http://www.google.co.uk/"), google_url());
374 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); 428 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
375 EXPECT_FALSE(observer_->notified()); 429 EXPECT_FALSE(observer_->notified());
376 } 430 }
377 431
432 TEST_F(GoogleURLTrackerTest, TabClosedOnPendingSearch) {
433 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
434 RequestServerCheck();
435 FinishSleep();
436 MockSearchDomainCheckResponse(".google.co.jp");
437 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
438 EXPECT_EQ(GURL("http://www.google.co.jp/"), fetched_google_url());
439 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
440 EXPECT_FALSE(observer_->notified());
441
442 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
443 TestInfoBarDelegate* infobar = GetInfoBar(1);
444 ASSERT_FALSE(infobar == NULL);
445 EXPECT_FALSE(infobar->showing());
446 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
447 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test"),
448 infobar->search_url());
449 EXPECT_EQ(GURL("http://www.google.co.jp/"), infobar->new_google_url());
450 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
451 EXPECT_FALSE(observer_->notified());
452
453 CloseTab(1);
454 EXPECT_TRUE(GetInfoBar(1) == NULL);
455 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
456 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
457 EXPECT_FALSE(observer_->notified());
458 }
459
460 TEST_F(GoogleURLTrackerTest, TabClosedOnCommittedSearch) {
461 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
462 RequestServerCheck();
463 FinishSleep();
464 MockSearchDomainCheckResponse(".google.co.jp");
465
466 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
467 CommitSearch(1);
468 TestInfoBarDelegate* infobar = GetInfoBar(1);
469 ASSERT_FALSE(infobar == NULL);
470 EXPECT_TRUE(infobar->showing());
471
472 CloseTab(1);
473 EXPECT_TRUE(GetInfoBar(1) == NULL);
474 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
475 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
476 EXPECT_FALSE(observer_->notified());
477 }
478
378 TEST_F(GoogleURLTrackerTest, InfobarClosed) { 479 TEST_F(GoogleURLTrackerTest, InfobarClosed) {
379 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); 480 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
380 RequestServerCheck(); 481 RequestServerCheck();
381 FinishSleep(); 482 FinishSleep();
382 MockSearchDomainCheckResponse(0, ".google.co.jp"); 483 MockSearchDomainCheckResponse(".google.co.jp");
383 484
384 SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); 485 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
385 NavEntryCommitted(); 486 CommitSearch(1);
386 EXPECT_TRUE(infobar_showing()); 487 TestInfoBarDelegate* infobar = GetInfoBar(1);
488 ASSERT_FALSE(infobar == NULL);
387 489
388 InfoBarClosed(); 490 infobar->InfoBarClosed();
389 EXPECT_FALSE(infobar_showing()); 491 EXPECT_TRUE(GetInfoBar(1) == NULL);
390 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); 492 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
391 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL()); 493 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
392 EXPECT_FALSE(observer_->notified()); 494 EXPECT_FALSE(observer_->notified());
393 } 495 }
394 496
395 TEST_F(GoogleURLTrackerTest, InfobarRefused) { 497 TEST_F(GoogleURLTrackerTest, InfobarRefused) {
396 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); 498 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
397 RequestServerCheck(); 499 RequestServerCheck();
398 FinishSleep(); 500 FinishSleep();
399 MockSearchDomainCheckResponse(0, ".google.co.jp"); 501 MockSearchDomainCheckResponse(".google.co.jp");
400 502
401 SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); 503 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
402 NavEntryCommitted(); 504 CommitSearch(1);
403 EXPECT_TRUE(infobar_showing()); 505 TestInfoBarDelegate* infobar = GetInfoBar(1);
506 ASSERT_FALSE(infobar == NULL);
404 507
405 CancelGoogleURL(); 508 infobar->Cancel();
406 InfoBarClosed(); 509 EXPECT_TRUE(GetInfoBar(1) == NULL);
407 EXPECT_FALSE(infobar_showing());
408 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url()); 510 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
409 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); 511 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
410 EXPECT_FALSE(observer_->notified()); 512 EXPECT_FALSE(observer_->notified());
411 } 513 }
412 514
413 TEST_F(GoogleURLTrackerTest, InfobarAccepted) { 515 TEST_F(GoogleURLTrackerTest, InfobarAccepted) {
414 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/")); 516 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
415 RequestServerCheck(); 517 RequestServerCheck();
416 FinishSleep(); 518 FinishSleep();
417 MockSearchDomainCheckResponse(0, ".google.co.jp"); 519 MockSearchDomainCheckResponse(".google.co.jp");
418 520
419 SearchCommitted(GURL("http://www.google.co.uk/search?q=test")); 521 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
420 NavEntryCommitted(); 522 CommitSearch(1);
421 EXPECT_TRUE(infobar_showing()); 523 TestInfoBarDelegate* infobar = GetInfoBar(1);
524 ASSERT_FALSE(infobar == NULL);
422 525
423 AcceptGoogleURL(); 526 infobar->Accept();
424 InfoBarClosed(); 527 EXPECT_TRUE(GetInfoBar(1) == NULL);
425 EXPECT_FALSE(infobar_showing());
426 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url()); 528 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
427 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL()); 529 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
428 EXPECT_TRUE(observer_->notified()); 530 EXPECT_TRUE(observer_->notified());
429 } 531 }
532
533 TEST_F(GoogleURLTrackerTest, InfobarShownAgainOnSearchAfterPendingSearch) {
534 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
535 RequestServerCheck();
536 FinishSleep();
537 MockSearchDomainCheckResponse(".google.co.jp");
538
539 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
540 TestInfoBarDelegate* infobar = GetInfoBar(1);
541 ASSERT_FALSE(infobar == NULL);
542 EXPECT_FALSE(infobar->showing());
543 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test"),
544 infobar->search_url());
545
546 SetSearchPending(GURL("http://www.google.co.uk/search?q=test2"), 1);
547 TestInfoBarDelegate* infobar2 = GetInfoBar(1);
548 ASSERT_FALSE(infobar2 == NULL);
549 EXPECT_FALSE(infobar2->showing());
550 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
551 infobar2->search_url());
552
553 CommitSearch(1);
554 EXPECT_TRUE(infobar2->showing());
555 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
556 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
557 EXPECT_FALSE(observer_->notified());
558
559 CloseTab(1);
560 }
561
562 TEST_F(GoogleURLTrackerTest, InfobarShownAgainOnSearchAfterCommittedSearch) {
563 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
564 RequestServerCheck();
565 FinishSleep();
566 MockSearchDomainCheckResponse(".google.co.jp");
567
568 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
569 CommitSearch(1);
570 TestInfoBarDelegate* infobar = GetInfoBar(1);
571 ASSERT_FALSE(infobar == NULL);
572 EXPECT_TRUE(infobar->showing());
573
574 SetSearchPending(GURL("http://www.google.co.uk/search?q=test2"), 1);
575 TestInfoBarDelegate* infobar2 = GetInfoBar(1);
576 ASSERT_FALSE(infobar2 == NULL);
577 EXPECT_FALSE(infobar2->showing());
578
579 CommitSearch(1);
580 EXPECT_TRUE(infobar2->showing());
581 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage), google_url());
582 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
583 EXPECT_FALSE(observer_->notified());
584
585 CloseTab(1);
586 }
587
588 TEST_F(GoogleURLTrackerTest, MultipleInfobars) {
589 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
590 RequestServerCheck();
591 FinishSleep();
592 MockSearchDomainCheckResponse(".google.co.jp");
593
594 SetSearchPending(GURL("http://www.google.co.uk/search?q=test"), 1);
595 TestInfoBarDelegate* infobar = GetInfoBar(1);
596 ASSERT_FALSE(infobar == NULL);
597 EXPECT_FALSE(infobar->showing());
598 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test"),
599 infobar->search_url());
600
601 SetSearchPending(GURL("http://www.google.co.uk/search?q=test2"), 2);
602 CommitSearch(2);
603 TestInfoBarDelegate* infobar2 = GetInfoBar(2);
604 ASSERT_FALSE(infobar2 == NULL);
605 EXPECT_TRUE(infobar2->showing());
606 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test2"),
607 infobar2->search_url());
608
609 SetSearchPending(GURL("http://www.google.co.uk/search?q=test3"), 3);
610 TestInfoBarDelegate* infobar3 = GetInfoBar(3);
611 ASSERT_FALSE(infobar3 == NULL);
612 EXPECT_FALSE(infobar3->showing());
613 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test3"),
614 infobar3->search_url());
615
616 SetSearchPending(GURL("http://www.google.co.uk/search?q=test4"), 4);
617 CommitSearch(4);
618 TestInfoBarDelegate* infobar4 = GetInfoBar(4);
619 ASSERT_FALSE(infobar4 == NULL);
620 EXPECT_TRUE(infobar4->showing());
621 EXPECT_EQ(GURL("http://www.google.co.uk/search?q=test4"),
622 infobar4->search_url());
623
624 CommitSearch(1);
625 EXPECT_TRUE(infobar->showing());
626
627 infobar2->InfoBarClosed();
628 EXPECT_TRUE(GetInfoBar(2) == NULL);
629 EXPECT_FALSE(observer_->notified());
630
631 infobar4->Accept();
632 EXPECT_TRUE(GetInfoBar(1) == NULL);
633 EXPECT_TRUE(GetInfoBar(3) == NULL);
634 EXPECT_TRUE(GetInfoBar(4) == NULL);
635 EXPECT_EQ(GURL("http://www.google.co.jp/"), google_url());
636 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
637 EXPECT_TRUE(observer_->notified());
638 }
OLDNEW
« chrome/browser/google/google_url_tracker.cc ('K') | « chrome/browser/google/google_url_tracker.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698