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 #include "chrome/browser/sync/syncable/model_type.h" 6 7 #include <string> 8 9 #include "base/memory/scoped_ptr.h" 10 #include "base/values.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace syncable { 14 namespace { 15 16 class ModelTypeTest : public testing::Test {}; 17 18 // TODO(akalin): Move this to values_test_util.h. 19 20 // Takes ownership of |actual|. 21 void ExpectStringValue(const std::string& expected_str, 22 StringValue* actual) { 23 scoped_ptr<StringValue> scoped_actual(actual); 24 std::string actual_str; 25 EXPECT_TRUE(scoped_actual->GetAsString(&actual_str)); 26 EXPECT_EQ(expected_str, actual_str); 27 } 28 29 TEST_F(ModelTypeTest, ModelTypeToValue) { 30 for (int i = syncable::FIRST_REAL_MODEL_TYPE; 31 i < syncable::MODEL_TYPE_COUNT; ++i) { 32 ModelType model_type = ModelTypeFromInt(i); 33 ExpectStringValue(ModelTypeToString(model_type), 34 ModelTypeToValue(model_type)); 35 } 36 ExpectStringValue("Top-level folder", 37 ModelTypeToValue(TOP_LEVEL_FOLDER)); 38 ExpectStringValue("Unspecified", 39 ModelTypeToValue(UNSPECIFIED)); 40 } 41 42 TEST_F(ModelTypeTest, ModelTypeBitSetToValue) { 43 ModelTypeBitSet model_types; 44 model_types.set(syncable::BOOKMARKS); 45 model_types.set(syncable::APPS); 46 47 scoped_ptr<ListValue> value(ModelTypeBitSetToValue(model_types)); 48 EXPECT_EQ(2u, value->GetSize()); 49 std::string types[2]; 50 EXPECT_TRUE(value->GetString(0, &types[0])); 51 EXPECT_TRUE(value->GetString(1, &types[1])); 52 EXPECT_EQ("Bookmarks", types[0]); 53 EXPECT_EQ("Apps", types[1]); 54 } 55 56 TEST_F(ModelTypeTest, ModelTypeSetToValue) { 57 ModelTypeSet model_types; 58 model_types.insert(syncable::BOOKMARKS); 59 model_types.insert(syncable::APPS); 60 61 scoped_ptr<ListValue> value(ModelTypeSetToValue(model_types)); 62 EXPECT_EQ(2u, value->GetSize()); 63 std::string types[2]; 64 EXPECT_TRUE(value->GetString(0, &types[0])); 65 EXPECT_TRUE(value->GetString(1, &types[1])); 66 EXPECT_EQ("Bookmarks", types[0]); 67 EXPECT_EQ("Apps", types[1]); 68 } 69 70 TEST_F(ModelTypeTest, ModelTypeBitSetFromString) { 71 ModelTypeBitSet input, output; 72 input.set(BOOKMARKS); 73 input.set(AUTOFILL); 74 input.set(APPS); 75 std::string input_string = input.to_string(); 76 EXPECT_TRUE(ModelTypeBitSetFromString(input_string, &output)); 77 EXPECT_EQ(input, output); 78 79 input_string.clear(); 80 EXPECT_FALSE(ModelTypeBitSetFromString(input_string, &output)); 81 82 input_string = "hello world"; 83 EXPECT_FALSE(ModelTypeBitSetFromString(input_string, &output)); 84 85 input_string.clear(); 86 for (int i = 0; i < MODEL_TYPE_COUNT; ++i) 87 input_string += '0' + (i%10); 88 EXPECT_FALSE(ModelTypeBitSetFromString(input_string, &output)); 89 } 90 91 } // namespace 92 } // namespace syncable 93