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

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: Rebase 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 return google_url_updated_subscription_.get(); 98 return google_url_updated_subscription_.get();
98 } 99 }
99 100
100 void TestCallbackListener::RegisterCallback( 101 void TestCallbackListener::RegisterCallback(
101 GoogleURLTracker* google_url_tracker) { 102 GoogleURLTracker* google_url_tracker) {
102 google_url_updated_subscription_ = 103 google_url_updated_subscription_ =
103 google_url_tracker->RegisterCallback(base::Bind( 104 google_url_tracker->RegisterCallback(base::Bind(
104 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this))); 105 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this)));
105 } 106 }
106 107
107 // TestGoogleURLTrackerNavigationHelper ------------------------------------- 108
109 // TestGoogleURLTrackerClient -------------------------------------------------
110
111 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
112 public:
113 TestGoogleURLTrackerClient();
114 virtual ~TestGoogleURLTrackerClient();
115
116 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
117 virtual bool IsListeningForNavigationStart() OVERRIDE;
118
119 private:
120 bool observe_nav_start_;
121
122 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
123 };
124
125 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient()
126 : observe_nav_start_(false) {
127 }
128
129 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
130 }
131
132 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
133 observe_nav_start_ = listen;
134 }
135
136 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
137 return observe_nav_start_;
138 }
139
140 // TestGoogleURLTrackerNavigationHelper ---------------------------------------
108 141
109 class TestGoogleURLTrackerNavigationHelper 142 class TestGoogleURLTrackerNavigationHelper
110 : public GoogleURLTrackerNavigationHelper { 143 : public GoogleURLTrackerNavigationHelper {
111 public: 144 public:
112 TestGoogleURLTrackerNavigationHelper(); 145 TestGoogleURLTrackerNavigationHelper();
113 virtual ~TestGoogleURLTrackerNavigationHelper(); 146 virtual ~TestGoogleURLTrackerNavigationHelper();
114 147
115 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE; 148 virtual void SetGoogleURLTracker(GoogleURLTracker* tracker) OVERRIDE;
116 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
117 virtual bool IsListeningForNavigationStart() OVERRIDE;
118 virtual void SetListeningForNavigationCommit( 149 virtual void SetListeningForNavigationCommit(
119 const content::NavigationController* nav_controller, 150 const content::NavigationController* nav_controller,
120 bool listen) OVERRIDE; 151 bool listen) OVERRIDE;
121 virtual bool IsListeningForNavigationCommit( 152 virtual bool IsListeningForNavigationCommit(
122 const content::NavigationController* nav_controller) OVERRIDE; 153 const content::NavigationController* nav_controller) OVERRIDE;
123 virtual void SetListeningForTabDestruction( 154 virtual void SetListeningForTabDestruction(
124 const content::NavigationController* nav_controller, 155 const content::NavigationController* nav_controller,
125 bool listen) OVERRIDE; 156 bool listen) OVERRIDE;
126 virtual bool IsListeningForTabDestruction( 157 virtual bool IsListeningForTabDestruction(
127 const content::NavigationController* nav_controller) OVERRIDE; 158 const content::NavigationController* nav_controller) OVERRIDE;
128 159
129 private: 160 private:
130 GoogleURLTracker* tracker_; 161 GoogleURLTracker* tracker_;
131 bool observe_nav_start_;
132 std::set<const content::NavigationController*> 162 std::set<const content::NavigationController*>
133 nav_controller_commit_listeners_; 163 nav_controller_commit_listeners_;
134 std::set<const content::NavigationController*> 164 std::set<const content::NavigationController*>
135 nav_controller_tab_close_listeners_; 165 nav_controller_tab_close_listeners_;
136 }; 166 };
137 167
138 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper() 168 TestGoogleURLTrackerNavigationHelper::TestGoogleURLTrackerNavigationHelper()
139 : tracker_(NULL), 169 : tracker_(NULL) {
140 observe_nav_start_(false) {
141 } 170 }
142 171
143 TestGoogleURLTrackerNavigationHelper:: 172 TestGoogleURLTrackerNavigationHelper::
144 ~TestGoogleURLTrackerNavigationHelper() { 173 ~TestGoogleURLTrackerNavigationHelper() {
145 } 174 }
146 175
147 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker( 176 void TestGoogleURLTrackerNavigationHelper::SetGoogleURLTracker(
148 GoogleURLTracker* tracker) { 177 GoogleURLTracker* tracker) {
149 tracker_ = tracker; 178 tracker_ = tracker;
150 } 179 }
151 180
152 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationStart(
153 bool listen) {
154 observe_nav_start_ = listen;
155 }
156
157 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationStart() {
158 return observe_nav_start_;
159 }
160
161 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit( 181 void TestGoogleURLTrackerNavigationHelper::SetListeningForNavigationCommit(
162 const content::NavigationController* nav_controller, 182 const content::NavigationController* nav_controller,
163 bool listen) { 183 bool listen) {
164 if (listen) 184 if (listen)
165 nav_controller_commit_listeners_.insert(nav_controller); 185 nav_controller_commit_listeners_.insert(nav_controller);
166 else 186 else
167 nav_controller_commit_listeners_.erase(nav_controller); 187 nav_controller_commit_listeners_.erase(nav_controller);
168 } 188 }
169 189
170 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit( 190 bool TestGoogleURLTrackerNavigationHelper::IsListeningForNavigationCommit(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 GoogleURLTracker* google_url_tracker, 274 GoogleURLTracker* google_url_tracker,
255 const GURL& search_url); 275 const GURL& search_url);
256 276
257 // These are required by the TestURLFetchers GoogleURLTracker will create (see 277 // These are required by the TestURLFetchers GoogleURLTracker will create (see
258 // test_url_fetcher_factory.h). 278 // test_url_fetcher_factory.h).
259 content::TestBrowserThreadBundle thread_bundle_; 279 content::TestBrowserThreadBundle thread_bundle_;
260 // Creating this allows us to call 280 // Creating this allows us to call
261 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(). 281 // net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests().
262 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 282 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
263 net::TestURLFetcherFactory fetcher_factory_; 283 net::TestURLFetcherFactory fetcher_factory_;
284 GoogleURLTrackerClient* client_;
264 GoogleURLTrackerNavigationHelper* nav_helper_; 285 GoogleURLTrackerNavigationHelper* nav_helper_;
265 TestingProfile profile_; 286 TestingProfile profile_;
266 scoped_ptr<GoogleURLTracker> google_url_tracker_; 287 scoped_ptr<GoogleURLTracker> google_url_tracker_;
267 TestCallbackListener listener_; 288 TestCallbackListener listener_;
268 // This tracks the different "tabs" a test has "opened", so we can close them 289 // This tracks the different "tabs" a test has "opened", so we can close them
269 // properly before shutting down |google_url_tracker_|, which expects that. 290 // properly before shutting down |google_url_tracker_|, which expects that.
270 std::set<int> unique_ids_seen_; 291 std::set<int> unique_ids_seen_;
271 }; 292 };
272 293
273 void GoogleURLTrackerTest::OnInfoBarClosed( 294 void GoogleURLTrackerTest::OnInfoBarClosed(
(...skipping 19 matching lines...) Expand all
293 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 314 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
294 GoogleURLTrackerFactory::GetInstance()-> 315 GoogleURLTrackerFactory::GetInstance()->
295 RegisterUserPrefsOnBrowserContextForTest(&profile_); 316 RegisterUserPrefsOnBrowserContextForTest(&profile_);
296 } 317 }
297 318
298 GoogleURLTrackerTest::~GoogleURLTrackerTest() { 319 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
299 } 320 }
300 321
301 void GoogleURLTrackerTest::SetUp() { 322 void GoogleURLTrackerTest::SetUp() {
302 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 323 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
303 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; 324 // Ownership is passed to google_url_tracker_, but weak pointers are kept;
304 // this is safe since GoogleURLTracker keeps the observer for its lifetime. 325 // this is safe since GoogleURLTracker keeps these objects for its lifetime.
326 client_ = new TestGoogleURLTrackerClient();
305 nav_helper_ = new TestGoogleURLTrackerNavigationHelper(); 327 nav_helper_ = new TestGoogleURLTrackerNavigationHelper();
328 scoped_ptr<GoogleURLTrackerClient> client(client_);
306 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_); 329 scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper(nav_helper_);
307 google_url_tracker_.reset( 330 google_url_tracker_.reset(
308 new GoogleURLTracker(&profile_, nav_helper.Pass(), 331 new GoogleURLTracker(&profile_,
332 client.Pass(),
333 nav_helper.Pass(),
309 GoogleURLTracker::UNIT_TEST_MODE)); 334 GoogleURLTracker::UNIT_TEST_MODE));
310 google_url_tracker_->infobar_creator_ = base::Bind( 335 google_url_tracker_->infobar_creator_ = base::Bind(
311 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this)); 336 &GoogleURLTrackerTest::CreateTestInfoBar, base::Unretained(this));
312 } 337 }
313 338
314 void GoogleURLTrackerTest::TearDown() { 339 void GoogleURLTrackerTest::TearDown() {
315 while (!unique_ids_seen_.empty()) 340 while (!unique_ids_seen_.empty())
316 CloseTab(*unique_ids_seen_.begin()); 341 CloseTab(*unique_ids_seen_.begin());
317
318 nav_helper_ = NULL;
319 network_change_notifier_.reset();
320 } 342 }
321 343
322 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() { 344 net::TestURLFetcher* GoogleURLTrackerTest::GetFetcher() {
323 // This will return the last fetcher created. If no fetchers have been 345 // This will return the last fetcher created. If no fetchers have been
324 // created, we'll pass GetFetcherByID() "-1", and it will return NULL. 346 // created, we'll pass GetFetcherByID() "-1", and it will return NULL.
325 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1); 347 return fetcher_factory_.GetFetcherByID(google_url_tracker_->fetcher_id_ - 1);
326 } 348 }
327 349
328 void GoogleURLTrackerTest::MockSearchDomainCheckResponse( 350 void GoogleURLTrackerTest::MockSearchDomainCheckResponse(
329 const std::string& domain) { 351 const std::string& domain) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 } 386 }
365 387
366 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id, 388 void GoogleURLTrackerTest::SetNavigationPending(intptr_t unique_id,
367 bool is_search) { 389 bool is_search) {
368 if (is_search) { 390 if (is_search) {
369 google_url_tracker_->SearchCommitted(); 391 google_url_tracker_->SearchCommitted();
370 // Note that the call above might not have actually registered a listener 392 // Note that the call above might not have actually registered a listener
371 // for navigation starts if the searchdomaincheck response was bogus. 393 // for navigation starts if the searchdomaincheck response was bogus.
372 } 394 }
373 unique_ids_seen_.insert(unique_id); 395 unique_ids_seen_.insert(unique_id);
374 if (nav_helper_->IsListeningForNavigationStart()) { 396 if (client_->IsListeningForNavigationStart()) {
375 google_url_tracker_->OnNavigationPending( 397 google_url_tracker_->OnNavigationPending(
376 reinterpret_cast<content::NavigationController*>(unique_id), 398 reinterpret_cast<content::NavigationController*>(unique_id),
377 reinterpret_cast<InfoBarService*>(unique_id), unique_id); 399 reinterpret_cast<InfoBarService*>(unique_id), unique_id);
378 } 400 }
379 } 401 }
380 402
381 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) { 403 void GoogleURLTrackerTest::CommitNonSearch(intptr_t unique_id) {
382 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id); 404 GoogleURLTrackerMapEntry* map_entry = GetMapEntry(unique_id);
383 if (!map_entry) 405 if (!map_entry)
384 return; 406 return;
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
1081 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2")); 1103 CommitSearch(2, GURL("http://www.google.co.uk/search?q=test2"));
1082 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL); 1104 EXPECT_FALSE(GetInfoBarDelegate(1) == NULL);
1083 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2); 1105 GoogleURLTrackerInfoBarDelegate* delegate2 = GetInfoBarDelegate(2);
1084 ASSERT_FALSE(delegate2 == NULL); 1106 ASSERT_FALSE(delegate2 == NULL);
1085 SetNavigationPending(1, true); 1107 SetNavigationPending(1, true);
1086 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true)); 1108 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, true));
1087 delegate2->Close(false); 1109 delegate2->Close(false);
1088 SetNavigationPending(1, false); 1110 SetNavigationPending(1, false);
1089 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false)); 1111 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(1, false));
1090 } 1112 }
OLDNEW
« no previous file with comments | « chrome/browser/google/google_url_tracker_navigation_helper_impl.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698