1 // Copyright 2015 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 #include "components/content_settings/core/browser/website_settings_registry.h"
6
7 #include <memory>
8
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "components/content_settings/core/browser/website_settings_info.h"
12 #include "components/content_settings/core/common/content_settings.h"
13 #include "components/content_settings/core/common/content_settings_types.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "components/prefs/pref_registry.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace content_settings {
19
20 class WebsiteSettingsRegistryTest : public testing::Test {
21 protected:
registry()22 WebsiteSettingsRegistry* registry() { return ®istry_; }
23
24 private:
25 WebsiteSettingsRegistry registry_;
26 };
27
TEST_F(WebsiteSettingsRegistryTest,Get)28 TEST_F(WebsiteSettingsRegistryTest, Get) {
29 // ContentSettingsType::AUTO_SELECT_CERTIFICATE should be registered.
30 const WebsiteSettingsInfo* info =
31 registry()->Get(ContentSettingsType::AUTO_SELECT_CERTIFICATE);
32 ASSERT_TRUE(info);
33 EXPECT_EQ(ContentSettingsType::AUTO_SELECT_CERTIFICATE, info->type());
34 EXPECT_EQ("auto-select-certificate", info->name());
35 }
36
TEST_F(WebsiteSettingsRegistryTest,GetByName)37 TEST_F(WebsiteSettingsRegistryTest, GetByName) {
38 // Random string shouldn't be registered.
39 EXPECT_FALSE(registry()->GetByName("abc"));
40
41 // "auto-select-certificate" should be registered.
42 const WebsiteSettingsInfo* info =
43 registry()->GetByName("auto-select-certificate");
44 ASSERT_TRUE(info);
45 EXPECT_EQ(ContentSettingsType::AUTO_SELECT_CERTIFICATE, info->type());
46 EXPECT_EQ("auto-select-certificate", info->name());
47 EXPECT_EQ(registry()->Get(ContentSettingsType::AUTO_SELECT_CERTIFICATE),
48 info);
49
50 // Register a new setting.
51 registry()->Register(static_cast<ContentSettingsType>(10), "test", nullptr,
52 WebsiteSettingsInfo::UNSYNCABLE,
53 WebsiteSettingsInfo::LOSSY,
54 WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE,
55 WebsiteSettingsRegistry::ALL_PLATFORMS,
56 WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
57 info = registry()->GetByName("test");
58 ASSERT_TRUE(info);
59 EXPECT_EQ(static_cast<ContentSettingsType>(10), info->type());
60 EXPECT_EQ("test", info->name());
61 EXPECT_EQ(registry()->Get(static_cast<ContentSettingsType>(10)), info);
62 }
63
TEST_F(WebsiteSettingsRegistryTest,GetPlatformDependent)64 TEST_F(WebsiteSettingsRegistryTest, GetPlatformDependent) {
65 #if defined(OS_IOS)
66 // App banner shouldn't be registered on iOS.
67 EXPECT_FALSE(registry()->Get(ContentSettingsType::APP_BANNER));
68 #else
69 // App banner should be registered on other platforms.
70 EXPECT_TRUE(registry()->Get(ContentSettingsType::APP_BANNER));
71 #endif
72
73 // Auto select certificate is registered on all platforms.
74 EXPECT_TRUE(registry()->Get(ContentSettingsType::AUTO_SELECT_CERTIFICATE));
75 }
76
TEST_F(WebsiteSettingsRegistryTest,Properties)77 TEST_F(WebsiteSettingsRegistryTest, Properties) {
78 // "auto-select-certificate" should be registered.
79 const WebsiteSettingsInfo* info =
80 registry()->Get(ContentSettingsType::AUTO_SELECT_CERTIFICATE);
81 ASSERT_TRUE(info);
82 EXPECT_EQ("profile.content_settings.exceptions.auto_select_certificate",
83 info->pref_name());
84 EXPECT_EQ("profile.default_content_setting_values.auto_select_certificate",
85 info->default_value_pref_name());
86 ASSERT_FALSE(info->initial_default_value());
87 EXPECT_EQ(PrefRegistry::NO_REGISTRATION_FLAGS,
88 info->GetPrefRegistrationFlags());
89
90 // Register a new setting.
91 registry()->Register(
92 static_cast<ContentSettingsType>(10), "test",
93 std::make_unique<base::Value>(999), WebsiteSettingsInfo::SYNCABLE,
94 WebsiteSettingsInfo::LOSSY, WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE,
95 WebsiteSettingsRegistry::ALL_PLATFORMS,
96 WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
97 info = registry()->Get(static_cast<ContentSettingsType>(10));
98 ASSERT_TRUE(info);
99 EXPECT_EQ("profile.content_settings.exceptions.test", info->pref_name());
100 EXPECT_EQ("profile.default_content_setting_values.test",
101 info->default_value_pref_name());
102 int setting;
103 ASSERT_TRUE(info->initial_default_value()->GetAsInteger(&setting));
104 EXPECT_EQ(999, setting);
105 #if defined(OS_ANDROID) || defined(OS_IOS)
106 EXPECT_EQ(PrefRegistry::LOSSY_PREF, info->GetPrefRegistrationFlags());
107 #else
108 EXPECT_EQ(PrefRegistry::LOSSY_PREF |
109 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF,
110 info->GetPrefRegistrationFlags());
111 #endif
112 EXPECT_EQ(WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE,
113 info->scoping_type());
114 EXPECT_EQ(WebsiteSettingsInfo::INHERIT_IN_INCOGNITO,
115 info->incognito_behavior());
116 }
117
TEST_F(WebsiteSettingsRegistryTest,Iteration)118 TEST_F(WebsiteSettingsRegistryTest, Iteration) {
119 registry()->Register(
120 static_cast<ContentSettingsType>(10), "test",
121 std::make_unique<base::Value>(999), WebsiteSettingsInfo::SYNCABLE,
122 WebsiteSettingsInfo::LOSSY, WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE,
123 WebsiteSettingsRegistry::ALL_PLATFORMS,
124 WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
125
126 bool found = false;
127 for (const WebsiteSettingsInfo* info : *registry()) {
128 EXPECT_EQ(registry()->Get(info->type()), info);
129 if (info->type() == static_cast<ContentSettingsType>(10)) {
130 EXPECT_FALSE(found);
131 found = true;
132 }
133 }
134
135 EXPECT_TRUE(found);
136 }
137
138 } // namespace content_settings
139