OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" | 5 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h" |
6 | 6 |
7 #import "base/mac/scoped_nsobject.h" | |
8 #include "base/values.h" | 7 #include "base/values.h" |
9 #include "components/prefs/pref_registry_simple.h" | 8 #include "components/prefs/pref_registry_simple.h" |
10 #include "components/prefs/testing_pref_service.h" | 9 #include "components/prefs/testing_pref_service.h" |
11 #import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h" | 10 #import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h" |
12 #include "ios/web/public/test/test_web_thread_bundle.h" | 11 #include "ios/web/public/test/test_web_thread_bundle.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
15 | 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 const char kTestSwitchPref[] = "test-pref"; | 21 const char kTestSwitchPref[] = "test-pref"; |
19 | 22 |
20 class PrefBackedBooleanTest : public PlatformTest { | 23 class PrefBackedBooleanTest : public PlatformTest { |
21 public: | 24 public: |
22 void SetUp() override { | 25 void SetUp() override { |
23 pref_service_.registry()->RegisterBooleanPref(kTestSwitchPref, false); | 26 pref_service_.registry()->RegisterBooleanPref(kTestSwitchPref, false); |
24 observable_boolean_.reset([[PrefBackedBoolean alloc] | 27 observable_boolean_ = |
25 initWithPrefService:&pref_service_ | 28 [[PrefBackedBoolean alloc] initWithPrefService:&pref_service_ |
26 prefName:kTestSwitchPref]); | 29 prefName:kTestSwitchPref]; |
27 } | 30 } |
28 | 31 |
29 protected: | 32 protected: |
30 bool GetPref() { return pref_service_.GetBoolean(kTestSwitchPref); } | 33 bool GetPref() { return pref_service_.GetBoolean(kTestSwitchPref); } |
31 | 34 |
32 void SetPref(bool value) { | 35 void SetPref(bool value) { |
33 base::Value* booleanValue = new base::FundamentalValue(value); | 36 base::Value* booleanValue = new base::FundamentalValue(value); |
34 pref_service_.SetUserPref(kTestSwitchPref, booleanValue); | 37 pref_service_.SetUserPref(kTestSwitchPref, booleanValue); |
35 } | 38 } |
36 | 39 |
37 PrefBackedBoolean* GetObservableBoolean() { | 40 PrefBackedBoolean* GetObservableBoolean() { return observable_boolean_; } |
38 return observable_boolean_.get(); | |
39 } | |
40 | 41 |
41 web::TestWebThreadBundle thread_bundle_; | 42 web::TestWebThreadBundle thread_bundle_; |
42 TestingPrefServiceSimple pref_service_; | 43 TestingPrefServiceSimple pref_service_; |
43 base::scoped_nsobject<PrefBackedBoolean> observable_boolean_; | 44 PrefBackedBoolean* observable_boolean_; |
44 }; | 45 }; |
45 | 46 |
46 TEST_F(PrefBackedBooleanTest, ReadFromPrefs) { | 47 TEST_F(PrefBackedBooleanTest, ReadFromPrefs) { |
47 SetPref(false); | 48 SetPref(false); |
48 EXPECT_FALSE(GetObservableBoolean().value); | 49 EXPECT_FALSE(GetObservableBoolean().value); |
49 | 50 |
50 SetPref(true); | 51 SetPref(true); |
51 EXPECT_TRUE(GetObservableBoolean().value); | 52 EXPECT_TRUE(GetObservableBoolean().value); |
52 } | 53 } |
53 | 54 |
54 TEST_F(PrefBackedBooleanTest, WriteToPrefs) { | 55 TEST_F(PrefBackedBooleanTest, WriteToPrefs) { |
55 GetObservableBoolean().value = true; | 56 GetObservableBoolean().value = true; |
56 EXPECT_TRUE(GetPref()); | 57 EXPECT_TRUE(GetPref()); |
57 | 58 |
58 GetObservableBoolean().value = false; | 59 GetObservableBoolean().value = false; |
59 EXPECT_FALSE(GetPref()); | 60 EXPECT_FALSE(GetPref()); |
60 } | 61 } |
61 | 62 |
62 TEST_F(PrefBackedBooleanTest, ObserverUpdates) { | 63 TEST_F(PrefBackedBooleanTest, ObserverUpdates) { |
63 SetPref(false); | 64 SetPref(false); |
64 base::scoped_nsobject<TestBooleanObserver> observer( | 65 TestBooleanObserver* observer = [[TestBooleanObserver alloc] init]; |
65 [[TestBooleanObserver alloc] init]); | |
66 GetObservableBoolean().observer = observer; | 66 GetObservableBoolean().observer = observer; |
67 EXPECT_EQ(0, observer.get().updateCount); | 67 EXPECT_EQ(0, observer.updateCount); |
68 | 68 |
69 SetPref(true); | 69 SetPref(true); |
70 EXPECT_EQ(1, observer.get().updateCount) | 70 EXPECT_EQ(1, observer.updateCount) << "Changing value should update observer"; |
71 << "Changing value should update observer"; | |
72 | 71 |
73 SetPref(true); | 72 SetPref(true); |
74 EXPECT_EQ(1, observer.get().updateCount) | 73 EXPECT_EQ(1, observer.updateCount) |
75 << "Setting the same value should not update observer"; | 74 << "Setting the same value should not update observer"; |
76 } | 75 } |
77 | 76 |
78 } // namespace | 77 } // namespace |
OLD | NEW |