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", new StringValue("test")));
20 EXPECT_FALSE(map
.SetValue("key", new StringValue("test")));
21 EXPECT_TRUE(map
.SetValue("key", 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", 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
, RemoveValue
) {
42 EXPECT_FALSE(map
.RemoveValue("key"));
44 EXPECT_TRUE(map
.SetValue("key", new StringValue("test")));
45 EXPECT_TRUE(map
.GetValue("key", NULL
));
47 EXPECT_TRUE(map
.RemoveValue("key"));
48 EXPECT_FALSE(map
.GetValue("key", NULL
));
50 EXPECT_FALSE(map
.RemoveValue("key"));
53 TEST(PrefValueMapTest
, Clear
) {
55 EXPECT_TRUE(map
.SetValue("key", new StringValue("test")));
56 EXPECT_TRUE(map
.GetValue("key", NULL
));
60 EXPECT_FALSE(map
.GetValue("key", NULL
));
63 TEST(PrefValueMapTest
, GetDifferingKeys
) {
64 PrefValueMap reference
;
65 EXPECT_TRUE(reference
.SetValue("b", new StringValue("test")));
66 EXPECT_TRUE(reference
.SetValue("c", new StringValue("test")));
67 EXPECT_TRUE(reference
.SetValue("e", new StringValue("test")));
70 std::vector
<std::string
> differing_paths
;
71 std::vector
<std::string
> expected_differing_paths
;
73 reference
.GetDifferingKeys(&check
, &differing_paths
);
74 expected_differing_paths
.push_back("b");
75 expected_differing_paths
.push_back("c");
76 expected_differing_paths
.push_back("e");
77 EXPECT_EQ(expected_differing_paths
, differing_paths
);
79 EXPECT_TRUE(check
.SetValue("a", new StringValue("test")));
80 EXPECT_TRUE(check
.SetValue("c", new StringValue("test")));
81 EXPECT_TRUE(check
.SetValue("d", new StringValue("test")));
83 reference
.GetDifferingKeys(&check
, &differing_paths
);
84 expected_differing_paths
.clear();
85 expected_differing_paths
.push_back("a");
86 expected_differing_paths
.push_back("b");
87 expected_differing_paths
.push_back("d");
88 expected_differing_paths
.push_back("e");
89 EXPECT_EQ(expected_differing_paths
, differing_paths
);
92 TEST(PrefValueMapTest
, SwapTwoMaps
) {
93 PrefValueMap first_map
;
94 EXPECT_TRUE(first_map
.SetValue("a", new StringValue("test")));
95 EXPECT_TRUE(first_map
.SetValue("b", new StringValue("test")));
96 EXPECT_TRUE(first_map
.SetValue("c", new StringValue("test")));
98 PrefValueMap second_map
;
99 EXPECT_TRUE(second_map
.SetValue("d", new StringValue("test")));
100 EXPECT_TRUE(second_map
.SetValue("e", new StringValue("test")));
101 EXPECT_TRUE(second_map
.SetValue("f", new StringValue("test")));
103 first_map
.Swap(&second_map
);
105 EXPECT_TRUE(first_map
.GetValue("d", NULL
));
106 EXPECT_TRUE(first_map
.GetValue("e", NULL
));
107 EXPECT_TRUE(first_map
.GetValue("f", NULL
));
109 EXPECT_TRUE(second_map
.GetValue("a", NULL
));
110 EXPECT_TRUE(second_map
.GetValue("b", NULL
));
111 EXPECT_TRUE(second_map
.GetValue("c", NULL
));