| OLD | NEW |
| 1 // Copyright (c) 2009 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "app/test/data/resource.h" | 7 #include "app/test/data/resource.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/dummy_pref_store.h" | 10 #include "chrome/browser/dummy_pref_store.h" |
| 11 #include "chrome/browser/pref_service.h" | |
| 12 #include "chrome/browser/pref_value_store.h" | 11 #include "chrome/browser/pref_value_store.h" |
| 13 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/notification_observer_mock.h" | 13 #include "chrome/common/notification_observer_mock.h" |
| 15 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/notification_type.h" | 15 #include "chrome/common/notification_type.h" |
| 17 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/testing_pref_service.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using testing::_; | 21 using testing::_; |
| 22 using testing::Mock; | 22 using testing::Mock; |
| 23 using testing::Pointee; | 23 using testing::Pointee; |
| 24 using testing::Property; | 24 using testing::Property; |
| 25 | 25 |
| 26 class TestPrefObserver : public NotificationObserver { | 26 class TestPrefObserver : public NotificationObserver { |
| 27 public: | 27 public: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 57 private: | 57 private: |
| 58 bool observer_fired_; | 58 bool observer_fired_; |
| 59 const PrefService* prefs_; | 59 const PrefService* prefs_; |
| 60 const std::wstring pref_name_; | 60 const std::wstring pref_name_; |
| 61 std::string new_pref_value_; | 61 std::string new_pref_value_; |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 // TODO(port): port this test to POSIX. | 64 // TODO(port): port this test to POSIX. |
| 65 #if defined(OS_WIN) | 65 #if defined(OS_WIN) |
| 66 TEST(PrefServiceTest, LocalizedPrefs) { | 66 TEST(PrefServiceTest, LocalizedPrefs) { |
| 67 PrefService prefs(new PrefValueStore(NULL, NULL, NULL, new DummyPrefStore(), | 67 TestingPrefService prefs; |
| 68 NULL)); | |
| 69 const wchar_t kBoolean[] = L"boolean"; | 68 const wchar_t kBoolean[] = L"boolean"; |
| 70 const wchar_t kInteger[] = L"integer"; | 69 const wchar_t kInteger[] = L"integer"; |
| 71 const wchar_t kString[] = L"string"; | 70 const wchar_t kString[] = L"string"; |
| 72 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); | 71 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); |
| 73 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); | 72 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); |
| 74 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); | 73 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); |
| 75 | 74 |
| 76 // The locale default should take preference over the user default. | 75 // The locale default should take preference over the user default. |
| 77 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); | 76 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); |
| 78 EXPECT_EQ(1, prefs.GetInteger(kInteger)); | 77 EXPECT_EQ(1, prefs.GetInteger(kInteger)); |
| 79 EXPECT_EQ("hello", prefs.GetString(kString)); | 78 EXPECT_EQ("hello", prefs.GetString(kString)); |
| 80 | 79 |
| 81 prefs.SetBoolean(kBoolean, true); | 80 prefs.SetBoolean(kBoolean, true); |
| 82 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); | 81 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); |
| 83 prefs.SetInteger(kInteger, 5); | 82 prefs.SetInteger(kInteger, 5); |
| 84 EXPECT_EQ(5, prefs.GetInteger(kInteger)); | 83 EXPECT_EQ(5, prefs.GetInteger(kInteger)); |
| 85 prefs.SetString(kString, "foo"); | 84 prefs.SetString(kString, "foo"); |
| 86 EXPECT_EQ("foo", prefs.GetString(kString)); | 85 EXPECT_EQ("foo", prefs.GetString(kString)); |
| 87 } | 86 } |
| 88 #endif | 87 #endif |
| 89 | 88 |
| 90 TEST(PrefServiceTest, NoObserverFire) { | 89 TEST(PrefServiceTest, NoObserverFire) { |
| 91 PrefService prefs(new PrefValueStore(NULL, NULL, NULL, new DummyPrefStore(), | 90 TestingPrefService prefs; |
| 92 NULL)); | |
| 93 | 91 |
| 94 const wchar_t pref_name[] = L"homepage"; | 92 const wchar_t pref_name[] = L"homepage"; |
| 95 prefs.RegisterStringPref(pref_name, ""); | 93 prefs.RegisterStringPref(pref_name, ""); |
| 96 | 94 |
| 97 const std::string new_pref_value("http://www.google.com/"); | 95 const std::string new_pref_value("http://www.google.com/"); |
| 98 TestPrefObserver obs(&prefs, pref_name, new_pref_value); | 96 TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
| 99 prefs.AddPrefObserver(pref_name, &obs); | 97 prefs.AddPrefObserver(pref_name, &obs); |
| 100 // This should fire the checks in TestPrefObserver::Observe. | 98 // This should fire the checks in TestPrefObserver::Observe. |
| 101 prefs.SetString(pref_name, new_pref_value); | 99 prefs.SetString(pref_name, new_pref_value); |
| 102 | 100 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 117 // Clearing the pref again should not cause the pref to fire. | 115 // Clearing the pref again should not cause the pref to fire. |
| 118 obs.Reset(""); | 116 obs.Reset(""); |
| 119 prefs.ClearPref(pref_name); | 117 prefs.ClearPref(pref_name); |
| 120 EXPECT_FALSE(obs.observer_fired()); | 118 EXPECT_FALSE(obs.observer_fired()); |
| 121 | 119 |
| 122 // Ok, clean up. | 120 // Ok, clean up. |
| 123 prefs.RemovePrefObserver(pref_name, &obs); | 121 prefs.RemovePrefObserver(pref_name, &obs); |
| 124 } | 122 } |
| 125 | 123 |
| 126 TEST(PrefServiceTest, HasPrefPath) { | 124 TEST(PrefServiceTest, HasPrefPath) { |
| 127 PrefService prefs(new PrefValueStore(NULL, NULL, NULL, new DummyPrefStore(), | 125 TestingPrefService prefs; |
| 128 NULL)); | |
| 129 | 126 |
| 130 const wchar_t path[] = L"fake.path"; | 127 const wchar_t path[] = L"fake.path"; |
| 131 | 128 |
| 132 // Shouldn't initially have a path. | 129 // Shouldn't initially have a path. |
| 133 EXPECT_FALSE(prefs.HasPrefPath(path)); | 130 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 134 | 131 |
| 135 // Register the path. This doesn't set a value, so the path still shouldn't | 132 // Register the path. This doesn't set a value, so the path still shouldn't |
| 136 // exist. | 133 // exist. |
| 137 prefs.RegisterStringPref(path, std::string()); | 134 prefs.RegisterStringPref(path, std::string()); |
| 138 EXPECT_FALSE(prefs.HasPrefPath(path)); | 135 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 139 | 136 |
| 140 // Set a value and make sure we have a path. | 137 // Set a value and make sure we have a path. |
| 141 prefs.SetString(path, "blah"); | 138 prefs.SetString(path, "blah"); |
| 142 EXPECT_TRUE(prefs.HasPrefPath(path)); | 139 EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 143 } | 140 } |
| 144 | 141 |
| 145 TEST(PrefServiceTest, Observers) { | 142 TEST(PrefServiceTest, Observers) { |
| 146 const wchar_t pref_name[] = L"homepage"; | 143 const wchar_t pref_name[] = L"homepage"; |
| 147 | 144 |
| 148 DictionaryValue* dict = new DictionaryValue(); | 145 TestingPrefService prefs; |
| 149 dict->SetString(pref_name, std::string("http://www.cnn.com")); | 146 prefs.SetUserPref(pref_name, Value::CreateStringValue(L"http://www.cnn.com")); |
| 150 DummyPrefStore* pref_store = new DummyPrefStore(); | |
| 151 pref_store->set_prefs(dict); | |
| 152 PrefService prefs(new PrefValueStore(NULL, NULL, NULL, pref_store, NULL)); | |
| 153 prefs.RegisterStringPref(pref_name, ""); | 147 prefs.RegisterStringPref(pref_name, ""); |
| 154 | 148 |
| 155 const std::string new_pref_value("http://www.google.com/"); | 149 const std::string new_pref_value("http://www.google.com/"); |
| 156 TestPrefObserver obs(&prefs, pref_name, new_pref_value); | 150 TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
| 157 prefs.AddPrefObserver(pref_name, &obs); | 151 prefs.AddPrefObserver(pref_name, &obs); |
| 158 // This should fire the checks in TestPrefObserver::Observe. | 152 // This should fire the checks in TestPrefObserver::Observe. |
| 159 prefs.SetString(pref_name, new_pref_value); | 153 prefs.SetString(pref_name, new_pref_value); |
| 160 | 154 |
| 161 // Make sure the tests were actually run. | 155 // Make sure the tests were actually run. |
| 162 EXPECT_TRUE(obs.observer_fired()); | 156 EXPECT_TRUE(obs.observer_fired()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 183 // Ok, clean up. | 177 // Ok, clean up. |
| 184 prefs.RemovePrefObserver(pref_name, &obs2); | 178 prefs.RemovePrefObserver(pref_name, &obs2); |
| 185 } | 179 } |
| 186 | 180 |
| 187 class PrefServiceSetValueTest : public testing::Test { | 181 class PrefServiceSetValueTest : public testing::Test { |
| 188 protected: | 182 protected: |
| 189 static const wchar_t name_[]; | 183 static const wchar_t name_[]; |
| 190 static const char value_[]; | 184 static const char value_[]; |
| 191 | 185 |
| 192 PrefServiceSetValueTest() | 186 PrefServiceSetValueTest() |
| 193 : prefs_(new PrefValueStore(NULL, NULL, NULL, new DummyPrefStore(), | 187 : name_string_(name_), |
| 194 NULL)), | |
| 195 name_string_(name_), | |
| 196 null_value_(Value::CreateNullValue()) {} | 188 null_value_(Value::CreateNullValue()) {} |
| 197 | 189 |
| 198 void SetExpectNoNotification() { | 190 void SetExpectNoNotification() { |
| 199 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); | 191 EXPECT_CALL(observer_, Observe(_, _, _)).Times(0); |
| 200 } | 192 } |
| 201 | 193 |
| 202 void SetExpectPrefChanged() { | 194 void SetExpectPrefChanged() { |
| 203 EXPECT_CALL(observer_, | 195 EXPECT_CALL(observer_, |
| 204 Observe(NotificationType(NotificationType::PREF_CHANGED), _, | 196 Observe(NotificationType(NotificationType::PREF_CHANGED), _, |
| 205 Property(&Details<std::wstring>::ptr, | 197 Property(&Details<std::wstring>::ptr, |
| 206 Pointee(name_string_)))); | 198 Pointee(name_string_)))); |
| 207 } | 199 } |
| 208 | 200 |
| 209 PrefService prefs_; | 201 TestingPrefService prefs_; |
| 210 std::wstring name_string_; | 202 std::wstring name_string_; |
| 211 scoped_ptr<Value> null_value_; | 203 scoped_ptr<Value> null_value_; |
| 212 NotificationObserverMock observer_; | 204 NotificationObserverMock observer_; |
| 213 }; | 205 }; |
| 214 | 206 |
| 215 const wchar_t PrefServiceSetValueTest::name_[] = L"name"; | 207 const wchar_t PrefServiceSetValueTest::name_[] = L"name"; |
| 216 const char PrefServiceSetValueTest::value_[] = "value"; | 208 const char PrefServiceSetValueTest::value_[] = "value"; |
| 217 | 209 |
| 218 TEST_F(PrefServiceSetValueTest, SetStringValue) { | 210 TEST_F(PrefServiceSetValueTest, SetStringValue) { |
| 219 const char default_string[] = "default"; | 211 const char default_string[] = "default"; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 Mock::VerifyAndClearExpectations(&observer_); | 280 Mock::VerifyAndClearExpectations(&observer_); |
| 289 | 281 |
| 290 SetExpectPrefChanged(); | 282 SetExpectPrefChanged(); |
| 291 prefs_.Set(name_, *null_value_); | 283 prefs_.Set(name_, *null_value_); |
| 292 Mock::VerifyAndClearExpectations(&observer_); | 284 Mock::VerifyAndClearExpectations(&observer_); |
| 293 list = prefs_.GetMutableList(name_); | 285 list = prefs_.GetMutableList(name_); |
| 294 EXPECT_EQ(0U, list->GetSize()); | 286 EXPECT_EQ(0U, list->GetSize()); |
| 295 | 287 |
| 296 prefs_.RemovePrefObserver(name_, &observer_); | 288 prefs_.RemovePrefObserver(name_, &observer_); |
| 297 } | 289 } |
| OLD | NEW |