[Android] Make adb_install_apk use the device blacklist.
[chromium-blink-merge.git] / base / prefs / pref_change_registrar_unittest.cc
blobda425cfad287b231215949c68553fef231393dd8
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.
5 #include "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/prefs/pref_change_registrar.h"
8 #include "base/prefs/pref_observer.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using testing::Mock;
15 using testing::Eq;
17 namespace base {
18 namespace {
20 const char kHomePage[] = "homepage";
21 const char kHomePageIsNewTabPage[] = "homepage_is_newtabpage";
22 const char kApplicationLocale[] = "intl.app_locale";
24 // A mock provider that allows us to capture pref observer changes.
25 class MockPrefService : public TestingPrefServiceSimple {
26 public:
27 MockPrefService() {}
28 virtual ~MockPrefService() {}
30 MOCK_METHOD2(AddPrefObserver, void(const std::string&, PrefObserver*));
31 MOCK_METHOD2(RemovePrefObserver, void(const std::string&, PrefObserver*));
34 } // namespace
36 class PrefChangeRegistrarTest : public testing::Test {
37 public:
38 PrefChangeRegistrarTest() {}
39 ~PrefChangeRegistrarTest() override {}
41 protected:
42 void SetUp() override;
44 base::Closure observer() const {
45 return base::Bind(&base::DoNothing);
48 MockPrefService* service() const { return service_.get(); }
50 private:
51 scoped_ptr<MockPrefService> service_;
54 void PrefChangeRegistrarTest::SetUp() {
55 service_.reset(new MockPrefService());
58 TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
59 PrefChangeRegistrar registrar;
60 registrar.Init(service());
62 // Test adding.
63 EXPECT_CALL(*service(),
64 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
65 EXPECT_CALL(*service(),
66 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
67 registrar.Add("test.pref.1", observer());
68 registrar.Add("test.pref.2", observer());
69 EXPECT_FALSE(registrar.IsEmpty());
71 // Test removing.
72 Mock::VerifyAndClearExpectations(service());
73 EXPECT_CALL(*service(),
74 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
75 EXPECT_CALL(*service(),
76 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
77 registrar.Remove("test.pref.1");
78 registrar.Remove("test.pref.2");
79 EXPECT_TRUE(registrar.IsEmpty());
81 // Explicitly check the expectations now to make sure that the Removes
82 // worked (rather than the registrar destructor doing the work).
83 Mock::VerifyAndClearExpectations(service());
86 TEST_F(PrefChangeRegistrarTest, AutoRemove) {
87 PrefChangeRegistrar registrar;
88 registrar.Init(service());
90 // Setup of auto-remove.
91 EXPECT_CALL(*service(),
92 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
93 registrar.Add("test.pref.1", observer());
94 Mock::VerifyAndClearExpectations(service());
95 EXPECT_FALSE(registrar.IsEmpty());
97 // Test auto-removing.
98 EXPECT_CALL(*service(),
99 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
102 TEST_F(PrefChangeRegistrarTest, RemoveAll) {
103 PrefChangeRegistrar registrar;
104 registrar.Init(service());
106 EXPECT_CALL(*service(),
107 AddPrefObserver(Eq(std::string("test.pref.1")), &registrar));
108 EXPECT_CALL(*service(),
109 AddPrefObserver(Eq(std::string("test.pref.2")), &registrar));
110 registrar.Add("test.pref.1", observer());
111 registrar.Add("test.pref.2", observer());
112 Mock::VerifyAndClearExpectations(service());
114 EXPECT_CALL(*service(),
115 RemovePrefObserver(Eq(std::string("test.pref.1")), &registrar));
116 EXPECT_CALL(*service(),
117 RemovePrefObserver(Eq(std::string("test.pref.2")), &registrar));
118 registrar.RemoveAll();
119 EXPECT_TRUE(registrar.IsEmpty());
121 // Explicitly check the expectations now to make sure that the RemoveAll
122 // worked (rather than the registrar destructor doing the work).
123 Mock::VerifyAndClearExpectations(service());
126 class ObserveSetOfPreferencesTest : public testing::Test {
127 public:
128 virtual void SetUp() {
129 pref_service_.reset(new TestingPrefServiceSimple);
130 PrefRegistrySimple* registry = pref_service_->registry();
131 registry->RegisterStringPref(kHomePage, "http://google.com");
132 registry->RegisterBooleanPref(kHomePageIsNewTabPage, false);
133 registry->RegisterStringPref(kApplicationLocale, std::string());
136 PrefChangeRegistrar* CreatePrefChangeRegistrar() {
137 PrefChangeRegistrar* pref_set = new PrefChangeRegistrar();
138 base::Closure callback = base::Bind(&base::DoNothing);
139 pref_set->Init(pref_service_.get());
140 pref_set->Add(kHomePage, callback);
141 pref_set->Add(kHomePageIsNewTabPage, callback);
142 return pref_set;
145 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
147 scoped_ptr<TestingPrefServiceSimple> pref_service_;
150 TEST_F(ObserveSetOfPreferencesTest, IsObserved) {
151 scoped_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
152 EXPECT_TRUE(pref_set->IsObserved(kHomePage));
153 EXPECT_TRUE(pref_set->IsObserved(kHomePageIsNewTabPage));
154 EXPECT_FALSE(pref_set->IsObserved(kApplicationLocale));
157 TEST_F(ObserveSetOfPreferencesTest, IsManaged) {
158 scoped_ptr<PrefChangeRegistrar> pref_set(CreatePrefChangeRegistrar());
159 EXPECT_FALSE(pref_set->IsManaged());
160 pref_service_->SetManagedPref(kHomePage,
161 new StringValue("http://crbug.com"));
162 EXPECT_TRUE(pref_set->IsManaged());
163 pref_service_->SetManagedPref(kHomePageIsNewTabPage,
164 new FundamentalValue(true));
165 EXPECT_TRUE(pref_set->IsManaged());
166 pref_service_->RemoveManagedPref(kHomePage);
167 EXPECT_TRUE(pref_set->IsManaged());
168 pref_service_->RemoveManagedPref(kHomePageIsNewTabPage);
169 EXPECT_FALSE(pref_set->IsManaged());
172 TEST_F(ObserveSetOfPreferencesTest, Observe) {
173 using testing::_;
174 using testing::Mock;
176 PrefChangeRegistrar pref_set;
177 PrefChangeRegistrar::NamedChangeCallback callback = base::Bind(
178 &ObserveSetOfPreferencesTest::OnPreferenceChanged,
179 base::Unretained(this));
180 pref_set.Init(pref_service_.get());
181 pref_set.Add(kHomePage, callback);
182 pref_set.Add(kHomePageIsNewTabPage, callback);
184 EXPECT_CALL(*this, OnPreferenceChanged(kHomePage));
185 pref_service_->SetUserPref(kHomePage, new StringValue("http://crbug.com"));
186 Mock::VerifyAndClearExpectations(this);
188 EXPECT_CALL(*this, OnPreferenceChanged(kHomePageIsNewTabPage));
189 pref_service_->SetUserPref(kHomePageIsNewTabPage,
190 new FundamentalValue(true));
191 Mock::VerifyAndClearExpectations(this);
193 EXPECT_CALL(*this, OnPreferenceChanged(_)).Times(0);
194 pref_service_->SetUserPref(kApplicationLocale, new StringValue("en_US.utf8"));
195 Mock::VerifyAndClearExpectations(this);
198 } // namespace base