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

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: Rebase 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
« no previous file with comments | « components/components_tests.gyp ('k') | components/infobars/test/infobar_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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::NotifyObserversOfNetworkChangeForTests(). 253 // net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests().
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(
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 29 matching lines...) Expand all
316 320
317 void GoogleURLTrackerTest::NotifyNetworkChanged() { 321 void GoogleURLTrackerTest::NotifyNetworkChanged() {
318 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 322 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
319 net::NetworkChangeNotifier::CONNECTION_UNKNOWN); 323 net::NetworkChangeNotifier::CONNECTION_UNKNOWN);
320 // For thread safety, the NCN queues tasks to do the actual notifications, so 324 // For thread safety, the NCN queues tasks to do the actual notifications, so
321 // we need to spin the message loop so the tracker will actually be notified. 325 // we need to spin the message loop so the tracker will actually be notified.
322 base::MessageLoop::current()->RunUntilIdle(); 326 base::MessageLoop::current()->RunUntilIdle();
323 } 327 }
324 328
325 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) { 329 void GoogleURLTrackerTest::SetLastPromptedGoogleURL(const GURL& url) {
326 profile_.GetPrefs()->SetString(prefs::kLastPromptedGoogleURL, url.spec()); 330 prefs_.SetString(prefs::kLastPromptedGoogleURL, url.spec());
327 } 331 }
328 332
329 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() { 333 GURL GoogleURLTrackerTest::GetLastPromptedGoogleURL() {
330 return GURL(profile_.GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); 334 return GURL(prefs_.GetString(prefs::kLastPromptedGoogleURL));
331 } 335 }
332 336
333 void GoogleURLTrackerTest::SetNavigationPending( 337 void GoogleURLTrackerTest::SetNavigationPending(
334 infobars::InfoBarManager* infobar_manager, 338 infobars::InfoBarManager* infobar_manager,
335 bool is_search) { 339 bool is_search) {
336 if (is_search) { 340 if (is_search) {
337 google_url_tracker_->SearchCommitted(); 341 google_url_tracker_->SearchCommitted();
338 // Note that the call above might not have actually registered a listener 342 // Note that the call above might not have actually registered a listener
339 // for navigation starts if the searchdomaincheck response was bogus. 343 // for navigation starts if the searchdomaincheck response was bogus.
340 } 344 }
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL); 1044 EXPECT_FALSE(GetInfoBarDelegate(&infobar_manager) == NULL);
1041 GoogleURLTrackerInfoBarDelegate* delegate2 = 1045 GoogleURLTrackerInfoBarDelegate* delegate2 =
1042 GetInfoBarDelegate(&infobar_manager2); 1046 GetInfoBarDelegate(&infobar_manager2);
1043 ASSERT_FALSE(delegate2 == NULL); 1047 ASSERT_FALSE(delegate2 == NULL);
1044 SetNavigationPending(&infobar_manager, true); 1048 SetNavigationPending(&infobar_manager, true);
1045 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true)); 1049 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, true));
1046 delegate2->Close(false); 1050 delegate2->Close(false);
1047 SetNavigationPending(&infobar_manager, false); 1051 SetNavigationPending(&infobar_manager, false);
1048 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false)); 1052 ASSERT_NO_FATAL_FAILURE(ExpectListeningForCommit(&infobar_manager, false));
1049 } 1053 }
OLDNEW
« no previous file with comments | « components/components_tests.gyp ('k') | components/infobars/test/infobar_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698