Home | History | Annotate | Download | only in prefs
      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/testing_pref_service.h"
      7 #include "content/common/notification_details.h"
      8 #include "content/common/notification_observer_mock.h"
      9 #include "content/common/notification_source.h"
     10 #include "content/common/notification_type.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, void(const char*, NotificationObserver*));
     26   MOCK_METHOD2(RemovePrefObserver, void(const char*, NotificationObserver*));
     27 };
     28 
     29 }  // namespace
     30 
     31 class PrefChangeRegistrarTest : public testing::Test {
     32  public:
     33   PrefChangeRegistrarTest() {}
     34   virtual ~PrefChangeRegistrarTest() {}
     35 
     36  protected:
     37   virtual void SetUp();
     38 
     39   NotificationObserver* observer() const { return observer_.get(); }
     40   MockPrefService* service() const { return service_.get(); }
     41 
     42  private:
     43   scoped_ptr<MockPrefService> service_;
     44   scoped_ptr<NotificationObserverMock> observer_;
     45 };
     46 
     47 void PrefChangeRegistrarTest::SetUp() {
     48   service_.reset(new MockPrefService());
     49   observer_.reset(new NotificationObserverMock());
     50 }
     51 
     52 TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
     53   PrefChangeRegistrar registrar;
     54   registrar.Init(service());
     55 
     56   // Test adding.
     57   EXPECT_CALL(*service(),
     58               AddPrefObserver(Eq(std::string("test.pref.1")), observer()));
     59   EXPECT_CALL(*service(),
     60               AddPrefObserver(Eq(std::string("test.pref.2")), observer()));
     61   registrar.Add("test.pref.1", observer());
     62   registrar.Add("test.pref.2", observer());
     63   EXPECT_FALSE(registrar.IsEmpty());
     64 
     65   // Test removing.
     66   Mock::VerifyAndClearExpectations(service());
     67   EXPECT_CALL(*service(),
     68               RemovePrefObserver(Eq(std::string("test.pref.1")), observer()));
     69   EXPECT_CALL(*service(),
     70               RemovePrefObserver(Eq(std::string("test.pref.2")), observer()));
     71   registrar.Remove("test.pref.1", observer());
     72   registrar.Remove("test.pref.2", observer());
     73   EXPECT_TRUE(registrar.IsEmpty());
     74 
     75   // Explicitly check the expectations now to make sure that the Removes
     76   // worked (rather than the registrar destructor doing the work).
     77   Mock::VerifyAndClearExpectations(service());
     78 }
     79 
     80 TEST_F(PrefChangeRegistrarTest, AutoRemove) {
     81   PrefChangeRegistrar registrar;
     82   registrar.Init(service());
     83 
     84   // Setup of auto-remove.
     85   EXPECT_CALL(*service(),
     86               AddPrefObserver(Eq(std::string("test.pref.1")), observer()));
     87   registrar.Add("test.pref.1", observer());
     88   Mock::VerifyAndClearExpectations(service());
     89   EXPECT_FALSE(registrar.IsEmpty());
     90 
     91   // Test auto-removing.
     92   EXPECT_CALL(*service(),
     93               RemovePrefObserver(Eq(std::string("test.pref.1")), observer()));
     94 }
     95 
     96 TEST_F(PrefChangeRegistrarTest, RemoveAll) {
     97   PrefChangeRegistrar registrar;
     98   registrar.Init(service());
     99 
    100   EXPECT_CALL(*service(),
    101               AddPrefObserver(Eq(std::string("test.pref.1")), observer()));
    102   EXPECT_CALL(*service(),
    103               AddPrefObserver(Eq(std::string("test.pref.2")), observer()));
    104   registrar.Add("test.pref.1", observer());
    105   registrar.Add("test.pref.2", observer());
    106   Mock::VerifyAndClearExpectations(service());
    107 
    108   EXPECT_CALL(*service(),
    109               RemovePrefObserver(Eq(std::string("test.pref.1")), observer()));
    110   EXPECT_CALL(*service(),
    111               RemovePrefObserver(Eq(std::string("test.pref.2")), observer()));
    112   registrar.RemoveAll();
    113   EXPECT_TRUE(registrar.IsEmpty());
    114 
    115   // Explicitly check the expectations now to make sure that the RemoveAll
    116   // worked (rather than the registrar destructor doing the work).
    117   Mock::VerifyAndClearExpectations(service());
    118 }
    119