| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/managed_mode/managed_user_registration_service.h" | 11 #include "chrome/browser/managed_mode/managed_user_registration_service.h" |
| 12 #include "chrome/browser/managed_mode/managed_user_token_fetcher.h" |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 11 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_pref_service_syncable.h" | 15 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 13 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 14 #include "sync/api/sync_change.h" | 17 #include "sync/api/sync_change.h" |
| 15 #include "sync/api/sync_error_factory_mock.h" | 18 #include "sync/api/sync_error_factory_mock.h" |
| 16 #include "sync/protocol/sync.pb.h" | 19 #include "sync/protocol/sync.pb.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| 19 using ::sync_pb::ManagedUserSpecifics; | 22 using ::sync_pb::ManagedUserSpecifics; |
| 20 using syncer::MANAGED_USERS; | 23 using syncer::MANAGED_USERS; |
| 21 using syncer::SyncChange; | 24 using syncer::SyncChange; |
| 22 using syncer::SyncChangeList; | 25 using syncer::SyncChangeList; |
| 23 using syncer::SyncChangeProcessor; | 26 using syncer::SyncChangeProcessor; |
| 24 using syncer::SyncData; | 27 using syncer::SyncData; |
| 25 using syncer::SyncDataList; | 28 using syncer::SyncDataList; |
| 26 using syncer::SyncError; | 29 using syncer::SyncError; |
| 27 using syncer::SyncErrorFactory; | 30 using syncer::SyncErrorFactory; |
| 28 using syncer::SyncMergeResult; | 31 using syncer::SyncMergeResult; |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 35 const char kManagedUserToken[] = "managedusertoken"; |
| 36 |
| 32 class MockChangeProcessor : public SyncChangeProcessor { | 37 class MockChangeProcessor : public SyncChangeProcessor { |
| 33 public: | 38 public: |
| 34 MockChangeProcessor() {} | 39 MockChangeProcessor() {} |
| 35 virtual ~MockChangeProcessor() {} | 40 virtual ~MockChangeProcessor() {} |
| 36 | 41 |
| 37 // SyncChangeProcessor implementation: | 42 // SyncChangeProcessor implementation: |
| 38 virtual SyncError ProcessSyncChanges( | 43 virtual SyncError ProcessSyncChanges( |
| 39 const tracked_objects::Location& from_here, | 44 const tracked_objects::Location& from_here, |
| 40 const SyncChangeList& change_list) OVERRIDE; | 45 const SyncChangeList& change_list) OVERRIDE; |
| 41 | 46 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 55 | 60 |
| 56 SyncChange MockChangeProcessor::GetChange(const std::string& id) const { | 61 SyncChange MockChangeProcessor::GetChange(const std::string& id) const { |
| 57 for (SyncChangeList::const_iterator it = change_list_.begin(); | 62 for (SyncChangeList::const_iterator it = change_list_.begin(); |
| 58 it != change_list_.end(); ++it) { | 63 it != change_list_.end(); ++it) { |
| 59 if (it->sync_data().GetSpecifics().managed_user().id() == id) | 64 if (it->sync_data().GetSpecifics().managed_user().id() == id) |
| 60 return *it; | 65 return *it; |
| 61 } | 66 } |
| 62 return SyncChange(); | 67 return SyncChange(); |
| 63 } | 68 } |
| 64 | 69 |
| 70 class MockManagedUserTokenFetcher : public ManagedUserTokenFetcher { |
| 71 public: |
| 72 MockManagedUserTokenFetcher() {} |
| 73 virtual ~MockManagedUserTokenFetcher() {} |
| 74 |
| 75 // ManagedUserTokenFetcher implementation: |
| 76 virtual void Start(const std::string& managed_user_id, |
| 77 const string16& name, |
| 78 const std::string& device_name, |
| 79 const TokenCallback& callback) OVERRIDE { |
| 80 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); |
| 81 callback.Run(error, kManagedUserToken); |
| 82 } |
| 83 }; |
| 84 |
| 65 } // namespace | 85 } // namespace |
| 66 | 86 |
| 67 class ManagedUserRegistrationServiceTest : public ::testing::Test { | 87 class ManagedUserRegistrationServiceTest : public ::testing::Test { |
| 68 public: | 88 public: |
| 69 ManagedUserRegistrationServiceTest(); | 89 ManagedUserRegistrationServiceTest(); |
| 70 virtual ~ManagedUserRegistrationServiceTest(); | 90 virtual ~ManagedUserRegistrationServiceTest(); |
| 71 | 91 |
| 72 protected: | 92 protected: |
| 73 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); | 93 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); |
| 74 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); | 94 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 87 MockChangeProcessor* change_processor() { return change_processor_; } | 107 MockChangeProcessor* change_processor() { return change_processor_; } |
| 88 | 108 |
| 89 bool received_callback() const { return received_callback_; } | 109 bool received_callback() const { return received_callback_; } |
| 90 const GoogleServiceAuthError& error() const { return error_; } | 110 const GoogleServiceAuthError& error() const { return error_; } |
| 91 const std::string& token() const { return token_; } | 111 const std::string& token() const { return token_; } |
| 92 | 112 |
| 93 private: | 113 private: |
| 94 void OnManagedUserRegistered(const GoogleServiceAuthError& error, | 114 void OnManagedUserRegistered(const GoogleServiceAuthError& error, |
| 95 const std::string& token); | 115 const std::string& token); |
| 96 | 116 |
| 117 base::MessageLoop message_loop_; |
| 118 base::RunLoop run_loop_; |
| 97 base::WeakPtrFactory<ManagedUserRegistrationServiceTest> weak_ptr_factory_; | 119 base::WeakPtrFactory<ManagedUserRegistrationServiceTest> weak_ptr_factory_; |
| 98 TestingPrefServiceSyncable prefs_; | 120 TestingPrefServiceSyncable prefs_; |
| 99 scoped_ptr<ManagedUserRegistrationService> service_; | 121 scoped_ptr<ManagedUserRegistrationService> service_; |
| 100 | 122 |
| 101 // Owned by the ManagedUserRegistrationService. | 123 // Owned by the ManagedUserRegistrationService. |
| 102 MockChangeProcessor* change_processor_; | 124 MockChangeProcessor* change_processor_; |
| 103 | 125 |
| 104 // A unique ID for creating "remote" Sync data. | 126 // A unique ID for creating "remote" Sync data. |
| 105 int64 sync_data_id_; | 127 int64 sync_data_id_; |
| 106 | 128 |
| 107 // Whether OnManagedUserRegistered has been called. | 129 // Whether OnManagedUserRegistered has been called. |
| 108 bool received_callback_; | 130 bool received_callback_; |
| 109 | 131 |
| 110 // Hold the registration result (either an error, or a token). | 132 // Hold the registration result (either an error, or a token). |
| 111 GoogleServiceAuthError error_; | 133 GoogleServiceAuthError error_; |
| 112 std::string token_; | 134 std::string token_; |
| 113 }; | 135 }; |
| 114 | 136 |
| 115 ManagedUserRegistrationServiceTest::ManagedUserRegistrationServiceTest() | 137 ManagedUserRegistrationServiceTest::ManagedUserRegistrationServiceTest() |
| 116 : weak_ptr_factory_(this), | 138 : weak_ptr_factory_(this), |
| 117 service_(new ManagedUserRegistrationService(&prefs_)), | |
| 118 change_processor_(NULL), | 139 change_processor_(NULL), |
| 119 sync_data_id_(0), | 140 sync_data_id_(0), |
| 120 received_callback_(false), | 141 received_callback_(false), |
| 121 error_(GoogleServiceAuthError::NUM_STATES) { | 142 error_(GoogleServiceAuthError::NUM_STATES) { |
| 122 ManagedUserRegistrationService::RegisterUserPrefs(prefs_.registry()); | 143 ManagedUserRegistrationService::RegisterUserPrefs(prefs_.registry()); |
| 144 scoped_ptr<ManagedUserTokenFetcher> token_fetcher( |
| 145 new MockManagedUserTokenFetcher); |
| 146 service_.reset( |
| 147 new ManagedUserRegistrationService(&prefs_, token_fetcher.Pass())); |
| 123 } | 148 } |
| 124 | 149 |
| 125 ManagedUserRegistrationServiceTest::~ManagedUserRegistrationServiceTest() { | 150 ManagedUserRegistrationServiceTest::~ManagedUserRegistrationServiceTest() { |
| 126 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs()); | 151 EXPECT_FALSE(weak_ptr_factory_.HasWeakPtrs()); |
| 127 } | 152 } |
| 128 | 153 |
| 129 scoped_ptr<SyncChangeProcessor> | 154 scoped_ptr<SyncChangeProcessor> |
| 130 ManagedUserRegistrationServiceTest::CreateChangeProcessor() { | 155 ManagedUserRegistrationServiceTest::CreateChangeProcessor() { |
| 131 EXPECT_FALSE(change_processor_); | 156 EXPECT_FALSE(change_processor_); |
| 132 change_processor_ = new MockChangeProcessor(); | 157 change_processor_ = new MockChangeProcessor(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 ++it) { | 198 ++it) { |
| 174 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type()); | 199 EXPECT_EQ(SyncChange::ACTION_ADD, it->change_type()); |
| 175 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics(); | 200 ::sync_pb::EntitySpecifics specifics = it->sync_data().GetSpecifics(); |
| 176 EXPECT_FALSE(specifics.managed_user().acknowledged()); | 201 EXPECT_FALSE(specifics.managed_user().acknowledged()); |
| 177 specifics.mutable_managed_user()->set_acknowledged(true); | 202 specifics.mutable_managed_user()->set_acknowledged(true); |
| 178 new_changes.push_back( | 203 new_changes.push_back( |
| 179 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, | 204 SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, |
| 180 SyncData::CreateRemoteData(++sync_data_id_, specifics))); | 205 SyncData::CreateRemoteData(++sync_data_id_, specifics))); |
| 181 } | 206 } |
| 182 service()->ProcessSyncChanges(FROM_HERE, new_changes); | 207 service()->ProcessSyncChanges(FROM_HERE, new_changes); |
| 208 |
| 209 run_loop_.Run(); |
| 183 } | 210 } |
| 184 | 211 |
| 185 void ManagedUserRegistrationServiceTest::ResetService() { | 212 void ManagedUserRegistrationServiceTest::ResetService() { |
| 186 service_->StopSyncing(MANAGED_USERS); | 213 service_->StopSyncing(MANAGED_USERS); |
| 187 service_->Shutdown(); | 214 service_->Shutdown(); |
| 188 service_.reset(); | 215 service_.reset(); |
| 189 } | 216 } |
| 190 | 217 |
| 191 void ManagedUserRegistrationServiceTest::OnManagedUserRegistered( | 218 void ManagedUserRegistrationServiceTest::OnManagedUserRegistered( |
| 192 const GoogleServiceAuthError& error, | 219 const GoogleServiceAuthError& error, |
| 193 const std::string& token) { | 220 const std::string& token) { |
| 194 received_callback_ = true; | 221 received_callback_ = true; |
| 195 error_ = error; | 222 error_ = error; |
| 196 token_ = token; | 223 token_ = token; |
| 224 run_loop_.Quit(); |
| 197 } | 225 } |
| 198 | 226 |
| 199 TEST_F(ManagedUserRegistrationServiceTest, MergeEmpty) { | 227 TEST_F(ManagedUserRegistrationServiceTest, MergeEmpty) { |
| 200 SyncMergeResult result = StartInitialSync(); | 228 SyncMergeResult result = StartInitialSync(); |
| 201 EXPECT_EQ(0, result.num_items_added()); | 229 EXPECT_EQ(0, result.num_items_added()); |
| 202 EXPECT_EQ(0, result.num_items_modified()); | 230 EXPECT_EQ(0, result.num_items_modified()); |
| 203 EXPECT_EQ(0, result.num_items_deleted()); | 231 EXPECT_EQ(0, result.num_items_deleted()); |
| 204 EXPECT_EQ(0, result.num_items_before_association()); | 232 EXPECT_EQ(0, result.num_items_before_association()); |
| 205 EXPECT_EQ(0, result.num_items_after_association()); | 233 EXPECT_EQ(0, result.num_items_after_association()); |
| 206 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); | 234 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 } | 355 } |
| 328 | 356 |
| 329 TEST_F(ManagedUserRegistrationServiceTest, StopSyncing) { | 357 TEST_F(ManagedUserRegistrationServiceTest, StopSyncing) { |
| 330 StartInitialSync(); | 358 StartInitialSync(); |
| 331 service()->Register(ASCIIToUTF16("Mike"), GetRegistrationCallback()); | 359 service()->Register(ASCIIToUTF16("Mike"), GetRegistrationCallback()); |
| 332 service()->StopSyncing(MANAGED_USERS); | 360 service()->StopSyncing(MANAGED_USERS); |
| 333 EXPECT_TRUE(received_callback()); | 361 EXPECT_TRUE(received_callback()); |
| 334 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state()); | 362 EXPECT_EQ(GoogleServiceAuthError::REQUEST_CANCELED, error().state()); |
| 335 EXPECT_EQ(std::string(), token()); | 363 EXPECT_EQ(std::string(), token()); |
| 336 } | 364 } |
| OLD | NEW |