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