OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <memory> | |
6 #include <string> | |
7 | |
8 #include "base/strings/string_util.h" | |
9 #include "base/test/values_test_util.h" | |
10 #include "base/values.h" | |
11 #include "sync/internal_api/public/base/model_type.h" | |
12 #include "sync/protocol/sync.pb.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace syncer { | |
16 namespace { | |
17 | |
18 class ModelTypeTest : public testing::Test {}; | |
19 | |
20 TEST_F(ModelTypeTest, ModelTypeToValue) { | |
21 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | |
22 ModelType model_type = ModelTypeFromInt(i); | |
23 base::ExpectStringValue(ModelTypeToString(model_type), | |
24 ModelTypeToValue(model_type)); | |
25 } | |
26 base::ExpectStringValue("Top-level folder", | |
27 ModelTypeToValue(TOP_LEVEL_FOLDER)); | |
28 base::ExpectStringValue("Unspecified", | |
29 ModelTypeToValue(UNSPECIFIED)); | |
30 } | |
31 | |
32 TEST_F(ModelTypeTest, ModelTypeFromValue) { | |
33 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | |
34 ModelType model_type = ModelTypeFromInt(i); | |
35 std::unique_ptr<base::StringValue> value(ModelTypeToValue(model_type)); | |
36 EXPECT_EQ(model_type, ModelTypeFromValue(*value)); | |
37 } | |
38 } | |
39 | |
40 TEST_F(ModelTypeTest, ModelTypeSetToValue) { | |
41 const ModelTypeSet model_types(BOOKMARKS, APPS); | |
42 | |
43 std::unique_ptr<base::ListValue> value(ModelTypeSetToValue(model_types)); | |
44 EXPECT_EQ(2u, value->GetSize()); | |
45 std::string types[2]; | |
46 EXPECT_TRUE(value->GetString(0, &types[0])); | |
47 EXPECT_TRUE(value->GetString(1, &types[1])); | |
48 EXPECT_EQ("Bookmarks", types[0]); | |
49 EXPECT_EQ("Apps", types[1]); | |
50 } | |
51 | |
52 TEST_F(ModelTypeTest, ModelTypeSetFromValue) { | |
53 // Try empty set first. | |
54 ModelTypeSet model_types; | |
55 std::unique_ptr<base::ListValue> value(ModelTypeSetToValue(model_types)); | |
56 EXPECT_EQ(model_types, ModelTypeSetFromValue(*value)); | |
57 | |
58 // Now try with a few random types. | |
59 model_types.Put(BOOKMARKS); | |
60 model_types.Put(APPS); | |
61 value = ModelTypeSetToValue(model_types); | |
62 EXPECT_EQ(model_types, ModelTypeSetFromValue(*value)); | |
63 } | |
64 | |
65 TEST_F(ModelTypeTest, IsRealDataType) { | |
66 EXPECT_FALSE(IsRealDataType(UNSPECIFIED)); | |
67 EXPECT_FALSE(IsRealDataType(MODEL_TYPE_COUNT)); | |
68 EXPECT_FALSE(IsRealDataType(TOP_LEVEL_FOLDER)); | |
69 EXPECT_TRUE(IsRealDataType(FIRST_REAL_MODEL_TYPE)); | |
70 EXPECT_TRUE(IsRealDataType(BOOKMARKS)); | |
71 EXPECT_TRUE(IsRealDataType(APPS)); | |
72 EXPECT_TRUE(IsRealDataType(ARC_PACKAGE)); | |
73 } | |
74 | |
75 TEST_F(ModelTypeTest, IsProxyType) { | |
76 EXPECT_FALSE(IsProxyType(BOOKMARKS)); | |
77 EXPECT_FALSE(IsProxyType(MODEL_TYPE_COUNT)); | |
78 EXPECT_TRUE(IsProxyType(PROXY_TABS)); | |
79 } | |
80 | |
81 // Make sure we can convert ModelTypes to and from specifics field | |
82 // numbers. | |
83 TEST_F(ModelTypeTest, ModelTypeToFromSpecificsFieldNumber) { | |
84 ModelTypeSet protocol_types = ProtocolTypes(); | |
85 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | |
86 iter.Inc()) { | |
87 int field_number = GetSpecificsFieldNumberFromModelType(iter.Get()); | |
88 EXPECT_EQ(iter.Get(), | |
89 GetModelTypeFromSpecificsFieldNumber(field_number)); | |
90 } | |
91 } | |
92 | |
93 TEST_F(ModelTypeTest, ModelTypeOfInvalidSpecificsFieldNumber) { | |
94 EXPECT_EQ(UNSPECIFIED, GetModelTypeFromSpecificsFieldNumber(0)); | |
95 } | |
96 | |
97 TEST_F(ModelTypeTest, ModelTypeHistogramMapping) { | |
98 std::set<int> histogram_values; | |
99 ModelTypeSet all_types = ModelTypeSet::All(); | |
100 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { | |
101 SCOPED_TRACE(ModelTypeToString(it.Get())); | |
102 int histogram_value = ModelTypeToHistogramInt(it.Get()); | |
103 | |
104 EXPECT_TRUE(histogram_values.insert(histogram_value).second) | |
105 << "Expected histogram values to be unique"; | |
106 | |
107 // This is not necessary for the mapping to be valid, but most instances of | |
108 // UMA_HISTOGRAM that use this mapping specify MODEL_TYPE_COUNT as the | |
109 // maximum possible value. If you break this assumption, you should update | |
110 // those histograms. | |
111 EXPECT_LT(histogram_value, MODEL_TYPE_COUNT); | |
112 } | |
113 } | |
114 | |
115 TEST_F(ModelTypeTest, ModelTypeSetFromString) { | |
116 syncer::ModelTypeSet empty; | |
117 syncer::ModelTypeSet one(BOOKMARKS); | |
118 syncer::ModelTypeSet two(BOOKMARKS, TYPED_URLS); | |
119 | |
120 EXPECT_EQ(empty, ModelTypeSetFromString(ModelTypeSetToString(empty))); | |
121 EXPECT_EQ(one, ModelTypeSetFromString(ModelTypeSetToString(one))); | |
122 EXPECT_EQ(two, ModelTypeSetFromString(ModelTypeSetToString(two))); | |
123 } | |
124 | |
125 TEST_F(ModelTypeTest, DefaultFieldValues) { | |
126 syncer::ModelTypeSet types = syncer::ProtocolTypes(); | |
127 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { | |
128 SCOPED_TRACE(ModelTypeToString(it.Get())); | |
129 | |
130 sync_pb::EntitySpecifics specifics; | |
131 syncer::AddDefaultFieldValue(it.Get(), &specifics); | |
132 EXPECT_TRUE(specifics.IsInitialized()); | |
133 | |
134 std::string tmp; | |
135 EXPECT_TRUE(specifics.SerializeToString(&tmp)); | |
136 | |
137 sync_pb::EntitySpecifics from_string; | |
138 EXPECT_TRUE(from_string.ParseFromString(tmp)); | |
139 EXPECT_TRUE(from_string.IsInitialized()); | |
140 | |
141 EXPECT_EQ(it.Get(), syncer::GetModelTypeFromSpecifics(from_string)); | |
142 } | |
143 } | |
144 | |
145 TEST_F(ModelTypeTest, ModelTypeToRootTagValues) { | |
146 ModelTypeSet all_types = ModelTypeSet::All(); | |
147 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { | |
148 ModelType model_type = it.Get(); | |
149 std::string root_tag = ModelTypeToRootTag(model_type); | |
150 if (IsProxyType(model_type)) { | |
151 EXPECT_EQ(root_tag, std::string()); | |
152 } else if (IsRealDataType(model_type)) { | |
153 EXPECT_TRUE(base::StartsWith(root_tag, "google_chrome_", | |
154 base::CompareCase::INSENSITIVE_ASCII)); | |
155 } else { | |
156 EXPECT_EQ(root_tag, "INVALID"); | |
157 } | |
158 } | |
159 } | |
160 | |
161 TEST_F(ModelTypeTest, ModelTypeStringMapping) { | |
162 ModelTypeSet all_types = ModelTypeSet::All(); | |
163 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { | |
164 ModelType model_type = it.Get(); | |
165 const char* model_type_string = ModelTypeToString(model_type); | |
166 ModelType converted_model_type = ModelTypeFromString(model_type_string); | |
167 if (IsRealDataType(model_type)) | |
168 EXPECT_EQ(converted_model_type, model_type); | |
169 else | |
170 EXPECT_EQ(converted_model_type, UNSPECIFIED); | |
171 } | |
172 } | |
173 | |
174 TEST_F(ModelTypeTest, ModelTypeNotificationTypeMapping) { | |
175 ModelTypeSet all_types = ModelTypeSet::All(); | |
176 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { | |
177 ModelType model_type = it.Get(); | |
178 std::string notification_type; | |
179 bool ret = RealModelTypeToNotificationType(model_type, ¬ification_type); | |
180 if (ret) { | |
181 ModelType notified_model_type; | |
182 EXPECT_TRUE(NotificationTypeToRealModelType(notification_type, | |
183 ¬ified_model_type)); | |
184 EXPECT_EQ(notified_model_type, model_type); | |
185 } else { | |
186 EXPECT_FALSE(ProtocolTypes().Has(model_type)); | |
187 EXPECT_TRUE(notification_type.empty()); | |
188 } | |
189 } | |
190 } | |
191 | |
192 } // namespace | |
193 } // namespace syncer | |
OLD | NEW |