Home | History | Annotate | Download | only in prefs
      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 "base/values.h"
      6 #include "chrome/browser/prefs/pref_value_map.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 class PrefValueMapTest : public testing::Test {
     10 };
     11 
     12 TEST_F(PrefValueMapTest, SetValue) {
     13   PrefValueMap map;
     14   const Value* result = NULL;
     15   EXPECT_FALSE(map.GetValue("key", &result));
     16   EXPECT_FALSE(result);
     17 
     18   EXPECT_TRUE(map.SetValue("key", Value::CreateStringValue("test")));
     19   EXPECT_FALSE(map.SetValue("key", Value::CreateStringValue("test")));
     20   EXPECT_TRUE(map.SetValue("key", Value::CreateStringValue("hi mom!")));
     21 
     22   EXPECT_TRUE(map.GetValue("key", &result));
     23   EXPECT_TRUE(StringValue("hi mom!").Equals(result));
     24 }
     25 
     26 TEST_F(PrefValueMapTest, RemoveValue) {
     27   PrefValueMap map;
     28   EXPECT_FALSE(map.RemoveValue("key"));
     29 
     30   EXPECT_TRUE(map.SetValue("key", Value::CreateStringValue("test")));
     31   EXPECT_TRUE(map.GetValue("key", NULL));
     32 
     33   EXPECT_TRUE(map.RemoveValue("key"));
     34   EXPECT_FALSE(map.GetValue("key", NULL));
     35 
     36   EXPECT_FALSE(map.RemoveValue("key"));
     37 }
     38 
     39 TEST_F(PrefValueMapTest, Clear) {
     40   PrefValueMap map;
     41   EXPECT_TRUE(map.SetValue("key", Value::CreateStringValue("test")));
     42   EXPECT_TRUE(map.GetValue("key", NULL));
     43 
     44   map.Clear();
     45 
     46   EXPECT_FALSE(map.GetValue("key", NULL));
     47 }
     48 
     49 TEST_F(PrefValueMapTest, GetDifferingKeys) {
     50   PrefValueMap reference;
     51   EXPECT_TRUE(reference.SetValue("b", Value::CreateStringValue("test")));
     52   EXPECT_TRUE(reference.SetValue("c", Value::CreateStringValue("test")));
     53   EXPECT_TRUE(reference.SetValue("e", Value::CreateStringValue("test")));
     54 
     55   PrefValueMap check;
     56   std::vector<std::string> differing_paths;
     57   std::vector<std::string> expected_differing_paths;
     58 
     59   reference.GetDifferingKeys(&check, &differing_paths);
     60   expected_differing_paths.push_back("b");
     61   expected_differing_paths.push_back("c");
     62   expected_differing_paths.push_back("e");
     63   EXPECT_EQ(expected_differing_paths, differing_paths);
     64 
     65   EXPECT_TRUE(check.SetValue("a", Value::CreateStringValue("test")));
     66   EXPECT_TRUE(check.SetValue("c", Value::CreateStringValue("test")));
     67   EXPECT_TRUE(check.SetValue("d", Value::CreateStringValue("test")));
     68 
     69   reference.GetDifferingKeys(&check, &differing_paths);
     70   expected_differing_paths.clear();
     71   expected_differing_paths.push_back("a");
     72   expected_differing_paths.push_back("b");
     73   expected_differing_paths.push_back("d");
     74   expected_differing_paths.push_back("e");
     75   EXPECT_EQ(expected_differing_paths, differing_paths);
     76 }
     77