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 base::WeakPtrFactory<ManagedUserRegistrationUtilityTest> weak_ptr_factory_;
121 TestingProfile profile_;
122 ManagedUserSyncService* service_;
123 scoped_ptr<ManagedUserRegistrationUtility> registration_utility_;
125 // Owned by the ManagedUserSyncService.
126 MockChangeProcessor* change_processor_;
128 // A unique ID for creating "remote" Sync data.
131 // Whether OnManagedUserRegistered has been called.
132 bool received_callback_;
134 // Hold the registration result (either an error, or a token).
135 GoogleServiceAuthError error_;
139 ManagedUserRegistrationUtilityTest::ManagedUserRegistrationUtilityTest()
140 : weak_ptr_factory_(this),
141 change_processor_(NULL),
143 received_callback_(false),
144 error_(GoogleServiceAuthError::NUM_STATES) {
145 service_ = ManagedUserSyncServiceFactory::GetForProfile(&profile_);
148 ManagedUserRegistrationUtilityTest::~ManagedUserRegistrationUtilityTest() {
149 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
152 void ManagedUserRegistrationUtilityTest::TearDown() {
153 content::BrowserThread::GetBlockingPool()->FlushForTesting();
154 base::RunLoop().RunUntilIdle();
157 scoped_ptr<SyncChangeProcessor>
158 ManagedUserRegistrationUtilityTest::CreateChangeProcessor() {
159 EXPECT_FALSE(change_processor_);
160 change_processor_ = new MockChangeProcessor();
161 return scoped_ptr<SyncChangeProcessor>(change_processor_);
164 scoped_ptr<SyncErrorFactory>
165 ManagedUserRegistrationUtilityTest::CreateErrorFactory() {
166 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
169 SyncMergeResult ManagedUserRegistrationUtilityTest::StartInitialSync() {
170 SyncDataList initial_sync_data;
171 SyncMergeResult result =
172 service()->MergeDataAndStartSyncing(MANAGED_USERS,
174 CreateChangeProcessor(),
175 CreateErrorFactory());
176 EXPECT_FALSE(result.error().IsSet());
180 ManagedUserRegistrationUtility::RegistrationCallback
181 ManagedUserRegistrationUtilityTest::GetRegistrationCallback() {
183 &ManagedUserRegistrationUtilityTest::OnManagedUserRegistered,
184 weak_ptr_factory_.GetWeakPtr());
187 ManagedUserRegistrationUtility*
188 ManagedUserRegistrationUtilityTest::GetRegistrationUtility() {
189 if (registration_utility_.get())
190 return registration_utility_.get();
192 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher(
193 new MockManagedUserRefreshTokenFetcher);
194 registration_utility_.reset(
195 ManagedUserRegistrationUtility::CreateImpl(prefs(),
196 token_fetcher.Pass(),
198 return registration_utility_.get();
201 void ManagedUserRegistrationUtilityTest::Acknowledge() {
202 SyncChangeList new_changes;
203 const SyncChangeList& changes = change_processor()->changes();
204 for (SyncChangeList::const_iterator it = changes.begin(); it != changes.end();
206 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type());
207 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics();
208 EXPECT_FALSE(specifics.managed_user().acknowledged());
209 specifics.mutable_managed_user()->set_acknowledged(true);
210 new_changes.push_back(
211 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE,
212 SyncData::CreateRemoteData(++sync_data_id_,
216 service()->ProcessSyncChanges(FROM_HERE, new_changes);
221 void ManagedUserRegistrationUtilityTest::OnManagedUserRegistered(
222 const GoogleServiceAuthError& error,
223 const std::string& token) {
224 received_callback_ = true;
230 TEST_F(ManagedUserRegistrationUtilityTest, Register) {
232 GetRegistrationUtility()->Register(
233 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
234 ManagedUserRegistrationInfo(ASCIIToUTF16("Dug"), 0),
235 GetRegistrationCallback());
236 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
239 EXPECT_TRUE(received_callback());
240 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
241 EXPECT_FALSE(token().empty());
244 TEST_F(ManagedUserRegistrationUtilityTest, RegisterBeforeInitialSync) {
245 GetRegistrationUtility()->Register(
246 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
247 ManagedUserRegistrationInfo(ASCIIToUTF16("Nemo"), 5),
248 GetRegistrationCallback());
249 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
253 EXPECT_TRUE(received_callback());
254 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
255 EXPECT_FALSE(token().empty());
258 TEST_F(ManagedUserRegistrationUtilityTest, SyncServiceShutdownBeforeRegFinish) {
260 GetRegistrationUtility()->Register(
261 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
262 ManagedUserRegistrationInfo(ASCIIToUTF16("Remy"), 12),
263 GetRegistrationCallback());
264 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
265 service()->Shutdown();
266 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
267 EXPECT_TRUE(received_callback());
268 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
269 EXPECT_EQ(std::string(), token());
272 TEST_F(ManagedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
274 GetRegistrationUtility()->Register(
275 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
276 ManagedUserRegistrationInfo(ASCIIToUTF16("Mike"), 17),
277 GetRegistrationCallback());
278 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
279 service()->StopSyncing(MANAGED_USERS);
280 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
281 EXPECT_TRUE(received_callback());
282 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
283 EXPECT_EQ(std::string(), token());