| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_pref_store.h" | 11 #include "chrome/browser/extensions/extension_pref_store.h" |
| 12 #include "chrome/browser/prefs/default_pref_store.h" | |
| 13 #include "chrome/browser/prefs/pref_service.h" | |
| 14 #include "chrome/browser/prefs/pref_value_store.h" | |
| 15 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/test/testing_pref_service.h" | 13 #include "chrome/common/pref_store_observer_mock.h" |
| 17 #include "chrome/test/testing_pref_value_store.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 16 |
| 17 using testing::_; |
| 18 |
| 21 namespace keys = extension_manifest_keys; | 19 namespace keys = extension_manifest_keys; |
| 22 | 20 |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 class TestExtensionPrefStore : public ExtensionPrefStore { | 23 class TestExtensionPrefStore : public ExtensionPrefStore { |
| 26 public: | 24 public: |
| 27 TestExtensionPrefStore() | 25 TestExtensionPrefStore() |
| 28 : ExtensionPrefStore(NULL, PrefNotifier::EXTENSION_STORE), | 26 : ExtensionPrefStore(NULL), |
| 29 ext1(NULL), | 27 ext1(NULL), |
| 30 ext2(NULL), | 28 ext2(NULL), |
| 31 ext3(NULL), | 29 ext3(NULL) { |
| 32 pref_service_(NULL) { | |
| 33 // Can't use ASSERT_TRUE here because a constructor can't return a value. | 30 // Can't use ASSERT_TRUE here because a constructor can't return a value. |
| 34 if (!temp_dir_.CreateUniqueTempDir()) { | 31 if (!temp_dir_.CreateUniqueTempDir()) { |
| 35 ADD_FAILURE() << "Failed to create temp dir"; | 32 ADD_FAILURE() << "Failed to create temp dir"; |
| 36 return; | 33 return; |
| 37 } | 34 } |
| 38 DictionaryValue simple_dict; | 35 DictionaryValue simple_dict; |
| 39 std::string error; | 36 std::string error; |
| 40 | 37 |
| 41 simple_dict.SetString(keys::kVersion, "1.0.0.0"); | 38 simple_dict.SetString(keys::kVersion, "1.0.0.0"); |
| 42 simple_dict.SetString(keys::kName, "unused"); | 39 simple_dict.SetString(keys::kName, "unused"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 ext1 = ext1_scoped_.get(); | 51 ext1 = ext1_scoped_.get(); |
| 55 ext2 = ext2_scoped_.get(); | 52 ext2 = ext2_scoped_.get(); |
| 56 ext3 = ext3_scoped_.get(); | 53 ext3 = ext3_scoped_.get(); |
| 57 } | 54 } |
| 58 | 55 |
| 59 typedef std::vector<std::string> ExtensionIDs; | 56 typedef std::vector<std::string> ExtensionIDs; |
| 60 void GetExtensionIDList(ExtensionIDs* result) { | 57 void GetExtensionIDList(ExtensionIDs* result) { |
| 61 GetExtensionIDs(result); | 58 GetExtensionIDs(result); |
| 62 } | 59 } |
| 63 | 60 |
| 64 void SetPrefService(PrefService* pref_service) { | |
| 65 pref_service_ = pref_service; | |
| 66 } | |
| 67 | |
| 68 // Overridden from ExtensionPrefStore. | |
| 69 virtual PrefService* GetPrefService() { | |
| 70 return pref_service_; | |
| 71 } | |
| 72 | |
| 73 // Weak references, for convenience. | 61 // Weak references, for convenience. |
| 74 Extension* ext1; | 62 Extension* ext1; |
| 75 Extension* ext2; | 63 Extension* ext2; |
| 76 Extension* ext3; | 64 Extension* ext3; |
| 77 | 65 |
| 78 private: | 66 private: |
| 79 ScopedTempDir temp_dir_; | 67 ScopedTempDir temp_dir_; |
| 80 | 68 |
| 81 scoped_refptr<Extension> ext1_scoped_; | 69 scoped_refptr<Extension> ext1_scoped_; |
| 82 scoped_refptr<Extension> ext2_scoped_; | 70 scoped_refptr<Extension> ext2_scoped_; |
| 83 scoped_refptr<Extension> ext3_scoped_; | 71 scoped_refptr<Extension> ext3_scoped_; |
| 84 | |
| 85 // Weak reference. | |
| 86 PrefService* pref_service_; | |
| 87 }; | |
| 88 | |
| 89 // Mock PrefNotifier that allows the notifications to be tracked. | |
| 90 class MockPrefNotifier : public PrefNotifier { | |
| 91 public: | |
| 92 MockPrefNotifier(PrefService* service, PrefValueStore* value_store) | |
| 93 : PrefNotifier(service, value_store) {} | |
| 94 | |
| 95 virtual ~MockPrefNotifier() {} | |
| 96 | |
| 97 MOCK_METHOD1(FireObservers, void(const char* path)); | |
| 98 }; | |
| 99 | |
| 100 // Mock PrefService that allows the PrefNotifier to be injected. | |
| 101 class MockPrefService : public PrefService { | |
| 102 public: | |
| 103 explicit MockPrefService(PrefValueStore* pref_value_store) | |
| 104 : PrefService(pref_value_store) { | |
| 105 } | |
| 106 | |
| 107 void SetPrefNotifier(MockPrefNotifier* notifier) { | |
| 108 pref_notifier_.reset(notifier); | |
| 109 } | |
| 110 }; | 72 }; |
| 111 | 73 |
| 112 // Use constants to avoid confusing std::map with hard-coded strings. | 74 // Use constants to avoid confusing std::map with hard-coded strings. |
| 113 const char kPref1[] = "path1.subpath"; | 75 const char kPref1[] = "path1.subpath"; |
| 114 const char kPref2[] = "path2"; | 76 const char kPref2[] = "path2"; |
| 115 const char kPref3[] = "path3"; | 77 const char kPref3[] = "path3"; |
| 116 const char kPref4[] = "path4"; | 78 const char kPref4[] = "path4"; |
| 117 | 79 |
| 118 } // namespace | 80 } // namespace |
| 119 | 81 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 EXPECT_EQ("val4", actual); | 292 EXPECT_EQ("val4", actual); |
| 331 EXPECT_TRUE(prefs->GetString(kPref3, &actual)); | 293 EXPECT_TRUE(prefs->GetString(kPref3, &actual)); |
| 332 EXPECT_EQ("val6", actual); | 294 EXPECT_EQ("val6", actual); |
| 333 EXPECT_FALSE(prefs->GetString(kPref4, &actual)); | 295 EXPECT_FALSE(prefs->GetString(kPref4, &actual)); |
| 334 } | 296 } |
| 335 | 297 |
| 336 TEST(ExtensionPrefStoreTest, NotifyWhenNeeded) { | 298 TEST(ExtensionPrefStoreTest, NotifyWhenNeeded) { |
| 337 using testing::Mock; | 299 using testing::Mock; |
| 338 | 300 |
| 339 TestExtensionPrefStore* eps = new TestExtensionPrefStore; | 301 TestExtensionPrefStore* eps = new TestExtensionPrefStore; |
| 340 DefaultPrefStore* dps = new DefaultPrefStore; | |
| 341 ASSERT_TRUE(eps->ext1 != NULL); | 302 ASSERT_TRUE(eps->ext1 != NULL); |
| 342 | 303 |
| 343 // The PrefValueStore takes ownership of the PrefStores; in this case, that's | 304 PrefStoreObserverMock observer; |
| 344 // only an ExtensionPrefStore. Likewise, the PrefService takes ownership of | 305 eps->AddObserver(&observer); |
| 345 // the PrefValueStore and PrefNotifier. | |
| 346 PrefValueStore* value_store = | |
| 347 new TestingPrefValueStore(NULL, NULL, eps, NULL, NULL, NULL, dps); | |
| 348 scoped_ptr<MockPrefService> pref_service(new MockPrefService(value_store)); | |
| 349 MockPrefNotifier* pref_notifier = new MockPrefNotifier(pref_service.get(), | |
| 350 value_store); | |
| 351 pref_service->SetPrefNotifier(pref_notifier); | |
| 352 | 306 |
| 353 eps->SetPrefService(pref_service.get()); | 307 EXPECT_CALL(observer, OnPrefValueChanged(kPref1)).Times(1); |
| 354 pref_service->RegisterStringPref(kPref1, std::string()); | |
| 355 | |
| 356 EXPECT_CALL(*pref_notifier, FireObservers(kPref1)); | |
| 357 eps->InstallExtensionPref(eps->ext1, kPref1, | 308 eps->InstallExtensionPref(eps->ext1, kPref1, |
| 358 Value::CreateStringValue("https://www.chromium.org")); | 309 Value::CreateStringValue("https://www.chromium.org")); |
| 359 Mock::VerifyAndClearExpectations(pref_notifier); | 310 Mock::VerifyAndClearExpectations(&observer); |
| 360 | 311 |
| 361 EXPECT_CALL(*pref_notifier, FireObservers(kPref1)).Times(0); | 312 EXPECT_CALL(observer, OnPrefValueChanged(_)).Times(0); |
| 362 eps->InstallExtensionPref(eps->ext1, kPref1, | 313 eps->InstallExtensionPref(eps->ext1, kPref1, |
| 363 Value::CreateStringValue("https://www.chromium.org")); | 314 Value::CreateStringValue("https://www.chromium.org")); |
| 364 Mock::VerifyAndClearExpectations(pref_notifier); | 315 Mock::VerifyAndClearExpectations(&observer); |
| 365 | 316 |
| 366 EXPECT_CALL(*pref_notifier, FireObservers(kPref1)).Times(2); | 317 EXPECT_CALL(observer, OnPrefValueChanged(kPref1)).Times(1); |
| 367 eps->InstallExtensionPref(eps->ext1, kPref1, | 318 eps->InstallExtensionPref(eps->ext1, kPref1, |
| 368 Value::CreateStringValue("chrome://newtab")); | 319 Value::CreateStringValue("chrome://newtab")); |
| 320 Mock::VerifyAndClearExpectations(&observer); |
| 321 |
| 322 EXPECT_CALL(observer, OnPrefValueChanged(kPref1)).Times(1); |
| 369 eps->UninstallExtension(eps->ext1); | 323 eps->UninstallExtension(eps->ext1); |
| 324 Mock::VerifyAndClearExpectations(&observer); |
| 325 |
| 326 eps->RemoveObserver(&observer); |
| 370 } | 327 } |
| OLD | NEW |