Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / supervised_user / legacy / supervised_user_registration_utility_unittest.cc
blobb9a8ab2e0a515b2eb0b2dd6e609146df9bcbb3eb
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/legacy/supervised_user_registration_utility.h"
7 #include "base/bind.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/legacy/supervised_user_refresh_token_fetcher.h"
14 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
15 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service_factory.h"
16 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h"
17 #include "chrome/browser/supervised_user/legacy/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;
42 namespace {
44 const char kSupervisedUserToken[] = "supervisedusertoken";
46 class MockChangeProcessor : public SyncChangeProcessor {
47 public:
48 MockChangeProcessor() {}
49 ~MockChangeProcessor() override {}
51 // SyncChangeProcessor implementation:
52 SyncError ProcessSyncChanges(const tracked_objects::Location& from_here,
53 const SyncChangeList& change_list) override;
55 SyncDataList GetAllSyncData(syncer::ModelType type) const override {
56 return SyncDataList();
59 const SyncChangeList& changes() const { return change_list_; }
61 private:
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;
69 return SyncError();
72 class MockSupervisedUserRefreshTokenFetcher
73 : public SupervisedUserRefreshTokenFetcher {
74 public:
75 MockSupervisedUserRefreshTokenFetcher() {}
76 ~MockSupervisedUserRefreshTokenFetcher() override {}
78 // SupervisedUserRefreshTokenFetcher implementation:
79 void Start(const std::string& supervised_user_id,
80 const std::string& device_name,
81 const TokenCallback& callback) override {
82 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
83 callback.Run(error, kSupervisedUserToken);
87 } // namespace
89 class SupervisedUserRegistrationUtilityTest : public ::testing::Test {
90 public:
91 SupervisedUserRegistrationUtilityTest();
92 ~SupervisedUserRegistrationUtilityTest() override;
94 void TearDown() override;
96 protected:
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 SupervisedUserRegistrationUtility::RegistrationCallback
104 GetRegistrationCallback();
106 SupervisedUserRegistrationUtility* GetRegistrationUtility();
108 void Acknowledge();
110 PrefService* prefs() { return profile_.GetTestingPrefService(); }
111 SupervisedUserSyncService* service() { return service_; }
112 SupervisedUserSharedSettingsService* 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_; }
121 private:
122 void OnSupervisedUserRegistered(const GoogleServiceAuthError& error,
123 const std::string& token);
125 base::MessageLoop message_loop_;
126 base::RunLoop run_loop_;
127 TestingProfile profile_;
128 SupervisedUserSyncService* service_;
129 SupervisedUserSharedSettingsService* shared_settings_service_;
130 scoped_ptr<SupervisedUserRegistrationUtility> registration_utility_;
132 // Owned by the SupervisedUserSyncService.
133 MockChangeProcessor* change_processor_;
135 // A unique ID for creating "remote" Sync data.
136 int64 sync_data_id_;
138 // Whether OnSupervisedUserRegistered has been called.
139 bool received_callback_;
141 // Hold the registration result (either an error, or a token).
142 GoogleServiceAuthError error_;
143 std::string token_;
145 base::WeakPtrFactory<SupervisedUserRegistrationUtilityTest> weak_ptr_factory_;
148 SupervisedUserRegistrationUtilityTest::SupervisedUserRegistrationUtilityTest()
149 : change_processor_(NULL),
150 sync_data_id_(0),
151 received_callback_(false),
152 error_(GoogleServiceAuthError::NUM_STATES),
153 weak_ptr_factory_(this) {
154 service_ = SupervisedUserSyncServiceFactory::GetForProfile(&profile_);
155 shared_settings_service_ =
156 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
157 &profile_);
160 SupervisedUserRegistrationUtilityTest::
161 ~SupervisedUserRegistrationUtilityTest() {
162 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
165 void SupervisedUserRegistrationUtilityTest::TearDown() {
166 content::RunAllBlockingPoolTasksUntilIdle();
169 scoped_ptr<SyncChangeProcessor>
170 SupervisedUserRegistrationUtilityTest::CreateChangeProcessor() {
171 EXPECT_FALSE(change_processor_);
172 change_processor_ = new MockChangeProcessor();
173 return scoped_ptr<SyncChangeProcessor>(change_processor_);
176 scoped_ptr<SyncErrorFactory>
177 SupervisedUserRegistrationUtilityTest::CreateErrorFactory() {
178 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
181 SyncMergeResult SupervisedUserRegistrationUtilityTest::StartInitialSync() {
182 SyncDataList initial_sync_data;
183 SyncMergeResult result =
184 service()->MergeDataAndStartSyncing(SUPERVISED_USERS,
185 initial_sync_data,
186 CreateChangeProcessor(),
187 CreateErrorFactory());
188 EXPECT_FALSE(result.error().IsSet());
189 return result;
192 SupervisedUserRegistrationUtility::RegistrationCallback
193 SupervisedUserRegistrationUtilityTest::GetRegistrationCallback() {
194 return base::Bind(
195 &SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered,
196 weak_ptr_factory_.GetWeakPtr());
199 SupervisedUserRegistrationUtility*
200 SupervisedUserRegistrationUtilityTest::GetRegistrationUtility() {
201 if (registration_utility_.get())
202 return registration_utility_.get();
204 scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher(
205 new MockSupervisedUserRefreshTokenFetcher);
206 registration_utility_.reset(
207 SupervisedUserRegistrationUtility::CreateImpl(prefs(),
208 token_fetcher.Pass(),
209 service(),
210 shared_settings_service()));
211 return registration_utility_.get();
214 void SupervisedUserRegistrationUtilityTest::Acknowledge() {
215 SyncChangeList new_changes;
216 for (const SyncChange& sync_change : change_processor()->changes()) {
217 EXPECT_EQ(SyncChange::ACTION_ADD, sync_change.change_type());
218 ::sync_pb::EntitySpecifics specifics =
219 sync_change.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(
226 ++sync_data_id_,
227 specifics,
228 base::Time(),
229 syncer::AttachmentIdList(),
230 syncer::AttachmentServiceProxyForTest::Create())));
232 service()->ProcessSyncChanges(FROM_HERE, new_changes);
234 run_loop_.Run();
237 void SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered(
238 const GoogleServiceAuthError& error,
239 const std::string& token) {
240 received_callback_ = true;
241 error_ = error;
242 token_ = token;
243 run_loop_.Quit();
246 TEST_F(SupervisedUserRegistrationUtilityTest, Register) {
247 StartInitialSync();
248 GetRegistrationUtility()->Register(
249 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
250 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
251 GetRegistrationCallback());
252 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
253 Acknowledge();
255 EXPECT_TRUE(received_callback());
256 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
257 EXPECT_FALSE(token().empty());
260 TEST_F(SupervisedUserRegistrationUtilityTest, RegisterBeforeInitialSync) {
261 GetRegistrationUtility()->Register(
262 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
263 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Nemo"), 5),
264 GetRegistrationCallback());
265 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
266 StartInitialSync();
267 Acknowledge();
269 EXPECT_TRUE(received_callback());
270 EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
271 EXPECT_FALSE(token().empty());
274 TEST_F(SupervisedUserRegistrationUtilityTest,
275 SyncServiceShutdownBeforeRegFinish) {
276 StartInitialSync();
277 GetRegistrationUtility()->Register(
278 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
279 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Remy"), 12),
280 GetRegistrationCallback());
281 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
282 service()->Shutdown();
283 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
284 EXPECT_TRUE(received_callback());
285 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
286 EXPECT_EQ(std::string(), token());
289 TEST_F(SupervisedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
290 StartInitialSync();
291 GetRegistrationUtility()->Register(
292 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
293 SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Mike"), 17),
294 GetRegistrationCallback());
295 EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
296 service()->StopSyncing(SUPERVISED_USERS);
297 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
298 EXPECT_TRUE(received_callback());
299 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
300 EXPECT_EQ(std::string(), token());