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

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
97 // TestGoogleURLTrackerClient -------------------------------------------------
96 98
97 // TestGoogleURLTrackerNavigationHelper ------------------------------------- 99 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
100 public:
101 TestGoogleURLTrackerClient();
102 virtual ~TestGoogleURLTrackerClient();
103
104 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
105 virtual bool IsListeningForNavigationStart() OVERRIDE;
106
107 private:
108 bool observe_nav_start_;
109
110 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
111 };
112
113 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient()
114 : observe_nav_start_(false) {
115 }
116
117 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
118 }
119
120 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
121 observe_nav_start_ = listen;
122 }
123
124 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
125 return observe_nav_start_;
126 }
127
128 // TestGoogleURLTrackerNavigationHelper ---------------------------------------
98 129
99 class TestGoogleURLTrackerNavigationHelper 130 class TestGoogleURLTrackerNavigationHelper
100 : public GoogleURLTrackerNavigationHelper { 131 : public GoogleURLTrackerNavigationHelper {
101 public: 132 public:
102 TestGoogleURLTrackerNavigationHelper(); 133 TestGoogleURLTrackerNavigationHelper();
103 virtual ~TestGoogleURLTrackerNavigationHelper(); 134 virtual ~TestGoogleURLTrackerNavigationHelper();
104 135
105 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE; 136 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
106 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
107 virtual bool IsListeningForNavigationStart() OVERRIDE;
108 virtual void SetListeningForNavigationCommit( 137 virtual void SetListeningForNavigationCommit(
109 const content::NavigationController* nav_controller, 138 const content::NavigationController* nav_controller,
110 bool listen) OVERRIDE; 139 bool listen) OVERRIDE;
111 virtual bool IsListeningForNavigationCommit( 140 virtual bool IsListeningForNavigationCommit(
112 const content::NavigationController* nav_controller) OVERRIDE; 141 const content::NavigationController* nav_controller) OVERRIDE;
113 virtual void SetListeningForTabDestruction( 142 virtual void SetListeningForTabDestruction(
114 const content::NavigationController* nav_controller, 143 const content::NavigationController* nav_controller,
115 bool listen) OVERRIDE; 144 bool listen) OVERRIDE;
116 virtual bool IsListeningForTabDestruction( 145 virtual bool IsListeningForTabDestruction(
117 const content::NavigationController* nav_controller) OVERRIDE; 146 const content::NavigationController* nav_controller) OVERRIDE;
118 147
119 private: 148 private:
120 GoogleURLTracker* tracker_; 149 GoogleURLTracker* tracker_;
121 bool observe_nav_start_;
122 std::set<const content::NavigationController*> 150 std::set<const content::NavigationController*>
123 nav_controller_commit_listeners_; 151 nav_controller_commit_listeners_;
124 std::set<const content::NavigationController*> 152 std::set<const content::NavigationController*>
125 nav_controller_tab_close_listeners_; 153 nav_controller_tab_close_listeners_;
126 }; 154 };
127 155
128 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper() 156 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper()
129 : tracker_(NULL), 157 : tracker_(NULL) {
130 observe_nav_start_(false) {
131 } 158 }
132 159
133 TestGoogleURLTrackerNavigationHelper:: 160 TestGoogleURLTrackerNavigationHelper::
134 ~TestGoogleURLTrackerNavigationHelper() { 161 ~TestGoogleURLTrackerNavigationHelper() {
135 } 162 }
136 163
137 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker( 164 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker(
138 GoogleURLTracker* tracker) { 165 GoogleURLTracker* tracker) {
139 tracker_ = tracker; 166 tracker_ = tracker;
140 } 167 }
141 168
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( 169 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
152 const content::NavigationController* nav_controller, 170 const content::NavigationController* nav_controller,
153 bool listen) { 171 bool listen) {
154 if (listen) 172 if (listen)
155 nav_controller_commit_listeners_.insert(nav_controller); 173 nav_controller_commit_listeners_.insert(nav_controller);
156 else 174 else
157 nav_controller_commit_listeners_.erase(nav_controller); 175 nav_controller_commit_listeners_.erase(nav_controller);
158 } 176 }
159 177
160 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit( 178 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit(
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 264
247 // These are required by the TestURLFetchers GoogleURLTracker will create (see 265 // These are required by the TestURLFetchers GoogleURLTracker will create (see
248 // test_url_fetcher_factory.h). 266 // test_url_fetcher_factory.h).
249 content::TestBrowserThreadBundle thread_bundle_; 267 content::TestBrowserThreadBundle thread_bundle_;
250 // Creating this allows us to call 268 // Creating this allows us to call
251 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). 269 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
252 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 270 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
253 net::TestURLFetcherFactory fetcher_factory_; 271 net::TestURLFetcherFactory fetcher_factory_;
254 content::NotificationRegistrar registrar_; 272 content::NotificationRegistrar registrar_;
255 TestNotificationObserver observer_; 273 TestNotificationObserver observer_;
274 GoogleURLTrackerClient* client_;
256 GoogleURLTrackerNavigationHelper* nav_helper_; 275 GoogleURLTrackerNavigationHelper* nav_helper_;
257 TestingProfile profile_; 276 TestingProfile profile_;
258 scoped_ptr<GoogleURLTracker> google_url_tracker_; 277 scoped_ptr<GoogleURLTracker> google_url_tracker_;
259 // This tracks the different "tabs" a test has "opened", so we can close them 278 // 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. 279 // properly before shutting down |google_url_tracker_|, which expects that.
261 std::set<int> unique_ids_seen_; 280 std::set<int> unique_ids_seen_;
262 }; 281 };
263 282
264 void GoogleURLTrackerTest::OnInfoBarClosed( 283 void GoogleURLTrackerTest::OnInfoBarClosed(
265 scoped_ptr<infobars::InfoBar> infobar, 284 scoped_ptr<infobars::InfoBar> infobar,
(...skipping 18 matching lines...) Expand all
284 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 303 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
285 GoogleURLTrackerFactory::GetInstance()-> 304 GoogleURLTrackerFactory::GetInstance()->
286 RegisterUserPrefsOnBrowserContextForTest(&profile_); 305 RegisterUserPrefsOnBrowserContextForTest(&profile_);
287 } 306 }
288 307
289 GoogleURLTrackerTest::~GoogleURLTrackerTest() { 308 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
290 } 309 }
291 310
292 void GoogleURLTrackerTest::SetUp() { 311 void GoogleURLTrackerTest::SetUp() {
293 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 312 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
294 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; 313 // Ownership is passed to google_url_tracker_, but weak pointers are kept;
295 // this is safe since GoogleURLTracker keeps the observer for its lifetime. 314 // this is safe since GoogleURLTracker keeps these objects for its lifetime.
315 client_ = new TestGoogleURLTrackerClient();
296 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); 316 nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
317 scoped_ptr<GoogleURLTrackerClient> client(client_);
297 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); 318 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
298 google_url_tracker_.reset( 319 google_url_tracker_.reset(
299 new GoogleURLTracker(&profile_, nav_helper.Pass(), 320 new GoogleURLTracker(&profile_,
321 client.Pass(),
322 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