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

Side by Side Diff: chrome/browser/policy/cloud/user_policy_signin_service_unittest.cc

Issue 1257623002: Componentize FakeSigninManager and SigninManager prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 5 years, 4 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 (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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/prefs/pref_service.h" 7 #include "base/prefs/pref_service.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/thread_task_runner_handle.h" 9 #include "base/thread_task_runner_handle.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" 13 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
14 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h" 14 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h"
15 #include "chrome/browser/prefs/browser_prefs.h" 15 #include "chrome/browser/prefs/browser_prefs.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/signin/account_fetcher_service_factory.h" 17 #include "chrome/browser/signin/account_fetcher_service_factory.h"
18 #include "chrome/browser/signin/account_tracker_service_factory.h"
18 #include "chrome/browser/signin/chrome_signin_client_factory.h" 19 #include "chrome/browser/signin/chrome_signin_client_factory.h"
19 #include "chrome/browser/signin/fake_account_fetcher_service.h" 20 #include "chrome/browser/signin/fake_account_fetcher_service.h"
20 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" 21 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
21 #include "chrome/browser/signin/fake_signin_manager.h" 22 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_manager_factory.h" 24 #include "chrome/browser/signin/signin_manager_factory.h"
24 #include "chrome/browser/signin/test_signin_client_builder.h" 25 #include "chrome/browser/signin/test_signin_client_builder.h"
25 #include "chrome/test/base/testing_browser_process.h" 26 #include "chrome/test/base/testing_browser_process.h"
26 #include "chrome/test/base/testing_pref_service_syncable.h" 27 #include "chrome/test/base/testing_pref_service_syncable.h"
27 #include "chrome/test/base/testing_profile.h" 28 #include "chrome/test/base/testing_profile.h"
28 #include "components/policy/core/browser/browser_policy_connector.h" 29 #include "components/policy/core/browser/browser_policy_connector.h"
29 #include "components/policy/core/common/cloud/cloud_external_data_manager.h" 30 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
30 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 31 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
31 #include "components/policy/core/common/cloud/mock_device_management_service.h" 32 #include "components/policy/core/common/cloud/mock_device_management_service.h"
32 #include "components/policy/core/common/cloud/mock_user_cloud_policy_store.h" 33 #include "components/policy/core/common/cloud/mock_user_cloud_policy_store.h"
33 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h" 34 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
34 #include "components/policy/core/common/schema_registry.h" 35 #include "components/policy/core/common/schema_registry.h"
35 #include "components/signin/core/browser/account_tracker_service.h" 36 #include "components/signin/core/browser/account_tracker_service.h"
36 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" 37 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
38 #include "components/signin/core/browser/fake_signin_manager.h"
37 #include "components/signin/core/browser/signin_manager.h" 39 #include "components/signin/core/browser/signin_manager.h"
38 #include "content/public/browser/browser_context.h" 40 #include "content/public/browser/browser_context.h"
39 #include "content/public/browser/notification_details.h" 41 #include "content/public/browser/notification_details.h"
40 #include "content/public/browser/notification_service.h" 42 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/notification_source.h" 43 #include "content/public/browser/notification_source.h"
42 #include "content/public/test/test_browser_thread_bundle.h" 44 #include "content/public/test/test_browser_thread_bundle.h"
43 #include "google_apis/gaia/gaia_constants.h" 45 #include "google_apis/gaia/gaia_constants.h"
44 #include "google_apis/gaia/google_service_auth_error.h" 46 #include "google_apis/gaia/google_service_auth_error.h"
45 #include "net/http/http_status_code.h" 47 #include "net/http/http_status_code.h"
46 #include "net/url_request/test_url_fetcher_factory.h" 48 #include "net/url_request/test_url_fetcher_factory.h"
(...skipping 29 matching lines...) Expand all
76 " \"expires_in\": 3600," 78 " \"expires_in\": 3600,"
77 " \"token_type\": \"Bearer\"" 79 " \"token_type\": \"Bearer\""
78 "}"; 80 "}";
79 #endif 81 #endif
80 82
81 const char kHostedDomainResponse[] = 83 const char kHostedDomainResponse[] =
82 "{" 84 "{"
83 " \"hd\": \"test.com\"" 85 " \"hd\": \"test.com\""
84 "}"; 86 "}";
85 87
86 class SigninManagerFake : public FakeSigninManager { 88 scoped_ptr<KeyedService> BuildFakeSigninManager(
87 public: 89 content::BrowserContext* context) {
88 explicit SigninManagerFake(Profile* profile) 90 Profile* profile = static_cast<Profile*>(context);
89 : FakeSigninManager(profile) { 91 scoped_ptr<FakeSigninManager> manager(new FakeSigninManager(
90 Initialize(NULL); 92 ChromeSigninClientFactory::GetForProfile(profile),
91 } 93 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
92 94 AccountTrackerServiceFactory::GetForProfile(profile),
93 void ForceSignOut() { 95 GaiaCookieManagerServiceFactory::GetForProfile(profile)));
94 // Allow signing out now. 96 manager->Initialize(nullptr);
95 prohibit_signout_ = false; 97 return manager.Pass();
96 SignOut(signin_metrics::SIGNOUT_TEST); 98 }
Roger Tawa OOO till Jul 10th 2015/07/24 14:46:50 Nit: not possible to use BuildFakeSigninManagerBas
droger 2015/07/24 14:57:03 I don't know. BuildFakeSigninManagerBase calls Not
droger 2015/07/24 15:03:19 BuildFakeSigninManagerBase also returns a SigninMa
97 }
98
99 static scoped_ptr<KeyedService> Build(content::BrowserContext* profile) {
100 return make_scoped_ptr(
101 new SigninManagerFake(static_cast<Profile*>(profile)));
102 }
103 };
104 99
105 UserCloudPolicyManager* BuildCloudPolicyManager( 100 UserCloudPolicyManager* BuildCloudPolicyManager(
106 content::BrowserContext* context) { 101 content::BrowserContext* context) {
107 MockUserCloudPolicyStore* store = new MockUserCloudPolicyStore(); 102 MockUserCloudPolicyStore* store = new MockUserCloudPolicyStore();
108 EXPECT_CALL(*store, Load()).Times(AnyNumber()); 103 EXPECT_CALL(*store, Load()).Times(AnyNumber());
109 104
110 return new UserCloudPolicyManager( 105 return new UserCloudPolicyManager(
111 scoped_ptr<UserCloudPolicyStore>(store), 106 scoped_ptr<UserCloudPolicyStore>(store),
112 base::FilePath(), 107 base::FilePath(),
113 scoped_ptr<CloudExternalDataManager>(), 108 scoped_ptr<CloudExternalDataManager>(),
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // UserCloudPolicyManagerFactory isn't a real 171 // UserCloudPolicyManagerFactory isn't a real
177 // BrowserContextKeyedServiceFactory (it derives from 172 // BrowserContextKeyedServiceFactory (it derives from
178 // BrowserContextKeyedBaseFactory and exposes its own APIs to get 173 // BrowserContextKeyedBaseFactory and exposes its own APIs to get
179 // instances) so we have to inject our testing factory via a special 174 // instances) so we have to inject our testing factory via a special
180 // API before creating the profile. 175 // API before creating the profile.
181 UserCloudPolicyManagerFactory::GetInstance()->RegisterTestingFactory( 176 UserCloudPolicyManagerFactory::GetInstance()->RegisterTestingFactory(
182 BuildCloudPolicyManager); 177 BuildCloudPolicyManager);
183 TestingProfile::Builder builder; 178 TestingProfile::Builder builder;
184 builder.SetPrefService(scoped_ptr<PrefServiceSyncable>(prefs.Pass())); 179 builder.SetPrefService(scoped_ptr<PrefServiceSyncable>(prefs.Pass()));
185 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), 180 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
186 SigninManagerFake::Build); 181 BuildFakeSigninManager);
187 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), 182 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
188 BuildFakeProfileOAuth2TokenService); 183 BuildFakeProfileOAuth2TokenService);
189 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(), 184 builder.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
190 FakeAccountFetcherService::BuildForTests); 185 FakeAccountFetcherService::BuildForTests);
191 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(), 186 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
192 signin::BuildTestSigninClient); 187 signin::BuildTestSigninClient);
193 188
194 profile_ = builder.Build().Pass(); 189 profile_ = builder.Build().Pass();
195 url_factory_.set_remove_fetcher_on_delete(true); 190 url_factory_.set_remove_fetcher_on_delete(true);
196 191
197 signin_manager_ = static_cast<SigninManagerFake*>( 192 signin_manager_ = static_cast<FakeSigninManager*>(
198 SigninManagerFactory::GetForProfile(profile_.get())); 193 SigninManagerFactory::GetForProfile(profile_.get()));
199 // Tests are responsible for freeing the UserCloudPolicyManager instances 194 // Tests are responsible for freeing the UserCloudPolicyManager instances
200 // they inject. 195 // they inject.
201 manager_.reset(UserCloudPolicyManagerFactory::GetForBrowserContext( 196 manager_.reset(UserCloudPolicyManagerFactory::GetForBrowserContext(
202 profile_.get())); 197 profile_.get()));
203 manager_->Init(&schema_registry_); 198 manager_->Init(&schema_registry_);
204 mock_store_ = static_cast<MockUserCloudPolicyStore*>( 199 mock_store_ = static_cast<MockUserCloudPolicyStore*>(
205 manager_->core()->store()); 200 manager_->core()->store());
206 DCHECK(mock_store_); 201 DCHECK(mock_store_);
207 AddProfile(); 202 AddProfile();
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 MockUserCloudPolicyStore* mock_store_; // Not owned. 357 MockUserCloudPolicyStore* mock_store_; // Not owned.
363 SchemaRegistry schema_registry_; 358 SchemaRegistry schema_registry_;
364 scoped_ptr<UserCloudPolicyManager> manager_; 359 scoped_ptr<UserCloudPolicyManager> manager_;
365 360
366 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free 361 // BrowserPolicyConnector and UrlFetcherFactory want to initialize and free
367 // various components asynchronously via tasks, so create fake threads here. 362 // various components asynchronously via tasks, so create fake threads here.
368 content::TestBrowserThreadBundle thread_bundle_; 363 content::TestBrowserThreadBundle thread_bundle_;
369 364
370 net::TestURLFetcherFactory url_factory_; 365 net::TestURLFetcherFactory url_factory_;
371 366
372 SigninManagerFake* signin_manager_; 367 FakeSigninManager* signin_manager_;
373 368
374 // Used in conjunction with OnRegisterCompleted() to test client registration 369 // Used in conjunction with OnRegisterCompleted() to test client registration
375 // callbacks. 370 // callbacks.
376 std::string dm_token_; 371 std::string dm_token_;
377 std::string client_id_; 372 std::string client_id_;
378 373
379 // True if OnRegisterCompleted() was called. 374 // True if OnRegisterCompleted() was called.
380 bool register_completed_; 375 bool register_completed_;
381 376
382 // Weak ptr to the MockDeviceManagementService (object is owned by the 377 // Weak ptr to the MockDeviceManagementService (object is owned by the
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 base::RunLoop().RunUntilIdle(); 827 base::RunLoop().RunUntilIdle();
833 EXPECT_FALSE(manager_->IsClientRegistered()); 828 EXPECT_FALSE(manager_->IsClientRegistered());
834 #if !defined(OS_ANDROID) 829 #if !defined(OS_ANDROID)
835 EXPECT_FALSE(signin_manager_->IsSignoutProhibited()); 830 EXPECT_FALSE(signin_manager_->IsSignoutProhibited());
836 #endif 831 #endif
837 } 832 }
838 833
839 } // namespace 834 } // namespace
840 835
841 } // namespace policy 836 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698