OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/file_path.h" |
5 #include "chrome/common/notification_service.h" | 6 #include "chrome/common/notification_service.h" |
6 #include "chrome/common/pref_member.h" | 7 #include "chrome/common/pref_member.h" |
7 #include "chrome/common/pref_service.h" | 8 #include "chrome/common/pref_service.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
12 static const wchar_t kBoolPref[] = L"bool"; | 13 static const wchar_t kBoolPref[] = L"bool"; |
13 static const wchar_t kIntPref[] = L"int"; | 14 static const wchar_t kIntPref[] = L"int"; |
14 static const wchar_t kRealPref[] = L"real"; | 15 static const wchar_t kRealPref[] = L"real"; |
(...skipping 27 matching lines...) Expand all Loading... |
42 StringPrefMember str_; | 43 StringPrefMember str_; |
43 int observe_cnt_; | 44 int observe_cnt_; |
44 | 45 |
45 private: | 46 private: |
46 PrefService* prefs_; | 47 PrefService* prefs_; |
47 }; | 48 }; |
48 | 49 |
49 } // anonymous namespace | 50 } // anonymous namespace |
50 | 51 |
51 TEST(PrefMemberTest, BasicGetAndSet) { | 52 TEST(PrefMemberTest, BasicGetAndSet) { |
52 PrefService prefs; | 53 PrefService prefs(FilePath(), NULL); |
53 RegisterTestPrefs(&prefs); | 54 RegisterTestPrefs(&prefs); |
54 | 55 |
55 // Test bool | 56 // Test bool |
56 BooleanPrefMember boolean; | 57 BooleanPrefMember boolean; |
57 boolean.Init(kBoolPref, &prefs, NULL); | 58 boolean.Init(kBoolPref, &prefs, NULL); |
58 | 59 |
59 // Check the defaults | 60 // Check the defaults |
60 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); | 61 EXPECT_FALSE(prefs.GetBoolean(kBoolPref)); |
61 EXPECT_FALSE(boolean.GetValue()); | 62 EXPECT_FALSE(boolean.GetValue()); |
62 EXPECT_FALSE(*boolean); | 63 EXPECT_FALSE(*boolean); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 133 |
133 // Try changing back through the pref. | 134 // Try changing back through the pref. |
134 prefs.SetString(kStringPref, L"bar"); | 135 prefs.SetString(kStringPref, L"bar"); |
135 EXPECT_EQ(L"bar", prefs.GetString(kStringPref)); | 136 EXPECT_EQ(L"bar", prefs.GetString(kStringPref)); |
136 EXPECT_EQ(L"bar", string.GetValue()); | 137 EXPECT_EQ(L"bar", string.GetValue()); |
137 EXPECT_EQ(L"bar", *string); | 138 EXPECT_EQ(L"bar", *string); |
138 } | 139 } |
139 | 140 |
140 TEST(PrefMemberTest, TwoPrefs) { | 141 TEST(PrefMemberTest, TwoPrefs) { |
141 // Make sure two RealPrefMembers stay in sync. | 142 // Make sure two RealPrefMembers stay in sync. |
142 PrefService prefs; | 143 PrefService prefs(FilePath(), NULL); |
143 RegisterTestPrefs(&prefs); | 144 RegisterTestPrefs(&prefs); |
144 | 145 |
145 RealPrefMember pref1; | 146 RealPrefMember pref1; |
146 pref1.Init(kRealPref, &prefs, NULL); | 147 pref1.Init(kRealPref, &prefs, NULL); |
147 RealPrefMember pref2; | 148 RealPrefMember pref2; |
148 pref2.Init(kRealPref, &prefs, NULL); | 149 pref2.Init(kRealPref, &prefs, NULL); |
149 | 150 |
150 pref1.SetValue(2.3); | 151 pref1.SetValue(2.3); |
151 EXPECT_EQ(2.3, *pref2); | 152 EXPECT_EQ(2.3, *pref2); |
152 | 153 |
153 pref2.SetValue(3.5); | 154 pref2.SetValue(3.5); |
154 EXPECT_EQ(3.5, *pref1); | 155 EXPECT_EQ(3.5, *pref1); |
155 | 156 |
156 prefs.SetReal(kRealPref, 4.2); | 157 prefs.SetReal(kRealPref, 4.2); |
157 EXPECT_EQ(4.2, *pref1); | 158 EXPECT_EQ(4.2, *pref1); |
158 EXPECT_EQ(4.2, *pref2); | 159 EXPECT_EQ(4.2, *pref2); |
159 } | 160 } |
160 | 161 |
161 TEST(PrefMemberTest, Observer) { | 162 TEST(PrefMemberTest, Observer) { |
162 PrefService prefs; | 163 PrefService prefs(FilePath(), NULL); |
163 RegisterTestPrefs(&prefs); | 164 RegisterTestPrefs(&prefs); |
164 | 165 |
165 PrefMemberTestClass test_obj(&prefs); | 166 PrefMemberTestClass test_obj(&prefs); |
166 EXPECT_EQ(L"default", *test_obj.str_); | 167 EXPECT_EQ(L"default", *test_obj.str_); |
167 | 168 |
168 // Calling SetValue should not fire the observer. | 169 // Calling SetValue should not fire the observer. |
169 test_obj.str_.SetValue(L"hello"); | 170 test_obj.str_.SetValue(L"hello"); |
170 EXPECT_EQ(0, test_obj.observe_cnt_); | 171 EXPECT_EQ(0, test_obj.observe_cnt_); |
171 EXPECT_EQ(L"hello", prefs.GetString(kStringPref)); | 172 EXPECT_EQ(L"hello", prefs.GetString(kStringPref)); |
172 | 173 |
173 // Changing the pref does fire the observer. | 174 // Changing the pref does fire the observer. |
174 prefs.SetString(kStringPref, L"world"); | 175 prefs.SetString(kStringPref, L"world"); |
175 EXPECT_EQ(1, test_obj.observe_cnt_); | 176 EXPECT_EQ(1, test_obj.observe_cnt_); |
176 EXPECT_EQ(L"world", *(test_obj.str_)); | 177 EXPECT_EQ(L"world", *(test_obj.str_)); |
177 | 178 |
178 // Not changing the value should not fire the observer. | 179 // Not changing the value should not fire the observer. |
179 prefs.SetString(kStringPref, L"world"); | 180 prefs.SetString(kStringPref, L"world"); |
180 EXPECT_EQ(1, test_obj.observe_cnt_); | 181 EXPECT_EQ(1, test_obj.observe_cnt_); |
181 EXPECT_EQ(L"world", *(test_obj.str_)); | 182 EXPECT_EQ(L"world", *(test_obj.str_)); |
182 | 183 |
183 prefs.SetString(kStringPref, L"hello"); | 184 prefs.SetString(kStringPref, L"hello"); |
184 EXPECT_EQ(2, test_obj.observe_cnt_); | 185 EXPECT_EQ(2, test_obj.observe_cnt_); |
185 EXPECT_EQ(L"hello", prefs.GetString(kStringPref)); | 186 EXPECT_EQ(L"hello", prefs.GetString(kStringPref)); |
186 } | 187 } |
187 | 188 |
188 TEST(PrefMemberTest, NoInit) { | 189 TEST(PrefMemberTest, NoInit) { |
189 // Make sure not calling Init on a PrefMember doesn't cause problems. | 190 // Make sure not calling Init on a PrefMember doesn't cause problems. |
190 IntegerPrefMember pref; | 191 IntegerPrefMember pref; |
191 } | 192 } |
OLD | NEW |