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_shared_settings_service.h"
14 #include "chrome/browser/managed_mode/managed_user_shared_settings_service_factory.h"
15 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
16 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/testing_pref_service_syncable.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "google_apis/gaia/google_service_auth_error.h"
22 #include "sync/api/attachments/attachment_id.h"
23 #include "sync/api/attachments/attachment_service_proxy_for_test.h"
24 #include "sync/api/sync_change.h"
25 #include "sync/api/sync_error_factory_mock.h"
26 #include "sync/protocol/sync.pb.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using sync_pb::ManagedUserSpecifics;
30 using syncer::MANAGED_USERS;
31 using syncer::SyncChange;
32 using syncer::SyncChangeList;
33 using syncer::SyncChangeProcessor;
34 using syncer::SyncData;
35 using syncer::SyncDataList;
36 using syncer::SyncError;
37 using syncer::SyncErrorFactory;
38 using syncer::SyncMergeResult;
42 const char kManagedUserToken[] = "managedusertoken";
44 class MockChangeProcessor : public SyncChangeProcessor {
46 MockChangeProcessor() {}
47 virtual ~MockChangeProcessor() {}
49 // SyncChangeProcessor implementation:
50 virtual SyncError ProcessSyncChanges(
51 const tracked_objects::Location& from_here,
52 const SyncChangeList& change_list) OVERRIDE;
54 virtual SyncDataList GetAllSyncData(syncer::ModelType type) const
56 return SyncDataList();
59 const SyncChangeList& changes() const { return change_list_; }
62 SyncChangeList change_list_;
65 SyncError MockChangeProcessor::ProcessSyncChanges(
66 const tracked_objects::Location& from_here,
67 const SyncChangeList& change_list) {
68 change_list_ = change_list;
72 class MockManagedUserRefreshTokenFetcher
73 : public ManagedUserRefreshTokenFetcher {
75 MockManagedUserRefreshTokenFetcher() {}
76 virtual ~MockManagedUserRefreshTokenFetcher() {}
78 // ManagedUserRefreshTokenFetcher implementation:
79 virtual void Start(const std::string& managed_user_id,
80 const std::string& device_name,
81 const TokenCallback& callback) OVERRIDE {
82 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
83 callback.Run(error, kManagedUserToken);
89 class ManagedUserRegistrationUtilityTest : public ::testing::Test {
91 ManagedUserRegistrationUtilityTest();
92 virtual ~ManagedUserRegistrationUtilityTest();
94 virtual void TearDown() OVERRIDE;
97 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
98 scoped_ptr<SyncErrorFactory> CreateErrorFactory();
99 SyncData CreateRemoteData(const std::string& id, const std::string& name);
101 SyncMergeResult StartInitialSync();
103 ManagedUserRegistrationUtility::RegistrationCallback
104 GetRegistrationCallback();
106 ManagedUserRegistrationUtility* GetRegistrationUtility();
110 PrefService* prefs() { return profile_.GetTestingPrefService(); }
111 ManagedUserSyncService* service() { return service_; }
112 ManagedUserSharedSettingsService* shared_settings_service() {
113 return shared_settings_service_;
115 MockChangeProcessor* change_processor() { return change_processor_; }
117 bool received_callback() const { return received_callback_; }
118 const GoogleServiceAuthError& error() const { return error_; }
119 const std::string& token() const { return token_; }
122 void OnManagedUserRegistered(const GoogleServiceAuthError& error,
123 const std::string& token);
125 base::MessageLoop message_loop_;
126 base::RunLoop run_loop_;
127 TestingProfile profile_;
128 ManagedUserSyncService* service_;
129 ManagedUserSharedSettingsService* shared_settings_service_;
130 scoped_ptr<ManagedUserRegistrationUtility> registration_utility_;
132 // Owned by the ManagedUserSyncService.
133 MockChangeProcessor* change_processor_;
135 // A unique ID for creating "remote" Sync data.
138 // Whether OnManagedUserRegistered has been called.
139 bool received_callback_;
141 // Hold the registration result (either an error, or a token).
142 GoogleServiceAuthError error_;
145 base::WeakPtrFactory<ManagedUserRegistrationUtilityTest> weak_ptr_factory_;
148 ManagedUserRegistrationUtilityTest::ManagedUserRegistrationUtilityTest()
149 : change_processor_(NULL),
151 received_callback_(false),
152 error_(GoogleServiceAuthError::NUM_STATES),
153 weak_ptr_factory_(this) {
154 service_ = ManagedUserSyncServiceFactory::GetForProfile(&profile_);
155 shared_settings_service_ =
156 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(&profile_);
159 ManagedUserRegistrationUtilityTest::~ManagedUserRegistrationUtilityTest() {
160 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
163 void ManagedUserRegistrationUtilityTest::TearDown() {
164 content::BrowserThread::GetBlockingPool()->FlushForTesting();
165 base::RunLoop().RunUntilIdle();
168 scoped_ptr<SyncChangeProcessor>
169 ManagedUserRegistrationUtilityTest::CreateChangeProcessor() {
170 EXPECT_FALSE(change_processor_);
171 change_processor_ = new MockChangeProcessor();
172 return scoped_ptr<SyncChangeProcessor>(change_processor_);
175 scoped_ptr<SyncErrorFactory>
176 ManagedUserRegistrationUtilityTest::CreateErrorFactory() {
177 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
180 SyncMergeResult ManagedUserRegistrationUtilityTest::StartInitialSync() {
181 SyncDataList initial_sync_data;
182 SyncMergeResult result =
183 service()->MergeDataAndStartSyncing(MANAGED_USERS,
185 CreateChangeProcessor(),
186 CreateErrorFactory());
187 EXPECT_FALSE(result.error().IsSet());
191 ManagedUserRegistrationUtility::RegistrationCallback
192 ManagedUserRegistrationUtilityTest::GetRegistrationCallback() {
194 &ManagedUserRegistrationUtilityTest::OnManagedUserRegistered,
195 weak_ptr_factory_.GetWeakPtr());
198 ManagedUserRegistrationUtility*
199 ManagedUserRegistrationUtilityTest::GetRegistrationUtility() {
200 if (registration_utility_.get())
201 return registration_utility_.get();
203 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher(
204 new MockManagedUserRefreshTokenFetcher);
205 registration_utility_.reset(
206 ManagedUserRegistrationUtility::CreateImpl(prefs(),
207 token_fetcher.Pass(),
209 shared_settings_service()));
210 return registration_utility_.get();
213 void ManagedUserRegistrationUtilityTest::Acknowledge() {
214 SyncChangeList new_changes;
215 const SyncChangeList& changes = change_processor()->changes();
216 for (SyncChangeList::const_iterator it = changes.begin(); it != changes.end();
218 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type());
219 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics();
220 EXPECT_FALSE(specifics.managed_user().acknowledged());
221 specifics.mutable_managed_user()->set_acknowledged(true);
222 new_changes.push_back(
223 SyncChange(FROM_HERE,
224 SyncChange::ACTION_UPDATE,
225 SyncData::CreateRemoteData(
229 syncer::AttachmentIdList(),
230 syncer::AttachmentServiceProxyForTest::Create())));
232 service()->ProcessSyncChanges(FROM_HERE, new_changes);
237 void ManagedUserRegistrationUtilityTest::OnManagedUserRegistered(
238 const GoogleServiceAuthError& error,
239 const std::string& token) {
240 received_callback_ = true;
246 TEST_F(ManagedUserRegistrationUtilityTest, Register) {
248 GetRegistrationUtility()->Register(
249 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
250 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
251 GetRegistrationCallback());
252 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
255 EXPECT_TRUE(received_callback());
256 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
257 EXPECT_FALSE(token().empty());
260 TEST_F(ManagedUserRegistrationUtilityTest, RegisterBeforeInitialSync) {
261 GetRegistrationUtility()->Register(
262 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
263 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Nemo"), 5),
264 GetRegistrationCallback());
265 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
269 EXPECT_TRUE(received_callback());
270 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
271 EXPECT_FALSE(token().empty());
274 TEST_F(ManagedUserRegistrationUtilityTest, SyncServiceShutdownBeforeRegFinish) {
276 GetRegistrationUtility()->Register(
277 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
278 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Remy"), 12),
279 GetRegistrationCallback());
280 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
281 service()->Shutdown();
282 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
283 EXPECT_TRUE(received_callback());
284 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
285 EXPECT_EQ(std::string(), token());
288 TEST_F(ManagedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
290 GetRegistrationUtility()->Register(
291 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
292 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Mike"), 17),
293 GetRegistrationCallback());
294 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
295 service()->StopSyncing(MANAGED_USERS);
296 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
297 EXPECT_TRUE(received_callback());
298 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
299 EXPECT_EQ(std::string(), token());