| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/prefs/pref_value_map.h" | 5 #include "base/prefs/pref_value_map.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 TEST(PrefValueMapTest, SetValue) { | 13 TEST(PrefValueMapTest, SetValue) { |
| 14 PrefValueMap map; | 14 PrefValueMap map; |
| 15 const Value* result = NULL; | 15 const Value* result = NULL; |
| 16 EXPECT_FALSE(map.GetValue("key", &result)); | 16 EXPECT_FALSE(map.GetValue("key", &result)); |
| 17 EXPECT_FALSE(result); | 17 EXPECT_FALSE(result); |
| 18 | 18 |
| 19 EXPECT_TRUE(map.SetValue("key", new StringValue("test"))); | 19 EXPECT_TRUE(map.SetValue("key", make_scoped_ptr(new StringValue("test")))); |
| 20 EXPECT_FALSE(map.SetValue("key", new StringValue("test"))); | 20 EXPECT_FALSE(map.SetValue("key", make_scoped_ptr(new StringValue("test")))); |
| 21 EXPECT_TRUE(map.SetValue("key", new StringValue("hi mom!"))); | 21 EXPECT_TRUE(map.SetValue("key", make_scoped_ptr(new StringValue("hi mom!")))); |
| 22 | 22 |
| 23 EXPECT_TRUE(map.GetValue("key", &result)); | 23 EXPECT_TRUE(map.GetValue("key", &result)); |
| 24 EXPECT_TRUE(StringValue("hi mom!").Equals(result)); | 24 EXPECT_TRUE(StringValue("hi mom!").Equals(result)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 TEST(PrefValueMapTest, GetAndSetIntegerValue) { | 27 TEST(PrefValueMapTest, GetAndSetIntegerValue) { |
| 28 PrefValueMap map; | 28 PrefValueMap map; |
| 29 ASSERT_TRUE(map.SetValue("key", new FundamentalValue(5))); | 29 ASSERT_TRUE(map.SetValue("key", make_scoped_ptr(new FundamentalValue(5)))); |
| 30 | 30 |
| 31 int int_value = 0; | 31 int int_value = 0; |
| 32 EXPECT_TRUE(map.GetInteger("key", &int_value)); | 32 EXPECT_TRUE(map.GetInteger("key", &int_value)); |
| 33 EXPECT_EQ(5, int_value); | 33 EXPECT_EQ(5, int_value); |
| 34 | 34 |
| 35 map.SetInteger("key", -14); | 35 map.SetInteger("key", -14); |
| 36 EXPECT_TRUE(map.GetInteger("key", &int_value)); | 36 EXPECT_TRUE(map.GetInteger("key", &int_value)); |
| 37 EXPECT_EQ(-14, int_value); | 37 EXPECT_EQ(-14, int_value); |
| 38 } | 38 } |
| 39 | 39 |
| 40 TEST(PrefValueMapTest, SetDoubleValue) { | 40 TEST(PrefValueMapTest, SetDoubleValue) { |
| 41 PrefValueMap map; | 41 PrefValueMap map; |
| 42 ASSERT_TRUE(map.SetValue("key", new FundamentalValue(5.5))); | 42 ASSERT_TRUE(map.SetValue("key", make_scoped_ptr(new FundamentalValue(5.5)))); |
| 43 | 43 |
| 44 const Value* result = NULL; | 44 const Value* result = NULL; |
| 45 ASSERT_TRUE(map.GetValue("key", &result)); | 45 ASSERT_TRUE(map.GetValue("key", &result)); |
| 46 double double_value = 0.; | 46 double double_value = 0.; |
| 47 EXPECT_TRUE(result->GetAsDouble(&double_value)); | 47 EXPECT_TRUE(result->GetAsDouble(&double_value)); |
| 48 EXPECT_DOUBLE_EQ(5.5, double_value); | 48 EXPECT_DOUBLE_EQ(5.5, double_value); |
| 49 } | 49 } |
| 50 | 50 |
| 51 TEST(PrefValueMapTest, RemoveValue) { | 51 TEST(PrefValueMapTest, RemoveValue) { |
| 52 PrefValueMap map; | 52 PrefValueMap map; |
| 53 EXPECT_FALSE(map.RemoveValue("key")); | 53 EXPECT_FALSE(map.RemoveValue("key")); |
| 54 | 54 |
| 55 EXPECT_TRUE(map.SetValue("key", new StringValue("test"))); | 55 EXPECT_TRUE(map.SetValue("key", make_scoped_ptr(new StringValue("test")))); |
| 56 EXPECT_TRUE(map.GetValue("key", NULL)); | 56 EXPECT_TRUE(map.GetValue("key", NULL)); |
| 57 | 57 |
| 58 EXPECT_TRUE(map.RemoveValue("key")); | 58 EXPECT_TRUE(map.RemoveValue("key")); |
| 59 EXPECT_FALSE(map.GetValue("key", NULL)); | 59 EXPECT_FALSE(map.GetValue("key", NULL)); |
| 60 | 60 |
| 61 EXPECT_FALSE(map.RemoveValue("key")); | 61 EXPECT_FALSE(map.RemoveValue("key")); |
| 62 } | 62 } |
| 63 | 63 |
| 64 TEST(PrefValueMapTest, Clear) { | 64 TEST(PrefValueMapTest, Clear) { |
| 65 PrefValueMap map; | 65 PrefValueMap map; |
| 66 EXPECT_TRUE(map.SetValue("key", new StringValue("test"))); | 66 EXPECT_TRUE(map.SetValue("key", make_scoped_ptr(new StringValue("test")))); |
| 67 EXPECT_TRUE(map.GetValue("key", NULL)); | 67 EXPECT_TRUE(map.GetValue("key", NULL)); |
| 68 | 68 |
| 69 map.Clear(); | 69 map.Clear(); |
| 70 | 70 |
| 71 EXPECT_FALSE(map.GetValue("key", NULL)); | 71 EXPECT_FALSE(map.GetValue("key", NULL)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 TEST(PrefValueMapTest, GetDifferingKeys) { | 74 TEST(PrefValueMapTest, GetDifferingKeys) { |
| 75 PrefValueMap reference; | 75 PrefValueMap reference; |
| 76 EXPECT_TRUE(reference.SetValue("b", new StringValue("test"))); | 76 EXPECT_TRUE( |
| 77 EXPECT_TRUE(reference.SetValue("c", new StringValue("test"))); | 77 reference.SetValue("b", make_scoped_ptr(new StringValue("test")))); |
| 78 EXPECT_TRUE(reference.SetValue("e", new StringValue("test"))); | 78 EXPECT_TRUE( |
| 79 reference.SetValue("c", make_scoped_ptr(new StringValue("test")))); |
| 80 EXPECT_TRUE( |
| 81 reference.SetValue("e", make_scoped_ptr(new StringValue("test")))); |
| 79 | 82 |
| 80 PrefValueMap check; | 83 PrefValueMap check; |
| 81 std::vector<std::string> differing_paths; | 84 std::vector<std::string> differing_paths; |
| 82 std::vector<std::string> expected_differing_paths; | 85 std::vector<std::string> expected_differing_paths; |
| 83 | 86 |
| 84 reference.GetDifferingKeys(&check, &differing_paths); | 87 reference.GetDifferingKeys(&check, &differing_paths); |
| 85 expected_differing_paths.push_back("b"); | 88 expected_differing_paths.push_back("b"); |
| 86 expected_differing_paths.push_back("c"); | 89 expected_differing_paths.push_back("c"); |
| 87 expected_differing_paths.push_back("e"); | 90 expected_differing_paths.push_back("e"); |
| 88 EXPECT_EQ(expected_differing_paths, differing_paths); | 91 EXPECT_EQ(expected_differing_paths, differing_paths); |
| 89 | 92 |
| 90 EXPECT_TRUE(check.SetValue("a", new StringValue("test"))); | 93 EXPECT_TRUE(check.SetValue("a", make_scoped_ptr(new StringValue("test")))); |
| 91 EXPECT_TRUE(check.SetValue("c", new StringValue("test"))); | 94 EXPECT_TRUE(check.SetValue("c", make_scoped_ptr(new StringValue("test")))); |
| 92 EXPECT_TRUE(check.SetValue("d", new StringValue("test"))); | 95 EXPECT_TRUE(check.SetValue("d", make_scoped_ptr(new StringValue("test")))); |
| 93 | 96 |
| 94 reference.GetDifferingKeys(&check, &differing_paths); | 97 reference.GetDifferingKeys(&check, &differing_paths); |
| 95 expected_differing_paths.clear(); | 98 expected_differing_paths.clear(); |
| 96 expected_differing_paths.push_back("a"); | 99 expected_differing_paths.push_back("a"); |
| 97 expected_differing_paths.push_back("b"); | 100 expected_differing_paths.push_back("b"); |
| 98 expected_differing_paths.push_back("d"); | 101 expected_differing_paths.push_back("d"); |
| 99 expected_differing_paths.push_back("e"); | 102 expected_differing_paths.push_back("e"); |
| 100 EXPECT_EQ(expected_differing_paths, differing_paths); | 103 EXPECT_EQ(expected_differing_paths, differing_paths); |
| 101 } | 104 } |
| 102 | 105 |
| 103 TEST(PrefValueMapTest, SwapTwoMaps) { | 106 TEST(PrefValueMapTest, SwapTwoMaps) { |
| 104 PrefValueMap first_map; | 107 PrefValueMap first_map; |
| 105 EXPECT_TRUE(first_map.SetValue("a", new StringValue("test"))); | 108 EXPECT_TRUE( |
| 106 EXPECT_TRUE(first_map.SetValue("b", new StringValue("test"))); | 109 first_map.SetValue("a", make_scoped_ptr(new StringValue("test")))); |
| 107 EXPECT_TRUE(first_map.SetValue("c", new StringValue("test"))); | 110 EXPECT_TRUE( |
| 111 first_map.SetValue("b", make_scoped_ptr(new StringValue("test")))); |
| 112 EXPECT_TRUE( |
| 113 first_map.SetValue("c", make_scoped_ptr(new StringValue("test")))); |
| 108 | 114 |
| 109 PrefValueMap second_map; | 115 PrefValueMap second_map; |
| 110 EXPECT_TRUE(second_map.SetValue("d", new StringValue("test"))); | 116 EXPECT_TRUE( |
| 111 EXPECT_TRUE(second_map.SetValue("e", new StringValue("test"))); | 117 second_map.SetValue("d", make_scoped_ptr(new StringValue("test")))); |
| 112 EXPECT_TRUE(second_map.SetValue("f", new StringValue("test"))); | 118 EXPECT_TRUE( |
| 119 second_map.SetValue("e", make_scoped_ptr(new StringValue("test")))); |
| 120 EXPECT_TRUE( |
| 121 second_map.SetValue("f", make_scoped_ptr(new StringValue("test")))); |
| 113 | 122 |
| 114 first_map.Swap(&second_map); | 123 first_map.Swap(&second_map); |
| 115 | 124 |
| 116 EXPECT_TRUE(first_map.GetValue("d", NULL)); | 125 EXPECT_TRUE(first_map.GetValue("d", NULL)); |
| 117 EXPECT_TRUE(first_map.GetValue("e", NULL)); | 126 EXPECT_TRUE(first_map.GetValue("e", NULL)); |
| 118 EXPECT_TRUE(first_map.GetValue("f", NULL)); | 127 EXPECT_TRUE(first_map.GetValue("f", NULL)); |
| 119 | 128 |
| 120 EXPECT_TRUE(second_map.GetValue("a", NULL)); | 129 EXPECT_TRUE(second_map.GetValue("a", NULL)); |
| 121 EXPECT_TRUE(second_map.GetValue("b", NULL)); | 130 EXPECT_TRUE(second_map.GetValue("b", NULL)); |
| 122 EXPECT_TRUE(second_map.GetValue("c", NULL)); | 131 EXPECT_TRUE(second_map.GetValue("c", NULL)); |
| 123 } | 132 } |
| 124 | 133 |
| 125 } // namespace | 134 } // namespace |
| 126 } // namespace base | 135 } // namespace base |
| OLD | NEW |