- add sources.
[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_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"
24
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;
35
36 namespace {
37
38 const char kManagedUserToken[] = "managedusertoken";
39
40 class MockChangeProcessor : public SyncChangeProcessor {
41  public:
42   MockChangeProcessor() {}
43   virtual ~MockChangeProcessor() {}
44
45   // SyncChangeProcessor implementation:
46   virtual SyncError ProcessSyncChanges(
47       const tracked_objects::Location& from_here,
48       const SyncChangeList& change_list) OVERRIDE;
49
50   virtual SyncDataList GetAllSyncData(syncer::ModelType type) const
51       OVERRIDE {
52     return SyncDataList();
53   }
54
55   const SyncChangeList& changes() const { return change_list_; }
56
57  private:
58   SyncChangeList change_list_;
59 };
60
61 SyncError MockChangeProcessor::ProcessSyncChanges(
62     const tracked_objects::Location& from_here,
63     const SyncChangeList& change_list) {
64   change_list_ = change_list;
65   return SyncError();
66 }
67
68 class MockManagedUserRefreshTokenFetcher
69     : public ManagedUserRefreshTokenFetcher {
70  public:
71   MockManagedUserRefreshTokenFetcher() {}
72   virtual ~MockManagedUserRefreshTokenFetcher() {}
73
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);
80   }
81 };
82
83 }  // namespace
84
85 class ManagedUserRegistrationUtilityTest : public ::testing::Test {
86  public:
87   ManagedUserRegistrationUtilityTest();
88   virtual ~ManagedUserRegistrationUtilityTest();
89
90   virtual void TearDown() OVERRIDE;
91
92  protected:
93   scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
94   scoped_ptr<SyncErrorFactory> CreateErrorFactory();
95   SyncData CreateRemoteData(const std::string& id, const std::string& name);
96
97   SyncMergeResult StartInitialSync();
98
99   ManagedUserRegistrationUtility::RegistrationCallback
100       GetRegistrationCallback();
101
102   ManagedUserRegistrationUtility* GetRegistrationUtility();
103
104   void Acknowledge();
105
106   PrefService* prefs() { return profile_.GetTestingPrefService(); }
107   ManagedUserSyncService* service() { return service_; }
108   MockChangeProcessor* change_processor() { return change_processor_; }
109
110   bool received_callback() const { return received_callback_; }
111   const GoogleServiceAuthError& error() const { return error_; }
112   const std::string& token() const { return token_; }
113
114  private:
115   void OnManagedUserRegistered(const GoogleServiceAuthError& error,
116                                const std::string& token);
117
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_;
124
125   // Owned by the ManagedUserSyncService.
126   MockChangeProcessor* change_processor_;
127
128   // A unique ID for creating "remote" Sync data.
129   int64 sync_data_id_;
130
131   // Whether OnManagedUserRegistered has been called.
132   bool received_callback_;
133
134   // Hold the registration result (either an error, or a token).
135   GoogleServiceAuthError error_;
136   std::string token_;
137 };
138
139 ManagedUserRegistrationUtilityTest::ManagedUserRegistrationUtilityTest()
140     : weak_ptr_factory_(this),
141       change_processor_(NULL),
142       sync_data_id_(0),
143       received_callback_(false),
144       error_(GoogleServiceAuthError::NUM_STATES) {
145   service_ = ManagedUserSyncServiceFactory::GetForProfile(&profile_);
146 }
147
148 ManagedUserRegistrationUtilityTest::~ManagedUserRegistrationUtilityTest() {
149   EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
150 }
151
152 void ManagedUserRegistrationUtilityTest::TearDown() {
153   content::BrowserThread::GetBlockingPool()->FlushForTesting();
154   base::RunLoop().RunUntilIdle();
155 }
156
157 scoped_ptr<SyncChangeProcessor>
158 ManagedUserRegistrationUtilityTest::CreateChangeProcessor() {
159   EXPECT_FALSE(change_processor_);
160   change_processor_ = new MockChangeProcessor();
161   return scoped_ptr<SyncChangeProcessor>(change_processor_);
162 }
163
164 scoped_ptr<SyncErrorFactory>
165 ManagedUserRegistrationUtilityTest::CreateErrorFactory() {
166   return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
167 }
168
169 SyncMergeResult ManagedUserRegistrationUtilityTest::StartInitialSync() {
170   SyncDataList initial_sync_data;
171   SyncMergeResult result =
172       service()->MergeDataAndStartSyncing(MANAGED_USERS,
173                                           initial_sync_data,
174                                           CreateChangeProcessor(),
175                                           CreateErrorFactory());
176   EXPECT_FALSE(result.error().IsSet());
177   return result;
178 }
179
180 ManagedUserRegistrationUtility::RegistrationCallback
181 ManagedUserRegistrationUtilityTest::GetRegistrationCallback() {
182   return base::Bind(
183       &ManagedUserRegistrationUtilityTest::OnManagedUserRegistered,
184       weak_ptr_factory_.GetWeakPtr());
185 }
186
187 ManagedUserRegistrationUtility*
188 ManagedUserRegistrationUtilityTest::GetRegistrationUtility() {
189   if (registration_utility_.get())
190     return registration_utility_.get();
191
192   scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher(
193       new MockManagedUserRefreshTokenFetcher);
194   registration_utility_.reset(
195       ManagedUserRegistrationUtility::CreateImpl(prefs(),
196                                                  token_fetcher.Pass(),
197                                                  service()));
198   return registration_utility_.get();
199 }
200
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();
205        ++it) {
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_,
213                                               specifics,
214                                               base::Time())));
215   }
216   service()->ProcessSyncChanges(FROM_HERE, new_changes);
217
218   run_loop_.Run();
219 }
220
221 void ManagedUserRegistrationUtilityTest::OnManagedUserRegistered(
222     const GoogleServiceAuthError& error,
223     const std::string& token) {
224   received_callback_ = true;
225   error_ = error;
226   token_ = token;
227   run_loop_.Quit();
228 }
229
230 TEST_F(ManagedUserRegistrationUtilityTest, Register) {
231   StartInitialSync();
232   GetRegistrationUtility()->Register(
233       ManagedUserRegistrationUtility::GenerateNewManagedUserId(),
234       ManagedUserRegistrationInfo(ASCIIToUTF16("Dug"), 0),
235       GetRegistrationCallback());
236   EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kManagedUsers)->size());
237   Acknowledge();
238
239   EXPECT_TRUE(received_callback());
240   EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
241   EXPECT_FALSE(token().empty());
242 }
243
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());
250   StartInitialSync();
251   Acknowledge();
252
253   EXPECT_TRUE(received_callback());
254   EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
255   EXPECT_FALSE(token().empty());
256 }
257
258 TEST_F(ManagedUserRegistrationUtilityTest, SyncServiceShutdownBeforeRegFinish) {
259   StartInitialSync();
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());
270 }
271
272 TEST_F(ManagedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
273   StartInitialSync();
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());
284 }