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
3 // found in the LICENSE file.
5 // Keep this file in sync with the .proto files in this directory.
7 #include "sync/protocol/proto_value_conversions.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/protocol/app_notification_specifics.pb.h"
15 #include "sync/protocol/app_setting_specifics.pb.h"
16 #include "sync/protocol/app_specifics.pb.h"
17 #include "sync/protocol/autofill_specifics.pb.h"
18 #include "sync/protocol/bookmark_specifics.pb.h"
19 #include "sync/protocol/device_info_specifics.pb.h"
20 #include "sync/protocol/encryption.pb.h"
21 #include "sync/protocol/experiments_specifics.pb.h"
22 #include "sync/protocol/extension_setting_specifics.pb.h"
23 #include "sync/protocol/extension_specifics.pb.h"
24 #include "sync/protocol/favicon_image_specifics.pb.h"
25 #include "sync/protocol/favicon_tracking_specifics.pb.h"
26 #include "sync/protocol/managed_user_setting_specifics.pb.h"
27 #include "sync/protocol/nigori_specifics.pb.h"
28 #include "sync/protocol/password_specifics.pb.h"
29 #include "sync/protocol/preference_specifics.pb.h"
30 #include "sync/protocol/priority_preference_specifics.pb.h"
31 #include "sync/protocol/search_engine_specifics.pb.h"
32 #include "sync/protocol/session_specifics.pb.h"
33 #include "sync/protocol/sync.pb.h"
34 #include "sync/protocol/theme_specifics.pb.h"
35 #include "sync/protocol/typed_url_specifics.pb.h"
36 #include "testing/gtest/include/gtest/gtest.h"
41 class ProtoValueConversionsTest
: public testing::Test
{
44 void TestSpecificsToValue(
45 base::DictionaryValue
* (*specifics_to_value
)(const T
&)) {
46 const T
& specifics(T::default_instance());
47 scoped_ptr
<base::DictionaryValue
> value(specifics_to_value(specifics
));
48 // We can't do much but make sure that this doesn't crash.
52 TEST_F(ProtoValueConversionsTest
, ProtoChangeCheck
) {
53 // If this number changes, that means we added or removed a data
54 // type. Don't forget to add a unit test for {New
55 // type}SpecificsToValue below.
56 EXPECT_EQ(29, MODEL_TYPE_COUNT
);
58 // We'd also like to check if we changed any field in our messages.
59 // However, that's hard to do: sizeof could work, but it's
60 // platform-dependent. default_instance().ByteSize() won't change
61 // for most changes, since most of our fields are optional. So we
62 // just settle for comments in the proto files.
65 TEST_F(ProtoValueConversionsTest
, EncryptedDataToValue
) {
66 TestSpecificsToValue(EncryptedDataToValue
);
69 TEST_F(ProtoValueConversionsTest
, SessionHeaderToValue
) {
70 TestSpecificsToValue(SessionHeaderToValue
);
73 TEST_F(ProtoValueConversionsTest
, SessionTabToValue
) {
74 TestSpecificsToValue(SessionTabToValue
);
77 TEST_F(ProtoValueConversionsTest
, SessionWindowToValue
) {
78 TestSpecificsToValue(SessionWindowToValue
);
81 TEST_F(ProtoValueConversionsTest
, TabNavigationToValue
) {
82 TestSpecificsToValue(TabNavigationToValue
);
85 TEST_F(ProtoValueConversionsTest
, PasswordSpecificsData
) {
86 sync_pb::PasswordSpecificsData specifics
;
87 specifics
.set_password_value("secret");
88 scoped_ptr
<base::DictionaryValue
> value(
89 PasswordSpecificsDataToValue(specifics
));
90 EXPECT_FALSE(value
->empty());
91 std::string password_value
;
92 EXPECT_TRUE(value
->GetString("password_value", &password_value
));
93 EXPECT_EQ("<redacted>", password_value
);
96 TEST_F(ProtoValueConversionsTest
, AppNotificationToValue
) {
97 TestSpecificsToValue(AppNotificationToValue
);
100 TEST_F(ProtoValueConversionsTest
, AppSettingSpecificsToValue
) {
101 sync_pb::AppNotificationSettings specifics
;
102 specifics
.set_disabled(true);
103 specifics
.set_oauth_client_id("some_id_value");
104 scoped_ptr
<base::DictionaryValue
> value(AppSettingsToValue(specifics
));
105 EXPECT_FALSE(value
->empty());
106 bool disabled_value
= false;
107 std::string oauth_client_id_value
;
108 EXPECT_TRUE(value
->GetBoolean("disabled", &disabled_value
));
109 EXPECT_EQ(true, disabled_value
);
110 EXPECT_TRUE(value
->GetString("oauth_client_id", &oauth_client_id_value
));
111 EXPECT_EQ("some_id_value", oauth_client_id_value
);
114 TEST_F(ProtoValueConversionsTest
, AppSpecificsToValue
) {
115 TestSpecificsToValue(AppSpecificsToValue
);
118 TEST_F(ProtoValueConversionsTest
, AutofillSpecificsToValue
) {
119 TestSpecificsToValue(AutofillSpecificsToValue
);
122 TEST_F(ProtoValueConversionsTest
, AutofillProfileSpecificsToValue
) {
123 TestSpecificsToValue(AutofillProfileSpecificsToValue
);
126 TEST_F(ProtoValueConversionsTest
, BookmarkSpecificsToValue
) {
127 TestSpecificsToValue(BookmarkSpecificsToValue
);
130 TEST_F(ProtoValueConversionsTest
, BookmarkSpecificsData
) {
131 const base::Time
creation_time(base::Time::Now());
132 const std::string icon_url
= "http://www.google.com/favicon.ico";
133 sync_pb::BookmarkSpecifics specifics
;
134 specifics
.set_creation_time_us(creation_time
.ToInternalValue());
135 specifics
.set_icon_url(icon_url
);
136 scoped_ptr
<base::DictionaryValue
> value(BookmarkSpecificsToValue(specifics
));
137 EXPECT_FALSE(value
->empty());
138 std::string encoded_time
;
139 EXPECT_TRUE(value
->GetString("creation_time_us", &encoded_time
));
140 EXPECT_EQ(base::Int64ToString(creation_time
.ToInternalValue()), encoded_time
);
141 std::string encoded_icon_url
;
142 EXPECT_TRUE(value
->GetString("icon_url", &encoded_icon_url
));
143 EXPECT_EQ(icon_url
, encoded_icon_url
);
146 TEST_F(ProtoValueConversionsTest
, PriorityPreferenceSpecificsToValue
) {
147 TestSpecificsToValue(PriorityPreferenceSpecificsToValue
);
150 TEST_F(ProtoValueConversionsTest
, DeviceInfoSpecificsToValue
) {
151 TestSpecificsToValue(DeviceInfoSpecificsToValue
);
154 TEST_F(ProtoValueConversionsTest
, ExperimentsSpecificsToValue
) {
155 TestSpecificsToValue(ExperimentsSpecificsToValue
);
158 TEST_F(ProtoValueConversionsTest
, ExtensionSettingSpecificsToValue
) {
159 TestSpecificsToValue(ExtensionSettingSpecificsToValue
);
162 TEST_F(ProtoValueConversionsTest
, ExtensionSpecificsToValue
) {
163 TestSpecificsToValue(ExtensionSpecificsToValue
);
166 TEST_F(ProtoValueConversionsTest
, FaviconImageSpecificsToValue
) {
167 TestSpecificsToValue(FaviconImageSpecificsToValue
);
170 TEST_F(ProtoValueConversionsTest
, FaviconTrackingSpecificsToValue
) {
171 TestSpecificsToValue(FaviconTrackingSpecificsToValue
);
174 TEST_F(ProtoValueConversionsTest
, HistoryDeleteDirectiveSpecificsToValue
) {
175 TestSpecificsToValue(HistoryDeleteDirectiveSpecificsToValue
);
178 TEST_F(ProtoValueConversionsTest
, ManagedUserSettingSpecificsToValue
) {
179 TestSpecificsToValue(ManagedUserSettingSpecificsToValue
);
182 TEST_F(ProtoValueConversionsTest
, ManagedUserSpecificsToValue
) {
183 TestSpecificsToValue(ManagedUserSpecificsToValue
);
186 TEST_F(ProtoValueConversionsTest
, NigoriSpecificsToValue
) {
187 TestSpecificsToValue(NigoriSpecificsToValue
);
190 TEST_F(ProtoValueConversionsTest
, PasswordSpecificsToValue
) {
191 TestSpecificsToValue(PasswordSpecificsToValue
);
194 TEST_F(ProtoValueConversionsTest
, PreferenceSpecificsToValue
) {
195 TestSpecificsToValue(PreferenceSpecificsToValue
);
198 TEST_F(ProtoValueConversionsTest
, SearchEngineSpecificsToValue
) {
199 TestSpecificsToValue(SearchEngineSpecificsToValue
);
202 TEST_F(ProtoValueConversionsTest
, SessionSpecificsToValue
) {
203 TestSpecificsToValue(SessionSpecificsToValue
);
206 TEST_F(ProtoValueConversionsTest
, SyncedNotificationSpecificsToValue
) {
207 TestSpecificsToValue(SyncedNotificationSpecificsToValue
);
210 TEST_F(ProtoValueConversionsTest
, ThemeSpecificsToValue
) {
211 TestSpecificsToValue(ThemeSpecificsToValue
);
214 TEST_F(ProtoValueConversionsTest
, TypedUrlSpecificsToValue
) {
215 TestSpecificsToValue(TypedUrlSpecificsToValue
);
218 TEST_F(ProtoValueConversionsTest
, DictionarySpecificsToValue
) {
219 TestSpecificsToValue(DictionarySpecificsToValue
);
222 TEST_F(ProtoValueConversionsTest
, ArticleSpecificsToValue
) {
223 TestSpecificsToValue(ArticleSpecificsToValue
);
226 // TODO(akalin): Figure out how to better test EntitySpecificsToValue.
228 TEST_F(ProtoValueConversionsTest
, EntitySpecificsToValue
) {
229 sync_pb::EntitySpecifics specifics
;
230 // Touch the extensions to make sure it shows up in the generated
232 #define SET_FIELD(key) (void)specifics.mutable_##key()
235 SET_FIELD(app_notification
);
236 SET_FIELD(app_setting
);
239 SET_FIELD(autofill_profile
);
241 SET_FIELD(device_info
);
242 SET_FIELD(dictionary
);
243 SET_FIELD(experiments
);
244 SET_FIELD(extension
);
245 SET_FIELD(extension_setting
);
246 SET_FIELD(favicon_image
);
247 SET_FIELD(favicon_tracking
);
248 SET_FIELD(history_delete_directive
);
249 SET_FIELD(managed_user_setting
);
250 SET_FIELD(managed_user
);
253 SET_FIELD(preference
);
254 SET_FIELD(priority_preference
);
255 SET_FIELD(search_engine
);
257 SET_FIELD(synced_notification
);
259 SET_FIELD(typed_url
);
263 scoped_ptr
<base::DictionaryValue
> value(EntitySpecificsToValue(specifics
));
264 EXPECT_EQ(MODEL_TYPE_COUNT
- FIRST_REAL_MODEL_TYPE
-
265 (LAST_PROXY_TYPE
- FIRST_PROXY_TYPE
+ 1),
266 static_cast<int>(value
->size()));
270 // Returns whether the given value has specifics under the entries in the given
272 bool ValueHasSpecifics(const base::DictionaryValue
& value
,
273 const std::string
& path
) {
274 const base::ListValue
* entities_list
= NULL
;
275 const base::DictionaryValue
* entry_dictionary
= NULL
;
276 const base::DictionaryValue
* specifics_dictionary
= NULL
;
278 if (!value
.GetList(path
, &entities_list
))
281 if (!entities_list
->GetDictionary(0, &entry_dictionary
))
284 return entry_dictionary
->GetDictionary("specifics",
285 &specifics_dictionary
);
289 // Create a ClientToServerMessage with an EntitySpecifics. Converting it to
290 // a value should respect the |include_specifics| flag.
291 TEST_F(ProtoValueConversionsTest
, ClientToServerMessageToValue
) {
292 sync_pb::ClientToServerMessage message
;
293 sync_pb::CommitMessage
* commit_message
= message
.mutable_commit();
294 sync_pb::SyncEntity
* entity
= commit_message
->add_entries();
295 entity
->mutable_specifics();
297 scoped_ptr
<base::DictionaryValue
> value_with_specifics(
298 ClientToServerMessageToValue(message
, true /* include_specifics */));
299 EXPECT_FALSE(value_with_specifics
->empty());
300 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics
.get()),
303 scoped_ptr
<base::DictionaryValue
> value_without_specifics(
304 ClientToServerMessageToValue(message
, false /* include_specifics */));
305 EXPECT_FALSE(value_without_specifics
->empty());
306 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics
.get()),
310 // Create a ClientToServerResponse with an EntitySpecifics. Converting it to
311 // a value should respect the |include_specifics| flag.
312 TEST_F(ProtoValueConversionsTest
, ClientToServerResponseToValue
) {
313 sync_pb::ClientToServerResponse message
;
314 sync_pb::GetUpdatesResponse
* response
= message
.mutable_get_updates();
315 sync_pb::SyncEntity
* entity
= response
->add_entries();
316 entity
->mutable_specifics();
318 scoped_ptr
<base::DictionaryValue
> value_with_specifics(
319 ClientToServerResponseToValue(message
, true /* include_specifics */));
320 EXPECT_FALSE(value_with_specifics
->empty());
321 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics
.get()),
322 "get_updates.entries"));
324 scoped_ptr
<base::DictionaryValue
> value_without_specifics(
325 ClientToServerResponseToValue(message
, false /* include_specifics */));
326 EXPECT_FALSE(value_without_specifics
->empty());
327 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics
.get()),
328 "get_updates.entries"));
332 } // namespace syncer