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

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

Issue 285193002: Create GoogleURLTrackerClient interface and //chrome implementation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Response to review Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 TestNotificationObserver::~TestNotificationObserver() { 87 TestNotificationObserver::~TestNotificationObserver() {
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
Peter Kasting 2014/05/16 23:21:07 Nit: Don't remove this blank line.
blundell 2014/05/17 19:06:07 Done.
97 // TestGoogleURLTrackerNavigationHelper ------------------------------------- 98 // TestGoogleURLTrackerClient -------------------------------------------------
99
100 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
101 public:
102 TestGoogleURLTrackerClient();
103 virtual ~TestGoogleURLTrackerClient();
104
105 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
106 virtual bool IsListeningForNavigationStart() OVERRIDE;
107
108 private:
109 bool observe_nav_start_;
110
111 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
112 };
113
114 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient()
115 : observe_nav_start_(false) {
116 }
117
118 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
119 }
120
121 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(
122 bool listen) {
123 observe_nav_start_ = listen;
124 }
125
126 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
127 return observe_nav_start_;
128 }
129
130 // TestGoogleURLTrackerNavigationHelper ---------------------------------------
98 131
99 class TestGoogleURLTrackerNavigationHelper 132 class TestGoogleURLTrackerNavigationHelper
100 : public GoogleURLTrackerNavigationHelper { 133 : public GoogleURLTrackerNavigationHelper {
101 public: 134 public:
102 TestGoogleURLTrackerNavigationHelper(); 135 TestGoogleURLTrackerNavigationHelper();
103 virtual ~TestGoogleURLTrackerNavigationHelper(); 136 virtual ~TestGoogleURLTrackerNavigationHelper();
104 137
105 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE; 138 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
106 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
107 virtual bool IsListeningForNavigationStart() OVERRIDE;
108 virtual void SetListeningForNavigationCommit( 139 virtual void SetListeningForNavigationCommit(
109 const content::NavigationController* nav_controller, 140 const content::NavigationController* nav_controller,
110 bool listen) OVERRIDE; 141 bool listen) OVERRIDE;
111 virtual bool IsListeningForNavigationCommit( 142 virtual bool IsListeningForNavigationCommit(
112 const content::NavigationController* nav_controller) OVERRIDE; 143 const content::NavigationController* nav_controller) OVERRIDE;
113 virtual void SetListeningForTabDestruction( 144 virtual void SetListeningForTabDestruction(
114 const content::NavigationController* nav_controller, 145 const content::NavigationController* nav_controller,
115 bool listen) OVERRIDE; 146 bool listen) OVERRIDE;
116 virtual bool IsListeningForTabDestruction( 147 virtual bool IsListeningForTabDestruction(
117 const content::NavigationController* nav_controller) OVERRIDE; 148 const content::NavigationController* nav_controller) OVERRIDE;
118 149
119 private: 150 private:
120 GoogleURLTracker* tracker_; 151 GoogleURLTracker* tracker_;
121 bool observe_nav_start_;
122 std::set<const content::NavigationController*> 152 std::set<const content::NavigationController*>
123 nav_controller_commit_listeners_; 153 nav_controller_commit_listeners_;
124 std::set<const content::NavigationController*> 154 std::set<const content::NavigationController*>
125 nav_controller_tab_close_listeners_; 155 nav_controller_tab_close_listeners_;
126 }; 156 };
127 157
128 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper() 158 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper()
129 : tracker_(NULL), 159 : tracker_(NULL) {
130 observe_nav_start_(false) {
131 } 160 }
132 161
133 TestGoogleURLTrackerNavigationHelper:: 162 TestGoogleURLTrackerNavigationHelper::
134 ~TestGoogleURLTrackerNavigationHelper() { 163 ~TestGoogleURLTrackerNavigationHelper() {
135 } 164 }
136 165
137 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker( 166 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker(
138 GoogleURLTracker* tracker) { 167 GoogleURLTracker* tracker) {
139 tracker_ = tracker; 168 tracker_ = tracker;
140 } 169 }
141 170
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( 171 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
152 const content::NavigationController* nav_controller, 172 const content::NavigationController* nav_controller,
153 bool listen) { 173 bool listen) {
154 if (listen) 174 if (listen)
155 nav_controller_commit_listeners_.insert(nav_controller); 175 nav_controller_commit_listeners_.insert(nav_controller);
156 else 176 else
157 nav_controller_commit_listeners_.erase(nav_controller); 177 nav_controller_commit_listeners_.erase(nav_controller);
158 } 178 }
159 179
160 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit( 180 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit(
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 266
247 // These are required by the TestURLFetchers GoogleURLTracker will create (see 267 // These are required by the TestURLFetchers GoogleURLTracker will create (see
248 // test_url_fetcher_factory.h). 268 // test_url_fetcher_factory.h).
249 content::TestBrowserThreadBundle thread_bundle_; 269 content::TestBrowserThreadBundle thread_bundle_;
250 // Creating this allows us to call 270 // Creating this allows us to call
251 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). 271 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 272 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
253 net::TestURLFetcherFactory fetcher_factory_; 273 net::TestURLFetcherFactory fetcher_factory_;
254 content::NotificationRegistrar registrar_; 274 content::NotificationRegistrar registrar_;
255 TestNotificationObserver observer_; 275 TestNotificationObserver observer_;
276 GoogleURLTrackerClient* client_;
256 GoogleURLTrackerNavigationHelper* nav_helper_; 277 GoogleURLTrackerNavigationHelper* nav_helper_;
257 TestingProfile profile_; 278 TestingProfile profile_;
258 scoped_ptr<GoogleURLTracker> google_url_tracker_; 279 scoped_ptr<GoogleURLTracker> google_url_tracker_;
259 // This tracks the different "tabs" a test has "opened", so we can close them 280 // 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. 281 // properly before shutting down |google_url_tracker_|, which expects that.
261 std::set<int> unique_ids_seen_; 282 std::set<int> unique_ids_seen_;
262 }; 283 };
263 284
264 void GoogleURLTrackerTest::OnInfoBarClosed( 285 void GoogleURLTrackerTest::OnInfoBarClosed(
265 scoped_ptr<infobars::InfoBar> infobar, 286 scoped_ptr<infobars::InfoBar> infobar,
(...skipping 18 matching lines...) Expand all
284 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 305 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
285 GoogleURLTrackerFactory::GetInstance()-> 306 GoogleURLTrackerFactory::GetInstance()->
286 RegisterUserPrefsOnBrowserContextForTest(&profile_); 307 RegisterUserPrefsOnBrowserContextForTest(&profile_);
287 } 308 }
288 309
289 GoogleURLTrackerTest::~GoogleURLTrackerTest() { 310 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
290 } 311 }
291 312
292 void GoogleURLTrackerTest::SetUp() { 313 void GoogleURLTrackerTest::SetUp() {
293 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 314 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
294 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; 315 // Ownership is passed to google_url_tracker_, but weak pointers are kept;
295 // this is safe since GoogleURLTracker keeps the observer for its lifetime. 316 // this is safe since GoogleURLTracker keeps these objects for its lifetime.
317 client_ = new TestGoogleURLTrackerClient();
296 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); 318 nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
319 scoped_ptr<GoogleURLTrackerClient> client(client_);
297 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); 320 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
298 google_url_tracker_.reset( 321 google_url_tracker_.reset(
299 new GoogleURLTracker(&profile_, nav_helper.Pass(), 322 new GoogleURLTracker(&profile_, client.Pass(), nav_helper.Pass(),
300 GoogleURLTracker::UNIT_TEST_MODE)); 323 GoogleURLTracker::UNIT_TEST_MODE));
301 google_url_tracker_->infobar_creator_ = base::Bind( 324 google_url_tracker_->infobar_creator_ = base::Bind(
302 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); 325 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this));
303 } 326 }
304 327
305 void GoogleURLTrackerTest::TearDown() { 328 void GoogleURLTrackerTest::TearDown() {
306 while (!unique_ids_seen_.empty()) 329 while (!unique_ids_seen_.empty())
307 CloseTab(*unique_ids_seen_.begin()); 330 CloseTab(*unique_ids_seen_.begin());
308
309 nav_helper_ = NULL;
310 google_url_tracker_.reset();
311 network_change_notifier_.reset();
312 } 331 }
313 332
314 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { 333 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
315 // This will return the last fetcher created. If no fetchers have been 334 // This will return the last fetcher created. If no fetchers have been
316 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. 335 // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
317 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); 336 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
318 } 337 }
319 338
320 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( 339 void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
321 const std::string& domain) { 340 const std::string& domain) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 } 379 }
361 380
362 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, 381 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id,
363 bool is_search) { 382 bool is_search) {
364 if (is_search) { 383 if (is_search) {
365 google_url_tracker_->SearchCommitted(); 384 google_url_tracker_->SearchCommitted();
366 // Note that the call above might not have actually registered a listener 385 // Note that the call above might not have actually registered a listener
367 // for navigation starts if the searchdomaincheck response was bogus. 386 // for navigation starts if the searchdomaincheck response was bogus.
368 } 387 }
369 unique_ids_seen_.insert(unique_id); 388 unique_ids_seen_.insert(unique_id);
370 if (nav_helper_->IsListeningForNavigationStart()) { 389 if (client_->IsListeningForNavigationStart()) {
371 google_url_tracker_->OnNavigationPending( 390 google_url_tracker_->OnNavigationPending(
372 reinterpret_cast<content::NavigationController*>(unique_id), 391 reinterpret_cast<content::NavigationController*>(unique_id),
373 reinterpret_cast<InfoBarService*>(unique_id), unique_id); 392 reinterpret_cast<InfoBarService*>(unique_id), unique_id);
374 } 393 }
375 } 394 }
376 395
377 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { 396 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) {
378 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 397 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
379 if (!map_entry) 398 if (!map_entry)
380 return; 399 return;
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); 1096 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1078 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); 1097 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
1079 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); 1098 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
1080 ASSERT_FALSE(delegate2 == NULL); 1099 ASSERT_FALSE(delegate2 == NULL);
1081 SetNavigationPending(1, true); 1100 SetNavigationPending(1, true);
1082 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); 1101 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1083 delegate2->Close(false); 1102 delegate2->Close(false);
1084 SetNavigationPending(1, false); 1103 SetNavigationPending(1, false);
1085 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); 1104 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1086 } 1105 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698