| 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/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "chrome/browser/managed_mode/managed_user_sync_service.h" | 10 #include "chrome/browser/managed_mode/managed_user_sync_service.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 57 |
| 58 SyncChange MockChangeProcessor::GetChange(const std::string& id) const { | 58 SyncChange MockChangeProcessor::GetChange(const std::string& id) const { |
| 59 for (SyncChangeList::const_iterator it = change_list_.begin(); | 59 for (SyncChangeList::const_iterator it = change_list_.begin(); |
| 60 it != change_list_.end(); ++it) { | 60 it != change_list_.end(); ++it) { |
| 61 if (it->sync_data().GetSpecifics().managed_user().id() == id) | 61 if (it->sync_data().GetSpecifics().managed_user().id() == id) |
| 62 return *it; | 62 return *it; |
| 63 } | 63 } |
| 64 return SyncChange(); | 64 return SyncChange(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Callback for ManagedUserSyncService::GetManagedUsersAsync(). |
| 68 void GetManagedUsersCallback(const base::DictionaryValue** dict, |
| 69 const base::DictionaryValue* managed_users) { |
| 70 *dict = managed_users; |
| 71 } |
| 72 |
| 67 } // namespace | 73 } // namespace |
| 68 | 74 |
| 69 class ManagedUserSyncServiceTest : public ::testing::Test { | 75 class ManagedUserSyncServiceTest : public ::testing::Test { |
| 70 public: | 76 public: |
| 71 ManagedUserSyncServiceTest(); | 77 ManagedUserSyncServiceTest(); |
| 72 virtual ~ManagedUserSyncServiceTest(); | 78 virtual ~ManagedUserSyncServiceTest(); |
| 73 | 79 |
| 74 protected: | 80 protected: |
| 75 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); | 81 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor(); |
| 76 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); | 82 scoped_ptr<SyncErrorFactory> CreateErrorFactory(); |
| 77 SyncData CreateRemoteData(const std::string& id, const std::string& name); | 83 SyncData CreateRemoteData(const std::string& id, const std::string& name); |
| 78 | 84 |
| 79 SyncMergeResult StartInitialSync(); | |
| 80 | |
| 81 void Acknowledge(); | |
| 82 void ResetService(); | |
| 83 | |
| 84 PrefService* prefs() { return profile_.GetPrefs(); } | 85 PrefService* prefs() { return profile_.GetPrefs(); } |
| 85 ManagedUserSyncService* service() { return service_; } | 86 ManagedUserSyncService* service() { return service_; } |
| 86 MockChangeProcessor* change_processor() { return change_processor_; } | 87 MockChangeProcessor* change_processor() { return change_processor_; } |
| 87 | 88 |
| 88 private: | 89 private: |
| 89 TestingProfile profile_; | 90 TestingProfile profile_; |
| 90 ManagedUserSyncService* service_; | 91 ManagedUserSyncService* service_; |
| 91 | 92 |
| 92 // Owned by the ManagedUserSyncService. | 93 // Owned by the ManagedUserSyncService. |
| 93 MockChangeProcessor* change_processor_; | 94 MockChangeProcessor* change_processor_; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 119 SyncData ManagedUserSyncServiceTest::CreateRemoteData( | 120 SyncData ManagedUserSyncServiceTest::CreateRemoteData( |
| 120 const std::string& id, | 121 const std::string& id, |
| 121 const std::string& name) { | 122 const std::string& name) { |
| 122 ::sync_pb::EntitySpecifics specifics; | 123 ::sync_pb::EntitySpecifics specifics; |
| 123 specifics.mutable_managed_user()->set_id(id); | 124 specifics.mutable_managed_user()->set_id(id); |
| 124 specifics.mutable_managed_user()->set_name(name); | 125 specifics.mutable_managed_user()->set_name(name); |
| 125 specifics.mutable_managed_user()->set_acknowledged(true); | 126 specifics.mutable_managed_user()->set_acknowledged(true); |
| 126 return SyncData::CreateRemoteData(++sync_data_id_, specifics, base::Time()); | 127 return SyncData::CreateRemoteData(++sync_data_id_, specifics, base::Time()); |
| 127 } | 128 } |
| 128 | 129 |
| 129 SyncMergeResult ManagedUserSyncServiceTest::StartInitialSync() { | 130 TEST_F(ManagedUserSyncServiceTest, MergeEmpty) { |
| 130 SyncDataList initial_sync_data; | |
| 131 SyncMergeResult result = | 131 SyncMergeResult result = |
| 132 service()->MergeDataAndStartSyncing(MANAGED_USERS, | 132 service()->MergeDataAndStartSyncing(MANAGED_USERS, |
| 133 initial_sync_data, | 133 SyncDataList(), |
| 134 CreateChangeProcessor(), | 134 CreateChangeProcessor(), |
| 135 CreateErrorFactory()); | 135 CreateErrorFactory()); |
| 136 EXPECT_FALSE(result.error().IsSet()); | 136 EXPECT_FALSE(result.error().IsSet()); |
| 137 return result; | |
| 138 } | |
| 139 | |
| 140 void ManagedUserSyncServiceTest::ResetService() { | |
| 141 service_->StopSyncing(MANAGED_USERS); | |
| 142 service_->Shutdown(); | |
| 143 } | |
| 144 | |
| 145 TEST_F(ManagedUserSyncServiceTest, MergeEmpty) { | |
| 146 SyncMergeResult result = StartInitialSync(); | |
| 147 EXPECT_EQ(0, result.num_items_added()); | 137 EXPECT_EQ(0, result.num_items_added()); |
| 148 EXPECT_EQ(0, result.num_items_modified()); | 138 EXPECT_EQ(0, result.num_items_modified()); |
| 149 EXPECT_EQ(0, result.num_items_deleted()); | 139 EXPECT_EQ(0, result.num_items_deleted()); |
| 150 EXPECT_EQ(0, result.num_items_before_association()); | 140 EXPECT_EQ(0, result.num_items_before_association()); |
| 151 EXPECT_EQ(0, result.num_items_after_association()); | 141 EXPECT_EQ(0, result.num_items_after_association()); |
| 152 EXPECT_EQ(0u, prefs()->GetDictionary(prefs::kManagedUsers)->size()); | 142 EXPECT_EQ(0u, service()->GetManagedUsers()->size()); |
| 153 EXPECT_EQ(0u, change_processor()->changes().size()); | 143 EXPECT_EQ(0u, change_processor()->changes().size()); |
| 154 | 144 |
| 155 ResetService(); | 145 service()->StopSyncing(MANAGED_USERS); |
| 146 service()->Shutdown(); |
| 156 } | 147 } |
| 157 | 148 |
| 158 TEST_F(ManagedUserSyncServiceTest, MergeExisting) { | 149 TEST_F(ManagedUserSyncServiceTest, MergeExisting) { |
| 159 const char kNameKey[] = "name"; | 150 const char kNameKey[] = "name"; |
| 160 const char kAcknowledgedKey[] = "acknowledged"; | 151 const char kAcknowledgedKey[] = "acknowledged"; |
| 161 | 152 |
| 162 const char kUserId1[] = "aaaaa"; | 153 const char kUserId1[] = "aaaaa"; |
| 163 const char kUserId2[] = "bbbbb"; | 154 const char kUserId2[] = "bbbbb"; |
| 164 const char kUserId3[] = "ccccc"; | 155 const char kUserId3[] = "ccccc"; |
| 165 const char kUserId4[] = "ddddd"; | 156 const char kUserId4[] = "ddddd"; |
| 166 const char kName1[] = "Anchor"; | 157 const char kName1[] = "Anchor"; |
| 167 const char kName2[] = "Buzz"; | 158 const char kName2[] = "Buzz"; |
| 168 const char kName3[] = "Crush"; | 159 const char kName3[] = "Crush"; |
| 169 const char kName4[] = "Dory"; | 160 const char kName4[] = "Dory"; |
| 170 { | 161 { |
| 171 DictionaryPrefUpdate update(prefs(), prefs::kManagedUsers); | 162 DictionaryPrefUpdate update(prefs(), prefs::kManagedUsers); |
| 172 DictionaryValue* managed_users = update.Get(); | 163 DictionaryValue* managed_users = update.Get(); |
| 173 DictionaryValue* dict = new DictionaryValue; | 164 DictionaryValue* dict = new DictionaryValue; |
| 174 dict->SetString(kNameKey, kName1); | 165 dict->SetString(kNameKey, kName1); |
| 175 managed_users->Set(kUserId1, dict); | 166 managed_users->Set(kUserId1, dict); |
| 176 dict = new DictionaryValue; | 167 dict = new DictionaryValue; |
| 177 dict->SetString(kNameKey, kName2); | 168 dict->SetString(kNameKey, kName2); |
| 178 dict->SetBoolean(kAcknowledgedKey, true); | 169 dict->SetBoolean(kAcknowledgedKey, true); |
| 179 managed_users->Set(kUserId2, dict); | 170 managed_users->Set(kUserId2, dict); |
| 180 } | 171 } |
| 181 | 172 |
| 173 const base::DictionaryValue* async_managed_users = NULL; |
| 174 service()->GetManagedUsersAsync( |
| 175 base::Bind(&GetManagedUsersCallback, &async_managed_users)); |
| 176 |
| 182 SyncDataList initial_sync_data; | 177 SyncDataList initial_sync_data; |
| 183 initial_sync_data.push_back(CreateRemoteData(kUserId2, kName2)); | 178 initial_sync_data.push_back(CreateRemoteData(kUserId2, kName2)); |
| 184 initial_sync_data.push_back(CreateRemoteData(kUserId3, kName3)); | 179 initial_sync_data.push_back(CreateRemoteData(kUserId3, kName3)); |
| 185 initial_sync_data.push_back(CreateRemoteData(kUserId4, kName4)); | 180 initial_sync_data.push_back(CreateRemoteData(kUserId4, kName4)); |
| 186 | 181 |
| 187 SyncMergeResult result = | 182 SyncMergeResult result = |
| 188 service()->MergeDataAndStartSyncing(MANAGED_USERS, | 183 service()->MergeDataAndStartSyncing(MANAGED_USERS, |
| 189 initial_sync_data, | 184 initial_sync_data, |
| 190 CreateChangeProcessor(), | 185 CreateChangeProcessor(), |
| 191 CreateErrorFactory()); | 186 CreateErrorFactory()); |
| 192 EXPECT_FALSE(result.error().IsSet()); | 187 EXPECT_FALSE(result.error().IsSet()); |
| 193 EXPECT_EQ(2, result.num_items_added()); | 188 EXPECT_EQ(2, result.num_items_added()); |
| 194 EXPECT_EQ(1, result.num_items_modified()); | 189 EXPECT_EQ(1, result.num_items_modified()); |
| 195 EXPECT_EQ(0, result.num_items_deleted()); | 190 EXPECT_EQ(0, result.num_items_deleted()); |
| 196 EXPECT_EQ(2, result.num_items_before_association()); | 191 EXPECT_EQ(2, result.num_items_before_association()); |
| 197 EXPECT_EQ(4, result.num_items_after_association()); | 192 EXPECT_EQ(4, result.num_items_after_association()); |
| 198 | 193 |
| 199 const DictionaryValue* managed_users = | 194 const DictionaryValue* managed_users = service()->GetManagedUsers(); |
| 200 prefs()->GetDictionary(prefs::kManagedUsers); | |
| 201 EXPECT_EQ(4u, managed_users->size()); | 195 EXPECT_EQ(4u, managed_users->size()); |
| 196 EXPECT_TRUE(async_managed_users); |
| 197 EXPECT_TRUE(managed_users->Equals(async_managed_users)); |
| 198 |
| 202 { | 199 { |
| 203 const DictionaryValue* managed_user = NULL; | 200 const DictionaryValue* managed_user = NULL; |
| 204 ASSERT_TRUE(managed_users->GetDictionary(kUserId2, &managed_user)); | 201 ASSERT_TRUE(managed_users->GetDictionary(kUserId2, &managed_user)); |
| 205 ASSERT_TRUE(managed_user); | 202 ASSERT_TRUE(managed_user); |
| 206 std::string name; | 203 std::string name; |
| 207 EXPECT_TRUE(managed_user->GetString(kNameKey, &name)); | 204 EXPECT_TRUE(managed_user->GetString(kNameKey, &name)); |
| 208 EXPECT_EQ(kName2, name); | 205 EXPECT_EQ(kName2, name); |
| 209 bool acknowledged = false; | 206 bool acknowledged = false; |
| 210 EXPECT_TRUE(managed_user->GetBoolean(kAcknowledgedKey, &acknowledged)); | 207 EXPECT_TRUE(managed_user->GetBoolean(kAcknowledgedKey, &acknowledged)); |
| 211 EXPECT_TRUE(acknowledged); | 208 EXPECT_TRUE(acknowledged); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 237 { | 234 { |
| 238 SyncChange change = change_processor()->GetChange(kUserId1); | 235 SyncChange change = change_processor()->GetChange(kUserId1); |
| 239 ASSERT_TRUE(change.IsValid()); | 236 ASSERT_TRUE(change.IsValid()); |
| 240 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type()); | 237 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type()); |
| 241 const ManagedUserSpecifics& managed_user = | 238 const ManagedUserSpecifics& managed_user = |
| 242 change.sync_data().GetSpecifics().managed_user(); | 239 change.sync_data().GetSpecifics().managed_user(); |
| 243 EXPECT_EQ(kName1, managed_user.name()); | 240 EXPECT_EQ(kName1, managed_user.name()); |
| 244 EXPECT_FALSE(managed_user.acknowledged()); | 241 EXPECT_FALSE(managed_user.acknowledged()); |
| 245 } | 242 } |
| 246 } | 243 } |
| OLD | NEW |