Home | History | Annotate | Download | only in tracked
      1 // Copyright 2014 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 "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h"
      6 
      7 #include <string>
      8 
      9 #include "base/prefs/pref_service.h"
     10 #include "base/prefs/testing_pref_service.h"
     11 #include "base/values.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 class PrefServiceHashStoreContentsTest : public testing::Test {
     15  public:
     16   virtual void SetUp() OVERRIDE {
     17     PrefServiceHashStoreContents::RegisterPrefs(local_state_.registry());
     18   }
     19 
     20  protected:
     21   TestingPrefServiceSimple local_state_;
     22 };
     23 
     24 TEST_F(PrefServiceHashStoreContentsTest, hash_store_id) {
     25   PrefServiceHashStoreContents contents("store_id", &local_state_);
     26   ASSERT_EQ("store_id", contents.hash_store_id());
     27 }
     28 
     29 TEST_F(PrefServiceHashStoreContentsTest, IsInitialized) {
     30   {
     31     PrefServiceHashStoreContents contents("store_id", &local_state_);
     32     ASSERT_FALSE(contents.IsInitialized());
     33     (*contents.GetMutableContents())->Set("foo", new base::StringValue("bar"));
     34     ASSERT_TRUE(contents.IsInitialized());
     35   }
     36   {
     37     PrefServiceHashStoreContents contents("store_id", &local_state_);
     38     ASSERT_TRUE(contents.IsInitialized());
     39     PrefServiceHashStoreContents other_contents("other_store_id",
     40                                                 &local_state_);
     41     ASSERT_FALSE(other_contents.IsInitialized());
     42   }
     43 }
     44 
     45 TEST_F(PrefServiceHashStoreContentsTest, Reset) {
     46   ASSERT_FALSE(local_state_.GetUserPrefValue(
     47       PrefServiceHashStoreContents::kProfilePreferenceHashes));
     48 
     49   {
     50     PrefServiceHashStoreContents contents("store_id", &local_state_);
     51     ASSERT_FALSE(contents.IsInitialized());
     52     (*contents.GetMutableContents())->Set("foo", new base::StringValue("bar"));
     53     ASSERT_TRUE(contents.IsInitialized());
     54     PrefServiceHashStoreContents other_contents("other_store_id",
     55                                                 &local_state_);
     56     (*other_contents.GetMutableContents())
     57         ->Set("foo", new base::StringValue("bar"));
     58   }
     59 
     60   ASSERT_TRUE(local_state_.GetUserPrefValue(
     61       PrefServiceHashStoreContents::kProfilePreferenceHashes));
     62 
     63   {
     64     PrefServiceHashStoreContents contents("store_id", &local_state_);
     65     ASSERT_TRUE(contents.IsInitialized());
     66     contents.Reset();
     67     ASSERT_FALSE(contents.IsInitialized());
     68   }
     69 
     70   ASSERT_TRUE(local_state_.GetUserPrefValue(
     71       PrefServiceHashStoreContents::kProfilePreferenceHashes));
     72 
     73   {
     74     PrefServiceHashStoreContents contents("store_id", &local_state_);
     75     ASSERT_FALSE(contents.IsInitialized());
     76     PrefServiceHashStoreContents other_contents("other_store_id",
     77                                                 &local_state_);
     78     ASSERT_TRUE(other_contents.IsInitialized());
     79   }
     80 
     81   {
     82     PrefServiceHashStoreContents other_contents("other_store_id",
     83                                                 &local_state_);
     84     other_contents.Reset();
     85   }
     86 
     87   ASSERT_FALSE(local_state_.GetUserPrefValue(
     88       PrefServiceHashStoreContents::kProfilePreferenceHashes));
     89 }
     90 
     91 TEST_F(PrefServiceHashStoreContentsTest, GetAndSetContents) {
     92   {
     93     PrefServiceHashStoreContents contents("store_id", &local_state_);
     94     ASSERT_EQ(NULL, contents.GetContents());
     95     (*contents.GetMutableContents())->Set("foo", new base::StringValue("bar"));
     96     ASSERT_FALSE(contents.GetContents() == NULL);
     97     std::string actual_value;
     98     ASSERT_TRUE(contents.GetContents()->GetString("foo", &actual_value));
     99     ASSERT_EQ("bar", actual_value);
    100     PrefServiceHashStoreContents other_contents("other_store_id",
    101                                                 &local_state_);
    102     ASSERT_EQ(NULL, other_contents.GetContents());
    103   }
    104   {
    105     PrefServiceHashStoreContents contents("store_id", &local_state_);
    106     ASSERT_FALSE(contents.GetContents() == NULL);
    107   }
    108 }
    109 
    110 TEST_F(PrefServiceHashStoreContentsTest, GetAndSetSuperMac) {
    111   {
    112     PrefServiceHashStoreContents contents("store_id", &local_state_);
    113     ASSERT_TRUE(contents.GetSuperMac().empty());
    114     (*contents.GetMutableContents())->Set("foo", new base::StringValue("bar"));
    115     ASSERT_TRUE(contents.GetSuperMac().empty());
    116     contents.SetSuperMac("0123456789");
    117     ASSERT_EQ("0123456789", contents.GetSuperMac());
    118   }
    119   {
    120     PrefServiceHashStoreContents contents("store_id", &local_state_);
    121     ASSERT_EQ("0123456789", contents.GetSuperMac());
    122     PrefServiceHashStoreContents other_contents("other_store_id",
    123                                                 &local_state_);
    124     ASSERT_TRUE(other_contents.GetSuperMac().empty());
    125   }
    126 }
    127 
    128 TEST_F(PrefServiceHashStoreContentsTest, ResetAllPrefHashStores) {
    129   {
    130     PrefServiceHashStoreContents contents_1("store_id_1", &local_state_);
    131     PrefServiceHashStoreContents contents_2("store_id_2", &local_state_);
    132     (*contents_1.GetMutableContents())
    133         ->Set("foo", new base::StringValue("bar"));
    134     (*contents_2.GetMutableContents())
    135         ->Set("foo", new base::StringValue("bar"));
    136   }
    137   {
    138     PrefServiceHashStoreContents contents_1("store_id_1", &local_state_);
    139     PrefServiceHashStoreContents contents_2("store_id_2", &local_state_);
    140     ASSERT_TRUE(contents_1.IsInitialized());
    141     ASSERT_TRUE(contents_2.IsInitialized());
    142   }
    143 
    144   PrefServiceHashStoreContents::ResetAllPrefHashStores(&local_state_);
    145 
    146   {
    147     PrefServiceHashStoreContents contents_1("store_id_1", &local_state_);
    148     PrefServiceHashStoreContents contents_2("store_id_2", &local_state_);
    149     ASSERT_FALSE(contents_1.IsInitialized());
    150     ASSERT_FALSE(contents_2.IsInitialized());
    151   }
    152 }
    153