OLD | NEW |
(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/content_setting_backed_boolean.h" |
| 6 |
| 7 #include "base/mac/scoped_nsobject.h" |
| 8 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 9 #include "components/content_settings/core/common/content_settings.h" |
| 10 #include "components/content_settings/core/common/content_settings_types.h" |
| 11 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 13 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 14 #import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h" |
| 15 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/platform_test.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 const ContentSettingsType kTestContentSettingID = CONTENT_SETTINGS_TYPE_POPUPS; |
| 22 |
| 23 class ContentSettingBackedBooleanTest : public PlatformTest { |
| 24 public: |
| 25 void SetUp() override { |
| 26 TestChromeBrowserState::Builder test_cbs_builder; |
| 27 chrome_browser_state_ = test_cbs_builder.Build(); |
| 28 observable_boolean_.reset([[ContentSettingBackedBoolean alloc] |
| 29 initWithHostContentSettingsMap:SettingsMap() |
| 30 settingID:kTestContentSettingID |
| 31 inverted:NO]); |
| 32 } |
| 33 |
| 34 protected: |
| 35 bool GetSetting() { |
| 36 ContentSetting setting = |
| 37 SettingsMap()->GetDefaultContentSetting(kTestContentSettingID, NULL); |
| 38 return setting == CONTENT_SETTING_ALLOW; |
| 39 } |
| 40 |
| 41 void SetSetting(bool booleanValue) { |
| 42 ContentSetting value = |
| 43 booleanValue ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; |
| 44 SettingsMap()->SetDefaultContentSetting(kTestContentSettingID, value); |
| 45 } |
| 46 |
| 47 HostContentSettingsMap* SettingsMap() { |
| 48 return ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 49 chrome_browser_state_.get()); |
| 50 } |
| 51 |
| 52 sync_preferences::TestingPrefServiceSyncable* PrefService() { |
| 53 return chrome_browser_state_->GetTestingPrefService(); |
| 54 } |
| 55 |
| 56 ContentSettingBackedBoolean* GetObservableBoolean() { |
| 57 return observable_boolean_.get(); |
| 58 } |
| 59 |
| 60 void SetUpInvertedContentSettingBackedBoolean() { |
| 61 observable_boolean_.reset([[ContentSettingBackedBoolean alloc] |
| 62 initWithHostContentSettingsMap:SettingsMap() |
| 63 settingID:kTestContentSettingID |
| 64 inverted:YES]); |
| 65 } |
| 66 |
| 67 web::TestWebThreadBundle thread_bundle_; |
| 68 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 69 base::scoped_nsobject<ContentSettingBackedBoolean> observable_boolean_; |
| 70 }; |
| 71 |
| 72 TEST_F(ContentSettingBackedBooleanTest, ReadFromSettings) { |
| 73 SetSetting(false); |
| 74 EXPECT_FALSE(GetObservableBoolean().value); |
| 75 |
| 76 SetSetting(true); |
| 77 EXPECT_TRUE(GetObservableBoolean().value); |
| 78 } |
| 79 |
| 80 TEST_F(ContentSettingBackedBooleanTest, WriteToSettings) { |
| 81 GetObservableBoolean().value = YES; |
| 82 EXPECT_TRUE(GetSetting()); |
| 83 |
| 84 GetObservableBoolean().value = NO; |
| 85 EXPECT_FALSE(GetSetting()); |
| 86 } |
| 87 |
| 88 TEST_F(ContentSettingBackedBooleanTest, InvertedReadFromSettings) { |
| 89 SetUpInvertedContentSettingBackedBoolean(); |
| 90 SetSetting(false); |
| 91 EXPECT_TRUE(GetObservableBoolean().value); |
| 92 |
| 93 SetSetting(true); |
| 94 EXPECT_FALSE(GetObservableBoolean().value); |
| 95 } |
| 96 |
| 97 TEST_F(ContentSettingBackedBooleanTest, InvertedWriteToSettings) { |
| 98 SetUpInvertedContentSettingBackedBoolean(); |
| 99 GetObservableBoolean().value = YES; |
| 100 EXPECT_FALSE(GetSetting()); |
| 101 |
| 102 GetObservableBoolean().value = NO; |
| 103 EXPECT_TRUE(GetSetting()); |
| 104 } |
| 105 |
| 106 TEST_F(ContentSettingBackedBooleanTest, ObserverUpdates) { |
| 107 SetSetting(false); |
| 108 base::scoped_nsobject<TestBooleanObserver> observer( |
| 109 [[TestBooleanObserver alloc] init]); |
| 110 GetObservableBoolean().observer = observer; |
| 111 EXPECT_EQ(0, observer.get().updateCount); |
| 112 |
| 113 SetSetting(true); |
| 114 EXPECT_EQ(1, observer.get().updateCount) |
| 115 << "Changing value should update observer"; |
| 116 |
| 117 SetSetting(true); |
| 118 EXPECT_EQ(2, observer.get().updateCount) << "ContentSettingBackedBoolean " |
| 119 "should update observer even " |
| 120 "when resetting the same value"; |
| 121 } |
| 122 |
| 123 } // namespace |
OLD | NEW |