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

Unified Diff: ios/chrome/browser/ui/settings/utils/content_setting_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 side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean_unittest.mm
diff --git a/ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean_unittest.mm b/ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..cf12cdce6ce1d84e31c1d37ad844b8eff7bd95b9
--- /dev/null
+++ b/ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean_unittest.mm
@@ -0,0 +1,123 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean.h"
+
+#include "base/mac/scoped_nsobject.h"
+#include "components/content_settings/core/browser/host_content_settings_map.h"
+#include "components/content_settings/core/common/content_settings.h"
+#include "components/content_settings/core/common/content_settings_types.h"
+#include "components/sync_preferences/testing_pref_service_syncable.h"
+#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
+#include "ios/chrome/browser/content_settings/host_content_settings_map_factory.h"
+#import "ios/chrome/browser/ui/settings/utils/fake_observable_boolean.h"
+#include "ios/web/public/test/test_web_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace {
+
+const ContentSettingsType kTestContentSettingID = CONTENT_SETTINGS_TYPE_POPUPS;
+
+class ContentSettingBackedBooleanTest : public PlatformTest {
+ public:
+ void SetUp() override {
+ TestChromeBrowserState::Builder test_cbs_builder;
+ chrome_browser_state_ = test_cbs_builder.Build();
+ observable_boolean_.reset([[ContentSettingBackedBoolean alloc]
+ initWithHostContentSettingsMap:SettingsMap()
+ settingID:kTestContentSettingID
+ inverted:NO]);
+ }
+
+ protected:
+ bool GetSetting() {
+ ContentSetting setting =
+ SettingsMap()->GetDefaultContentSetting(kTestContentSettingID, NULL);
+ return setting == CONTENT_SETTING_ALLOW;
+ }
+
+ void SetSetting(bool booleanValue) {
+ ContentSetting value =
+ booleanValue ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
+ SettingsMap()->SetDefaultContentSetting(kTestContentSettingID, value);
+ }
+
+ HostContentSettingsMap* SettingsMap() {
+ return ios::HostContentSettingsMapFactory::GetForBrowserState(
+ chrome_browser_state_.get());
+ }
+
+ sync_preferences::TestingPrefServiceSyncable* PrefService() {
+ return chrome_browser_state_->GetTestingPrefService();
+ }
+
+ ContentSettingBackedBoolean* GetObservableBoolean() {
+ return observable_boolean_.get();
+ }
+
+ void SetUpInvertedContentSettingBackedBoolean() {
+ observable_boolean_.reset([[ContentSettingBackedBoolean alloc]
+ initWithHostContentSettingsMap:SettingsMap()
+ settingID:kTestContentSettingID
+ inverted:YES]);
+ }
+
+ web::TestWebThreadBundle thread_bundle_;
+ std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
+ base::scoped_nsobject<ContentSettingBackedBoolean> observable_boolean_;
+};
+
+TEST_F(ContentSettingBackedBooleanTest, ReadFromSettings) {
+ SetSetting(false);
+ EXPECT_FALSE(GetObservableBoolean().value);
+
+ SetSetting(true);
+ EXPECT_TRUE(GetObservableBoolean().value);
+}
+
+TEST_F(ContentSettingBackedBooleanTest, WriteToSettings) {
+ GetObservableBoolean().value = YES;
+ EXPECT_TRUE(GetSetting());
+
+ GetObservableBoolean().value = NO;
+ EXPECT_FALSE(GetSetting());
+}
+
+TEST_F(ContentSettingBackedBooleanTest, InvertedReadFromSettings) {
+ SetUpInvertedContentSettingBackedBoolean();
+ SetSetting(false);
+ EXPECT_TRUE(GetObservableBoolean().value);
+
+ SetSetting(true);
+ EXPECT_FALSE(GetObservableBoolean().value);
+}
+
+TEST_F(ContentSettingBackedBooleanTest, InvertedWriteToSettings) {
+ SetUpInvertedContentSettingBackedBoolean();
+ GetObservableBoolean().value = YES;
+ EXPECT_FALSE(GetSetting());
+
+ GetObservableBoolean().value = NO;
+ EXPECT_TRUE(GetSetting());
+}
+
+TEST_F(ContentSettingBackedBooleanTest, ObserverUpdates) {
+ SetSetting(false);
+ base::scoped_nsobject<TestBooleanObserver> observer(
+ [[TestBooleanObserver alloc] init]);
+ GetObservableBoolean().observer = observer;
+ EXPECT_EQ(0, observer.get().updateCount);
+
+ SetSetting(true);
+ EXPECT_EQ(1, observer.get().updateCount)
+ << "Changing value should update observer";
+
+ SetSetting(true);
+ EXPECT_EQ(2, observer.get().updateCount) << "ContentSettingBackedBoolean "
+ "should update observer even "
+ "when resetting the same value";
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698