Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / managed_mode / managed_user_registration_utility_unittest.cc
blobb0917d999b89004684cbd91310d8d6200916fc9c
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.
5 #include "base/bind.h"
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;
36 namespace {
38 const char kManagedUserToken[] = "managedusertoken";
40 class MockChangeProcessor : public SyncChangeProcessor {
41 public:
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
51 OVERRIDE {
52 return SyncDataList();
55 const SyncChangeList& changes() const { return change_list_; }
57 private:
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;
65 return SyncError();
68 class MockManagedUserRefreshTokenFetcher
69 : public ManagedUserRefreshTokenFetcher {
70 public:
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);
83 } // namespace
85 class ManagedUserRegistrationUtilityTest : public ::testing::Test {
86 public:
87 ManagedUserRegistrationUtilityTest();
88 virtual ~ManagedUserRegistrationUtilityTest();
90 virtual void TearDown() OVERRIDE;
92 protected:
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();
104 void Acknowledge();
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_; }
114 private:
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.
128 int64 sync_data_id_;
130 // Whether OnManagedUserRegistered has been called.
131 bool received_callback_;
133 // Hold the registration result (either an error, or a token).
134 GoogleServiceAuthError error_;
135 std::string token_;
137 base::WeakPtrFactory<ManagedUserRegistrationUtilityTest> weak_ptr_factory_;
140 ManagedUserRegistrationUtilityTest::ManagedUserRegistrationUtilityTest()
141 : change_processor_(NULL),
142 sync_data_id_(0),
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,
174 initial_sync_data,
175 CreateChangeProcessor(),
176 CreateErrorFactory());
177 EXPECT_FALSE(result.error().IsSet());
178 return result;
181 ManagedUserRegistrationUtility::RegistrationCallback
182 ManagedUserRegistrationUtilityTest::GetRegistrationCallback() {
183 return base::Bind(
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(),
198 service()));
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();
206 ++it) {
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_,
214 specifics,
215 base::Time())));
217 service()->ProcessSyncChanges(FROM_HERE, new_changes);
219 run_loop_.Run();
222 void ManagedUserRegistrationUtilityTest::OnManagedUserRegistered(
223 const GoogleServiceAuthError& error,
224 const std::string& token) {
225 received_callback_ = true;
226 error_ = error;
227 token_ = token;
228 run_loop_.Quit();
231 TEST_F(ManagedUserRegistrationUtilityTest, Register) {
232 StartInitialSync();
233 GetRegistrationUtility()->Register(
234 ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
235 ManagedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
236 GetRegistrationCallback());
237 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
238 Acknowledge();
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());
251 StartInitialSync();
252 Acknowledge();
254 EXPECT_TRUE(received_callback());
255 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
256 EXPECT_FALSE(token().empty());
259 TEST_F(ManagedUserRegistrationUtilityTest, SyncServiceShutdownBeforeRegFinish) {
260 StartInitialSync();
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) {
274 StartInitialSync();
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());