Remove link_chrome_on_windows GN flag.
[chromium-blink-merge.git] / sync / protocol / proto_value_conversions_unittest.cc
blobd52223bcc7318ac71640831656783cfeb8d526c6
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/managed_user_shared_setting_specifics.pb.h"
28 #include "sync/protocol/managed_user_specifics.pb.h"
29 #include "sync/protocol/managed_user_whitelist_specifics.pb.h"
30 #include "sync/protocol/nigori_specifics.pb.h"
31 #include "sync/protocol/password_specifics.pb.h"
32 #include "sync/protocol/preference_specifics.pb.h"
33 #include "sync/protocol/priority_preference_specifics.pb.h"
34 #include "sync/protocol/search_engine_specifics.pb.h"
35 #include "sync/protocol/session_specifics.pb.h"
36 #include "sync/protocol/sync.pb.h"
37 #include "sync/protocol/theme_specifics.pb.h"
38 #include "sync/protocol/typed_url_specifics.pb.h"
39 #include "sync/protocol/wifi_credential_specifics.pb.h"
40 #include "testing/gtest/include/gtest/gtest.h"
42 namespace syncer {
43 namespace {
45 class ProtoValueConversionsTest : public testing::Test {
46 protected:
47 template <class T>
48 void TestSpecificsToValue(
49 base::DictionaryValue* (*specifics_to_value)(const T&)) {
50 const T& specifics(T::default_instance());
51 scoped_ptr<base::DictionaryValue> value(specifics_to_value(specifics));
52 // We can't do much but make sure that this doesn't crash.
56 TEST_F(ProtoValueConversionsTest, ProtoChangeCheck) {
57 // If this number changes, that means we added or removed a data
58 // type. Don't forget to add a unit test for {New
59 // type}SpecificsToValue below.
60 EXPECT_EQ(35, MODEL_TYPE_COUNT);
62 // We'd also like to check if we changed any field in our messages.
63 // However, that's hard to do: sizeof could work, but it's
64 // platform-dependent. default_instance().ByteSize() won't change
65 // for most changes, since most of our fields are optional. So we
66 // just settle for comments in the proto files.
69 TEST_F(ProtoValueConversionsTest, EncryptedDataToValue) {
70 TestSpecificsToValue(EncryptedDataToValue);
73 TEST_F(ProtoValueConversionsTest, SessionHeaderToValue) {
74 TestSpecificsToValue(SessionHeaderToValue);
77 TEST_F(ProtoValueConversionsTest, SessionTabToValue) {
78 TestSpecificsToValue(SessionTabToValue);
81 TEST_F(ProtoValueConversionsTest, SessionWindowToValue) {
82 TestSpecificsToValue(SessionWindowToValue);
85 TEST_F(ProtoValueConversionsTest, TabNavigationToValue) {
86 TestSpecificsToValue(TabNavigationToValue);
89 TEST_F(ProtoValueConversionsTest, NavigationRedirectToValue) {
90 TestSpecificsToValue(NavigationRedirectToValue);
93 TEST_F(ProtoValueConversionsTest, PasswordSpecificsData) {
94 sync_pb::PasswordSpecificsData specifics;
95 specifics.set_password_value("secret");
96 scoped_ptr<base::DictionaryValue> value(
97 PasswordSpecificsDataToValue(specifics));
98 EXPECT_FALSE(value->empty());
99 std::string password_value;
100 EXPECT_TRUE(value->GetString("password_value", &password_value));
101 EXPECT_EQ("<redacted>", password_value);
104 TEST_F(ProtoValueConversionsTest, AppListSpecificsToValue) {
105 TestSpecificsToValue(AppListSpecificsToValue);
108 TEST_F(ProtoValueConversionsTest, AppNotificationToValue) {
109 TestSpecificsToValue(AppNotificationToValue);
112 TEST_F(ProtoValueConversionsTest, AppSettingSpecificsToValue) {
113 sync_pb::AppNotificationSettings specifics;
114 specifics.set_disabled(true);
115 specifics.set_oauth_client_id("some_id_value");
116 scoped_ptr<base::DictionaryValue> value(AppSettingsToValue(specifics));
117 EXPECT_FALSE(value->empty());
118 bool disabled_value = false;
119 std::string oauth_client_id_value;
120 EXPECT_TRUE(value->GetBoolean("disabled", &disabled_value));
121 EXPECT_EQ(true, disabled_value);
122 EXPECT_TRUE(value->GetString("oauth_client_id", &oauth_client_id_value));
123 EXPECT_EQ("some_id_value", oauth_client_id_value);
126 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) {
127 TestSpecificsToValue(AppSpecificsToValue);
130 TEST_F(ProtoValueConversionsTest, AutofillSpecificsToValue) {
131 TestSpecificsToValue(AutofillSpecificsToValue);
134 TEST_F(ProtoValueConversionsTest, AutofillProfileSpecificsToValue) {
135 TestSpecificsToValue(AutofillProfileSpecificsToValue);
138 TEST_F(ProtoValueConversionsTest, AutofillWalletSpecificsToValue) {
139 TestSpecificsToValue(AutofillWalletSpecificsToValue);
142 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsToValue) {
143 TestSpecificsToValue(BookmarkSpecificsToValue);
146 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsData) {
147 const base::Time creation_time(base::Time::Now());
148 const std::string icon_url = "http://www.google.com/favicon.ico";
149 sync_pb::BookmarkSpecifics specifics;
150 specifics.set_creation_time_us(creation_time.ToInternalValue());
151 specifics.set_icon_url(icon_url);
152 sync_pb::MetaInfo* meta_1 = specifics.add_meta_info();
153 meta_1->set_key("key1");
154 meta_1->set_value("value1");
155 sync_pb::MetaInfo* meta_2 = specifics.add_meta_info();
156 meta_2->set_key("key2");
157 meta_2->set_value("value2");
159 scoped_ptr<base::DictionaryValue> value(BookmarkSpecificsToValue(specifics));
160 EXPECT_FALSE(value->empty());
161 std::string encoded_time;
162 EXPECT_TRUE(value->GetString("creation_time_us", &encoded_time));
163 EXPECT_EQ(base::Int64ToString(creation_time.ToInternalValue()), encoded_time);
164 std::string encoded_icon_url;
165 EXPECT_TRUE(value->GetString("icon_url", &encoded_icon_url));
166 EXPECT_EQ(icon_url, encoded_icon_url);
167 base::ListValue* meta_info_list;
168 ASSERT_TRUE(value->GetList("meta_info", &meta_info_list));
169 EXPECT_EQ(2u, meta_info_list->GetSize());
170 base::DictionaryValue* meta_info;
171 std::string meta_key;
172 std::string meta_value;
173 ASSERT_TRUE(meta_info_list->GetDictionary(0, &meta_info));
174 EXPECT_TRUE(meta_info->GetString("key", &meta_key));
175 EXPECT_TRUE(meta_info->GetString("value", &meta_value));
176 EXPECT_EQ("key1", meta_key);
177 EXPECT_EQ("value1", meta_value);
178 ASSERT_TRUE(meta_info_list->GetDictionary(1, &meta_info));
179 EXPECT_TRUE(meta_info->GetString("key", &meta_key));
180 EXPECT_TRUE(meta_info->GetString("value", &meta_value));
181 EXPECT_EQ("key2", meta_key);
182 EXPECT_EQ("value2", meta_value);
185 TEST_F(ProtoValueConversionsTest, PriorityPreferenceSpecificsToValue) {
186 TestSpecificsToValue(PriorityPreferenceSpecificsToValue);
189 TEST_F(ProtoValueConversionsTest, DeviceInfoSpecificsToValue) {
190 TestSpecificsToValue(DeviceInfoSpecificsToValue);
193 TEST_F(ProtoValueConversionsTest, ExperimentsSpecificsToValue) {
194 TestSpecificsToValue(ExperimentsSpecificsToValue);
197 TEST_F(ProtoValueConversionsTest, ExtensionSettingSpecificsToValue) {
198 TestSpecificsToValue(ExtensionSettingSpecificsToValue);
201 TEST_F(ProtoValueConversionsTest, ExtensionSpecificsToValue) {
202 TestSpecificsToValue(ExtensionSpecificsToValue);
205 TEST_F(ProtoValueConversionsTest, FaviconImageSpecificsToValue) {
206 TestSpecificsToValue(FaviconImageSpecificsToValue);
209 TEST_F(ProtoValueConversionsTest, FaviconTrackingSpecificsToValue) {
210 TestSpecificsToValue(FaviconTrackingSpecificsToValue);
213 TEST_F(ProtoValueConversionsTest, HistoryDeleteDirectiveSpecificsToValue) {
214 TestSpecificsToValue(HistoryDeleteDirectiveSpecificsToValue);
217 TEST_F(ProtoValueConversionsTest, ManagedUserSettingSpecificsToValue) {
218 TestSpecificsToValue(ManagedUserSettingSpecificsToValue);
221 TEST_F(ProtoValueConversionsTest, ManagedUserSpecificsToValue) {
222 TestSpecificsToValue(ManagedUserSpecificsToValue);
225 TEST_F(ProtoValueConversionsTest, ManagedUserSharedSettingSpecificsToValue) {
226 TestSpecificsToValue(ManagedUserSharedSettingSpecificsToValue);
229 TEST_F(ProtoValueConversionsTest, ManagedUserWhitelistSpecificsToValue) {
230 TestSpecificsToValue(ManagedUserWhitelistSpecificsToValue);
233 TEST_F(ProtoValueConversionsTest, NigoriSpecificsToValue) {
234 TestSpecificsToValue(NigoriSpecificsToValue);
237 TEST_F(ProtoValueConversionsTest, PasswordSpecificsToValue) {
238 TestSpecificsToValue(PasswordSpecificsToValue);
241 TEST_F(ProtoValueConversionsTest, PreferenceSpecificsToValue) {
242 TestSpecificsToValue(PreferenceSpecificsToValue);
245 TEST_F(ProtoValueConversionsTest, SearchEngineSpecificsToValue) {
246 TestSpecificsToValue(SearchEngineSpecificsToValue);
249 TEST_F(ProtoValueConversionsTest, SessionSpecificsToValue) {
250 TestSpecificsToValue(SessionSpecificsToValue);
253 TEST_F(ProtoValueConversionsTest, SyncedNotificationAppInfoSpecificsToValue) {
254 TestSpecificsToValue(SyncedNotificationAppInfoSpecificsToValue);
257 TEST_F(ProtoValueConversionsTest, SyncedNotificationSpecificsToValue) {
258 TestSpecificsToValue(SyncedNotificationSpecificsToValue);
261 TEST_F(ProtoValueConversionsTest, ThemeSpecificsToValue) {
262 TestSpecificsToValue(ThemeSpecificsToValue);
265 TEST_F(ProtoValueConversionsTest, TypedUrlSpecificsToValue) {
266 TestSpecificsToValue(TypedUrlSpecificsToValue);
269 TEST_F(ProtoValueConversionsTest, DictionarySpecificsToValue) {
270 TestSpecificsToValue(DictionarySpecificsToValue);
273 TEST_F(ProtoValueConversionsTest, ArticleSpecificsToValue) {
274 TestSpecificsToValue(ArticleSpecificsToValue);
277 TEST_F(ProtoValueConversionsTest, WifiCredentialSpecificsToValue) {
278 TestSpecificsToValue(WifiCredentialSpecificsToValue);
281 // TODO(akalin): Figure out how to better test EntitySpecificsToValue.
283 TEST_F(ProtoValueConversionsTest, EntitySpecificsToValue) {
284 sync_pb::EntitySpecifics specifics;
285 // Touch the extensions to make sure it shows up in the generated
286 // value.
287 #define SET_FIELD(key) (void)specifics.mutable_##key()
289 SET_FIELD(app);
290 SET_FIELD(app_list);
291 SET_FIELD(app_notification);
292 SET_FIELD(app_setting);
293 SET_FIELD(article);
294 SET_FIELD(autofill);
295 SET_FIELD(autofill_profile);
296 SET_FIELD(bookmark);
297 SET_FIELD(device_info);
298 SET_FIELD(dictionary);
299 SET_FIELD(experiments);
300 SET_FIELD(extension);
301 SET_FIELD(extension_setting);
302 SET_FIELD(favicon_image);
303 SET_FIELD(favicon_tracking);
304 SET_FIELD(history_delete_directive);
305 SET_FIELD(managed_user_setting);
306 SET_FIELD(managed_user_shared_setting);
307 SET_FIELD(managed_user_whitelist);
308 SET_FIELD(managed_user);
309 SET_FIELD(nigori);
310 SET_FIELD(password);
311 SET_FIELD(preference);
312 SET_FIELD(priority_preference);
313 SET_FIELD(search_engine);
314 SET_FIELD(session);
315 SET_FIELD(synced_notification);
316 SET_FIELD(synced_notification_app_info);
317 SET_FIELD(theme);
318 SET_FIELD(typed_url);
319 SET_FIELD(wifi_credential);
321 SET_FIELD(autofill_wallet);
323 #undef SET_FIELD
325 scoped_ptr<base::DictionaryValue> value(EntitySpecificsToValue(specifics));
326 EXPECT_EQ(MODEL_TYPE_COUNT - FIRST_REAL_MODEL_TYPE -
327 (LAST_PROXY_TYPE - FIRST_PROXY_TYPE + 1),
328 static_cast<int>(value->size()));
331 namespace {
332 // Returns whether the given value has specifics under the entries in the given
333 // path.
334 bool ValueHasSpecifics(const base::DictionaryValue& value,
335 const std::string& path) {
336 const base::ListValue* entities_list = NULL;
337 const base::DictionaryValue* entry_dictionary = NULL;
338 const base::DictionaryValue* specifics_dictionary = NULL;
340 if (!value.GetList(path, &entities_list))
341 return false;
343 if (!entities_list->GetDictionary(0, &entry_dictionary))
344 return false;
346 return entry_dictionary->GetDictionary("specifics",
347 &specifics_dictionary);
349 } // namespace
351 // Create a ClientToServerMessage with an EntitySpecifics. Converting it to
352 // a value should respect the |include_specifics| flag.
353 TEST_F(ProtoValueConversionsTest, ClientToServerMessageToValue) {
354 sync_pb::ClientToServerMessage message;
355 sync_pb::CommitMessage* commit_message = message.mutable_commit();
356 sync_pb::SyncEntity* entity = commit_message->add_entries();
357 entity->mutable_specifics();
359 scoped_ptr<base::DictionaryValue> value_with_specifics(
360 ClientToServerMessageToValue(message, true /* include_specifics */));
361 EXPECT_FALSE(value_with_specifics->empty());
362 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()),
363 "commit.entries"));
365 scoped_ptr<base::DictionaryValue> value_without_specifics(
366 ClientToServerMessageToValue(message, false /* include_specifics */));
367 EXPECT_FALSE(value_without_specifics->empty());
368 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()),
369 "commit.entries"));
372 // Create a ClientToServerResponse with an EntitySpecifics. Converting it to
373 // a value should respect the |include_specifics| flag.
374 TEST_F(ProtoValueConversionsTest, ClientToServerResponseToValue) {
375 sync_pb::ClientToServerResponse message;
376 sync_pb::GetUpdatesResponse* response = message.mutable_get_updates();
377 sync_pb::SyncEntity* entity = response->add_entries();
378 entity->mutable_specifics();
380 scoped_ptr<base::DictionaryValue> value_with_specifics(
381 ClientToServerResponseToValue(message, true /* include_specifics */));
382 EXPECT_FALSE(value_with_specifics->empty());
383 EXPECT_TRUE(ValueHasSpecifics(*(value_with_specifics.get()),
384 "get_updates.entries"));
386 scoped_ptr<base::DictionaryValue> value_without_specifics(
387 ClientToServerResponseToValue(message, false /* include_specifics */));
388 EXPECT_FALSE(value_without_specifics->empty());
389 EXPECT_FALSE(ValueHasSpecifics(*(value_without_specifics.get()),
390 "get_updates.entries"));
393 TEST_F(ProtoValueConversionsTest, AttachmentIdProtoToValue) {
394 TestSpecificsToValue(AttachmentIdProtoToValue);
397 } // namespace
398 } // namespace syncer