1 // Copyright (c) 2011 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.
4
5 // Keep this file in sync with the .proto files in this directory.
6
7 #include "chrome/browser/sync/protocol/proto_value_conversions.h"
8
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #include "chrome/browser/sync/protocol/app_specifics.pb.h"
12 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h"
13 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h"
14 #include "chrome/browser/sync/protocol/encryption.pb.h"
15 #include "chrome/browser/sync/protocol/extension_specifics.pb.h"
16 #include "chrome/browser/sync/protocol/nigori_specifics.pb.h"
17 #include "chrome/browser/sync/protocol/password_specifics.pb.h"
18 #include "chrome/browser/sync/protocol/preference_specifics.pb.h"
19 #include "chrome/browser/sync/protocol/session_specifics.pb.h"
20 #include "chrome/browser/sync/protocol/sync.pb.h"
21 #include "chrome/browser/sync/protocol/theme_specifics.pb.h"
22 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h"
23 #include "chrome/browser/sync/syncable/model_type.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace browser_sync {
27 namespace {
28
29 class ProtoValueConversionsTest : public testing::Test {
30 protected:
31 template <class T>
TestSpecificsToValue(DictionaryValue * (* specifics_to_value)(const T &))32 void TestSpecificsToValue(
33 DictionaryValue* (*specifics_to_value)(const T&)) {
34 const T& specifics(T::default_instance());
35 scoped_ptr<DictionaryValue> value(specifics_to_value(specifics));
36 // We can't do much but make sure that the returned value has
37 // something in it.
38 EXPECT_FALSE(value->empty());
39 }
40 };
41
TEST_F(ProtoValueConversionsTest,ProtoChangeCheck)42 TEST_F(ProtoValueConversionsTest, ProtoChangeCheck) {
43 // If this number changes, that means we added or removed a data
44 // type. Don't forget to add a unit test for {New
45 // type}SpecificsToValue below.
46 EXPECT_EQ(13, syncable::MODEL_TYPE_COUNT);
47
48 // We'd also like to check if we changed any field in our messages.
49 // However, that's hard to do: sizeof could work, but it's
50 // platform-dependent. default_instance().ByteSize() won't change
51 // for most changes, since most of our fields are optional. So we
52 // just settle for comments in the proto files.
53 }
54
TEST_F(ProtoValueConversionsTest,EncryptedDataToValue)55 TEST_F(ProtoValueConversionsTest, EncryptedDataToValue) {
56 TestSpecificsToValue(EncryptedDataToValue);
57 }
58
TEST_F(ProtoValueConversionsTest,SessionHeaderToValue)59 TEST_F(ProtoValueConversionsTest, SessionHeaderToValue) {
60 TestSpecificsToValue(SessionHeaderToValue);
61 }
62
TEST_F(ProtoValueConversionsTest,SessionTabToValue)63 TEST_F(ProtoValueConversionsTest, SessionTabToValue) {
64 TestSpecificsToValue(SessionTabToValue);
65 }
66
TEST_F(ProtoValueConversionsTest,SessionWindowToValue)67 TEST_F(ProtoValueConversionsTest, SessionWindowToValue) {
68 TestSpecificsToValue(SessionWindowToValue);
69 }
70
TEST_F(ProtoValueConversionsTest,TabNavigationToValue)71 TEST_F(ProtoValueConversionsTest, TabNavigationToValue) {
72 TestSpecificsToValue(TabNavigationToValue);
73 }
74
TEST_F(ProtoValueConversionsTest,PasswordSpecificsData)75 TEST_F(ProtoValueConversionsTest, PasswordSpecificsData) {
76 sync_pb::PasswordSpecificsData specifics;
77 specifics.set_password_value("secret");
78 scoped_ptr<DictionaryValue> value(PasswordSpecificsDataToValue(specifics));
79 EXPECT_FALSE(value->empty());
80 std::string password_value;
81 EXPECT_TRUE(value->GetString("password_value", &password_value));
82 EXPECT_EQ("<redacted>", password_value);
83 }
84
TEST_F(ProtoValueConversionsTest,AppSpecificsToValue)85 TEST_F(ProtoValueConversionsTest, AppSpecificsToValue) {
86 TestSpecificsToValue(AppSpecificsToValue);
87 }
88
TEST_F(ProtoValueConversionsTest,AutofillSpecificsToValue)89 TEST_F(ProtoValueConversionsTest, AutofillSpecificsToValue) {
90 TestSpecificsToValue(AutofillSpecificsToValue);
91 }
92
TEST_F(ProtoValueConversionsTest,AutofillCreditCardSpecificsToValue)93 TEST_F(ProtoValueConversionsTest, AutofillCreditCardSpecificsToValue) {
94 TestSpecificsToValue(AutofillCreditCardSpecificsToValue);
95 }
96
TEST_F(ProtoValueConversionsTest,AutofillProfileSpecificsToValue)97 TEST_F(ProtoValueConversionsTest, AutofillProfileSpecificsToValue) {
98 TestSpecificsToValue(AutofillProfileSpecificsToValue);
99 }
100
TEST_F(ProtoValueConversionsTest,BookmarkSpecificsToValue)101 TEST_F(ProtoValueConversionsTest, BookmarkSpecificsToValue) {
102 TestSpecificsToValue(BookmarkSpecificsToValue);
103 }
104
TEST_F(ProtoValueConversionsTest,ExtensionSpecificsToValue)105 TEST_F(ProtoValueConversionsTest, ExtensionSpecificsToValue) {
106 TestSpecificsToValue(ExtensionSpecificsToValue);
107 }
108
TEST_F(ProtoValueConversionsTest,NigoriSpecificsToValue)109 TEST_F(ProtoValueConversionsTest, NigoriSpecificsToValue) {
110 TestSpecificsToValue(NigoriSpecificsToValue);
111 }
112
TEST_F(ProtoValueConversionsTest,PasswordSpecificsToValue)113 TEST_F(ProtoValueConversionsTest, PasswordSpecificsToValue) {
114 TestSpecificsToValue(PasswordSpecificsToValue);
115 }
116
TEST_F(ProtoValueConversionsTest,PreferenceSpecificsToValue)117 TEST_F(ProtoValueConversionsTest, PreferenceSpecificsToValue) {
118 TestSpecificsToValue(PreferenceSpecificsToValue);
119 }
120
TEST_F(ProtoValueConversionsTest,SessionSpecificsToValue)121 TEST_F(ProtoValueConversionsTest, SessionSpecificsToValue) {
122 TestSpecificsToValue(SessionSpecificsToValue);
123 }
124
TEST_F(ProtoValueConversionsTest,ThemeSpecificsToValue)125 TEST_F(ProtoValueConversionsTest, ThemeSpecificsToValue) {
126 TestSpecificsToValue(ThemeSpecificsToValue);
127 }
128
TEST_F(ProtoValueConversionsTest,TypedUrlSpecificsToValue)129 TEST_F(ProtoValueConversionsTest, TypedUrlSpecificsToValue) {
130 TestSpecificsToValue(TypedUrlSpecificsToValue);
131 }
132
133 // TODO(akalin): Figure out how to better test EntitySpecificsToValue.
134
TEST_F(ProtoValueConversionsTest,EntitySpecificsToValue)135 TEST_F(ProtoValueConversionsTest, EntitySpecificsToValue) {
136 sync_pb::EntitySpecifics specifics;
137 // Touch the extensions to make sure it shows up in the generated
138 // value.
139 #define SET_EXTENSION(key) (void)specifics.MutableExtension(sync_pb::key)
140
141 SET_EXTENSION(app);
142 SET_EXTENSION(autofill);
143 SET_EXTENSION(autofill_profile);
144 SET_EXTENSION(bookmark);
145 SET_EXTENSION(extension);
146 SET_EXTENSION(nigori);
147 SET_EXTENSION(password);
148 SET_EXTENSION(preference);
149 SET_EXTENSION(session);
150 SET_EXTENSION(theme);
151 SET_EXTENSION(typed_url);
152
153 #undef SET_EXTENSION
154
155 scoped_ptr<DictionaryValue> value(EntitySpecificsToValue(specifics));
156 EXPECT_EQ(syncable::MODEL_TYPE_COUNT - syncable::FIRST_REAL_MODEL_TYPE,
157 static_cast<int>(value->size()));
158 }
159
160 } // namespace
161 } // namespace browser_sync
162