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