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

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

Issue 3171019: Reland r56483 - Monitor network change in GoogleURLTracker (Closed)
Patch Set: check Chrome Frame Created 10 years, 4 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
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/google_url_tracker.h" 5 #include "chrome/browser/google_url_tracker.h"
6 #include "base/command_line.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/profile.h"
9 #include "chrome/browser/tab_contents/infobar_delegate.h"
10 #include "chrome/common/net/url_fetcher.h"
11 #include "chrome/common/net/url_request_context_getter.h"
12 #include "chrome/common/net/test_url_fetcher_factory.h"
13 #include "chrome/common/notification_service.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/testing_browser_process.h"
16 #include "chrome/test/testing_pref_service.h"
17 #include "chrome/test/testing_profile.h"
18 #include "net/url_request/url_request.h"
19 #include "net/url_request/url_request_unittest.h"
6 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
7 21
8 TEST(GoogleURLTrackerTest, CheckAndConvertURL) { 22 namespace {
9 static const struct { 23
10 const char* const source_url; 24 class TestNotificationObserver : public NotificationObserver {
11 const bool can_convert; 25 public:
12 const char* const base_url; 26 TestNotificationObserver() : notified_(false) {}
13 } data[] = { 27 virtual ~TestNotificationObserver() {}
14 { "http://www.google.com/", true, "http://www.google.com/", }, 28
15 { "http://google.fr/", true, "http://google.fr/", }, 29 virtual void Observe(NotificationType type,
16 { "http://google.co.uk/", true, "http://google.co.uk/", }, 30 const NotificationSource& source,
17 { "http://www.google.com.by/", true, "http://www.google.com.by/", }, 31 const NotificationDetails& details) {
18 { "http://www.google.com/ig", true, "http://www.google.com/", }, 32 notified_ = true;
19 { "http://www.google.com/intl/xx", true, "http://www.google.com/intl/xx", }, 33 }
20 { "http://a.b.c.google.com/", true, "http://a.b.c.google.com/", }, 34 bool notified() const { return notified_; }
21 { "http://www.yahoo.com/", false, "", }, 35 void ClearNotified() { notified_ = false; }
22 { "http://google.evil.com/", false, "", }, 36 private:
23 { "http://google.com.com.com/", false, "", }, 37 bool notified_;
24 { "http://google/", false, "", }, 38 };
25 { "http://wifi.corp.google.com/", false, "" }, 39
26 }; 40 class TestInfoBar : public InfoBarDelegate {
27 41 public:
28 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 42 TestInfoBar(GoogleURLTracker* google_url_tracker,
29 GURL base_url; 43 const GURL& new_google_url)
30 const bool can_convert = GoogleURLTracker::CheckAndConvertToGoogleBaseURL( 44 : InfoBarDelegate(NULL),
31 GURL(data[i].source_url), &base_url); 45 google_url_tracker_(google_url_tracker),
32 EXPECT_EQ(data[i].can_convert, can_convert); 46 new_google_url_(new_google_url) {}
33 EXPECT_STREQ(data[i].base_url, base_url.spec().c_str()); 47 virtual ~TestInfoBar() {}
34 } 48 GoogleURLTracker* google_url_tracker() const { return google_url_tracker_; }
35 } 49 const GURL& new_google_url() const { return new_google_url_; }
50
51 virtual InfoBar* CreateInfoBar() { return NULL; }
52
53 private:
54 GoogleURLTracker* google_url_tracker_;
55 GURL new_google_url_;
56 };
57
58 class TestInfoBarDelegateFactory
59 : public GoogleURLTracker::InfoBarDelegateFactory {
60 public:
61 virtual InfoBarDelegate* CreateInfoBar(TabContents* tab_contents,
62 GoogleURLTracker* google_url_tracker,
63 const GURL& new_google_url) {
64 return new TestInfoBar(google_url_tracker, new_google_url);
65 }
66 };
67
68 } // anonymous namespace
69
70 class GoogleURLTrackerTest : public testing::Test {
71 protected:
72 GoogleURLTrackerTest()
73 : original_default_request_context_(NULL) {
74 }
75
76 void SetUp() {
77 original_default_request_context_ = Profile::GetDefaultRequestContext();
78 Profile::set_default_request_context(NULL);
79 message_loop_ = new MessageLoop(MessageLoop::TYPE_IO);
80 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
81 testing_profile_.reset(new TestingProfile);
82 TestingBrowserProcess* testing_browser_process =
83 static_cast<TestingBrowserProcess*>(g_browser_process);
84 PrefService* pref_service = testing_profile_->GetPrefs();
85 testing_browser_process->SetPrefService(pref_service);
86 testing_browser_process->SetGoogleURLTracker(new GoogleURLTracker);
87
88 URLFetcher::set_factory(&fetcher_factory_);
89 observer_.reset(new TestNotificationObserver);
90 g_browser_process->google_url_tracker()->infobar_factory_.reset(
91 new TestInfoBarDelegateFactory);
92 }
93
94 void TearDown() {
95 URLFetcher::set_factory(NULL);
96 TestingBrowserProcess* testing_browser_process =
97 static_cast<TestingBrowserProcess*>(g_browser_process);
98 testing_browser_process->SetGoogleURLTracker(NULL);
99 testing_browser_process->SetPrefService(NULL);
100 testing_profile_.reset();
101 network_change_notifier_.reset();
102 delete message_loop_;
103 Profile::set_default_request_context(original_default_request_context_);
104 }
105
106 void CreateRequestContext() {
107 testing_profile_->CreateRequestContext();
108 Profile::set_default_request_context(testing_profile_->GetRequestContext());
109 NotificationService::current()->Notify(
110 NotificationType::DEFAULT_REQUEST_CONTEXT_AVAILABLE,
111 NotificationService::AllSources(), NotificationService::NoDetails());
112 }
113
114 TestURLFetcher* GetFetcherByID(int expected_id) {
115 return fetcher_factory_.GetFetcherByID(expected_id);
116 }
117
118 void MockSearchDomainCheckResponse(
119 int expected_id, const std::string& domain) {
120 TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(expected_id);
121 ASSERT_TRUE(fetcher);
122 fetcher->delegate()->OnURLFetchComplete(
123 fetcher,
124 GURL(GoogleURLTracker::kSearchDomainCheckURL),
125 URLRequestStatus(),
126 200,
127 ResponseCookies(),
128 domain);
129 MessageLoop::current()->RunAllPending();
130 }
131
132 void RequestServerCheck() {
133 registrar_.Add(observer_.get(), NotificationType::GOOGLE_URL_UPDATED,
134 NotificationService::AllSources());
135 GoogleURLTracker::RequestServerCheck();
136 MessageLoop::current()->RunAllPending();
137 }
138
139 void FinishSleep() {
140 g_browser_process->google_url_tracker()->FinishSleep();
141 MessageLoop::current()->RunAllPending();
142 }
143
144 void NotifyIPAddressChanged() {
145 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
146 MessageLoop::current()->RunAllPending();
147 }
148
149 GURL GetFetchedGoogleURL() {
150 return g_browser_process->google_url_tracker()->fetched_google_url_;
151 }
152
153 void SetGoogleURL(const GURL& url) {
154 g_browser_process->google_url_tracker()->google_url_ = url;
155 }
156
157 void SetLastPromptedGoogleURL(const GURL& url) {
158 g_browser_process->local_state()->SetString(
159 prefs::kLastPromptedGoogleURL, url.spec());
160 }
161
162 GURL GetLastPromptedGoogleURL() {
163 return GURL(g_browser_process->local_state()->GetString(
164 prefs::kLastPromptedGoogleURL));
165 }
166
167 void SearchCommitted(const GURL& search_url) {
168 GoogleURLTracker* google_url_tracker =
169 g_browser_process->google_url_tracker();
170
171 google_url_tracker->SearchCommitted();
172 // GoogleURLTracker wait for NAV_ENTRY_PENDING.
173 // In NAV_ENTRY_PENDING, it will set search_url_.
174 google_url_tracker->search_url_ = search_url;
175 }
176
177 void NavEntryCommitted() {
178 GoogleURLTracker* google_url_tracker =
179 g_browser_process->google_url_tracker();
180 google_url_tracker->ShowGoogleURLInfoBarIfNecessary(NULL);
181 }
182
183 bool InfoBarIsShown() {
184 return (g_browser_process->google_url_tracker()->infobar_ != NULL);
185 }
186
187 GURL GetInfoBarShowingURL() {
188 TestInfoBar* infobar = static_cast<TestInfoBar*>(
189 g_browser_process->google_url_tracker()->infobar_);
190 return infobar->new_google_url();
191 }
192
193 void AcceptGoogleURL() {
194 TestInfoBar* infobar = static_cast<TestInfoBar*>(
195 g_browser_process->google_url_tracker()->infobar_);
196 ASSERT_TRUE(infobar);
197 ASSERT_TRUE(infobar->google_url_tracker());
198 infobar->google_url_tracker()->AcceptGoogleURL(infobar->new_google_url());
199 }
200
201 void InfoBarClosed() {
202 TestInfoBar* infobar = static_cast<TestInfoBar*>(
203 g_browser_process->google_url_tracker()->infobar_);
204 ASSERT_TRUE(infobar);
205 ASSERT_TRUE(infobar->google_url_tracker());
206 infobar->google_url_tracker()->InfoBarClosed();
207 delete infobar;
208 }
209
210 void ExpectDefaultURLs() {
211 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
212 GoogleURLTracker::GoogleURL());
213 EXPECT_EQ(GURL(), GetFetchedGoogleURL());
214 }
215
216 private:
217 MessageLoop* message_loop_;
218 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
219 scoped_ptr<TestingProfile> testing_profile_;
220
221 TestURLFetcherFactory fetcher_factory_;
222 NotificationRegistrar registrar_;
223 scoped_ptr<NotificationObserver> observer_;
224
225 URLRequestContextGetter* original_default_request_context_;
226 };
227
228 TEST_F(GoogleURLTrackerTest, StartupSleepFinish) {
229 CreateRequestContext();
230
231 RequestServerCheck();
232 EXPECT_FALSE(GetFetcherByID(0));
233 ExpectDefaultURLs();
234
235 FinishSleep();
236 MockSearchDomainCheckResponse(0, ".google.co.uk");
237
238 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
239 // GoogleURL is updated, becase it was not the last prompted URL.
240 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
241 }
242
243 TEST_F(GoogleURLTrackerTest, StartupSleepFinishWithLastPrompted) {
244 CreateRequestContext();
245 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
246
247 RequestServerCheck();
248 EXPECT_FALSE(GetFetcherByID(0));
249 ExpectDefaultURLs();
250
251 FinishSleep();
252 MockSearchDomainCheckResponse(0, ".google.co.uk");
253
254 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
255 // GoogleURL should not be updated.
256 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
257 GoogleURLTracker::GoogleURL());
258 }
259
260 TEST_F(GoogleURLTrackerTest, StartupSleepFinishNoObserver) {
261 CreateRequestContext();
262
263 ExpectDefaultURLs();
264
265 FinishSleep();
266 EXPECT_FALSE(GetFetcherByID(0));
267
268 ExpectDefaultURLs();
269 }
270
271 TEST_F(GoogleURLTrackerTest, MonitorNetworkChange) {
272 CreateRequestContext();
273
274 RequestServerCheck();
275 EXPECT_FALSE(GetFetcherByID(0));
276 ExpectDefaultURLs();
277
278 FinishSleep();
279 MockSearchDomainCheckResponse(0, ".google.co.uk");
280
281 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
282 // GoogleURL is updated, becase it was not the last prompted URL.
283 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
284
285 NotifyIPAddressChanged();
286 MockSearchDomainCheckResponse(1, ".google.co.in");
287
288 EXPECT_EQ(GURL("http://www.google.co.in/"), GetFetchedGoogleURL());
289 // Don't update GoogleURL.
290 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
291 }
292
293 TEST_F(GoogleURLTrackerTest, MonitorNetworkChangeNoObserver) {
294 CreateRequestContext();
295
296 ExpectDefaultURLs();
297
298 FinishSleep();
299 NotifyIPAddressChanged();
300 EXPECT_FALSE(GetFetcherByID(0));
301
302 ExpectDefaultURLs();
303 }
304
305 TEST_F(GoogleURLTrackerTest, MonitorNetworkChangeAndObserverRegister) {
306 CreateRequestContext();
307
308 ExpectDefaultURLs();
309
310 FinishSleep();
311 NotifyIPAddressChanged();
312 EXPECT_FALSE(GetFetcherByID(0));
313
314 ExpectDefaultURLs();
315
316 RequestServerCheck();
317 MockSearchDomainCheckResponse(0, ".google.co.uk");
318
319 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
320 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
321 }
322
323 TEST_F(GoogleURLTrackerTest, NoSearchCommitedAndPromptedNotChanged) {
324 CreateRequestContext();
325 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
326 RequestServerCheck();
327 EXPECT_FALSE(GetFetcherByID(0));
328 ExpectDefaultURLs();
329
330 FinishSleep();
331 MockSearchDomainCheckResponse(0, ".google.co.jp");
332
333 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetFetchedGoogleURL());
334 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
335 }
336
337 TEST_F(GoogleURLTrackerTest, SearchCommitedAndUserSayNo) {
338 CreateRequestContext();
339 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
340
341 ExpectDefaultURLs();
342
343 RequestServerCheck();
344 EXPECT_FALSE(GetFetcherByID(0));
345 ExpectDefaultURLs();
346
347 FinishSleep();
348 MockSearchDomainCheckResponse(0, ".google.co.jp");
349
350 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
351 GoogleURLTracker::GoogleURL());
352 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetFetchedGoogleURL());
353 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
354
355 SearchCommitted(GURL("http://www.google.co.uk/search?q=test"));
356 NavEntryCommitted();
357
358 EXPECT_TRUE(InfoBarIsShown());
359 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
360 GoogleURLTracker::GoogleURL());
361 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetInfoBarShowingURL());
362 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
363
364 InfoBarClosed();
365 EXPECT_FALSE(InfoBarIsShown());
366 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
367 GoogleURLTracker::GoogleURL());
368 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
369 }
370
371 TEST_F(GoogleURLTrackerTest, SearchCommitedAndUserSayYes) {
372 CreateRequestContext();
373 SetLastPromptedGoogleURL(GURL("http://www.google.co.uk/"));
374
375 ExpectDefaultURLs();
376
377 RequestServerCheck();
378 EXPECT_FALSE(GetFetcherByID(0));
379 ExpectDefaultURLs();
380
381 FinishSleep();
382 MockSearchDomainCheckResponse(0, ".google.co.jp");
383
384 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
385 GoogleURLTracker::GoogleURL());
386 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetFetchedGoogleURL());
387 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
388
389 SearchCommitted(GURL("http://www.google.co.uk/search?q=test"));
390 NavEntryCommitted();
391
392 EXPECT_TRUE(InfoBarIsShown());
393 EXPECT_EQ(GURL(GoogleURLTracker::kDefaultGoogleHomepage),
394 GoogleURLTracker::GoogleURL());
395 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetInfoBarShowingURL());
396 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
397
398 AcceptGoogleURL();
399 InfoBarClosed();
400
401 EXPECT_FALSE(InfoBarIsShown());
402 EXPECT_EQ(GURL("http://www.google.co.jp/"), GoogleURLTracker::GoogleURL());
403 EXPECT_EQ(GURL("http://www.google.co.jp/"), GetLastPromptedGoogleURL());
404 }
405
406 TEST_F(GoogleURLTrackerTest, InitialUpdate) {
407 CreateRequestContext();
408 ExpectDefaultURLs();
409 EXPECT_EQ(GURL(), GetLastPromptedGoogleURL());
410
411 RequestServerCheck();
412 EXPECT_FALSE(GetFetcherByID(0));
413 ExpectDefaultURLs();
414 EXPECT_EQ(GURL(), GetLastPromptedGoogleURL());
415
416 FinishSleep();
417 MockSearchDomainCheckResponse(0, ".google.co.uk");
418
419 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
420 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
421 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
422
423 SearchCommitted(GURL("http://www.google.co.uk/search?q=test"));
424 NavEntryCommitted();
425
426 EXPECT_FALSE(InfoBarIsShown());
427 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
428 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
429 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
430 }
431
432 TEST_F(GoogleURLTrackerTest, UpdatePromptedURLWhenBack) {
433 CreateRequestContext();
434 SetLastPromptedGoogleURL(GURL("http://www.google.co.jp/"));
435 SetGoogleURL(GURL("http://www.google.co.uk/"));
436
437 RequestServerCheck();
438 FinishSleep();
439 MockSearchDomainCheckResponse(0, ".google.co.uk");
440
441 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
442 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
443 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
444
445 SearchCommitted(GURL("http://www.google.co.uk/search?q=test"));
446 NavEntryCommitted();
447
448 EXPECT_FALSE(InfoBarIsShown());
449 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetFetchedGoogleURL());
450 EXPECT_EQ(GURL("http://www.google.co.uk/"), GoogleURLTracker::GoogleURL());
451 EXPECT_EQ(GURL("http://www.google.co.uk/"), GetLastPromptedGoogleURL());
452 }
OLDNEW
« no previous file with comments | « chrome/browser/google_url_tracker.cc ('k') | chrome/browser/search_engines/template_url_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698