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.
5 #include "base/prefs/pref_value_map.h"
7 #include "base/values.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(PrefValueMapTest
, SetValue
) {
15 const Value
* result
= NULL
;
16 EXPECT_FALSE(map
.GetValue("key", &result
));
19 EXPECT_TRUE(map
.SetValue("key", make_scoped_ptr(new StringValue("test"))));
20 EXPECT_FALSE(map
.SetValue("key", make_scoped_ptr(new StringValue("test"))));
21 EXPECT_TRUE(map
.SetValue("key", make_scoped_ptr(new StringValue("hi mom!"))));
23 EXPECT_TRUE(map
.GetValue("key", &result
));
24 EXPECT_TRUE(StringValue("hi mom!").Equals(result
));
27 TEST(PrefValueMapTest
, GetAndSetIntegerValue
) {
29 ASSERT_TRUE(map
.SetValue("key", make_scoped_ptr(new FundamentalValue(5))));
32 EXPECT_TRUE(map
.GetInteger("key", &int_value
));
33 EXPECT_EQ(5, int_value
);
35 map
.SetInteger("key", -14);
36 EXPECT_TRUE(map
.GetInteger("key", &int_value
));
37 EXPECT_EQ(-14, int_value
);
40 TEST(PrefValueMapTest
, SetDoubleValue
) {
42 ASSERT_TRUE(map
.SetValue("key", make_scoped_ptr(new FundamentalValue(5.5))));
44 const Value
* result
= NULL
;
45 ASSERT_TRUE(map
.GetValue("key", &result
));
46 double double_value
= 0.;
47 EXPECT_TRUE(result
->GetAsDouble(&double_value
));
48 EXPECT_DOUBLE_EQ(5.5, double_value
);
51 TEST(PrefValueMapTest
, RemoveValue
) {
53 EXPECT_FALSE(map
.RemoveValue("key"));
55 EXPECT_TRUE(map
.SetValue("key", make_scoped_ptr(new StringValue("test"))));
56 EXPECT_TRUE(map
.GetValue("key", NULL
));
58 EXPECT_TRUE(map
.RemoveValue("key"));
59 EXPECT_FALSE(map
.GetValue("key", NULL
));
61 EXPECT_FALSE(map
.RemoveValue("key"));
64 TEST(PrefValueMapTest
, Clear
) {
66 EXPECT_TRUE(map
.SetValue("key", make_scoped_ptr(new StringValue("test"))));
67 EXPECT_TRUE(map
.GetValue("key", NULL
));
71 EXPECT_FALSE(map
.GetValue("key", NULL
));
74 TEST(PrefValueMapTest
, GetDifferingKeys
) {
75 PrefValueMap reference
;
77 reference
.SetValue("b", make_scoped_ptr(new StringValue("test"))));
79 reference
.SetValue("c", make_scoped_ptr(new StringValue("test"))));
81 reference
.SetValue("e", make_scoped_ptr(new StringValue("test"))));
84 std::vector
<std::string
> differing_paths
;
85 std::vector
<std::string
> expected_differing_paths
;
87 reference
.GetDifferingKeys(&check
, &differing_paths
);
88 expected_differing_paths
.push_back("b");
89 expected_differing_paths
.push_back("c");
90 expected_differing_paths
.push_back("e");
91 EXPECT_EQ(expected_differing_paths
, differing_paths
);
93 EXPECT_TRUE(check
.SetValue("a", make_scoped_ptr(new StringValue("test"))));
94 EXPECT_TRUE(check
.SetValue("c", make_scoped_ptr(new StringValue("test"))));
95 EXPECT_TRUE(check
.SetValue("d", make_scoped_ptr(new StringValue("test"))));
97 reference
.GetDifferingKeys(&check
, &differing_paths
);
98 expected_differing_paths
.clear();
99 expected_differing_paths
.push_back("a");
100 expected_differing_paths
.push_back("b");
101 expected_differing_paths
.push_back("d");
102 expected_differing_paths
.push_back("e");
103 EXPECT_EQ(expected_differing_paths
, differing_paths
);
106 TEST(PrefValueMapTest
, SwapTwoMaps
) {
107 PrefValueMap first_map
;
109 first_map
.SetValue("a", make_scoped_ptr(new StringValue("test"))));
111 first_map
.SetValue("b", make_scoped_ptr(new StringValue("test"))));
113 first_map
.SetValue("c", make_scoped_ptr(new StringValue("test"))));
115 PrefValueMap second_map
;
117 second_map
.SetValue("d", make_scoped_ptr(new StringValue("test"))));
119 second_map
.SetValue("e", make_scoped_ptr(new StringValue("test"))));
121 second_map
.SetValue("f", make_scoped_ptr(new StringValue("test"))));
123 first_map
.Swap(&second_map
);
125 EXPECT_TRUE(first_map
.GetValue("d", NULL
));
126 EXPECT_TRUE(first_map
.GetValue("e", NULL
));
127 EXPECT_TRUE(first_map
.GetValue("f", NULL
));
129 EXPECT_TRUE(second_map
.GetValue("a", NULL
));
130 EXPECT_TRUE(second_map
.GetValue("b", NULL
));
131 EXPECT_TRUE(second_map
.GetValue("c", NULL
));