| 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 // Keep this file in sync with the .proto files in this directory. | 5 // Keep this file in sync with the .proto files in this directory. |
| 6 | 6 |
| 7 #include "sync/protocol/proto_value_conversions.h" | 7 #include "sync/protocol/proto_value_conversions.h" |
| 8 | 8 |
| 9 #include <memory> |
| 9 #include <string> | 10 #include <string> |
| 10 | 11 |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "sync/internal_api/public/base/model_type.h" | 15 #include "sync/internal_api/public/base/model_type.h" |
| 16 #include "sync/protocol/app_notification_specifics.pb.h" | 16 #include "sync/protocol/app_notification_specifics.pb.h" |
| 17 #include "sync/protocol/app_setting_specifics.pb.h" | 17 #include "sync/protocol/app_setting_specifics.pb.h" |
| 18 #include "sync/protocol/app_specifics.pb.h" | 18 #include "sync/protocol/app_specifics.pb.h" |
| 19 #include "sync/protocol/autofill_specifics.pb.h" | 19 #include "sync/protocol/autofill_specifics.pb.h" |
| 20 #include "sync/protocol/bookmark_specifics.pb.h" | 20 #include "sync/protocol/bookmark_specifics.pb.h" |
| 21 #include "sync/protocol/device_info_specifics.pb.h" | 21 #include "sync/protocol/device_info_specifics.pb.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 41 #include "sync/protocol/wifi_credential_specifics.pb.h" | 41 #include "sync/protocol/wifi_credential_specifics.pb.h" |
| 42 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 43 | 43 |
| 44 namespace syncer { | 44 namespace syncer { |
| 45 namespace { | 45 namespace { |
| 46 | 46 |
| 47 class ProtoValueConversionsTest : public testing::Test { | 47 class ProtoValueConversionsTest : public testing::Test { |
| 48 protected: | 48 protected: |
| 49 template <class T> | 49 template <class T> |
| 50 void TestSpecificsToValue( | 50 void TestSpecificsToValue( |
| 51 scoped_ptr<base::DictionaryValue>(*specifics_to_value)(const T&)) { | 51 std::unique_ptr<base::DictionaryValue> (*specifics_to_value)(const T&)) { |
| 52 const T& specifics(T::default_instance()); | 52 const T& specifics(T::default_instance()); |
| 53 scoped_ptr<base::DictionaryValue> value = specifics_to_value(specifics); | 53 std::unique_ptr<base::DictionaryValue> value = |
| 54 specifics_to_value(specifics); |
| 54 // We can't do much but make sure that this doesn't crash. | 55 // We can't do much but make sure that this doesn't crash. |
| 55 } | 56 } |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 TEST_F(ProtoValueConversionsTest, ProtoChangeCheck) { | 59 TEST_F(ProtoValueConversionsTest, ProtoChangeCheck) { |
| 59 // If this number changes, that means we added or removed a data | 60 // If this number changes, that means we added or removed a data |
| 60 // type. Don't forget to add a unit test for {New | 61 // type. Don't forget to add a unit test for {New |
| 61 // type}SpecificsToValue below. | 62 // type}SpecificsToValue below. |
| 62 EXPECT_EQ(36, MODEL_TYPE_COUNT); | 63 EXPECT_EQ(36, MODEL_TYPE_COUNT); |
| 63 | 64 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 88 TestSpecificsToValue(TabNavigationToValue); | 89 TestSpecificsToValue(TabNavigationToValue); |
| 89 } | 90 } |
| 90 | 91 |
| 91 TEST_F(ProtoValueConversionsTest, NavigationRedirectToValue) { | 92 TEST_F(ProtoValueConversionsTest, NavigationRedirectToValue) { |
| 92 TestSpecificsToValue(NavigationRedirectToValue); | 93 TestSpecificsToValue(NavigationRedirectToValue); |
| 93 } | 94 } |
| 94 | 95 |
| 95 TEST_F(ProtoValueConversionsTest, PasswordSpecificsData) { | 96 TEST_F(ProtoValueConversionsTest, PasswordSpecificsData) { |
| 96 sync_pb::PasswordSpecificsData specifics; | 97 sync_pb::PasswordSpecificsData specifics; |
| 97 specifics.set_password_value("secret"); | 98 specifics.set_password_value("secret"); |
| 98 scoped_ptr<base::DictionaryValue> value( | 99 std::unique_ptr<base::DictionaryValue> value( |
| 99 PasswordSpecificsDataToValue(specifics)); | 100 PasswordSpecificsDataToValue(specifics)); |
| 100 EXPECT_FALSE(value->empty()); | 101 EXPECT_FALSE(value->empty()); |
| 101 std::string password_value; | 102 std::string password_value; |
| 102 EXPECT_TRUE(value->GetString("password_value", &password_value)); | 103 EXPECT_TRUE(value->GetString("password_value", &password_value)); |
| 103 EXPECT_EQ("<redacted>", password_value); | 104 EXPECT_EQ("<redacted>", password_value); |
| 104 } | 105 } |
| 105 | 106 |
| 106 TEST_F(ProtoValueConversionsTest, AppListSpecificsToValue) { | 107 TEST_F(ProtoValueConversionsTest, AppListSpecificsToValue) { |
| 107 TestSpecificsToValue(AppListSpecificsToValue); | 108 TestSpecificsToValue(AppListSpecificsToValue); |
| 108 } | 109 } |
| 109 | 110 |
| 110 TEST_F(ProtoValueConversionsTest, AppNotificationToValue) { | 111 TEST_F(ProtoValueConversionsTest, AppNotificationToValue) { |
| 111 TestSpecificsToValue(AppNotificationToValue); | 112 TestSpecificsToValue(AppNotificationToValue); |
| 112 } | 113 } |
| 113 | 114 |
| 114 TEST_F(ProtoValueConversionsTest, AppSettingSpecificsToValue) { | 115 TEST_F(ProtoValueConversionsTest, AppSettingSpecificsToValue) { |
| 115 sync_pb::AppNotificationSettings specifics; | 116 sync_pb::AppNotificationSettings specifics; |
| 116 specifics.set_disabled(true); | 117 specifics.set_disabled(true); |
| 117 specifics.set_oauth_client_id("some_id_value"); | 118 specifics.set_oauth_client_id("some_id_value"); |
| 118 scoped_ptr<base::DictionaryValue> value(AppSettingsToValue(specifics)); | 119 std::unique_ptr<base::DictionaryValue> value(AppSettingsToValue(specifics)); |
| 119 EXPECT_FALSE(value->empty()); | 120 EXPECT_FALSE(value->empty()); |
| 120 bool disabled_value = false; | 121 bool disabled_value = false; |
| 121 std::string oauth_client_id_value; | 122 std::string oauth_client_id_value; |
| 122 EXPECT_TRUE(value->GetBoolean("disabled", &disabled_value)); | 123 EXPECT_TRUE(value->GetBoolean("disabled", &disabled_value)); |
| 123 EXPECT_EQ(true, disabled_value); | 124 EXPECT_EQ(true, disabled_value); |
| 124 EXPECT_TRUE(value->GetString("oauth_client_id", &oauth_client_id_value)); | 125 EXPECT_TRUE(value->GetString("oauth_client_id", &oauth_client_id_value)); |
| 125 EXPECT_EQ("some_id_value", oauth_client_id_value); | 126 EXPECT_EQ("some_id_value", oauth_client_id_value); |
| 126 } | 127 } |
| 127 | 128 |
| 128 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) { | 129 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 155 sync_pb::BookmarkSpecifics specifics; | 156 sync_pb::BookmarkSpecifics specifics; |
| 156 specifics.set_creation_time_us(creation_time.ToInternalValue()); | 157 specifics.set_creation_time_us(creation_time.ToInternalValue()); |
| 157 specifics.set_icon_url(icon_url); | 158 specifics.set_icon_url(icon_url); |
| 158 sync_pb::MetaInfo* meta_1 = specifics.add_meta_info(); | 159 sync_pb::MetaInfo* meta_1 = specifics.add_meta_info(); |
| 159 meta_1->set_key("key1"); | 160 meta_1->set_key("key1"); |
| 160 meta_1->set_value("value1"); | 161 meta_1->set_value("value1"); |
| 161 sync_pb::MetaInfo* meta_2 = specifics.add_meta_info(); | 162 sync_pb::MetaInfo* meta_2 = specifics.add_meta_info(); |
| 162 meta_2->set_key("key2"); | 163 meta_2->set_key("key2"); |
| 163 meta_2->set_value("value2"); | 164 meta_2->set_value("value2"); |
| 164 | 165 |
| 165 scoped_ptr<base::DictionaryValue> value(BookmarkSpecificsToValue(specifics)); | 166 std::unique_ptr<base::DictionaryValue> value( |
| 167 BookmarkSpecificsToValue(specifics)); |
| 166 EXPECT_FALSE(value->empty()); | 168 EXPECT_FALSE(value->empty()); |
| 167 std::string encoded_time; | 169 std::string encoded_time; |
| 168 EXPECT_TRUE(value->GetString("creation_time_us", &encoded_time)); | 170 EXPECT_TRUE(value->GetString("creation_time_us", &encoded_time)); |
| 169 EXPECT_EQ(base::Int64ToString(creation_time.ToInternalValue()), encoded_time); | 171 EXPECT_EQ(base::Int64ToString(creation_time.ToInternalValue()), encoded_time); |
| 170 std::string encoded_icon_url; | 172 std::string encoded_icon_url; |
| 171 EXPECT_TRUE(value->GetString("icon_url", &encoded_icon_url)); | 173 EXPECT_TRUE(value->GetString("icon_url", &encoded_icon_url)); |
| 172 EXPECT_EQ(icon_url, encoded_icon_url); | 174 EXPECT_EQ(icon_url, encoded_icon_url); |
| 173 base::ListValue* meta_info_list; | 175 base::ListValue* meta_info_list; |
| 174 ASSERT_TRUE(value->GetList("meta_info", &meta_info_list)); | 176 ASSERT_TRUE(value->GetList("meta_info", &meta_info_list)); |
| 175 EXPECT_EQ(2u, meta_info_list->GetSize()); | 177 EXPECT_EQ(2u, meta_info_list->GetSize()); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 SET_FIELD(synced_notification); | 327 SET_FIELD(synced_notification); |
| 326 SET_FIELD(synced_notification_app_info); | 328 SET_FIELD(synced_notification_app_info); |
| 327 SET_FIELD(theme); | 329 SET_FIELD(theme); |
| 328 SET_FIELD(typed_url); | 330 SET_FIELD(typed_url); |
| 329 SET_FIELD(wifi_credential); | 331 SET_FIELD(wifi_credential); |
| 330 SET_FIELD(autofill_wallet); | 332 SET_FIELD(autofill_wallet); |
| 331 SET_FIELD(wallet_metadata); | 333 SET_FIELD(wallet_metadata); |
| 332 | 334 |
| 333 #undef SET_FIELD | 335 #undef SET_FIELD |
| 334 | 336 |
| 335 scoped_ptr<base::DictionaryValue> value(EntitySpecificsToValue(specifics)); | 337 std::unique_ptr<base::DictionaryValue> value( |
| 338 EntitySpecificsToValue(specifics)); |
| 336 EXPECT_EQ(MODEL_TYPE_COUNT - FIRST_REAL_MODEL_TYPE - | 339 EXPECT_EQ(MODEL_TYPE_COUNT - FIRST_REAL_MODEL_TYPE - |
| 337 (LAST_PROXY_TYPE - FIRST_PROXY_TYPE + 1), | 340 (LAST_PROXY_TYPE - FIRST_PROXY_TYPE + 1), |
| 338 static_cast<int>(value->size())); | 341 static_cast<int>(value->size())); |
| 339 } | 342 } |
| 340 | 343 |
| 341 namespace { | 344 namespace { |
| 342 // Returns whether the given value has specifics under the entries in the given | 345 // Returns whether the given value has specifics under the entries in the given |
| 343 // path. | 346 // path. |
| 344 bool ValueHasSpecifics(const base::DictionaryValue& value, | 347 bool ValueHasSpecifics(const base::DictionaryValue& value, |
| 345 const std::string& path) { | 348 const std::string& path) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 359 } // namespace | 362 } // namespace |
| 360 | 363 |
| 361 // Create a ClientToServerMessage with an EntitySpecifics. Converting it to | 364 // Create a ClientToServerMessage with an EntitySpecifics. Converting it to |
| 362 // a value should respect the |include_specifics| flag. | 365 // a value should respect the |include_specifics| flag. |
| 363 TEST_F(ProtoValueConversionsTest, ClientToServerMessageToValue) { | 366 TEST_F(ProtoValueConversionsTest, ClientToServerMessageToValue) { |
| 364 sync_pb::ClientToServerMessage message; | 367 sync_pb::ClientToServerMessage message; |
| 365 sync_pb::CommitMessage* commit_message = message.mutable_commit(); | 368 sync_pb::CommitMessage* commit_message = message.mutable_commit(); |
| 366 sync_pb::SyncEntity* entity = commit_message->add_entries(); | 369 sync_pb::SyncEntity* entity = commit_message->add_entries(); |
| 367 entity->mutable_specifics(); | 370 entity->mutable_specifics(); |
| 368 | 371 |
| 369 scoped_ptr<base::DictionaryValue> value_with_specifics( | 372 std::unique_ptr<base::DictionaryValue> value_with_specifics( |
| 370 ClientToServerMessageToValue(message, true /* include_specifics */)); | 373 ClientToServerMessageToValue(message, true /* include_specifics */)); |
| 371 EXPECT_FALSE(value_with_specifics->empty()); | 374 EXPECT_FALSE(value_with_specifics->empty()); |
| 372 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()), | 375 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()), |
| 373 "commit.entries")); | 376 "commit.entries")); |
| 374 | 377 |
| 375 scoped_ptr<base::DictionaryValue> value_without_specifics( | 378 std::unique_ptr<base::DictionaryValue> value_without_specifics( |
| 376 ClientToServerMessageToValue(message, false /* include_specifics */)); | 379 ClientToServerMessageToValue(message, false /* include_specifics */)); |
| 377 EXPECT_FALSE(value_without_specifics->empty()); | 380 EXPECT_FALSE(value_without_specifics->empty()); |
| 378 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()), | 381 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()), |
| 379 "commit.entries")); | 382 "commit.entries")); |
| 380 } | 383 } |
| 381 | 384 |
| 382 // Create a ClientToServerResponse with an EntitySpecifics. Converting it to | 385 // Create a ClientToServerResponse with an EntitySpecifics. Converting it to |
| 383 // a value should respect the |include_specifics| flag. | 386 // a value should respect the |include_specifics| flag. |
| 384 TEST_F(ProtoValueConversionsTest, ClientToServerResponseToValue) { | 387 TEST_F(ProtoValueConversionsTest, ClientToServerResponseToValue) { |
| 385 sync_pb::ClientToServerResponse message; | 388 sync_pb::ClientToServerResponse message; |
| 386 sync_pb::GetUpdatesResponse* response = message.mutable_get_updates(); | 389 sync_pb::GetUpdatesResponse* response = message.mutable_get_updates(); |
| 387 sync_pb::SyncEntity* entity = response->add_entries(); | 390 sync_pb::SyncEntity* entity = response->add_entries(); |
| 388 entity->mutable_specifics(); | 391 entity->mutable_specifics(); |
| 389 | 392 |
| 390 scoped_ptr<base::DictionaryValue> value_with_specifics( | 393 std::unique_ptr<base::DictionaryValue> value_with_specifics( |
| 391 ClientToServerResponseToValue(message, true /* include_specifics */)); | 394 ClientToServerResponseToValue(message, true /* include_specifics */)); |
| 392 EXPECT_FALSE(value_with_specifics->empty()); | 395 EXPECT_FALSE(value_with_specifics->empty()); |
| 393 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()), | 396 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()), |
| 394 "get_updates.entries")); | 397 "get_updates.entries")); |
| 395 | 398 |
| 396 scoped_ptr<base::DictionaryValue> value_without_specifics( | 399 std::unique_ptr<base::DictionaryValue> value_without_specifics( |
| 397 ClientToServerResponseToValue(message, false /* include_specifics */)); | 400 ClientToServerResponseToValue(message, false /* include_specifics */)); |
| 398 EXPECT_FALSE(value_without_specifics->empty()); | 401 EXPECT_FALSE(value_without_specifics->empty()); |
| 399 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()), | 402 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()), |
| 400 "get_updates.entries")); | 403 "get_updates.entries")); |
| 401 } | 404 } |
| 402 | 405 |
| 403 TEST_F(ProtoValueConversionsTest, AttachmentIdProtoToValue) { | 406 TEST_F(ProtoValueConversionsTest, AttachmentIdProtoToValue) { |
| 404 TestSpecificsToValue(AttachmentIdProtoToValue); | 407 TestSpecificsToValue(AttachmentIdProtoToValue); |
| 405 } | 408 } |
| 406 | 409 |
| 407 } // namespace | 410 } // namespace |
| 408 } // namespace syncer | 411 } // namespace syncer |
| OLD | NEW |