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