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 "sync/internal_api/public/base/model_type.h" 6 7 #include <string> 8 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" 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 scoped_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 scoped_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 scoped_ptr<base::ListValue> value(ModelTypeSetToValue(model_types)); 56 EXPECT_TRUE(model_types.Equals(ModelTypeSetFromValue(*value))); 57 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))); 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 } 73 74 TEST_F(ModelTypeTest, IsProxyType) { 75 EXPECT_FALSE(IsProxyType(BOOKMARKS)); 76 EXPECT_FALSE(IsProxyType(MODEL_TYPE_COUNT)); 77 EXPECT_TRUE(IsProxyType(PROXY_TABS)); 78 } 79 80 // Make sure we can convert ModelTypes to and from specifics field 81 // numbers. 82 TEST_F(ModelTypeTest, ModelTypeToFromSpecificsFieldNumber) { 83 ModelTypeSet protocol_types = ProtocolTypes(); 84 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); 85 iter.Inc()) { 86 int field_number = GetSpecificsFieldNumberFromModelType(iter.Get()); 87 EXPECT_EQ(iter.Get(), 88 GetModelTypeFromSpecificsFieldNumber(field_number)); 89 } 90 } 91 92 TEST_F(ModelTypeTest, ModelTypeOfInvalidSpecificsFieldNumber) { 93 EXPECT_EQ(UNSPECIFIED, GetModelTypeFromSpecificsFieldNumber(0)); 94 } 95 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()); 102 103 EXPECT_TRUE(histogram_values.insert(histogram_value).second) 104 << "Expected histogram values to be unique"; 105 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 109 // those histograms. 110 EXPECT_LT(histogram_value, MODEL_TYPE_COUNT); 111 } 112 } 113 114 TEST_F(ModelTypeTest, ModelTypeSetFromString) { 115 syncer::ModelTypeSet empty; 116 syncer::ModelTypeSet one(BOOKMARKS); 117 syncer::ModelTypeSet two(BOOKMARKS, TYPED_URLS); 118 119 EXPECT_TRUE( 120 empty.Equals(ModelTypeSetFromString(ModelTypeSetToString(empty)))); 121 EXPECT_TRUE( 122 one.Equals(ModelTypeSetFromString(ModelTypeSetToString(one)))); 123 EXPECT_TRUE( 124 two.Equals(ModelTypeSetFromString(ModelTypeSetToString(two)))); 125 } 126 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())); 131 132 sync_pb::EntitySpecifics specifics; 133 syncer::AddDefaultFieldValue(it.Get(), &specifics); 134 EXPECT_TRUE(specifics.IsInitialized()); 135 136 std::string tmp; 137 EXPECT_TRUE(specifics.SerializeToString(&tmp)); 138 139 sync_pb::EntitySpecifics from_string; 140 EXPECT_TRUE(from_string.ParseFromString(tmp)); 141 EXPECT_TRUE(from_string.IsInitialized()); 142 143 EXPECT_EQ(it.Get(), syncer::GetModelTypeFromSpecifics(from_string)); 144 } 145 } 146 147 } // namespace 148 } // namespace syncer 149