| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/prefs/pref_change_registrar.h" | |
| 6 #include "chrome/test/base/testing_pref_service.h" | |
| 7 #include "content/public/browser/notification_details.h" | |
| 8 #include "content/public/browser/notification_source.h" | |
| 9 #include "content/public/browser/notification_types.h" | |
| 10 #include "content/public/test/mock_notification_observer.h" | |
| 11 #include "testing/gmock/include/gmock/gmock.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using testing::Mock; | |
| 15 using testing::Eq; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // A mock provider that allows us to capture pref observer changes. | |
| 20 class MockPrefService : public TestingPrefService { | |
| 21 public: | |
| 22 MockPrefService() {} | |
| 23 virtual ~MockPrefService() {} | |
| 24 | |
| 25 MOCK_METHOD2(AddPrefObserver, | |
| 26 void(const char*, content::NotificationObserver*)); | |
| 27 MOCK_METHOD2(RemovePrefObserver, | |
| 28 void(const char*, content::NotificationObserver*)); | |
| 29 }; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 class PrefChangeRegistrarTest : public testing::Test { | |
| 34 public: | |
| 35 PrefChangeRegistrarTest() {} | |
| 36 virtual ~PrefChangeRegistrarTest() {} | |
| 37 | |
| 38 protected: | |
| 39 virtual void SetUp(); | |
| 40 | |
| 41 content::NotificationObserver* observer() const { return observer_.get(); } | |
| 42 MockPrefService* service() const { return service_.get(); } | |
| 43 | |
| 44 private: | |
| 45 scoped_ptr<MockPrefService> service_; | |
| 46 scoped_ptr<content::MockNotificationObserver> observer_; | |
| 47 }; | |
| 48 | |
| 49 void PrefChangeRegistrarTest::SetUp() { | |
| 50 service_.reset(new MockPrefService()); | |
| 51 observer_.reset(new content::MockNotificationObserver()); | |
| 52 } | |
| 53 | |
| 54 TEST_F(PrefChangeRegistrarTest, AddAndRemove) { | |
| 55 PrefChangeRegistrar registrar; | |
| 56 registrar.Init(service()); | |
| 57 | |
| 58 // Test adding. | |
| 59 EXPECT_CALL(*service(), | |
| 60 AddPrefObserver(Eq(std::string("test.pref.1")), observer())); | |
| 61 EXPECT_CALL(*service(), | |
| 62 AddPrefObserver(Eq(std::string("test.pref.2")), observer())); | |
| 63 registrar.Add("test.pref.1", observer()); | |
| 64 registrar.Add("test.pref.2", observer()); | |
| 65 EXPECT_FALSE(registrar.IsEmpty()); | |
| 66 | |
| 67 // Test removing. | |
| 68 Mock::VerifyAndClearExpectations(service()); | |
| 69 EXPECT_CALL(*service(), | |
| 70 RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); | |
| 71 EXPECT_CALL(*service(), | |
| 72 RemovePrefObserver(Eq(std::string("test.pref.2")), observer())); | |
| 73 registrar.Remove("test.pref.1", observer()); | |
| 74 registrar.Remove("test.pref.2", observer()); | |
| 75 EXPECT_TRUE(registrar.IsEmpty()); | |
| 76 | |
| 77 // Explicitly check the expectations now to make sure that the Removes | |
| 78 // worked (rather than the registrar destructor doing the work). | |
| 79 Mock::VerifyAndClearExpectations(service()); | |
| 80 } | |
| 81 | |
| 82 TEST_F(PrefChangeRegistrarTest, AutoRemove) { | |
| 83 PrefChangeRegistrar registrar; | |
| 84 registrar.Init(service()); | |
| 85 | |
| 86 // Setup of auto-remove. | |
| 87 EXPECT_CALL(*service(), | |
| 88 AddPrefObserver(Eq(std::string("test.pref.1")), observer())); | |
| 89 registrar.Add("test.pref.1", observer()); | |
| 90 Mock::VerifyAndClearExpectations(service()); | |
| 91 EXPECT_FALSE(registrar.IsEmpty()); | |
| 92 | |
| 93 // Test auto-removing. | |
| 94 EXPECT_CALL(*service(), | |
| 95 RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); | |
| 96 } | |
| 97 | |
| 98 TEST_F(PrefChangeRegistrarTest, RemoveAll) { | |
| 99 PrefChangeRegistrar registrar; | |
| 100 registrar.Init(service()); | |
| 101 | |
| 102 EXPECT_CALL(*service(), | |
| 103 AddPrefObserver(Eq(std::string("test.pref.1")), observer())); | |
| 104 EXPECT_CALL(*service(), | |
| 105 AddPrefObserver(Eq(std::string("test.pref.2")), observer())); | |
| 106 registrar.Add("test.pref.1", observer()); | |
| 107 registrar.Add("test.pref.2", observer()); | |
| 108 Mock::VerifyAndClearExpectations(service()); | |
| 109 | |
| 110 EXPECT_CALL(*service(), | |
| 111 RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); | |
| 112 EXPECT_CALL(*service(), | |
| 113 RemovePrefObserver(Eq(std::string("test.pref.2")), observer())); | |
| 114 registrar.RemoveAll(); | |
| 115 EXPECT_TRUE(registrar.IsEmpty()); | |
| 116 | |
| 117 // Explicitly check the expectations now to make sure that the RemoveAll | |
| 118 // worked (rather than the registrar destructor doing the work). | |
| 119 Mock::VerifyAndClearExpectations(service()); | |
| 120 } | |
| OLD | NEW |