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

Side by Side Diff: components/google/core/browser/google_url_tracker_unittest.cc

Issue 501393004: Componentize GoogleURLTracker unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 "components/google/core/browser/google_url_tracker.h" 5 #include "components/google/core/browser/google_url_tracker.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/google/google_url_tracker_factory.h" 14 #include "base/prefs/testing_pref_service.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/google/core/browser/google_pref_names.h" 15 #include "components/google/core/browser/google_pref_names.h"
16 #include "components/google/core/browser/google_url_tracker_client.h" 16 #include "components/google/core/browser/google_url_tracker_client.h"
17 #include "components/google/core/browser/google_url_tracker_infobar_delegate.h" 17 #include "components/google/core/browser/google_url_tracker_infobar_delegate.h"
18 #include "components/google/core/browser/google_url_tracker_navigation_helper.h" 18 #include "components/google/core/browser/google_url_tracker_navigation_helper.h"
19 #include "components/infobars/core/infobar.h" 19 #include "components/infobars/core/infobar.h"
20 #include "components/infobars/core/infobar_delegate.h" 20 #include "components/infobars/core/infobar_delegate.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "net/url_request/test_url_fetcher_factory.h" 21 #include "net/url_request/test_url_fetcher_factory.h"
23 #include "net/url_request/url_fetcher.h" 22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 25
26 namespace { 26 namespace {
27 27
28 // TestCallbackListener --------------------------------------------------- 28 // TestCallbackListener ---------------------------------------------------
29 29
30 class TestCallbackListener { 30 class TestCallbackListener {
31 public: 31 public:
32 TestCallbackListener(); 32 TestCallbackListener();
33 virtual ~TestCallbackListener(); 33 virtual ~TestCallbackListener();
(...skipping 30 matching lines...) Expand all
64 google_url_updated_subscription_ = 64 google_url_updated_subscription_ =
65 google_url_tracker->RegisterCallback(base::Bind( 65 google_url_tracker->RegisterCallback(base::Bind(
66 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this))); 66 &TestCallbackListener::OnGoogleURLUpdated, base::Unretained(this)));
67 } 67 }
68 68
69 69
70 // TestGoogleURLTrackerClient ------------------------------------------------- 70 // TestGoogleURLTrackerClient -------------------------------------------------
71 71
72 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient { 72 class TestGoogleURLTrackerClient : public GoogleURLTrackerClient {
73 public: 73 public:
74 explicit TestGoogleURLTrackerClient(Profile* profile_); 74 explicit TestGoogleURLTrackerClient(PrefService* prefs_);
75 virtual ~TestGoogleURLTrackerClient(); 75 virtual ~TestGoogleURLTrackerClient();
76 76
77 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE; 77 virtual void SetListeningForNavigationStart(bool listen) OVERRIDE;
78 virtual bool IsListeningForNavigationStart() OVERRIDE; 78 virtual bool IsListeningForNavigationStart() OVERRIDE;
79 virtual bool IsBackgroundNetworkingEnabled() OVERRIDE; 79 virtual bool IsBackgroundNetworkingEnabled() OVERRIDE;
80 virtual PrefService* GetPrefs() OVERRIDE; 80 virtual PrefService* GetPrefs() OVERRIDE;
81 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; 81 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
82 82
83 private: 83 private:
84 Profile* profile_; 84 PrefService* prefs_;
85 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
85 bool observe_nav_start_; 86 bool observe_nav_start_;
86 87
87 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient); 88 DISALLOW_COPY_AND_ASSIGN(TestGoogleURLTrackerClient);
88 }; 89 };
89 90
90 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(Profile* profile) 91 TestGoogleURLTrackerClient::TestGoogleURLTrackerClient(PrefService* prefs)
91 : profile_(profile), 92 : prefs_(prefs),
93 request_context_(new net::TestURLRequestContextGetter(
94 base::MessageLoopProxy::current())),
92 observe_nav_start_(false) { 95 observe_nav_start_(false) {
93 } 96 }
94 97
95 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() { 98 TestGoogleURLTrackerClient::~TestGoogleURLTrackerClient() {
96 } 99 }
97 100
98 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) { 101 void TestGoogleURLTrackerClient::SetListeningForNavigationStart(bool listen) {
99 observe_nav_start_ = listen; 102 observe_nav_start_ = listen;
100 } 103 }
101 104
102 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() { 105 bool TestGoogleURLTrackerClient::IsListeningForNavigationStart() {
103 return observe_nav_start_; 106 return observe_nav_start_;
104 } 107 }
105 108
106 bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() { 109 bool TestGoogleURLTrackerClient::IsBackgroundNetworkingEnabled() {
107 return true; 110 return true;
108 } 111 }
109 112
110 PrefService* TestGoogleURLTrackerClient::GetPrefs() { 113 PrefService* TestGoogleURLTrackerClient::GetPrefs() {
111 return profile_->GetPrefs(); 114 return prefs_;
112 } 115 }
113 116
114 net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() { 117 net::URLRequestContextGetter* TestGoogleURLTrackerClient::GetRequestContext() {
115 return profile_->GetRequestContext(); 118 return request_context_.get();
116 } 119 }
117 120
118 121
119 // TestGoogleURLTrackerNavigationHelper --------------------------------------- 122 // TestGoogleURLTrackerNavigationHelper ---------------------------------------
120 123
121 class TestGoogleURLTrackerNavigationHelper 124 class TestGoogleURLTrackerNavigationHelper
122 : public GoogleURLTrackerNavigationHelper { 125 : public GoogleURLTrackerNavigationHelper {
123 public: 126 public:
124 explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker); 127 explicit TestGoogleURLTrackerNavigationHelper(GoogleURLTracker* tracker);
125 virtual ~TestGoogleURLTrackerNavigationHelper(); 128 virtual ~TestGoogleURLTrackerNavigationHelper();
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 infobars::InfoBarManager* infobar_manager); 239 infobars::InfoBarManager* infobar_manager);
237 GoogleURLTrackerNavigationHelper* GetNavigationHelper( 240 GoogleURLTrackerNavigationHelper* GetNavigationHelper(
238 infobars::InfoBarManager* infobar_manager); 241 infobars::InfoBarManager* infobar_manager);
239 void ExpectDefaultURLs() const; 242 void ExpectDefaultURLs() const;
240 void ExpectListeningForCommit(infobars::InfoBarManager* infobar_manager, 243 void ExpectListeningForCommit(infobars::InfoBarManager* infobar_manager,
241 bool listening); 244 bool listening);
242 bool listener_notified() const { return listener_.notified(); } 245 bool listener_notified() const { return listener_.notified(); }
243 void clear_listener_notified() { listener_.clear_notified(); } 246 void clear_listener_notified() { listener_.clear_notified(); }
244 247
245 private: 248 private:
246 // These are required by the TestURLFetchers GoogleURLTracker will create (see 249 base::MessageLoop message_loop_;
247 // test_url_fetcher_factory.h). 250 TestingPrefServiceSimple prefs_;
248 content::TestBrowserThreadBundle thread_bundle_;
249 251
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 GoogleURLTrackerClient* client_; 256 GoogleURLTrackerClient* client_;
255 TestingProfile profile_;
256 scoped_ptr<GoogleURLTracker> google_url_tracker_; 257 scoped_ptr<GoogleURLTracker> google_url_tracker_;
257 TestCallbackListener listener_; 258 TestCallbackListener listener_;
258 // This tracks the different "tabs" a test has "opened", so we can close them 259 // This tracks the different "tabs" a test has "opened", so we can close them
259 // properly before shutting down |google_url_tracker_|, which expects that. 260 // properly before shutting down |google_url_tracker_|, which expects that.
260 std::set<infobars::InfoBarManager*> infobar_managers_seen_; 261 std::set<infobars::InfoBarManager*> infobar_managers_seen_;
261 }; 262 };
262 263
263 GoogleURLTrackerTest::GoogleURLTrackerTest() 264 GoogleURLTrackerTest::GoogleURLTrackerTest() {
264 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { 265 prefs_.registry()->RegisterStringPref(
Peter Kasting 2014/08/26 17:57:24 Curiosity: Do these changes imply that TestingProf
blundell 2014/09/12 12:48:08 No, the registration was previously happening via
265 GoogleURLTrackerFactory::GetInstance()-> 266 prefs::kLastKnownGoogleURL,
266 RegisterUserPrefsOnBrowserContextForTest(&profile_); 267 GoogleURLTracker::kDefaultGoogleHomepage);
268 prefs_.registry()->RegisterStringPref(
269 prefs::kLastPromptedGoogleURL,
270 std::string());
267 } 271 }
268 272
269 GoogleURLTrackerTest::~GoogleURLTrackerTest() { 273 GoogleURLTrackerTest::~GoogleURLTrackerTest() {
270 } 274 }
271 275
272 void GoogleURLTrackerTest::SetUp() { 276 void GoogleURLTrackerTest::SetUp() {
273 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 277 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
274 // Ownership is passed to google_url_tracker_, but a weak pointer is kept; 278 // Ownership is passed to google_url_tracker_, but a weak pointer is kept;
275 // this is safe since GoogleURLTracker keeps the client for its lifetime. 279 // this is safe since GoogleURLTracker keeps the client for its lifetime.
276 client_ = new TestGoogleURLTrackerClient(&profile_); 280 client_ = new TestGoogleURLTrackerClient(&prefs_);
277 scoped_ptr<GoogleURLTrackerClient> client(client_); 281 scoped_ptr<GoogleURLTrackerClient> client(client_);
278 google_url_tracker_.reset(new GoogleURLTracker( 282 google_url_tracker_.reset(new GoogleURLTracker(
279 client.Pass(), GoogleURLTracker::UNIT_TEST_MODE)); 283 client.Pass(), GoogleURLTracker::UNIT_TEST_MODE));
280 } 284 }
281 285
282 void GoogleURLTrackerTest::TearDown() { 286 void GoogleURLTrackerTest::TearDown() {
283 while (!infobar_managers_seen_.empty()) 287 while (!infobar_managers_seen_.empty())
284 CloseTab(*infobar_managers_seen_.begin()); 288 CloseTab(*infobar_managers_seen_.begin());
285 google_url_tracker_->Shutdown(); 289 google_url_tracker_->Shutdown();
286 } 290 }
(...skipping 28 matching lines...) Expand all
315 } 319 }
316 320
317 void GoogleURLTrackerTest::NotifyIPAddressChanged() { 321 void GoogleURLTrackerTest::NotifyIPAddressChanged() {
318 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); 322 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
319 // For thread safety, the NCN queues tasks to do the actual notifications, so 323 // For thread safety, the NCN queues tasks to do the actual notifications, so
320 // we need to spin the message loop so the tracker will actually be notified. 324 // we need to spin the message loop so the tracker will actually be notified.
321 base::MessageLoop::current()->RunUntilIdle(); 325 base::MessageLoop::current()->RunUntilIdle();
322 } 326 }
323 327
324 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { 328 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
325 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); 329 prefs_.SetString(prefs::kLastPromptedGoogleURL, url.spec());
326 } 330 }
327 331
328 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { 332 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
329 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); 333 return GURL(prefs_.GetString(prefs::kLastPromptedGoogleURL));
330 } 334 }
331 335
332 void GoogleURLTrackerTest::SetNavigationPending( 336 void GoogleURLTrackerTest::SetNavigationPending(
333 infobars::InfoBarManager* infobar_manager, 337 infobars::InfoBarManager* infobar_manager,
334 bool is_search) { 338 bool is_search) {
335 if (is_search) { 339 if (is_search) {
336 google_url_tracker_->SearchCommitted(); 340 google_url_tracker_->SearchCommitted();
337 // Note that the call above might not have actually registered a listener 341 // Note that the call above might not have actually registered a listener
338 // for navigation starts if the searchdomaincheck response was bogus. 342 // for navigation starts if the searchdomaincheck response was bogus.
339 } 343 }
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); 1043 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
1040 GoogleURLTrackerInfoBarDelegate* delegate2 = 1044 GoogleURLTrackerInfoBarDelegate* delegate2 =
1041 GetInfoBarDelegate(&infobar_manager2); 1045 GetInfoBarDelegate(&infobar_manager2);
1042 ASSERT_FALSE(delegate2 == NULL); 1046 ASSERT_FALSE(delegate2 == NULL);
1043 SetNavigationPending(&infobar_manager, true); 1047 SetNavigationPending(&infobar_manager, true);
1044 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); 1048 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
1045 delegate2->Close(false); 1049 delegate2->Close(false);
1046 SetNavigationPending(&infobar_manager, false); 1050 SetNavigationPending(&infobar_manager, false);
1047 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); 1051 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
1048 } 1052 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698