OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/test/data/resource.h" | 5 #include "app/test/data/resource.h" |
6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/dummy_pref_store.h" | 8 #include "chrome/browser/dummy_pref_store.h" |
9 #include "chrome/browser/pref_value_store.h" | 9 #include "chrome/browser/pref_value_store.h" |
10 | 10 |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using testing::_; | 14 using testing::_; |
15 using testing::Mock; | 15 using testing::Mock; |
16 | 16 |
17 // Names of the preferences used in this test program. | 17 // Names of the preferences used in this test program. |
18 namespace prefs { | 18 namespace prefs { |
| 19 const wchar_t kCurrentThemeID[] = L"extensions.theme.id"; |
19 const wchar_t kDeleteCache[] = L"browser.clear_data.cache"; | 20 const wchar_t kDeleteCache[] = L"browser.clear_data.cache"; |
| 21 const wchar_t kExtensionPref[] = L"extension.pref"; |
| 22 const wchar_t kHomepage[] = L"homepage"; |
20 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; | 23 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; |
21 const wchar_t kHomepage[] = L"homepage"; | |
22 const wchar_t kMissingPref[] = L"this.pref.does_not_exist"; | 24 const wchar_t kMissingPref[] = L"this.pref.does_not_exist"; |
23 const wchar_t kRecommendedPref[] = L"this.pref.recommended_value_only"; | 25 const wchar_t kRecommendedPref[] = L"this.pref.recommended_value_only"; |
24 const wchar_t kSampleDict[] = L"sample.dict"; | 26 const wchar_t kSampleDict[] = L"sample.dict"; |
25 const wchar_t kSampleList[] = L"sample.list"; | 27 const wchar_t kSampleList[] = L"sample.list"; |
26 } | 28 } |
27 | 29 |
28 // Expected values of all preferences used in this test programm. | 30 // Potentailly expected values of all preferences used in this test program. |
29 namespace expected { | 31 namespace user { |
30 const int kMaxTabsValue = 31; | 32 const int kMaxTabsValue = 31; |
31 const bool kDeleteCacheValue = true; | 33 const bool kDeleteCacheValue = true; |
| 34 const std::wstring kCurrentThemeIDValue = L"abcdefg"; |
32 const std::wstring kHomepageValue = L"http://www.google.com"; | 35 const std::wstring kHomepageValue = L"http://www.google.com"; |
33 } | 36 } |
34 | 37 |
35 namespace enforced { | 38 namespace enforced { |
36 const std::wstring kHomepageValue = L"http://www.topeka.com"; | 39 const std::wstring kHomepageValue = L"http://www.topeka.com"; |
37 } | 40 } |
38 | 41 |
| 42 namespace extension { |
| 43 const std::wstring kCurrentThemeIDValue = L"set by extension"; |
| 44 const std::wstring kHomepageValue = L"http://www.chromium.org"; |
| 45 } |
| 46 |
39 namespace recommended { | 47 namespace recommended { |
40 const int kMaxTabsValue = 10; | 48 const int kMaxTabsValue = 10; |
41 const bool kRecommendedPrefValue = true; | 49 const bool kRecommendedPrefValue = true; |
42 } | 50 } |
43 | 51 |
44 class PrefValueStoreTest : public testing::Test { | 52 class PrefValueStoreTest : public testing::Test { |
45 protected: | 53 protected: |
46 scoped_ptr<PrefValueStore> pref_value_store_; | 54 scoped_ptr<PrefValueStore> pref_value_store_; |
47 | 55 |
48 // |PrefStore|s are owned by the |PrefValueStore|. | 56 // |PrefStore|s are owned by the |PrefValueStore|. |
49 DummyPrefStore* enforced_pref_store_; | 57 DummyPrefStore* enforced_pref_store_; |
| 58 DummyPrefStore* extension_pref_store_; |
50 DummyPrefStore* recommended_pref_store_; | 59 DummyPrefStore* recommended_pref_store_; |
51 DummyPrefStore* user_pref_store_; | 60 DummyPrefStore* user_pref_store_; |
52 | 61 |
53 // Preferences are owned by the individual |DummyPrefStores|. | 62 // Preferences are owned by the individual |DummyPrefStores|. |
54 DictionaryValue* enforced_prefs_; | 63 DictionaryValue* enforced_prefs_; |
| 64 DictionaryValue* extension_prefs_; |
55 DictionaryValue* user_prefs_; | 65 DictionaryValue* user_prefs_; |
56 DictionaryValue* recommended_prefs_; | 66 DictionaryValue* recommended_prefs_; |
57 | 67 |
58 virtual void SetUp() { | 68 virtual void SetUp() { |
59 // Create dummy user preferences. | 69 // Create dummy user preferences. |
60 enforced_prefs_= CreateEnforcedPrefs(); | 70 enforced_prefs_= CreateEnforcedPrefs(); |
| 71 extension_prefs_ = CreateExtensionPrefs(); |
61 user_prefs_ = CreateUserPrefs(); | 72 user_prefs_ = CreateUserPrefs(); |
62 recommended_prefs_ = CreateRecommendedPrefs(); | 73 recommended_prefs_ = CreateRecommendedPrefs(); |
63 | 74 |
64 // Create |DummyPrefStore|s. | 75 // Create |DummyPrefStore|s. |
65 enforced_pref_store_ = new DummyPrefStore(); | 76 enforced_pref_store_ = new DummyPrefStore(); |
66 enforced_pref_store_->set_prefs(enforced_prefs_); | 77 enforced_pref_store_->set_prefs(enforced_prefs_); |
| 78 extension_pref_store_ = new DummyPrefStore(); |
| 79 extension_pref_store_->set_prefs(extension_prefs_); |
67 user_pref_store_ = new DummyPrefStore(); | 80 user_pref_store_ = new DummyPrefStore(); |
68 user_pref_store_->set_read_only(false); | 81 user_pref_store_->set_read_only(false); |
69 user_pref_store_->set_prefs(user_prefs_); | 82 user_pref_store_->set_prefs(user_prefs_); |
70 recommended_pref_store_ = new DummyPrefStore(); | 83 recommended_pref_store_ = new DummyPrefStore(); |
71 recommended_pref_store_->set_prefs(recommended_prefs_); | 84 recommended_pref_store_->set_prefs(recommended_prefs_); |
72 | 85 |
73 // Create a new pref-value-store. | 86 // Create a new pref-value-store. |
74 pref_value_store_.reset(new PrefValueStore(enforced_pref_store_, | 87 pref_value_store_.reset(new PrefValueStore(enforced_pref_store_, |
| 88 extension_pref_store_, |
75 user_pref_store_, | 89 user_pref_store_, |
76 recommended_pref_store_)); | 90 recommended_pref_store_)); |
77 } | 91 } |
78 | 92 |
79 // Creates a new dictionary and stores some sample user preferences | 93 // Creates a new dictionary and stores some sample user preferences |
80 // in it. | 94 // in it. |
81 DictionaryValue* CreateUserPrefs() { | 95 DictionaryValue* CreateUserPrefs() { |
82 DictionaryValue* user_prefs = new DictionaryValue(); | 96 DictionaryValue* user_prefs = new DictionaryValue(); |
83 user_prefs->SetBoolean(prefs::kDeleteCache, expected::kDeleteCacheValue); | 97 user_prefs->SetBoolean(prefs::kDeleteCache, user::kDeleteCacheValue); |
84 user_prefs->SetInteger(prefs::kMaxTabs, expected::kMaxTabsValue); | 98 user_prefs->SetInteger(prefs::kMaxTabs, user::kMaxTabsValue); |
85 user_prefs->SetString(prefs::kHomepage, expected::kHomepageValue); | 99 user_prefs->SetString(prefs::kCurrentThemeID, user::kCurrentThemeIDValue); |
| 100 user_prefs->SetString(prefs::kHomepage, user::kHomepageValue); |
86 return user_prefs; | 101 return user_prefs; |
87 } | 102 } |
88 | 103 |
89 DictionaryValue* CreateEnforcedPrefs() { | 104 DictionaryValue* CreateEnforcedPrefs() { |
90 DictionaryValue* enforced_prefs = new DictionaryValue(); | 105 DictionaryValue* enforced_prefs = new DictionaryValue(); |
91 enforced_prefs->SetString(prefs::kHomepage, enforced::kHomepageValue); | 106 enforced_prefs->SetString(prefs::kHomepage, enforced::kHomepageValue); |
92 return enforced_prefs; | 107 return enforced_prefs; |
93 } | 108 } |
94 | 109 |
| 110 DictionaryValue* CreateExtensionPrefs() { |
| 111 DictionaryValue* extension_prefs = new DictionaryValue(); |
| 112 extension_prefs->SetString(prefs::kCurrentThemeID, |
| 113 extension::kCurrentThemeIDValue); |
| 114 extension_prefs->SetString(prefs::kHomepage, extension::kHomepageValue); |
| 115 return extension_prefs; |
| 116 } |
| 117 |
95 DictionaryValue* CreateRecommendedPrefs() { | 118 DictionaryValue* CreateRecommendedPrefs() { |
96 DictionaryValue* recommended_prefs = new DictionaryValue(); | 119 DictionaryValue* recommended_prefs = new DictionaryValue(); |
97 recommended_prefs->SetInteger(prefs::kMaxTabs, recommended::kMaxTabsValue); | 120 recommended_prefs->SetInteger(prefs::kMaxTabs, recommended::kMaxTabsValue); |
98 recommended_prefs->SetBoolean( | 121 recommended_prefs->SetBoolean( |
99 prefs::kRecommendedPref, | 122 prefs::kRecommendedPref, |
100 recommended::kRecommendedPrefValue); | 123 recommended::kRecommendedPrefValue); |
101 return recommended_prefs; } | 124 return recommended_prefs; } |
102 | 125 |
103 DictionaryValue* CreateSampleDictValue() { | 126 DictionaryValue* CreateSampleDictValue() { |
104 DictionaryValue* sample_dict = new DictionaryValue(); | 127 DictionaryValue* sample_dict = new DictionaryValue(); |
(...skipping 11 matching lines...) Expand all Loading... |
116 sample_list->Set(3, Value::CreateIntegerValue(3)); | 139 sample_list->Set(3, Value::CreateIntegerValue(3)); |
117 return sample_list; | 140 return sample_list; |
118 } | 141 } |
119 | 142 |
120 virtual void TearDown() {} | 143 virtual void TearDown() {} |
121 }; | 144 }; |
122 | 145 |
123 | 146 |
124 TEST_F(PrefValueStoreTest, IsReadOnly) { | 147 TEST_F(PrefValueStoreTest, IsReadOnly) { |
125 enforced_pref_store_->set_read_only(true); | 148 enforced_pref_store_->set_read_only(true); |
| 149 extension_pref_store_->set_read_only(true); |
126 user_pref_store_->set_read_only(true); | 150 user_pref_store_->set_read_only(true); |
127 recommended_pref_store_->set_read_only(true); | 151 recommended_pref_store_->set_read_only(true); |
128 EXPECT_TRUE(pref_value_store_->ReadOnly()); | 152 EXPECT_TRUE(pref_value_store_->ReadOnly()); |
129 | 153 |
130 user_pref_store_->set_read_only(false); | 154 user_pref_store_->set_read_only(false); |
131 EXPECT_FALSE(pref_value_store_->ReadOnly()); | 155 EXPECT_FALSE(pref_value_store_->ReadOnly()); |
132 } | 156 } |
133 | 157 |
134 TEST_F(PrefValueStoreTest, GetValue) { | 158 TEST_F(PrefValueStoreTest, GetValue) { |
135 Value* value; | 159 Value* value; |
136 | 160 |
137 // Test getting an enforced value overwriting a user defined value. | 161 // Test getting an enforced value overwriting a user-defined and |
| 162 // extension-defined value. |
138 value = NULL; | 163 value = NULL; |
139 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kHomepage, &value)); | 164 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kHomepage, &value)); |
140 std::wstring actual_str_value; | 165 std::wstring actual_str_value; |
141 EXPECT_TRUE(value->GetAsString(&actual_str_value)); | 166 EXPECT_TRUE(value->GetAsString(&actual_str_value)); |
142 EXPECT_EQ(enforced::kHomepageValue, actual_str_value); | 167 EXPECT_EQ(enforced::kHomepageValue, actual_str_value); |
143 | 168 |
144 // Test getting a user set value. | 169 // Test getting an extension value overwriting a user-defined value. |
| 170 value = NULL; |
| 171 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kCurrentThemeID, &value)); |
| 172 EXPECT_TRUE(value->GetAsString(&actual_str_value)); |
| 173 EXPECT_EQ(extension::kCurrentThemeIDValue, actual_str_value); |
| 174 |
| 175 // Test getting a user-set value. |
145 value = NULL; | 176 value = NULL; |
146 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kDeleteCache, &value)); | 177 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kDeleteCache, &value)); |
147 bool actual_bool_value = false; | 178 bool actual_bool_value = false; |
148 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 179 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
149 EXPECT_EQ(expected::kDeleteCacheValue, actual_bool_value); | 180 EXPECT_EQ(user::kDeleteCacheValue, actual_bool_value); |
150 | 181 |
151 // Test getting a user set value overwriting a recommended value. | 182 // Test getting a user set value overwriting a recommended value. |
152 value = NULL; | 183 value = NULL; |
153 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kMaxTabs, &value)); | 184 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kMaxTabs, &value)); |
154 int actual_int_value = -1; | 185 int actual_int_value = -1; |
155 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 186 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
156 EXPECT_EQ(expected::kMaxTabsValue, actual_int_value); | 187 EXPECT_EQ(user::kMaxTabsValue, actual_int_value); |
157 | 188 |
158 // Test getting a recommended value. | 189 // Test getting a recommended value. |
159 value = NULL; | 190 value = NULL; |
160 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kRecommendedPref, &value)); | 191 ASSERT_TRUE(pref_value_store_->GetValue(prefs::kRecommendedPref, &value)); |
161 actual_bool_value = false; | 192 actual_bool_value = false; |
162 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 193 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
163 EXPECT_EQ(recommended::kRecommendedPrefValue, actual_bool_value); | 194 EXPECT_EQ(recommended::kRecommendedPrefValue, actual_bool_value); |
164 | 195 |
165 // Test getting a preference value that the |PrefValueStore| | 196 // Test getting a preference value that the |PrefValueStore| |
166 // does not contain. | 197 // does not contain. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 std::wstring value_str; | 241 std::wstring value_str; |
211 actual_value->GetAsString(&value_str); | 242 actual_value->GetAsString(&value_str); |
212 ASSERT_EQ(enforced::kHomepageValue, value_str); | 243 ASSERT_EQ(enforced::kHomepageValue, value_str); |
213 | 244 |
214 // User preferences values can be set | 245 // User preferences values can be set |
215 ASSERT_FALSE(pref_value_store_->PrefValueIsManaged(prefs::kMaxTabs)); | 246 ASSERT_FALSE(pref_value_store_->PrefValueIsManaged(prefs::kMaxTabs)); |
216 actual_value = NULL; | 247 actual_value = NULL; |
217 pref_value_store_->GetValue(prefs::kMaxTabs, &actual_value); | 248 pref_value_store_->GetValue(prefs::kMaxTabs, &actual_value); |
218 int int_value; | 249 int int_value; |
219 EXPECT_TRUE(actual_value->GetAsInteger(&int_value)); | 250 EXPECT_TRUE(actual_value->GetAsInteger(&int_value)); |
220 EXPECT_EQ(expected::kMaxTabsValue, int_value); | 251 EXPECT_EQ(user::kMaxTabsValue, int_value); |
221 | 252 |
222 new_value = Value::CreateIntegerValue(1); | 253 new_value = Value::CreateIntegerValue(1); |
223 pref_value_store_->SetUserPrefValue(prefs::kMaxTabs, new_value); | 254 pref_value_store_->SetUserPrefValue(prefs::kMaxTabs, new_value); |
224 actual_value = NULL; | 255 actual_value = NULL; |
225 pref_value_store_->GetValue(prefs::kMaxTabs, &actual_value); | 256 pref_value_store_->GetValue(prefs::kMaxTabs, &actual_value); |
226 EXPECT_TRUE(new_value->Equals(actual_value)); | 257 EXPECT_TRUE(new_value->Equals(actual_value)); |
227 | 258 |
228 // Set and Get |DictionaryValue| | 259 // Set and Get |DictionaryValue| |
229 DictionaryValue* expected_dict_value = CreateSampleDictValue(); | 260 DictionaryValue* expected_dict_value = CreateSampleDictValue(); |
230 pref_value_store_->SetUserPrefValue(prefs::kSampleDict, expected_dict_value); | 261 pref_value_store_->SetUserPrefValue(prefs::kSampleDict, expected_dict_value); |
(...skipping 28 matching lines...) Expand all Loading... |
259 | 290 |
260 // Test a preference from the recommended pref store. | 291 // Test a preference from the recommended pref store. |
261 ASSERT_TRUE(pref_value_store_->HasPrefPath(prefs::kRecommendedPref)); | 292 ASSERT_TRUE(pref_value_store_->HasPrefPath(prefs::kRecommendedPref)); |
262 EXPECT_FALSE(pref_value_store_->PrefValueIsManaged(prefs::kRecommendedPref)); | 293 EXPECT_FALSE(pref_value_store_->PrefValueIsManaged(prefs::kRecommendedPref)); |
263 | 294 |
264 // Test a preference for which the PrefValueStore does not contain a value. | 295 // Test a preference for which the PrefValueStore does not contain a value. |
265 ASSERT_FALSE(pref_value_store_->HasPrefPath(prefs::kMissingPref)); | 296 ASSERT_FALSE(pref_value_store_->HasPrefPath(prefs::kMissingPref)); |
266 EXPECT_FALSE(pref_value_store_->PrefValueIsManaged(prefs::kMissingPref)); | 297 EXPECT_FALSE(pref_value_store_->PrefValueIsManaged(prefs::kMissingPref)); |
267 } | 298 } |
268 | 299 |
OLD | NEW |