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 #include "sync/internal_api/public/base/model_type.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/test/values_test_util.h"
11 #include "base/values.h"
12 #include "sync/protocol/sync.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class ModelTypeTest
: public testing::Test
{};
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
));
26 base::ExpectStringValue("Top-level folder",
27 ModelTypeToValue(TOP_LEVEL_FOLDER
));
28 base::ExpectStringValue("Unspecified",
29 ModelTypeToValue(UNSPECIFIED
));
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 scoped_ptr
<base::StringValue
> value(ModelTypeToValue(model_type
));
36 EXPECT_EQ(model_type
, ModelTypeFromValue(*value
));
40 TEST_F(ModelTypeTest
, ModelTypeSetToValue
) {
41 const ModelTypeSet
model_types(BOOKMARKS
, APPS
);
43 scoped_ptr
<base::ListValue
> value(ModelTypeSetToValue(model_types
));
44 EXPECT_EQ(2u, value
->GetSize());
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]);
52 TEST_F(ModelTypeTest
, ModelTypeSetFromValue
) {
53 // Try empty set first.
54 ModelTypeSet model_types
;
55 scoped_ptr
<base::ListValue
> value(ModelTypeSetToValue(model_types
));
56 EXPECT_TRUE(model_types
.Equals(ModelTypeSetFromValue(*value
)));
58 // Now try with a few random types.
59 model_types
.Put(BOOKMARKS
);
60 model_types
.Put(APPS
);
61 value
.reset(ModelTypeSetToValue(model_types
));
62 EXPECT_TRUE(model_types
.Equals(ModelTypeSetFromValue(*value
)));
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
));
74 TEST_F(ModelTypeTest
, IsProxyType
) {
75 EXPECT_FALSE(IsProxyType(BOOKMARKS
));
76 EXPECT_FALSE(IsProxyType(MODEL_TYPE_COUNT
));
77 EXPECT_TRUE(IsProxyType(PROXY_TABS
));
80 // Make sure we can convert ModelTypes to and from specifics field
82 TEST_F(ModelTypeTest
, ModelTypeToFromSpecificsFieldNumber
) {
83 ModelTypeSet protocol_types
= ProtocolTypes();
84 for (ModelTypeSet::Iterator iter
= protocol_types
.First(); iter
.Good();
86 int field_number
= GetSpecificsFieldNumberFromModelType(iter
.Get());
88 GetModelTypeFromSpecificsFieldNumber(field_number
));
92 TEST_F(ModelTypeTest
, ModelTypeOfInvalidSpecificsFieldNumber
) {
93 EXPECT_EQ(UNSPECIFIED
, GetModelTypeFromSpecificsFieldNumber(0));
96 TEST_F(ModelTypeTest
, ModelTypeHistogramMapping
) {
97 std::set
<int> histogram_values
;
98 ModelTypeSet all_types
= ModelTypeSet::All();
99 for (ModelTypeSet::Iterator it
= all_types
.First(); it
.Good(); it
.Inc()) {
100 SCOPED_TRACE(ModelTypeToString(it
.Get()));
101 int histogram_value
= ModelTypeToHistogramInt(it
.Get());
103 EXPECT_TRUE(histogram_values
.insert(histogram_value
).second
)
104 << "Expected histogram values to be unique";
106 // This is not necessary for the mapping to be valid, but most instances of
107 // UMA_HISTOGRAM that use this mapping specify MODEL_TYPE_COUNT as the
108 // maximum possible value. If you break this assumption, you should update
110 EXPECT_LT(histogram_value
, MODEL_TYPE_COUNT
);
114 TEST_F(ModelTypeTest
, ModelTypeSetFromString
) {
115 syncer::ModelTypeSet empty
;
116 syncer::ModelTypeSet
one(BOOKMARKS
);
117 syncer::ModelTypeSet
two(BOOKMARKS
, TYPED_URLS
);
120 empty
.Equals(ModelTypeSetFromString(ModelTypeSetToString(empty
))));
122 one
.Equals(ModelTypeSetFromString(ModelTypeSetToString(one
))));
124 two
.Equals(ModelTypeSetFromString(ModelTypeSetToString(two
))));
127 TEST_F(ModelTypeTest
, DefaultFieldValues
) {
128 syncer::ModelTypeSet types
= syncer::ProtocolTypes();
129 for (ModelTypeSet::Iterator it
= types
.First(); it
.Good(); it
.Inc()) {
130 SCOPED_TRACE(ModelTypeToString(it
.Get()));
132 sync_pb::EntitySpecifics specifics
;
133 syncer::AddDefaultFieldValue(it
.Get(), &specifics
);
134 EXPECT_TRUE(specifics
.IsInitialized());
137 EXPECT_TRUE(specifics
.SerializeToString(&tmp
));
139 sync_pb::EntitySpecifics from_string
;
140 EXPECT_TRUE(from_string
.ParseFromString(tmp
));
141 EXPECT_TRUE(from_string
.IsInitialized());
143 EXPECT_EQ(it
.Get(), syncer::GetModelTypeFromSpecifics(from_string
));
148 } // namespace syncer