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