| 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 "chrome/browser/prefs/pref_change_registrar.h" | 5 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 6 #include "chrome/common/notification_details.h" | 6 #include "chrome/common/notification_details.h" |
| 7 #include "chrome/common/notification_observer.h" | 7 #include "chrome/common/notification_observer_mock.h" |
| 8 #include "chrome/common/notification_source.h" | 8 #include "chrome/common/notification_source.h" |
| 9 #include "chrome/common/notification_type.h" | 9 #include "chrome/common/notification_type.h" |
| 10 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/testing_pref_service.h" | 11 #include "chrome/test/testing_pref_service.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using testing::Mock; | 15 using testing::Mock; |
| 16 using testing::Eq; | 16 using testing::Eq; |
| 17 | 17 |
| 18 // A mock provider that allows us to capture pref observer changes. | 18 // A mock provider that allows us to capture pref observer changes. |
| 19 class MockPrefService : public TestingPrefService { | 19 class MockPrefService : public TestingPrefService { |
| 20 public: | 20 public: |
| 21 MockPrefService() {} | 21 MockPrefService() {} |
| 22 virtual ~MockPrefService() {}; | 22 virtual ~MockPrefService() {}; |
| 23 | 23 |
| 24 MOCK_METHOD2(AddPrefObserver, void(const char*, NotificationObserver*)); | 24 MOCK_METHOD2(AddPrefObserver, void(const char*, NotificationObserver*)); |
| 25 MOCK_METHOD2(RemovePrefObserver, void(const char*, NotificationObserver*)); | 25 MOCK_METHOD2(RemovePrefObserver, void(const char*, NotificationObserver*)); |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 // A mock observer used as a pref observer | |
| 29 class MockObserver : public NotificationObserver { | |
| 30 public: | |
| 31 MOCK_METHOD3(Observe, void(NotificationType, const NotificationSource& source, | |
| 32 const NotificationDetails& details)); | |
| 33 }; | |
| 34 | |
| 35 class PrefChangeRegistrarTest : public testing::Test { | 28 class PrefChangeRegistrarTest : public testing::Test { |
| 36 public: | 29 public: |
| 37 PrefChangeRegistrarTest() {} | 30 PrefChangeRegistrarTest() {} |
| 38 virtual ~PrefChangeRegistrarTest() {} | 31 virtual ~PrefChangeRegistrarTest() {} |
| 39 | 32 |
| 40 protected: | 33 protected: |
| 41 virtual void SetUp(); | 34 virtual void SetUp(); |
| 42 | 35 |
| 43 NotificationObserver* observer() const { return observer_.get(); } | 36 NotificationObserver* observer() const { return observer_.get(); } |
| 44 MockPrefService* service() const { return service_.get(); } | 37 MockPrefService* service() const { return service_.get(); } |
| 45 | 38 |
| 46 private: | 39 private: |
| 47 scoped_ptr<MockPrefService> service_; | 40 scoped_ptr<MockPrefService> service_; |
| 48 scoped_ptr<MockObserver> observer_; | 41 scoped_ptr<NotificationObserverMock> observer_; |
| 49 }; | 42 }; |
| 50 | 43 |
| 51 void PrefChangeRegistrarTest::SetUp() { | 44 void PrefChangeRegistrarTest::SetUp() { |
| 52 service_.reset(new MockPrefService()); | 45 service_.reset(new MockPrefService()); |
| 53 observer_.reset(new MockObserver()); | 46 observer_.reset(new NotificationObserverMock()); |
| 54 } | 47 } |
| 55 | 48 |
| 56 TEST_F(PrefChangeRegistrarTest, AddAndRemove) { | 49 TEST_F(PrefChangeRegistrarTest, AddAndRemove) { |
| 57 PrefChangeRegistrar registrar; | 50 PrefChangeRegistrar registrar; |
| 58 registrar.Init(service()); | 51 registrar.Init(service()); |
| 59 | 52 |
| 60 // Test adding. | 53 // Test adding. |
| 61 EXPECT_CALL(*service(), | 54 EXPECT_CALL(*service(), |
| 62 AddPrefObserver(Eq(std::string("test.pref.1")), observer())); | 55 AddPrefObserver(Eq(std::string("test.pref.1")), observer())); |
| 63 EXPECT_CALL(*service(), | 56 EXPECT_CALL(*service(), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); | 106 RemovePrefObserver(Eq(std::string("test.pref.1")), observer())); |
| 114 EXPECT_CALL(*service(), | 107 EXPECT_CALL(*service(), |
| 115 RemovePrefObserver(Eq(std::string("test.pref.2")), observer())); | 108 RemovePrefObserver(Eq(std::string("test.pref.2")), observer())); |
| 116 registrar.RemoveAll(); | 109 registrar.RemoveAll(); |
| 117 EXPECT_TRUE(registrar.IsEmpty()); | 110 EXPECT_TRUE(registrar.IsEmpty()); |
| 118 | 111 |
| 119 // Explicitly check the expectations now to make sure that the RemoveAll | 112 // Explicitly check the expectations now to make sure that the RemoveAll |
| 120 // worked (rather than the registrar destructor doing the work). | 113 // worked (rather than the registrar destructor doing the work). |
| 121 Mock::VerifyAndClearExpectations(service()); | 114 Mock::VerifyAndClearExpectations(service()); |
| 122 } | 115 } |
| OLD | NEW |