1f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/supervised_user/supervised_user_registration_utility.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/bind.h"
8ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/run_loop.h"
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/strings/utf_string_conversions.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_refresh_token_fetcher.h"
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_shared_settings_service.h"
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_shared_settings_service_factory.h"
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_sync_service.h"
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h"
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/common/pref_names.h"
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/test/base/testing_pref_service_syncable.h"
20a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/test/test_utils.h"
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "google_apis/gaia/google_service_auth_error.h"
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "sync/api/attachments/attachment_id.h"
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sync/api/sync_change.h"
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sync/api/sync_error_factory_mock.h"
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "sync/protocol/sync.pb.h"
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using sync_pb::ManagedUserSpecifics;
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)using syncer::SUPERVISED_USERS;
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncChange;
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncChangeList;
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncChangeProcessor;
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncData;
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncDataList;
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncError;
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncErrorFactory;
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using syncer::SyncMergeResult;
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace {
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const char kSupervisedUserToken[] = "supervisedusertoken";
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class MockChangeProcessor : public SyncChangeProcessor {
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MockChangeProcessor() {}
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~MockChangeProcessor() {}
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // SyncChangeProcessor implementation:
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual SyncError ProcessSyncChanges(
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const tracked_objects::Location& from_here,
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      const SyncChangeList& change_list) OVERRIDE;
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  virtual SyncDataList GetAllSyncData(syncer::ModelType type) const
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      OVERRIDE {
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return SyncDataList();
5958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const SyncChangeList& changes() const { return change_list_; }
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SyncChangeList change_list_;
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)SyncError MockChangeProcessor::ProcessSyncChanges(
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const tracked_objects::Location& from_here,
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const SyncChangeList& change_list) {
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  change_list_ = change_list;
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return SyncError();
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class MockSupervisedUserRefreshTokenFetcher
75f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : public SupervisedUserRefreshTokenFetcher {
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  MockSupervisedUserRefreshTokenFetcher() {}
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual ~MockSupervisedUserRefreshTokenFetcher() {}
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // SupervisedUserRefreshTokenFetcher implementation:
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual void Start(const std::string& supervised_user_id,
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     const std::string& device_name,
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     const TokenCallback& callback) OVERRIDE {
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    callback.Run(error, kSupervisedUserToken);
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class SupervisedUserRegistrationUtilityTest : public ::testing::Test {
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserRegistrationUtilityTest();
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual ~SupervisedUserRegistrationUtilityTest();
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void TearDown() OVERRIDE;
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected:
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<SyncErrorFactory> CreateErrorFactory();
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SyncData CreateRemoteData(const std::string& id, const std::string& name);
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SyncMergeResult StartInitialSync();
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserRegistrationUtility::RegistrationCallback
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetRegistrationCallback();
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
108f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserRegistrationUtility* GetRegistrationUtility();
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void Acknowledge();
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
112a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  PrefService* prefs() { return profile_.GetTestingPrefService(); }
113f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserSyncService* service() { return service_; }
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserSharedSettingsService* shared_settings_service() {
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return shared_settings_service_;
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MockChangeProcessor* change_processor() { return change_processor_; }
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool received_callback() const { return received_callback_; }
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const GoogleServiceAuthError& error() const { return error_; }
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const std::string& token() const { return token_; }
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
124f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void OnSupervisedUserRegistered(const GoogleServiceAuthError& error,
125f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                  const std::string& token);
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::MessageLoop message_loop_;
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::RunLoop run_loop_;
129a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  TestingProfile profile_;
130f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserSyncService* service_;
131f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  SupervisedUserSharedSettingsService* shared_settings_service_;
132f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_ptr<SupervisedUserRegistrationUtility> registration_utility_;
13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
134f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Owned by the SupervisedUserSyncService.
13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  MockChangeProcessor* change_processor_;
13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // A unique ID for creating "remote" Sync data.
13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int64 sync_data_id_;
13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
140f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Whether OnSupervisedUserRegistered has been called.
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool received_callback_;
14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Hold the registration result (either an error, or a token).
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  GoogleServiceAuthError error_;
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::string token_;
146f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
147f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::WeakPtrFactory<SupervisedUserRegistrationUtilityTest> weak_ptr_factory_;
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
150f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtilityTest::SupervisedUserRegistrationUtilityTest()
151f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : change_processor_(NULL),
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      sync_data_id_(0),
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      received_callback_(false),
154f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      error_(GoogleServiceAuthError::NUM_STATES),
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      weak_ptr_factory_(this) {
156f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  service_ = SupervisedUserSyncServiceFactory::GetForProfile(&profile_);
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  shared_settings_service_ =
158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(
159f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          &profile_);
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtilityTest::
163f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    ~SupervisedUserRegistrationUtilityTest() {
16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs());
16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
167f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void SupervisedUserRegistrationUtilityTest::TearDown() {
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::RunAllBlockingPoolTasksUntilIdle();
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)scoped_ptr<SyncChangeProcessor>
172f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtilityTest::CreateChangeProcessor() {
17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(change_processor_);
17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  change_processor_ = new MockChangeProcessor();
17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return scoped_ptr<SyncChangeProcessor>(change_processor_);
17690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
17790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
17890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)scoped_ptr<SyncErrorFactory>
179f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtilityTest::CreateErrorFactory() {
18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
18190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
18290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
183f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SyncMergeResult SupervisedUserRegistrationUtilityTest::StartInitialSync() {
18490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SyncDataList initial_sync_data;
18590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SyncMergeResult result =
186f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      service()->MergeDataAndStartSyncing(SUPERVISED_USERS,
18790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                          initial_sync_data,
18890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                          CreateChangeProcessor(),
18990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                          CreateErrorFactory());
19090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(result.error().IsSet());
19190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return result;
19290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
19390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
194f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtility::RegistrationCallback
195f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtilityTest::GetRegistrationCallback() {
19690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return base::Bind(
197f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      &SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered,
19890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      weak_ptr_factory_.GetWeakPtr());
19990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
20090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
201f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtility*
202f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SupervisedUserRegistrationUtilityTest::GetRegistrationUtility() {
203424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (registration_utility_.get())
204424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return registration_utility_.get();
205424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
206f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher(
207f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      new MockSupervisedUserRefreshTokenFetcher);
208424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  registration_utility_.reset(
209f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationUtility::CreateImpl(prefs(),
210f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                                    token_fetcher.Pass(),
211f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                                    service(),
212f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                                    shared_settings_service()));
213424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return registration_utility_.get();
214424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
215424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
216f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void SupervisedUserRegistrationUtilityTest::Acknowledge() {
21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SyncChangeList new_changes;
21890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const SyncChangeList& changes = change_processor()->changes();
21990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (SyncChangeList::const_iterator it = changes.begin(); it != changes.end();
22090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)       ++it) {
22190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type());
22290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics();
22390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    EXPECT_FALSE(specifics.managed_user().acknowledged());
22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    specifics.mutable_managed_user()->set_acknowledged(true);
22590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    new_changes.push_back(
226a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        SyncChange(FROM_HERE,
227a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   SyncChange::ACTION_UPDATE,
228a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                   SyncData::CreateRemoteData(
229a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       ++sync_data_id_,
230a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       specifics,
231a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       base::Time(),
232a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       syncer::AttachmentIdList(),
233a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                       syncer::AttachmentServiceProxyForTest::Create())));
23490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  service()->ProcessSyncChanges(FROM_HERE, new_changes);
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop_.Run();
23890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
23990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void SupervisedUserRegistrationUtilityTest::OnSupervisedUserRegistered(
24190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const GoogleServiceAuthError& error,
24290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::string& token) {
24390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  received_callback_ = true;
24490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  error_ = error;
24590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  token_ = token;
246868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  run_loop_.Quit();
24790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
24890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST_F(SupervisedUserRegistrationUtilityTest, Register) {
25090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  StartInitialSync();
251424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GetRegistrationUtility()->Register(
252f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Dug"), 0),
2547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      GetRegistrationCallback());
255f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
25690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Acknowledge();
257868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(received_callback());
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(token().empty());
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
263f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST_F(SupervisedUserRegistrationUtilityTest, RegisterBeforeInitialSync) {
264424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GetRegistrationUtility()->Register(
265f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
266f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Nemo"), 5),
2677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      GetRegistrationCallback());
268f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  StartInitialSync();
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Acknowledge();
271868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(received_callback());
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(GoogleServiceAuthError::NONE, error().state());
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(token().empty());
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
277f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST_F(SupervisedUserRegistrationUtilityTest,
278f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)       SyncServiceShutdownBeforeRegFinish) {
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  StartInitialSync();
280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GetRegistrationUtility()->Register(
281f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
282f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Remy"), 12),
2837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      GetRegistrationCallback());
284f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
285a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  service()->Shutdown();
286f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(received_callback());
28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(std::string(), token());
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
292f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST_F(SupervisedUserRegistrationUtilityTest, StopSyncingBeforeRegFinish) {
29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  StartInitialSync();
294424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  GetRegistrationUtility()->Register(
295f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(),
296f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      SupervisedUserRegistrationInfo(base::ASCIIToUTF16("Mike"), 17),
2977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      GetRegistrationCallback());
298f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(1u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
299f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  service()->StopSyncing(SUPERVISED_USERS);
300f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kSupervisedUsers)->size());
30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_TRUE(received_callback());
30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state());
30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_EQ(std::string(), token());
30490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
305