OLD | NEW |
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 "chrome/browser/google/google_url_tracker.h" | 5 #include "chrome/browser/google/google_url_tracker.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/google/google_url_tracker_factory.h" | 13 #include "chrome/browser/google/google_url_tracker_factory.h" |
14 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" | 14 #include "chrome/browser/google/google_url_tracker_infobar_delegate.h" |
15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" | 15 #include "chrome/browser/google/google_url_tracker_navigation_helper.h" |
16 #include "chrome/browser/infobars/infobar.h" | |
17 #include "chrome/browser/infobars/infobar_delegate.h" | |
18 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/infobars/core/infobar.h" |
| 19 #include "components/infobars/core/infobar_delegate.h" |
20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
21 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
22 #include "net/url_request/test_url_fetcher_factory.h" | 22 #include "net/url_request/test_url_fetcher_factory.h" |
23 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
25 | 25 |
26 class GoogleURLTrackerTest; | 26 class GoogleURLTrackerTest; |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // TestInfoBarDelegate -------------------------------------------------------- | 30 // TestInfoBarDelegate -------------------------------------------------------- |
31 | 31 |
32 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { | 32 class TestInfoBarDelegate : public GoogleURLTrackerInfoBarDelegate { |
33 public: | 33 public: |
34 // Creates a test infobar and delegate and returns the infobar. Unlike the | 34 // Creates a test infobar and delegate and returns the infobar. Unlike the |
35 // parent class, this does not add the infobar to |infobar_service|, since | 35 // parent class, this does not add the infobar to |infobar_service|, since |
36 // that "pointer" is really just a magic number. Thus there is no | 36 // that "pointer" is really just a magic number. Thus there is no |
37 // InfoBarService ownership of the returned object; and since the caller | 37 // InfoBarService ownership of the returned object; and since the caller |
38 // doesn't own the returned object, we rely on |test_harness| cleaning this up | 38 // doesn't own the returned object, we rely on |test_harness| cleaning this up |
39 // eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks. | 39 // eventually in GoogleURLTrackerTest::OnInfoBarClosed() to avoid leaks. |
40 static InfoBar* Create(GoogleURLTrackerTest* test_harness, | 40 static infobars::InfoBar* Create(GoogleURLTrackerTest* test_harness, |
41 InfoBarService* infobar_service, | 41 InfoBarService* infobar_service, |
42 GoogleURLTracker* google_url_tracker, | 42 GoogleURLTracker* google_url_tracker, |
43 const GURL& search_url); | 43 const GURL& search_url); |
44 | 44 |
45 private: | 45 private: |
46 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, | 46 TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, |
47 InfoBarService* infobar_service, | 47 InfoBarService* infobar_service, |
48 GoogleURLTracker* google_url_tracker, | 48 GoogleURLTracker* google_url_tracker, |
49 const GURL& search_url); | 49 const GURL& search_url); |
50 virtual ~TestInfoBarDelegate(); | 50 virtual ~TestInfoBarDelegate(); |
51 | 51 |
52 // GoogleURLTrackerInfoBarDelegate: | 52 // GoogleURLTrackerInfoBarDelegate: |
53 virtual void Update(const GURL& search_url) OVERRIDE; | 53 virtual void Update(const GURL& search_url) OVERRIDE; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 // TestInfoBarDelegate above, to make everything continue to work. | 192 // TestInfoBarDelegate above, to make everything continue to work. |
193 // | 193 // |
194 // Technically, the C++98 spec defines the result of casting | 194 // Technically, the C++98 spec defines the result of casting |
195 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what | 195 // T* -> intptr_t -> T* to be an identity, but intptr_t -> T* -> intptr_t (what |
196 // we use here) is "implementation-defined". Since I've never seen a compiler | 196 // we use here) is "implementation-defined". Since I've never seen a compiler |
197 // break this, though, and the result would simply be a failing test rather than | 197 // break this, though, and the result would simply be a failing test rather than |
198 // a bug in Chrome, we'll use it anyway. | 198 // a bug in Chrome, we'll use it anyway. |
199 class GoogleURLTrackerTest : public testing::Test { | 199 class GoogleURLTrackerTest : public testing::Test { |
200 public: | 200 public: |
201 // Called by TestInfoBarDelegate::Close(). | 201 // Called by TestInfoBarDelegate::Close(). |
202 void OnInfoBarClosed(scoped_ptr<InfoBar> infobar, | 202 void OnInfoBarClosed(scoped_ptr<infobars::InfoBar> infobar, |
203 InfoBarService* infobar_service); | 203 InfoBarService* infobar_service); |
204 | 204 |
205 protected: | 205 protected: |
206 GoogleURLTrackerTest(); | 206 GoogleURLTrackerTest(); |
207 virtual ~GoogleURLTrackerTest(); | 207 virtual ~GoogleURLTrackerTest(); |
208 | 208 |
209 // testing::Test | 209 // testing::Test |
210 virtual void SetUp() OVERRIDE; | 210 virtual void SetUp() OVERRIDE; |
211 virtual void TearDown() OVERRIDE; | 211 virtual void TearDown() OVERRIDE; |
212 | 212 |
(...skipping 20 matching lines...) Expand all Loading... |
233 void ExpectDefaultURLs() const; | 233 void ExpectDefaultURLs() const; |
234 void ExpectListeningForCommit(intptr_t unique_id, bool listening); | 234 void ExpectListeningForCommit(intptr_t unique_id, bool listening); |
235 bool observer_notified() const { return observer_.notified(); } | 235 bool observer_notified() const { return observer_.notified(); } |
236 void clear_observer_notified() { observer_.clear_notified(); } | 236 void clear_observer_notified() { observer_.clear_notified(); } |
237 | 237 |
238 private: | 238 private: |
239 // Since |infobar_service| is really a magic number rather than an actual | 239 // Since |infobar_service| is really a magic number rather than an actual |
240 // object, we don't add the created infobar to it. Instead we will simulate | 240 // object, we don't add the created infobar to it. Instead we will simulate |
241 // any helper<->infobar interaction necessary. The returned object will be | 241 // any helper<->infobar interaction necessary. The returned object will be |
242 // cleaned up in OnInfoBarClosed(). | 242 // cleaned up in OnInfoBarClosed(). |
243 InfoBar* CreateTestInfoBar(InfoBarService* infobar_service, | 243 infobars::InfoBar* CreateTestInfoBar(InfoBarService* infobar_service, |
244 GoogleURLTracker* google_url_tracker, | 244 GoogleURLTracker* google_url_tracker, |
245 const GURL& search_url); | 245 const GURL& search_url); |
246 | 246 |
247 // These are required by the TestURLFetchers GoogleURLTracker will create (see | 247 // These are required by the TestURLFetchers GoogleURLTracker will create (see |
248 // test_url_fetcher_factory.h). | 248 // test_url_fetcher_factory.h). |
249 content::TestBrowserThreadBundle thread_bundle_; | 249 content::TestBrowserThreadBundle thread_bundle_; |
250 // Creating this allows us to call | 250 // Creating this allows us to call |
251 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). | 251 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). |
252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
253 net::TestURLFetcherFactory fetcher_factory_; | 253 net::TestURLFetcherFactory fetcher_factory_; |
254 content::NotificationRegistrar registrar_; | 254 content::NotificationRegistrar registrar_; |
255 TestNotificationObserver observer_; | 255 TestNotificationObserver observer_; |
256 GoogleURLTrackerNavigationHelper* nav_helper_; | 256 GoogleURLTrackerNavigationHelper* nav_helper_; |
257 TestingProfile profile_; | 257 TestingProfile profile_; |
258 scoped_ptr<GoogleURLTracker> google_url_tracker_; | 258 scoped_ptr<GoogleURLTracker> google_url_tracker_; |
259 // This tracks the different "tabs" a test has "opened", so we can close them | 259 // This tracks the different "tabs" a test has "opened", so we can close them |
260 // properly before shutting down |google_url_tracker_|, which expects that. | 260 // properly before shutting down |google_url_tracker_|, which expects that. |
261 std::set<int> unique_ids_seen_; | 261 std::set<int> unique_ids_seen_; |
262 }; | 262 }; |
263 | 263 |
264 void GoogleURLTrackerTest::OnInfoBarClosed(scoped_ptr<InfoBar> infobar, | 264 void GoogleURLTrackerTest::OnInfoBarClosed( |
265 InfoBarService* infobar_service) { | 265 scoped_ptr<infobars::InfoBar> infobar, |
| 266 InfoBarService* infobar_service) { |
266 // First, simulate the InfoBarService firing INFOBAR_REMOVED. | 267 // First, simulate the InfoBarService firing INFOBAR_REMOVED. |
267 InfoBar::RemovedDetails removed_details(infobar.get(), false); | 268 infobars::InfoBar::RemovedDetails removed_details(infobar.get(), false); |
268 GoogleURLTracker::EntryMap::const_iterator i = | 269 GoogleURLTracker::EntryMap::const_iterator i = |
269 google_url_tracker_->entry_map_.find(infobar_service); | 270 google_url_tracker_->entry_map_.find(infobar_service); |
270 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end()); | 271 ASSERT_FALSE(i == google_url_tracker_->entry_map_.end()); |
271 GoogleURLTrackerMapEntry* map_entry = i->second; | 272 GoogleURLTrackerMapEntry* map_entry = i->second; |
272 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate()); | 273 ASSERT_EQ(infobar->delegate(), map_entry->infobar_delegate()); |
273 map_entry->Observe( | 274 map_entry->Observe( |
274 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 275 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
275 content::Source<InfoBarService>(infobar_service), | 276 content::Source<InfoBarService>(infobar_service), |
276 content::Details<InfoBar::RemovedDetails>(&removed_details)); | 277 content::Details<infobars::InfoBar::RemovedDetails>(&removed_details)); |
277 | 278 |
278 // Second, simulate the infobar container closing the infobar in response. | 279 // Second, simulate the infobar container closing the infobar in response. |
279 // This happens automatically as |infobar| goes out of scope. | 280 // This happens automatically as |infobar| goes out of scope. |
280 } | 281 } |
281 | 282 |
282 GoogleURLTrackerTest::GoogleURLTrackerTest() | 283 GoogleURLTrackerTest::GoogleURLTrackerTest() |
283 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 284 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
284 GoogleURLTrackerFactory::GetInstance()-> | 285 GoogleURLTrackerFactory::GetInstance()-> |
285 RegisterUserPrefsOnBrowserContextForTest(&profile_); | 286 RegisterUserPrefsOnBrowserContextForTest(&profile_); |
286 } | 287 } |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 bool listening) { | 440 bool listening) { |
440 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); | 441 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); |
441 if (map_entry) { | 442 if (map_entry) { |
442 EXPECT_EQ(listening, nav_helper_->IsListeningForNavigationCommit( | 443 EXPECT_EQ(listening, nav_helper_->IsListeningForNavigationCommit( |
443 map_entry->navigation_controller())); | 444 map_entry->navigation_controller())); |
444 } else { | 445 } else { |
445 EXPECT_FALSE(listening); | 446 EXPECT_FALSE(listening); |
446 } | 447 } |
447 } | 448 } |
448 | 449 |
449 InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( | 450 infobars::InfoBar* GoogleURLTrackerTest::CreateTestInfoBar( |
450 InfoBarService* infobar_service, | 451 InfoBarService* infobar_service, |
451 GoogleURLTracker* google_url_tracker, | 452 GoogleURLTracker* google_url_tracker, |
452 const GURL& search_url) { | 453 const GURL& search_url) { |
453 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker, | 454 return TestInfoBarDelegate::Create(this, infobar_service, google_url_tracker, |
454 search_url); | 455 search_url); |
455 } | 456 } |
456 | 457 |
457 | 458 |
458 // TestInfoBarDelegate -------------------------------------------------------- | 459 // TestInfoBarDelegate -------------------------------------------------------- |
459 | 460 |
460 namespace { | 461 namespace { |
461 | 462 |
462 // static | 463 // static |
463 InfoBar* TestInfoBarDelegate::Create(GoogleURLTrackerTest* test_harness, | 464 infobars::InfoBar* TestInfoBarDelegate::Create( |
464 InfoBarService* infobar_service, | 465 GoogleURLTrackerTest* test_harness, |
465 GoogleURLTracker* google_url_tracker, | 466 InfoBarService* infobar_service, |
466 const GURL& search_url) { | 467 GoogleURLTracker* google_url_tracker, |
| 468 const GURL& search_url) { |
467 return ConfirmInfoBarDelegate::CreateInfoBar( | 469 return ConfirmInfoBarDelegate::CreateInfoBar( |
468 scoped_ptr<ConfirmInfoBarDelegate>(new TestInfoBarDelegate( | 470 scoped_ptr<ConfirmInfoBarDelegate>(new TestInfoBarDelegate( |
469 test_harness, infobar_service, google_url_tracker, | 471 test_harness, infobar_service, google_url_tracker, |
470 search_url))).release(); | 472 search_url))).release(); |
471 } | 473 } |
472 | 474 |
473 TestInfoBarDelegate::TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, | 475 TestInfoBarDelegate::TestInfoBarDelegate(GoogleURLTrackerTest* test_harness, |
474 InfoBarService* infobar_service, | 476 InfoBarService* infobar_service, |
475 GoogleURLTracker* google_url_tracker, | 477 GoogleURLTracker* google_url_tracker, |
476 const GURL& search_url) | 478 const GURL& search_url) |
477 : GoogleURLTrackerInfoBarDelegate(google_url_tracker, search_url), | 479 : GoogleURLTrackerInfoBarDelegate(google_url_tracker, search_url), |
478 test_harness_(test_harness), | 480 test_harness_(test_harness), |
479 infobar_service_(infobar_service) { | 481 infobar_service_(infobar_service) { |
480 } | 482 } |
481 | 483 |
482 TestInfoBarDelegate::~TestInfoBarDelegate() { | 484 TestInfoBarDelegate::~TestInfoBarDelegate() { |
483 } | 485 } |
484 | 486 |
485 void TestInfoBarDelegate::Update(const GURL& search_url) { | 487 void TestInfoBarDelegate::Update(const GURL& search_url) { |
486 set_search_url(search_url); | 488 set_search_url(search_url); |
487 set_pending_id(0); | 489 set_pending_id(0); |
488 } | 490 } |
489 | 491 |
490 void TestInfoBarDelegate::Close(bool redo_search) { | 492 void TestInfoBarDelegate::Close(bool redo_search) { |
491 test_harness_->OnInfoBarClosed(scoped_ptr<InfoBar>(infobar()), | 493 test_harness_->OnInfoBarClosed(scoped_ptr<infobars::InfoBar>(infobar()), |
492 infobar_service_); | 494 infobar_service_); |
493 // WARNING: At this point |this| has been deleted! | 495 // WARNING: At this point |this| has been deleted! |
494 } | 496 } |
495 | 497 |
496 } // namespace | 498 } // namespace |
497 | 499 |
498 | 500 |
499 // Tests ---------------------------------------------------------------------- | 501 // Tests ---------------------------------------------------------------------- |
500 | 502 |
501 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { | 503 TEST_F(GoogleURLTrackerTest, DontFetchWhenNoOneRequestsCheck) { |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); | 1078 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); |
1077 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); | 1079 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); |
1078 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); | 1080 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); |
1079 ASSERT_FALSE(delegate2 == NULL); | 1081 ASSERT_FALSE(delegate2 == NULL); |
1080 SetNavigationPending(1, true); | 1082 SetNavigationPending(1, true); |
1081 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); | 1083 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); |
1082 delegate2->Close(false); | 1084 delegate2->Close(false); |
1083 SetNavigationPending(1, false); | 1085 SetNavigationPending(1, false); |
1084 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); | 1086 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); |
1085 } | 1087 } |
OLD | NEW |