1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/supervised_user/supervised_user_registration_utility.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/supervised_user/supervised_user_refresh_token_fetcher.h"
14 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service.h"
15 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service_factory.h"
16 #include "chrome/browser/supervised_user/supervised_user_sync_service.h"
17 #include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/testing_pref_service_syncable.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_utils.h"
23 #include "google_apis/gaia/google_service_auth_error.h"
24 #include "sync/api/attachments/attachment_id.h"
25 #include "sync/api/sync_change.h"
26 #include "sync/api/sync_error_factory_mock.h"
27 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
28 #include "sync/protocol/sync.pb.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 using sync_pb::ManagedUserSpecifics
;
32 using syncer::SUPERVISED_USERS
;
33 using syncer::SyncChange
;
34 using syncer::SyncChangeList
;
35 using syncer::SyncChangeProcessor
;
36 using syncer::SyncData
;
37 using syncer::SyncDataList
;
38 using syncer::SyncError
;
39 using syncer::SyncErrorFactory
;
40 using syncer::SyncMergeResult
;
44 const char kSupervisedUserToken
[] = "supervisedusertoken";
46 class MockChangeProcessor
: public SyncChangeProcessor
{
48 MockChangeProcessor() {}
49 virtual ~MockChangeProcessor() {}
51 // SyncChangeProcessor implementation:
52 virtual SyncError
ProcessSyncChanges(
53 const tracked_objects::Location
& from_here
,
54 const SyncChangeList
& change_list
) OVERRIDE
;
56 virtual SyncDataList
GetAllSyncData(syncer::ModelType type
) const
58 return SyncDataList();
61 const SyncChangeList
& changes() const { return change_list_
; }
64 SyncChangeList change_list_
;
67 SyncError
MockChangeProcessor::ProcessSyncChanges(
68 const tracked_objects::Location
& from_here
,
69 const SyncChangeList
& change_list
) {
70 change_list_
= change_list
;
74 class MockSupervisedUserRefreshTokenFetcher
75 : public SupervisedUserRefreshTokenFetcher
{
77 MockSupervisedUserRefreshTokenFetcher() {}
78 virtual ~MockSupervisedUserRefreshTokenFetcher() {}
80 // SupervisedUserRefreshTokenFetcher implementation:
81 virtual void Start(const std::string
& supervised_user_id
,
82 const std::string
& device_name
,
83 const TokenCallback
& callback
) OVERRIDE
{
84 GoogleServiceAuthError
error(GoogleServiceAuthError::NONE
);
85 callback
.Run(error
, kSupervisedUserToken
);
91 class SupervisedUserRegistrationUtilityTest
: public ::testing::Test
{
93 SupervisedUserRegistrationUtilityTest();
94 virtual ~SupervisedUserRegistrationUtilityTest();
96 virtual void TearDown() OVERRIDE
;
99 scoped_ptr
<SyncChangeProcessor
> CreateChangeProcessor();
100 scoped_ptr
<SyncErrorFactory
> CreateErrorFactory();
101 SyncData
CreateRemoteData(const std::string
& id
, const std::string
& name
);
103 SyncMergeResult
StartInitialSync();
105 SupervisedUserRegistrationUtility::RegistrationCallback
106 GetRegistrationCallback();
108 SupervisedUserRegistrationUtility
* GetRegistrationUtility();
112 PrefService
* prefs() { return profile_
.GetTestingPrefService(); }
113 SupervisedUserSyncService
* service() { return service_
; }
114 SupervisedUserSharedSettingsService
* shared_settings_service() {
115 return shared_settings_service_
;
117 MockChangeProcessor
* change_processor() { return change_processor_
; }
119 bool received_callback() const { return received_callback_
; }
120 const GoogleServiceAuthError
& error() const { return error_
; }
121 const std::string
& token() const { return token_
; }
124 void OnSupervisedUserRegistered(const GoogleServiceAuthError
& error
,
125 const std::string
& token
);
127 base::MessageLoop message_loop_
;
128 base::RunLoop run_loop_
;
129 TestingProfile profile_
;
130 SupervisedUserSyncService
* service_
;
131 SupervisedUserSharedSettingsService
* shared_settings_service_
;
132 scoped_ptr
<SupervisedUserRegistrationUtility
> registration_utility_
;
134 // Owned by the SupervisedUserSyncService.
135 MockChangeProcessor
* change_processor_
;
137 // A unique ID for creating "remote" Sync data.
140 // Whether OnSupervisedUserRegistered has been called.
141 bool received_callback_
;
143 // Hold the registration result (either an error, or a token).
144 GoogleServiceAuthError error_
;
147 base::WeakPtrFactory
<SupervisedUserRegistrationUtilityTest
> weak_ptr_factory_
;
150 SupervisedUserRegistrationUtilityTest::SupervisedUserRegistrationUtilityTest()
151 : change_processor_(NULL
),
153 received_callback_(false),
154 error_(GoogleServiceAuthError::NUM_STATES
),
155 weak_ptr_factory_(this) {
156 service_
= SupervisedUserSyncServiceFactory::GetForProfile(&profile_
);
157 shared_settings_service_
=
158 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
162 SupervisedUserRegistrationUtilityTest::
163 ~SupervisedUserRegistrationUtilityTest() {
164 EXPECT_FALSE(weak_ptr_factory_
.HasWeakPtrs());
167 void SupervisedUserRegistrationUtilityTest::TearDown() {
168 content::RunAllBlockingPoolTasksUntilIdle();
171 scoped_ptr
<SyncChangeProcessor
>
172 SupervisedUserRegistrationUtilityTest::CreateChangeProcessor() {
173 EXPECT_FALSE(change_processor_
);
174 change_processor_
= new MockChangeProcessor();
175 return scoped_ptr
<SyncChangeProcessor
>(change_processor_
);
178 scoped_ptr
<SyncErrorFactory
>
179 SupervisedUserRegistrationUtilityTest::CreateErrorFactory() {
180 return scoped_ptr
<SyncErrorFactory
>(new syncer::SyncErrorFactoryMock());
183 SyncMergeResult
SupervisedUserRegistrationUtilityTest::StartInitialSync() {
184 SyncDataList initial_sync_data
;
185 SyncMergeResult result
=
186 service()->MergeDataAndStartSyncing(SUPERVISED_USERS
,
188 CreateChangeProcessor(),
189 CreateErrorFactory());
190 EXPECT_FALSE(result
.error().IsSet());
194 SupervisedUserRegistrationUtility::RegistrationCallback
195 SupervisedUserRegistrationUtilityTest::GetRegistrationCallback() {
197 &SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered
,
198 weak_ptr_factory_
.GetWeakPtr());
201 SupervisedUserRegistrationUtility
*
202 SupervisedUserRegistrationUtilityTest::GetRegistrationUtility() {
203 if (registration_utility_
.get())
204 return registration_utility_
.get();
206 scoped_ptr
<SupervisedUserRefreshTokenFetcher
> token_fetcher(
207 new MockSupervisedUserRefreshTokenFetcher
);
208 registration_utility_
.reset(
209 SupervisedUserRegistrationUtility::CreateImpl(prefs(),
210 token_fetcher
.Pass(),
212 shared_settings_service()));
213 return registration_utility_
.get();
216 void SupervisedUserRegistrationUtilityTest::Acknowledge() {
217 SyncChangeList new_changes
;
218 const SyncChangeList
& changes
= change_processor()->changes();
219 for (SyncChangeList::const_iterator it
= changes
.begin(); it
!= changes
.end();
221 EXPECT_EQ(SyncChange::ACTION_ADD
, it
->change_type());
222 ::sync_pb::EntitySpecifics specifics
= it
->sync_data().GetSpecifics();
223 EXPECT_FALSE(specifics
.managed_user().acknowledged());
224 specifics
.mutable_managed_user()->set_acknowledged(true);
225 new_changes
.push_back(
226 SyncChange(FROM_HERE
,
227 SyncChange::ACTION_UPDATE
,
228 SyncData::CreateRemoteData(
232 syncer::AttachmentIdList(),
233 syncer::AttachmentServiceProxyForTest::Create())));
235 service()->ProcessSyncChanges(FROM_HERE
, new_changes
);
240 void SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered(
241 const GoogleServiceAuthError
& error
,
242 const std::string
& token
) {
243 received_callback_
= true;
249 TEST_F(SupervisedUserRegistrationUtilityTest
, Register
) {
251 GetRegistrationUtility()->Register(
252 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
253 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
254 GetRegistrationCallback());
255 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers
)->size());
258 EXPECT_TRUE(received_callback());
259 EXPECT_EQ(GoogleServiceAuthError::NONE
, error().state());
260 EXPECT_FALSE(token().empty());
263 TEST_F(SupervisedUserRegistrationUtilityTest
, RegisterBeforeInitialSync
) {
264 GetRegistrationUtility()->Register(
265 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
266 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Nemo"), 5),
267 GetRegistrationCallback());
268 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers
)->size());
272 EXPECT_TRUE(received_callback());
273 EXPECT_EQ(GoogleServiceAuthError::NONE
, error().state());
274 EXPECT_FALSE(token().empty());
277 TEST_F(SupervisedUserRegistrationUtilityTest
,
278 SyncServiceShutdownBeforeRegFinish
) {
280 GetRegistrationUtility()->Register(
281 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
282 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Remy"), 12),
283 GetRegistrationCallback());
284 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers
)->size());
285 service()->Shutdown();
286 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers
)->size());
287 EXPECT_TRUE(received_callback());
288 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
, error().state());
289 EXPECT_EQ(std::string(), token());
292 TEST_F(SupervisedUserRegistrationUtilityTest
, StopSyncingBeforeRegFinish
) {
294 GetRegistrationUtility()->Register(
295 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
296 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Mike"), 17),
297 GetRegistrationCallback());
298 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers
)->size());
299 service()->StopSyncing(SUPERVISED_USERS
);
300 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers
)->size());
301 EXPECT_TRUE(received_callback());
302 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
, error().state());
303 EXPECT_EQ(std::string(), token());