1 // Copyright 2013 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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/prefs/scoped_user_pref_update.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h"
12 #include "chrome/browser/managed_mode/managed_user_registration_utility.h"
13 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
14 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "google_apis/gaia/google_service_auth_error.h"
20 #include "sync/api/sync_change.h"
21 #include "sync/api/sync_error_factory_mock.h"
22 #include "sync/protocol/sync.pb.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using sync_pb::ManagedUserSpecifics
;
26 using syncer::MANAGED_USERS
;
27 using syncer::SyncChange
;
28 using syncer::SyncChangeList
;
29 using syncer::SyncChangeProcessor
;
30 using syncer::SyncData
;
31 using syncer::SyncDataList
;
32 using syncer::SyncError
;
33 using syncer::SyncErrorFactory
;
34 using syncer::SyncMergeResult
;
38 const char kManagedUserToken
[] = "managedusertoken";
40 class MockChangeProcessor
: public SyncChangeProcessor
{
42 MockChangeProcessor() {}
43 virtual ~MockChangeProcessor() {}
45 // SyncChangeProcessor implementation:
46 virtual SyncError
ProcessSyncChanges(
47 const tracked_objects::Location
& from_here
,
48 const SyncChangeList
& change_list
) OVERRIDE
;
50 virtual SyncDataList
GetAllSyncData(syncer::ModelType type
) const
52 return SyncDataList();
55 const SyncChangeList
& changes() const { return change_list_
; }
58 SyncChangeList change_list_
;
61 SyncError
MockChangeProcessor::ProcessSyncChanges(
62 const tracked_objects::Location
& from_here
,
63 const SyncChangeList
& change_list
) {
64 change_list_
= change_list
;
68 class MockManagedUserRefreshTokenFetcher
69 : public ManagedUserRefreshTokenFetcher
{
71 MockManagedUserRefreshTokenFetcher() {}
72 virtual ~MockManagedUserRefreshTokenFetcher() {}
74 // ManagedUserRefreshTokenFetcher implementation:
75 virtual void Start(const std::string
& managed_user_id
,
76 const std::string
& device_name
,
77 const TokenCallback
& callback
) OVERRIDE
{
78 GoogleServiceAuthError
error(GoogleServiceAuthError::NONE
);
79 callback
.Run(error
, kManagedUserToken
);
85 class ManagedUserRegistrationUtilityTest
: public ::testing::Test
{
87 ManagedUserRegistrationUtilityTest();
88 virtual ~ManagedUserRegistrationUtilityTest();
90 virtual void TearDown() OVERRIDE
;
93 scoped_ptr
<SyncChangeProcessor
> CreateChangeProcessor();
94 scoped_ptr
<SyncErrorFactory
> CreateErrorFactory();
95 SyncData
CreateRemoteData(const std::string
& id
, const std::string
& name
);
97 SyncMergeResult
StartInitialSync();
99 ManagedUserRegistrationUtility::RegistrationCallback
100 GetRegistrationCallback();
102 ManagedUserRegistrationUtility
* GetRegistrationUtility();
106 PrefService
* prefs() { return profile_
.GetTestingPrefService(); }
107 ManagedUserSyncService
* service() { return service_
; }
108 MockChangeProcessor
* change_processor() { return change_processor_
; }
110 bool received_callback() const { return received_callback_
; }
111 const GoogleServiceAuthError
& error() const { return error_
; }
112 const std::string
& token() const { return token_
; }
115 void OnManagedUserRegistered(const GoogleServiceAuthError
& error
,
116 const std::string
& token
);
118 base::MessageLoop message_loop_
;
119 base::RunLoop run_loop_
;
120 TestingProfile profile_
;
121 ManagedUserSyncService
* service_
;
122 scoped_ptr
<ManagedUserRegistrationUtility
> registration_utility_
;
124 // Owned by the ManagedUserSyncService.
125 MockChangeProcessor
* change_processor_
;
127 // A unique ID for creating "remote" Sync data.
130 // Whether OnManagedUserRegistered has been called.
131 bool received_callback_
;
133 // Hold the registration result (either an error, or a token).
134 GoogleServiceAuthError error_
;
137 base::WeakPtrFactory
<ManagedUserRegistrationUtilityTest
> weak_ptr_factory_
;
140 ManagedUserRegistrationUtilityTest::ManagedUserRegistrationUtilityTest()
141 : change_processor_(NULL
),
143 received_callback_(false),
144 error_(GoogleServiceAuthError::NUM_STATES
),
145 weak_ptr_factory_(this) {
146 service_
= ManagedUserSyncServiceFactory::GetForProfile(&profile_
);
149 ManagedUserRegistrationUtilityTest::~ManagedUserRegistrationUtilityTest() {
150 EXPECT_FALSE(weak_ptr_factory_
.HasWeakPtrs());
153 void ManagedUserRegistrationUtilityTest::TearDown() {
154 content::BrowserThread::GetBlockingPool()->FlushForTesting();
155 base::RunLoop().RunUntilIdle();
158 scoped_ptr
<SyncChangeProcessor
>
159 ManagedUserRegistrationUtilityTest::CreateChangeProcessor() {
160 EXPECT_FALSE(change_processor_
);
161 change_processor_
= new MockChangeProcessor();
162 return scoped_ptr
<SyncChangeProcessor
>(change_processor_
);
165 scoped_ptr
<SyncErrorFactory
>
166 ManagedUserRegistrationUtilityTest::CreateErrorFactory() {
167 return scoped_ptr
<SyncErrorFactory
>(new syncer::SyncErrorFactoryMock());
170 SyncMergeResult
ManagedUserRegistrationUtilityTest::StartInitialSync() {
171 SyncDataList initial_sync_data
;
172 SyncMergeResult result
=
173 service()->MergeDataAndStartSyncing(MANAGED_USERS
,
175 CreateChangeProcessor(),
176 CreateErrorFactory());
177 EXPECT_FALSE(result
.error().IsSet());
181 ManagedUserRegistrationUtility::RegistrationCallback
182 ManagedUserRegistrationUtilityTest::GetRegistrationCallback() {
184 &ManagedUserRegistrationUtilityTest::OnManagedUserRegistered
,
185 weak_ptr_factory_
.GetWeakPtr());
188 ManagedUserRegistrationUtility
*
189 ManagedUserRegistrationUtilityTest::GetRegistrationUtility() {
190 if (registration_utility_
.get())
191 return registration_utility_
.get();
193 scoped_ptr
<ManagedUserRefreshTokenFetcher
> token_fetcher(
194 new MockManagedUserRefreshTokenFetcher
);
195 registration_utility_
.reset(
196 ManagedUserRegistrationUtility::CreateImpl(prefs(),
197 token_fetcher
.Pass(),
199 return registration_utility_
.get();
202 void ManagedUserRegistrationUtilityTest::Acknowledge() {
203 SyncChangeList new_changes
;
204 const SyncChangeList
& changes
= change_processor()->changes();
205 for (SyncChangeList::const_iterator it
= changes
.begin(); it
!= changes
.end();
207 EXPECT_EQ(SyncChange::ACTION_ADD
, it
->change_type());
208 ::sync_pb::EntitySpecifics specifics
= it
->sync_data().GetSpecifics();
209 EXPECT_FALSE(specifics
.managed_user().acknowledged());
210 specifics
.mutable_managed_user()->set_acknowledged(true);
211 new_changes
.push_back(
212 SyncChange(FROM_HERE
, SyncChange::ACTION_UPDATE
,
213 SyncData::CreateRemoteData(++sync_data_id_
,
217 service()->ProcessSyncChanges(FROM_HERE
, new_changes
);
222 void ManagedUserRegistrationUtilityTest::OnManagedUserRegistered(
223 const GoogleServiceAuthError
& error
,
224 const std::string
& token
) {
225 received_callback_
= true;
231 TEST_F(ManagedUserRegistrationUtilityTest
, Register
) {
233 GetRegistrationUtility()->Register(
234 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
235 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
236 GetRegistrationCallback());
237 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers
)->size());
240 EXPECT_TRUE(received_callback());
241 EXPECT_EQ(GoogleServiceAuthError::NONE
, error().state());
242 EXPECT_FALSE(token().empty());
245 TEST_F(ManagedUserRegistrationUtilityTest
, RegisterBeforeInitialSync
) {
246 GetRegistrationUtility()->Register(
247 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
248 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Nemo"), 5),
249 GetRegistrationCallback());
250 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers
)->size());
254 EXPECT_TRUE(received_callback());
255 EXPECT_EQ(GoogleServiceAuthError::NONE
, error().state());
256 EXPECT_FALSE(token().empty());
259 TEST_F(ManagedUserRegistrationUtilityTest
, SyncServiceShutdownBeforeRegFinish
) {
261 GetRegistrationUtility()->Register(
262 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
263 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Remy"), 12),
264 GetRegistrationCallback());
265 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers
)->size());
266 service()->Shutdown();
267 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers
)->size());
268 EXPECT_TRUE(received_callback());
269 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
, error().state());
270 EXPECT_EQ(std::string(), token());
273 TEST_F(ManagedUserRegistrationUtilityTest
, StopSyncingBeforeRegFinish
) {
275 GetRegistrationUtility()->Register(
276 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
277 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Mike"), 17),
278 GetRegistrationCallback());
279 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers
)->size());
280 service()->StopSyncing(MANAGED_USERS
);
281 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers
)->size());
282 EXPECT_TRUE(received_callback());
283 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED
, error().state());
284 EXPECT_EQ(std::string(), token());