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.
5 #include "components/user_prefs/tracked/pref_service_hash_store_contents.h"
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"
14 class PrefServiceHashStoreContentsTest
: public testing::Test
{
16 void SetUp() override
{
17 PrefServiceHashStoreContents::RegisterPrefs(local_state_
.registry());
21 TestingPrefServiceSimple local_state_
;
24 TEST_F(PrefServiceHashStoreContentsTest
, hash_store_id
) {
25 PrefServiceHashStoreContents
contents("store_id", &local_state_
);
26 ASSERT_EQ("store_id", contents
.hash_store_id());
29 TEST_F(PrefServiceHashStoreContentsTest
, IsInitialized
) {
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());
37 PrefServiceHashStoreContents
contents("store_id", &local_state_
);
38 ASSERT_TRUE(contents
.IsInitialized());
39 PrefServiceHashStoreContents
other_contents("other_store_id",
41 ASSERT_FALSE(other_contents
.IsInitialized());
45 TEST_F(PrefServiceHashStoreContentsTest
, Reset
) {
46 ASSERT_FALSE(local_state_
.GetUserPrefValue(
47 PrefServiceHashStoreContents::kProfilePreferenceHashes
));
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",
56 (*other_contents
.GetMutableContents())
57 ->Set("foo", new base::StringValue("bar"));
60 ASSERT_TRUE(local_state_
.GetUserPrefValue(
61 PrefServiceHashStoreContents::kProfilePreferenceHashes
));
64 PrefServiceHashStoreContents
contents("store_id", &local_state_
);
65 ASSERT_TRUE(contents
.IsInitialized());
67 ASSERT_FALSE(contents
.IsInitialized());
70 ASSERT_TRUE(local_state_
.GetUserPrefValue(
71 PrefServiceHashStoreContents::kProfilePreferenceHashes
));
74 PrefServiceHashStoreContents
contents("store_id", &local_state_
);
75 ASSERT_FALSE(contents
.IsInitialized());
76 PrefServiceHashStoreContents
other_contents("other_store_id",
78 ASSERT_TRUE(other_contents
.IsInitialized());
82 PrefServiceHashStoreContents
other_contents("other_store_id",
84 other_contents
.Reset();
87 ASSERT_FALSE(local_state_
.GetUserPrefValue(
88 PrefServiceHashStoreContents::kProfilePreferenceHashes
));
91 TEST_F(PrefServiceHashStoreContentsTest
, GetAndSetContents
) {
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",
102 ASSERT_EQ(NULL
, other_contents
.GetContents());
105 PrefServiceHashStoreContents
contents("store_id", &local_state_
);
106 ASSERT_FALSE(contents
.GetContents() == NULL
);
110 TEST_F(PrefServiceHashStoreContentsTest
, GetAndSetSuperMac
) {
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());
120 PrefServiceHashStoreContents
contents("store_id", &local_state_
);
121 ASSERT_EQ("0123456789", contents
.GetSuperMac());
122 PrefServiceHashStoreContents
other_contents("other_store_id",
124 ASSERT_TRUE(other_contents
.GetSuperMac().empty());
128 TEST_F(PrefServiceHashStoreContentsTest
, ResetAllPrefHashStores
) {
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"));
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());
144 PrefServiceHashStoreContents::ResetAllPrefHashStores(&local_state_
);
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());