Chromium Code Reviews| 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/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/google/core/browser/google_url_tracker_client.h" | |
| 18 #include "components/infobars/core/infobar.h" | 19 #include "components/infobars/core/infobar.h" |
| 19 #include "components/infobars/core/infobar_delegate.h" | 20 #include "components/infobars/core/infobar_delegate.h" |
| 20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | 23 #include "net/url_request/test_url_fetcher_factory.h" |
| 23 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 class GoogleURLTrackerTest; | 27 class GoogleURLTrackerTest; |
| 27 | 28 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 } | 88 } |
| 88 | 89 |
| 89 void TestNotificationObserver::Observe( | 90 void TestNotificationObserver::Observe( |
| 90 int type, | 91 int type, |
| 91 const content::NotificationSource& source, | 92 const content::NotificationSource& source, |
| 92 const content::NotificationDetails& details) { | 93 const content::NotificationDetails& details) { |
| 93 notified_ = true; | 94 notified_ = true; |
| 94 } | 95 } |
| 95 | 96 |
| 96 | 97 |
| 98 // TestGoogleURLTrackerClient ------------------------------------- | |
|
Peter Kasting
2014/05/14 23:30:28
Tiny nit: Can you make all the lines of dashes in
blundell
2014/05/15 07:58:03
Done.
| |
| 99 | |
| 100 class TestGoogleURLTrackerClient | |
| 101 : public GoogleURLTrackerClient { | |
|
Peter Kasting
2014/05/14 23:30:28
Nit: Linebreak not necessary
blundell
2014/05/15 07:58:03
Done.
| |
| 102 public: | |
| 103 TestGoogleURLTrackerClient(); | |
| 104 virtual ~TestGoogleURLTrackerClient(); | |
| 105 | |
| 106 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE; | |
| 107 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE; | |
| 108 virtual bool IsListeningForNavigationStart() OVERRIDE; | |
| 109 | |
| 110 private: | |
| 111 GoogleURLTracker* tracker_; | |
| 112 bool observe_nav_start_; | |
| 113 }; | |
|
Peter Kasting
2014/05/14 23:30:28
Nit: DISALLOW_COPY_AND_ASSIGN
blundell
2014/05/15 07:58:03
Done.
| |
| 114 | |
| 115 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient() | |
| 116 : tracker_(NULL), | |
| 117 observe_nav_start_(false) { | |
| 118 } | |
| 119 | |
| 120 TestGoogleURLTrackerClient:: | |
| 121 ~TestGoogleURLTrackerClient() { | |
|
Peter Kasting
2014/05/14 23:30:28
Nit: Linebreak not necessary
blundell
2014/05/15 07:58:03
Done.
| |
| 122 } | |
| 123 | |
| 124 void TestGoogleURLTrackerClient::SetGoogleURLTracker( | |
| 125 GoogleURLTracker* tracker) { | |
| 126 tracker_ = tracker; | |
|
Peter Kasting
2014/05/14 23:30:28
Since both GoogleURLTrackerClient implementations
blundell
2014/05/15 07:58:03
Done.
| |
| 127 } | |
| 128 | |
| 129 void TestGoogleURLTrackerClient::SetListeningForNavigationStart( | |
| 130 bool listen) { | |
| 131 observe_nav_start_ = listen; | |
| 132 } | |
| 133 | |
| 134 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() { | |
| 135 return observe_nav_start_; | |
| 136 } | |
| 137 | |
| 97 // TestGoogleURLTrackerNavigationHelper ------------------------------------- | 138 // TestGoogleURLTrackerNavigationHelper ------------------------------------- |
| 98 | 139 |
| 99 class TestGoogleURLTrackerNavigationHelper | 140 class TestGoogleURLTrackerNavigationHelper |
| 100 : public GoogleURLTrackerNavigationHelper { | 141 : public GoogleURLTrackerNavigationHelper { |
| 101 public: | 142 public: |
| 102 TestGoogleURLTrackerNavigationHelper(); | 143 TestGoogleURLTrackerNavigationHelper(); |
| 103 virtual ~TestGoogleURLTrackerNavigationHelper(); | 144 virtual ~TestGoogleURLTrackerNavigationHelper(); |
| 104 | 145 |
| 105 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE; | 146 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE; |
| 106 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE; | |
| 107 virtual bool IsListeningForNavigationStart() OVERRIDE; | |
| 108 virtual void SetListeningForNavigationCommit( | 147 virtual void SetListeningForNavigationCommit( |
| 109 const content::NavigationController* nav_controller, | 148 const content::NavigationController* nav_controller, |
| 110 bool listen) OVERRIDE; | 149 bool listen) OVERRIDE; |
| 111 virtual bool IsListeningForNavigationCommit( | 150 virtual bool IsListeningForNavigationCommit( |
| 112 const content::NavigationController* nav_controller) OVERRIDE; | 151 const content::NavigationController* nav_controller) OVERRIDE; |
| 113 virtual void SetListeningForTabDestruction( | 152 virtual void SetListeningForTabDestruction( |
| 114 const content::NavigationController* nav_controller, | 153 const content::NavigationController* nav_controller, |
| 115 bool listen) OVERRIDE; | 154 bool listen) OVERRIDE; |
| 116 virtual bool IsListeningForTabDestruction( | 155 virtual bool IsListeningForTabDestruction( |
| 117 const content::NavigationController* nav_controller) OVERRIDE; | 156 const content::NavigationController* nav_controller) OVERRIDE; |
| 118 | 157 |
| 119 private: | 158 private: |
| 120 GoogleURLTracker* tracker_; | 159 GoogleURLTracker* tracker_; |
| 121 bool observe_nav_start_; | |
| 122 std::set<const content::NavigationController*> | 160 std::set<const content::NavigationController*> |
| 123 nav_controller_commit_listeners_; | 161 nav_controller_commit_listeners_; |
| 124 std::set<const content::NavigationController*> | 162 std::set<const content::NavigationController*> |
| 125 nav_controller_tab_close_listeners_; | 163 nav_controller_tab_close_listeners_; |
| 126 }; | 164 }; |
| 127 | 165 |
| 128 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper() | 166 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper() |
| 129 : tracker_(NULL), | 167 : tracker_(NULL) { |
| 130 observe_nav_start_(false) { | |
| 131 } | 168 } |
| 132 | 169 |
| 133 TestGoogleURLTrackerNavigationHelper:: | 170 TestGoogleURLTrackerNavigationHelper:: |
| 134 ~TestGoogleURLTrackerNavigationHelper() { | 171 ~TestGoogleURLTrackerNavigationHelper() { |
| 135 } | 172 } |
| 136 | 173 |
| 137 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker( | 174 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker( |
| 138 GoogleURLTracker* tracker) { | 175 GoogleURLTracker* tracker) { |
| 139 tracker_ = tracker; | 176 tracker_ = tracker; |
| 140 } | 177 } |
| 141 | 178 |
| 142 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationStart( | |
| 143 bool listen) { | |
| 144 observe_nav_start_ = listen; | |
| 145 } | |
| 146 | |
| 147 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationStart() { | |
| 148 return observe_nav_start_; | |
| 149 } | |
| 150 | |
| 151 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit( | 179 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit( |
| 152 const content::NavigationController* nav_controller, | 180 const content::NavigationController* nav_controller, |
| 153 bool listen) { | 181 bool listen) { |
| 154 if (listen) | 182 if (listen) |
| 155 nav_controller_commit_listeners_.insert(nav_controller); | 183 nav_controller_commit_listeners_.insert(nav_controller); |
| 156 else | 184 else |
| 157 nav_controller_commit_listeners_.erase(nav_controller); | 185 nav_controller_commit_listeners_.erase(nav_controller); |
| 158 } | 186 } |
| 159 | 187 |
| 160 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit( | 188 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit( |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 | 274 |
| 247 // These are required by the TestURLFetchers GoogleURLTracker will create (see | 275 // These are required by the TestURLFetchers GoogleURLTracker will create (see |
| 248 // test_url_fetcher_factory.h). | 276 // test_url_fetcher_factory.h). |
| 249 content::TestBrowserThreadBundle thread_bundle_; | 277 content::TestBrowserThreadBundle thread_bundle_; |
| 250 // Creating this allows us to call | 278 // Creating this allows us to call |
| 251 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). | 279 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). |
| 252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 280 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 253 net::TestURLFetcherFactory fetcher_factory_; | 281 net::TestURLFetcherFactory fetcher_factory_; |
| 254 content::NotificationRegistrar registrar_; | 282 content::NotificationRegistrar registrar_; |
| 255 TestNotificationObserver observer_; | 283 TestNotificationObserver observer_; |
| 284 GoogleURLTrackerClient* client_; | |
| 256 GoogleURLTrackerNavigationHelper* nav_helper_; | 285 GoogleURLTrackerNavigationHelper* nav_helper_; |
| 257 TestingProfile profile_; | 286 TestingProfile profile_; |
| 258 scoped_ptr<GoogleURLTracker> google_url_tracker_; | 287 scoped_ptr<GoogleURLTracker> google_url_tracker_; |
| 259 // This tracks the different "tabs" a test has "opened", so we can close them | 288 // 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. | 289 // properly before shutting down |google_url_tracker_|, which expects that. |
| 261 std::set<int> unique_ids_seen_; | 290 std::set<int> unique_ids_seen_; |
| 262 }; | 291 }; |
| 263 | 292 |
| 264 void GoogleURLTrackerTest::OnInfoBarClosed( | 293 void GoogleURLTrackerTest::OnInfoBarClosed( |
| 265 scoped_ptr<infobars::InfoBar> infobar, | 294 scoped_ptr<infobars::InfoBar> infobar, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 284 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 313 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 285 GoogleURLTrackerFactory::GetInstance()-> | 314 GoogleURLTrackerFactory::GetInstance()-> |
| 286 RegisterUserPrefsOnBrowserContextForTest(&profile_); | 315 RegisterUserPrefsOnBrowserContextForTest(&profile_); |
| 287 } | 316 } |
| 288 | 317 |
| 289 GoogleURLTrackerTest::~GoogleURLTrackerTest() { | 318 GoogleURLTrackerTest::~GoogleURLTrackerTest() { |
| 290 } | 319 } |
| 291 | 320 |
| 292 void GoogleURLTrackerTest::SetUp() { | 321 void GoogleURLTrackerTest::SetUp() { |
| 293 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | 322 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
| 294 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; | 323 // Ownership is passed to google_url_tracker_, but weak pointers are kept; |
| 295 // this is safe since GoogleURLTracker keeps the observer for its lifetime. | 324 // this is safe since GoogleURLTracker keeps these objects for its lifetime. |
| 325 client_ = new TestGoogleURLTrackerClient(); | |
| 296 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); | 326 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); |
| 327 scoped_ptr<GoogleURLTrackerClient> client(client_); | |
| 297 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); | 328 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); |
| 298 google_url_tracker_.reset( | 329 google_url_tracker_.reset( |
| 299 new GoogleURLTracker(&profile_, nav_helper.Pass(), | 330 new GoogleURLTracker(&profile_, client.Pass(), nav_helper.Pass(), |
| 300 GoogleURLTracker::UNIT_TEST_MODE)); | 331 GoogleURLTracker::UNIT_TEST_MODE)); |
| 301 google_url_tracker_->infobar_creator_ = base::Bind( | 332 google_url_tracker_->infobar_creator_ = base::Bind( |
| 302 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); | 333 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); |
| 303 } | 334 } |
| 304 | 335 |
| 305 void GoogleURLTrackerTest::TearDown() { | 336 void GoogleURLTrackerTest::TearDown() { |
| 306 while (!unique_ids_seen_.empty()) | 337 while (!unique_ids_seen_.empty()) |
| 307 CloseTab(*unique_ids_seen_.begin()); | 338 CloseTab(*unique_ids_seen_.begin()); |
| 308 | 339 |
| 340 client_ = NULL; | |
| 309 nav_helper_ = NULL; | 341 nav_helper_ = NULL; |
| 310 google_url_tracker_.reset(); | 342 google_url_tracker_.reset(); |
| 311 network_change_notifier_.reset(); | 343 network_change_notifier_.reset(); |
|
Peter Kasting
2014/05/14 23:30:28
Why are any of these four lines necessary?
blundell
2014/05/15 07:58:03
Removed.
On 2014/05/14 23:30:28, Peter Kasting wr
| |
| 312 } | 344 } |
| 313 | 345 |
| 314 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { | 346 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { |
| 315 // This will return the last fetcher created. If no fetchers have been | 347 // This will return the last fetcher created. If no fetchers have been |
| 316 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. | 348 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. |
| 317 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); | 349 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); |
| 318 } | 350 } |
| 319 | 351 |
| 320 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( | 352 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( |
| 321 const std::string& domain) { | 353 const std::string& domain) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 } | 392 } |
| 361 | 393 |
| 362 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, | 394 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, |
| 363 bool is_search) { | 395 bool is_search) { |
| 364 if (is_search) { | 396 if (is_search) { |
| 365 google_url_tracker_->SearchCommitted(); | 397 google_url_tracker_->SearchCommitted(); |
| 366 // Note that the call above might not have actually registered a listener | 398 // Note that the call above might not have actually registered a listener |
| 367 // for navigation starts if the searchdomaincheck response was bogus. | 399 // for navigation starts if the searchdomaincheck response was bogus. |
| 368 } | 400 } |
| 369 unique_ids_seen_.insert(unique_id); | 401 unique_ids_seen_.insert(unique_id); |
| 370 if (nav_helper_->IsListeningForNavigationStart()) { | 402 if (client_->IsListeningForNavigationStart()) { |
| 371 google_url_tracker_->OnNavigationPending( | 403 google_url_tracker_->OnNavigationPending( |
| 372 reinterpret_cast<content::NavigationController*>(unique_id), | 404 reinterpret_cast<content::NavigationController*>(unique_id), |
| 373 reinterpret_cast<InfoBarService*>(unique_id), unique_id); | 405 reinterpret_cast<InfoBarService*>(unique_id), unique_id); |
| 374 } | 406 } |
| 375 } | 407 } |
| 376 | 408 |
| 377 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { | 409 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { |
| 378 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); | 410 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); |
| 379 if (!map_entry) | 411 if (!map_entry) |
| 380 return; | 412 return; |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1077 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); | 1109 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); |
| 1078 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); | 1110 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); |
| 1079 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); | 1111 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); |
| 1080 ASSERT_FALSE(delegate2 == NULL); | 1112 ASSERT_FALSE(delegate2 == NULL); |
| 1081 SetNavigationPending(1, true); | 1113 SetNavigationPending(1, true); |
| 1082 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); | 1114 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); |
| 1083 delegate2->Close(false); | 1115 delegate2->Close(false); |
| 1084 SetNavigationPending(1, false); | 1116 SetNavigationPending(1, false); |
| 1085 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); | 1117 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); |
| 1086 } | 1118 } |
| OLD | NEW |