| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/sync/protocol/proto_value_conversions.h" | 5 #include "components/sync/protocol/proto_value_conversions.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "components/sync/base/model_type.h" | 12 #include "components/sync/base/model_type.h" |
| 13 #include "components/sync/base/unique_position.h" |
| 13 #include "components/sync/protocol/app_notification_specifics.pb.h" | 14 #include "components/sync/protocol/app_notification_specifics.pb.h" |
| 14 #include "components/sync/protocol/app_setting_specifics.pb.h" | 15 #include "components/sync/protocol/app_setting_specifics.pb.h" |
| 15 #include "components/sync/protocol/app_specifics.pb.h" | 16 #include "components/sync/protocol/app_specifics.pb.h" |
| 16 #include "components/sync/protocol/autofill_specifics.pb.h" | 17 #include "components/sync/protocol/autofill_specifics.pb.h" |
| 17 #include "components/sync/protocol/bookmark_specifics.pb.h" | 18 #include "components/sync/protocol/bookmark_specifics.pb.h" |
| 18 #include "components/sync/protocol/device_info_specifics.pb.h" | 19 #include "components/sync/protocol/device_info_specifics.pb.h" |
| 19 #include "components/sync/protocol/encryption.pb.h" | 20 #include "components/sync/protocol/encryption.pb.h" |
| 20 #include "components/sync/protocol/experiments_specifics.pb.h" | 21 #include "components/sync/protocol/experiments_specifics.pb.h" |
| 21 #include "components/sync/protocol/extension_setting_specifics.pb.h" | 22 #include "components/sync/protocol/extension_setting_specifics.pb.h" |
| 22 #include "components/sync/protocol/extension_specifics.pb.h" | 23 #include "components/sync/protocol/extension_specifics.pb.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 } | 108 } |
| 108 | 109 |
| 109 TEST_F(ProtoValueConversionsTest, AppNotificationToValue) { | 110 TEST_F(ProtoValueConversionsTest, AppNotificationToValue) { |
| 110 TestSpecificsToValue(AppNotificationToValue); | 111 TestSpecificsToValue(AppNotificationToValue); |
| 111 } | 112 } |
| 112 | 113 |
| 113 TEST_F(ProtoValueConversionsTest, AppSettingSpecificsToValue) { | 114 TEST_F(ProtoValueConversionsTest, AppSettingSpecificsToValue) { |
| 114 sync_pb::AppNotificationSettings specifics; | 115 sync_pb::AppNotificationSettings specifics; |
| 115 specifics.set_disabled(true); | 116 specifics.set_disabled(true); |
| 116 specifics.set_oauth_client_id("some_id_value"); | 117 specifics.set_oauth_client_id("some_id_value"); |
| 117 std::unique_ptr<base::DictionaryValue> value(AppSettingsToValue(specifics)); | 118 std::unique_ptr<base::DictionaryValue> |
| 119 value(AppNotificationSettingsToValue(specifics)); |
| 118 EXPECT_FALSE(value->empty()); | 120 EXPECT_FALSE(value->empty()); |
| 119 bool disabled_value = false; | 121 bool disabled_value = false; |
| 120 std::string oauth_client_id_value; | 122 std::string oauth_client_id_value; |
| 121 EXPECT_TRUE(value->GetBoolean("disabled", &disabled_value)); | 123 EXPECT_TRUE(value->GetBoolean("disabled", &disabled_value)); |
| 122 EXPECT_EQ(true, disabled_value); | 124 EXPECT_EQ(true, disabled_value); |
| 123 EXPECT_TRUE(value->GetString("oauth_client_id", &oauth_client_id_value)); | 125 EXPECT_TRUE(value->GetString("oauth_client_id", &oauth_client_id_value)); |
| 124 EXPECT_EQ("some_id_value", oauth_client_id_value); | 126 EXPECT_EQ("some_id_value", oauth_client_id_value); |
| 125 } | 127 } |
| 126 | 128 |
| 127 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) { | 129 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) { |
| 128 TestSpecificsToValue(AppSpecificsToValue); | 130 TestSpecificsToValue(AppSpecificsToValue); |
| 129 } | 131 } |
| 130 | 132 |
| 131 TEST_F(ProtoValueConversionsTest, ArcPackageSpecificsToValue) { | 133 TEST_F(ProtoValueConversionsTest, ArcPackageSpecificsToValue) { |
| 132 TestSpecificsToValue(ArcPackageSpecificsToValue); | 134 TestSpecificsToValue(ArcPackageSpecificsToValue); |
| 133 } | 135 } |
| 134 | 136 |
| 135 TEST_F(ProtoValueConversionsTest, AutofillSpecificsToValue) { | 137 TEST_F(ProtoValueConversionsTest, AutofillSpecificsToValue) { |
| 136 TestSpecificsToValue(AutofillSpecificsToValue); | 138 TestSpecificsToValue(AutofillSpecificsToValue); |
| 137 } | 139 } |
| 138 | 140 |
| 139 TEST_F(ProtoValueConversionsTest, AutofillProfileSpecificsToValue) { | 141 TEST_F(ProtoValueConversionsTest, AutofillProfileSpecificsToValue) { |
| 140 TestSpecificsToValue(AutofillProfileSpecificsToValue); | 142 TestSpecificsToValue(AutofillProfileSpecificsToValue); |
| 141 } | 143 } |
| 142 | 144 |
| 143 TEST_F(ProtoValueConversionsTest, AutofillWalletSpecificsToValue) { | 145 TEST_F(ProtoValueConversionsTest, AutofillWalletSpecificsToValue) { |
| 144 TestSpecificsToValue(AutofillWalletSpecificsToValue); | 146 sync_pb::AutofillWalletSpecifics specifics; |
| 147 specifics.mutable_masked_card()->set_name_on_card("Igloo"); |
| 148 specifics.mutable_address()->set_recipient_name("John"); |
| 149 |
| 150 specifics.set_type(sync_pb::AutofillWalletSpecifics::UNKNOWN); |
| 151 auto value = AutofillWalletSpecificsToValue(specifics); |
| 152 EXPECT_FALSE(value->Get("masked_card", nullptr)); |
| 153 EXPECT_FALSE(value->Get("address", nullptr)); |
| 154 |
| 155 specifics.set_type(sync_pb::AutofillWalletSpecifics::MASKED_CREDIT_CARD); |
| 156 value = AutofillWalletSpecificsToValue(specifics); |
| 157 EXPECT_TRUE(value->Get("masked_card", nullptr)); |
| 158 EXPECT_FALSE(value->Get("address", nullptr)); |
| 159 |
| 160 specifics.set_type(sync_pb::AutofillWalletSpecifics::POSTAL_ADDRESS); |
| 161 value = AutofillWalletSpecificsToValue(specifics); |
| 162 EXPECT_FALSE(value->Get("masked_card", nullptr)); |
| 163 EXPECT_TRUE(value->Get("address", nullptr)); |
| 145 } | 164 } |
| 146 | 165 |
| 147 TEST_F(ProtoValueConversionsTest, WalletMetadataSpecificsToValue) { | 166 TEST_F(ProtoValueConversionsTest, WalletMetadataSpecificsToValue) { |
| 148 TestSpecificsToValue(WalletMetadataSpecificsToValue); | 167 TestSpecificsToValue(WalletMetadataSpecificsToValue); |
| 149 } | 168 } |
| 150 | 169 |
| 151 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsToValue) { | 170 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsToValue) { |
| 152 TestSpecificsToValue(BookmarkSpecificsToValue); | 171 TestSpecificsToValue(BookmarkSpecificsToValue); |
| 153 } | 172 } |
| 154 | 173 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 217 |
| 199 TEST_F(ProtoValueConversionsTest, PriorityPreferenceSpecificsToValue) { | 218 TEST_F(ProtoValueConversionsTest, PriorityPreferenceSpecificsToValue) { |
| 200 TestSpecificsToValue(PriorityPreferenceSpecificsToValue); | 219 TestSpecificsToValue(PriorityPreferenceSpecificsToValue); |
| 201 } | 220 } |
| 202 | 221 |
| 203 TEST_F(ProtoValueConversionsTest, DeviceInfoSpecificsToValue) { | 222 TEST_F(ProtoValueConversionsTest, DeviceInfoSpecificsToValue) { |
| 204 TestSpecificsToValue(DeviceInfoSpecificsToValue); | 223 TestSpecificsToValue(DeviceInfoSpecificsToValue); |
| 205 } | 224 } |
| 206 | 225 |
| 207 TEST_F(ProtoValueConversionsTest, ExperimentsSpecificsToValue) { | 226 TEST_F(ProtoValueConversionsTest, ExperimentsSpecificsToValue) { |
| 208 TestSpecificsToValue(ExperimentsSpecificsToValue); | 227 #define TEST_EXPERIMENT_ENABLED_FIELD(field) \ |
| 228 { \ |
| 229 sync_pb::ExperimentsSpecifics specifics; \ |
| 230 specifics.mutable_##field(); \ |
| 231 auto value = ExperimentsSpecificsToValue(specifics); \ |
| 232 EXPECT_TRUE(value->empty()); \ |
| 233 } \ |
| 234 { \ |
| 235 sync_pb::ExperimentsSpecifics specifics; \ |
| 236 specifics.mutable_##field()->set_enabled(false); \ |
| 237 auto value = ExperimentsSpecificsToValue(specifics); \ |
| 238 bool field_enabled = true; \ |
| 239 EXPECT_EQ(1u, value->size()); \ |
| 240 EXPECT_TRUE(value->GetBoolean(#field, &field_enabled)); \ |
| 241 EXPECT_FALSE(field_enabled); \ |
| 242 } |
| 243 |
| 244 TEST_EXPERIMENT_ENABLED_FIELD(keystore_encryption) |
| 245 TEST_EXPERIMENT_ENABLED_FIELD(history_delete_directives) |
| 246 TEST_EXPERIMENT_ENABLED_FIELD(autofill_culling) |
| 247 TEST_EXPERIMENT_ENABLED_FIELD(pre_commit_update_avoidance) |
| 248 TEST_EXPERIMENT_ENABLED_FIELD(gcm_channel) |
| 249 TEST_EXPERIMENT_ENABLED_FIELD(gcm_invalidations) |
| 250 |
| 251 #undef TEST_EXPERIMENT_ENABLED_FIELD |
| 209 } | 252 } |
| 210 | 253 |
| 211 TEST_F(ProtoValueConversionsTest, ExtensionSettingSpecificsToValue) { | 254 TEST_F(ProtoValueConversionsTest, ExtensionSettingSpecificsToValue) { |
| 212 TestSpecificsToValue(ExtensionSettingSpecificsToValue); | 255 TestSpecificsToValue(ExtensionSettingSpecificsToValue); |
| 213 } | 256 } |
| 214 | 257 |
| 215 TEST_F(ProtoValueConversionsTest, ExtensionSpecificsToValue) { | 258 TEST_F(ProtoValueConversionsTest, ExtensionSpecificsToValue) { |
| 216 TestSpecificsToValue(ExtensionSpecificsToValue); | 259 TestSpecificsToValue(ExtensionSpecificsToValue); |
| 217 } | 260 } |
| 218 | 261 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 | 390 |
| 348 #undef SET_FIELD | 391 #undef SET_FIELD |
| 349 | 392 |
| 350 std::unique_ptr<base::DictionaryValue> value( | 393 std::unique_ptr<base::DictionaryValue> value( |
| 351 EntitySpecificsToValue(specifics)); | 394 EntitySpecificsToValue(specifics)); |
| 352 EXPECT_EQ(MODEL_TYPE_COUNT - FIRST_REAL_MODEL_TYPE - | 395 EXPECT_EQ(MODEL_TYPE_COUNT - FIRST_REAL_MODEL_TYPE - |
| 353 (LAST_PROXY_TYPE - FIRST_PROXY_TYPE + 1), | 396 (LAST_PROXY_TYPE - FIRST_PROXY_TYPE + 1), |
| 354 static_cast<int>(value->size())); | 397 static_cast<int>(value->size())); |
| 355 } | 398 } |
| 356 | 399 |
| 400 TEST_F(ProtoValueConversionsTest, UniquePositionToValue) { |
| 401 sync_pb::SyncEntity entity; |
| 402 entity.mutable_unique_position()->set_custom_compressed_v1("test"); |
| 403 |
| 404 auto value = SyncEntityToValue(entity, false); |
| 405 std::string unique_position; |
| 406 EXPECT_TRUE(value->GetString("unique_position", &unique_position)); |
| 407 |
| 408 std::string expected_unique_position = |
| 409 UniquePosition::FromProto(entity.unique_position()).ToDebugString(); |
| 410 EXPECT_EQ(expected_unique_position, unique_position); |
| 411 } |
| 412 |
| 413 TEST_F(ProtoValueConversionsTest, SyncEntityToValueIncludeSpecifics) { |
| 414 sync_pb::SyncEntity entity; |
| 415 entity.mutable_specifics(); |
| 416 |
| 417 auto value = SyncEntityToValue(entity, true /* include_specifics */); |
| 418 EXPECT_TRUE(value->GetDictionary("specifics", nullptr)); |
| 419 |
| 420 value = SyncEntityToValue(entity, false /* include_specifics */); |
| 421 EXPECT_FALSE(value->GetDictionary("specifics", nullptr)); |
| 422 } |
| 423 |
| 357 namespace { | 424 namespace { |
| 358 // Returns whether the given value has specifics under the entries in the given | 425 // Returns whether the given value has specifics under the entries in the given |
| 359 // path. | 426 // path. |
| 360 bool ValueHasSpecifics(const base::DictionaryValue& value, | 427 bool ValueHasSpecifics(const base::DictionaryValue& value, |
| 361 const std::string& path) { | 428 const std::string& path) { |
| 362 const base::ListValue* entities_list = nullptr; | 429 const base::ListValue* entities_list = nullptr; |
| 363 const base::DictionaryValue* entry_dictionary = nullptr; | 430 const base::DictionaryValue* entry_dictionary = nullptr; |
| 364 const base::DictionaryValue* specifics_dictionary = nullptr; | 431 const base::DictionaryValue* specifics_dictionary = nullptr; |
| 365 | 432 |
| 366 if (!value.GetList(path, &entities_list)) | 433 if (!value.GetList(path, &entities_list)) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()), | 481 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()), |
| 415 "get_updates.entries")); | 482 "get_updates.entries")); |
| 416 } | 483 } |
| 417 | 484 |
| 418 TEST_F(ProtoValueConversionsTest, AttachmentIdProtoToValue) { | 485 TEST_F(ProtoValueConversionsTest, AttachmentIdProtoToValue) { |
| 419 TestSpecificsToValue(AttachmentIdProtoToValue); | 486 TestSpecificsToValue(AttachmentIdProtoToValue); |
| 420 } | 487 } |
| 421 | 488 |
| 422 } // namespace | 489 } // namespace |
| 423 } // namespace syncer | 490 } // namespace syncer |
| OLD | NEW |