Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: ios/chrome/browser/ui/settings/utils/pref_backed_boolean_unittest.mm

Issue 2587023002: Upstream Chrome on iOS source code [8/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
6
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/values.h"
9 #include "components/prefs/pref_registry_simple.h"
10 #include "components/prefs/testing_pref_service.h"
11 #import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h"
12 #include "ios/web/public/test/test_web_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15
16 namespace {
17
18 const char kTestSwitchPref[] = "test-pref";
19
20 class PrefBackedBooleanTest : public PlatformTest {
21 public:
22 void SetUp() override {
23 pref_service_.registry()->RegisterBooleanPref(kTestSwitchPref, false);
24 observable_boolean_.reset([[PrefBackedBoolean alloc]
25 initWithPrefService:&pref_service_
26 prefName:kTestSwitchPref]);
27 }
28
29 protected:
30 bool GetPref() { return pref_service_.GetBoolean(kTestSwitchPref); }
31
32 void SetPref(bool value) {
33 base::Value* booleanValue = new base::FundamentalValue(value);
34 pref_service_.SetUserPref(kTestSwitchPref, booleanValue);
35 }
36
37 PrefBackedBoolean* GetObservableBoolean() {
38 return observable_boolean_.get();
39 }
40
41 web::TestWebThreadBundle thread_bundle_;
42 TestingPrefServiceSimple pref_service_;
43 base::scoped_nsobject<PrefBackedBoolean> observable_boolean_;
44 };
45
46 TEST_F(PrefBackedBooleanTest, ReadFromPrefs) {
47 SetPref(false);
48 EXPECT_FALSE(GetObservableBoolean().value);
49
50 SetPref(true);
51 EXPECT_TRUE(GetObservableBoolean().value);
52 }
53
54 TEST_F(PrefBackedBooleanTest, WriteToPrefs) {
55 GetObservableBoolean().value = true;
56 EXPECT_TRUE(GetPref());
57
58 GetObservableBoolean().value = false;
59 EXPECT_FALSE(GetPref());
60 }
61
62 TEST_F(PrefBackedBooleanTest, ObserverUpdates) {
63 SetPref(false);
64 base::scoped_nsobject<TestBooleanObserver> observer(
65 [[TestBooleanObserver alloc] init]);
66 GetObservableBoolean().observer = observer;
67 EXPECT_EQ(0, observer.get().updateCount);
68
69 SetPref(true);
70 EXPECT_EQ(1, observer.get().updateCount)
71 << "Changing value should update observer";
72
73 SetPref(true);
74 EXPECT_EQ(1, observer.get().updateCount)
75 << "Setting the same value should not update observer";
76 }
77
78 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698