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 #include <utility> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
7 #include "base/values.h" | 10 #include "base/values.h" |
8 #include "components/prefs/pref_registry_simple.h" | 11 #include "components/prefs/pref_registry_simple.h" |
9 #include "components/prefs/testing_pref_service.h" | 12 #include "components/prefs/testing_pref_service.h" |
10 #import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h" | 13 #import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h" |
11 #include "ios/web/public/test/test_web_thread_bundle.h" | 14 #include "ios/web/public/test/test_web_thread_bundle.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
14 | 17 |
15 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
16 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
17 #endif | 20 #endif |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 const char kTestSwitchPref[] = "test-pref"; | 24 const char kTestSwitchPref[] = "test-pref"; |
22 | 25 |
23 class PrefBackedBooleanTest : public PlatformTest { | 26 class PrefBackedBooleanTest : public PlatformTest { |
24 public: | 27 public: |
25 void SetUp() override { | 28 void SetUp() override { |
26 pref_service_.registry()->RegisterBooleanPref(kTestSwitchPref, false); | 29 pref_service_.registry()->RegisterBooleanPref(kTestSwitchPref, false); |
27 observable_boolean_ = | 30 observable_boolean_ = |
28 [[PrefBackedBoolean alloc] initWithPrefService:&pref_service_ | 31 [[PrefBackedBoolean alloc] initWithPrefService:&pref_service_ |
29 prefName:kTestSwitchPref]; | 32 prefName:kTestSwitchPref]; |
30 } | 33 } |
31 | 34 |
32 protected: | 35 protected: |
33 bool GetPref() { return pref_service_.GetBoolean(kTestSwitchPref); } | 36 bool GetPref() { return pref_service_.GetBoolean(kTestSwitchPref); } |
34 | 37 |
35 void SetPref(bool value) { | 38 void SetPref(bool value) { |
36 base::Value* booleanValue = new base::Value(value); | 39 auto booleanValue = base::MakeUnique<base::Value>(value); |
37 pref_service_.SetUserPref(kTestSwitchPref, booleanValue); | 40 pref_service_.SetUserPref(kTestSwitchPref, std::move(booleanValue)); |
38 } | 41 } |
39 | 42 |
40 PrefBackedBoolean* GetObservableBoolean() { return observable_boolean_; } | 43 PrefBackedBoolean* GetObservableBoolean() { return observable_boolean_; } |
41 | 44 |
42 web::TestWebThreadBundle thread_bundle_; | 45 web::TestWebThreadBundle thread_bundle_; |
43 TestingPrefServiceSimple pref_service_; | 46 TestingPrefServiceSimple pref_service_; |
44 PrefBackedBoolean* observable_boolean_; | 47 PrefBackedBoolean* observable_boolean_; |
45 }; | 48 }; |
46 | 49 |
47 TEST_F(PrefBackedBooleanTest, ReadFromPrefs) { | 50 TEST_F(PrefBackedBooleanTest, ReadFromPrefs) { |
(...skipping 20 matching lines...) Expand all Loading... |
68 | 71 |
69 SetPref(true); | 72 SetPref(true); |
70 EXPECT_EQ(1, observer.updateCount) << "Changing value should update observer"; | 73 EXPECT_EQ(1, observer.updateCount) << "Changing value should update observer"; |
71 | 74 |
72 SetPref(true); | 75 SetPref(true); |
73 EXPECT_EQ(1, observer.updateCount) | 76 EXPECT_EQ(1, observer.updateCount) |
74 << "Setting the same value should not update observer"; | 77 << "Setting the same value should not update observer"; |
75 } | 78 } |
76 | 79 |
77 } // namespace | 80 } // namespace |
OLD | NEW |