Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / managed_mode / managed_user_registration_utility_unittest.cc
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.
4
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_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"
28
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;
39
40 namespace {
41
42 const char kManagedUserToken[] = "managedusertoken";
43
44 class MockChangeProcessor : public SyncChangeProcessor {
45  public:
46   MockChangeProcessor() {}
47   virtual ~MockChangeProcessor() {}
48
49   // SyncChangeProcessor implementation:
50   virtual SyncError ProcessSyncChanges(
51       const tracked_objects::Location& from_here,
52       const SyncChangeList& change_list) OVERRIDE;
53
54   virtual SyncDataList GetAllSyncData(syncer::ModelType type) const
55       OVERRIDE {
56     return SyncDataList();
57   }
58
59   const SyncChangeList& changes() const { return change_list_; }
60
61  private:
62   SyncChangeList change_list_;
63 };
64
65 SyncError MockChangeProcessor::ProcessSyncChanges(
66     const tracked_objects::Location& from_here,
67     const SyncChangeList& change_list) {
68   change_list_ = change_list;
69   return SyncError();
70 }
71
72 class MockManagedUserRefreshTokenFetcher
73     : public ManagedUserRefreshTokenFetcher {
74  public:
75   MockManagedUserRefreshTokenFetcher() {}
76   virtual ~MockManagedUserRefreshTokenFetcher() {}
77
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);
84   }
85 };
86
87 }  // namespace
88
89 class ManagedUserRegistrationUtilityTest : public ::testing::Test {
90  public:
91   ManagedUserRegistrationUtilityTest();
92   virtual ~ManagedUserRegistrationUtilityTest();
93
94   virtual void TearDown() OVERRIDE;
95
96  protected:
97   scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
98   scoped_ptr<SyncErrorFactory> CreateErrorFactory();
99   SyncData CreateRemoteData(const std::string& id, const std::string& name);
100
101   SyncMergeResult StartInitialSync();
102
103   ManagedUserRegistrationUtility::RegistrationCallback
104       GetRegistrationCallback();
105
106   ManagedUserRegistrationUtility* GetRegistrationUtility();
107
108   void Acknowledge();
109
110   PrefService* prefs() { return profile_.GetTestingPrefService(); }
111   ManagedUserSyncService* service() { return service_; }
112   ManagedUserSharedSettingsService* shared_settings_service() {
113     return shared_settings_service_;
114   }
115   MockChangeProcessor* change_processor() { return change_processor_; }
116
117   bool received_callback() const { return received_callback_; }
118   const GoogleServiceAuthError& error() const { return error_; }
119   const std::string& token() const { return token_; }
120
121  private:
122   void OnManagedUserRegistered(const GoogleServiceAuthError& error,
123                                const std::string& token);
124
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_;
131
132   // Owned by the ManagedUserSyncService.
133   MockChangeProcessor* change_processor_;
134
135   // A unique ID for creating "remote" Sync data.
136   int64 sync_data_id_;
137
138   // Whether OnManagedUserRegistered has been called.
139   bool received_callback_;
140
141   // Hold the registration result (either an error, or a token).
142   GoogleServiceAuthError error_;
143   std::string token_;
144
145   base::WeakPtrFactory<ManagedUserRegistrationUtilityTest> weak_ptr_factory_;
146 };
147
148 ManagedUserRegistrationUtilityTest::ManagedUserRegistrationUtilityTest()
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_ = ManagedUserSyncServiceFactory::GetForProfile(&profile_);
155   shared_settings_service_ =
156       ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(&profile_);
157 }
158
159 ManagedUserRegistrationUtilityTest::~ManagedUserRegistrationUtilityTest() {
160   EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
161 }
162
163 void ManagedUserRegistrationUtilityTest::TearDown() {
164   content::BrowserThread::GetBlockingPool()->FlushForTesting();
165   base::RunLoop().RunUntilIdle();
166 }
167
168 scoped_ptr<SyncChangeProcessor>
169 ManagedUserRegistrationUtilityTest::CreateChangeProcessor() {
170   EXPECT_FALSE(change_processor_);
171   change_processor_ = new MockChangeProcessor();
172   return scoped_ptr<SyncChangeProcessor>(change_processor_);
173 }
174
175 scoped_ptr<SyncErrorFactory>
176 ManagedUserRegistrationUtilityTest::CreateErrorFactory() {
177   return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
178 }
179
180 SyncMergeResult ManagedUserRegistrationUtilityTest::StartInitialSync() {
181   SyncDataList initial_sync_data;
182   SyncMergeResult result =
183       service()->MergeDataAndStartSyncing(MANAGED_USERS,
184                                           initial_sync_data,
185                                           CreateChangeProcessor(),
186                                           CreateErrorFactory());
187   EXPECT_FALSE(result.error().IsSet());
188   return result;
189 }
190
191 ManagedUserRegistrationUtility::RegistrationCallback
192 ManagedUserRegistrationUtilityTest::GetRegistrationCallback() {
193   return base::Bind(
194       &ManagedUserRegistrationUtilityTest::OnManagedUserRegistered,
195       weak_ptr_factory_.GetWeakPtr());
196 }
197
198 ManagedUserRegistrationUtility*
199 ManagedUserRegistrationUtilityTest::GetRegistrationUtility() {
200   if (registration_utility_.get())
201     return registration_utility_.get();
202
203   scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher(
204       new MockManagedUserRefreshTokenFetcher);
205   registration_utility_.reset(
206       ManagedUserRegistrationUtility::CreateImpl(prefs(),
207                                                  token_fetcher.Pass(),
208                                                  service(),
209                                                  shared_settings_service()));
210   return registration_utility_.get();
211 }
212
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();
217        ++it) {
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(
226                        ++sync_data_id_,
227                        specifics,
228                        base::Time(),
229                        syncer::AttachmentIdList(),
230                        syncer::AttachmentServiceProxyForTest::Create())));
231   }
232   service()->ProcessSyncChanges(FROM_HERE, new_changes);
233
234   run_loop_.Run();
235 }
236
237 void ManagedUserRegistrationUtilityTest::OnManagedUserRegistered(
238     const GoogleServiceAuthError& error,
239     const std::string& token) {
240   received_callback_ = true;
241   error_ = error;
242   token_ = token;
243   run_loop_.Quit();
244 }
245
246 TEST_F(ManagedUserRegistrationUtilityTest, Register) {
247   StartInitialSync();
248   GetRegistrationUtility()->Register(
249       ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
250       ManagedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
251       GetRegistrationCallback());
252   EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
253   Acknowledge();
254
255   EXPECT_TRUE(received_callback());
256   EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
257   EXPECT_FALSE(token().empty());
258 }
259
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());
266   StartInitialSync();
267   Acknowledge();
268
269   EXPECT_TRUE(received_callback());
270   EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
271   EXPECT_FALSE(token().empty());
272 }
273
274 TEST_F(ManagedUserRegistrationUtilityTest, SyncServiceShutdownBeforeRegFinish) {
275   StartInitialSync();
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());
286 }
287
288 TEST_F(ManagedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
289   StartInitialSync();
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());
300 }