OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "extensions/common/features/base_feature_provider.h" | 5 #include "extensions/common/features/base_feature_provider.h" |
6 | 6 |
7 #include "chrome/common/extensions/features/chrome_channel_feature_filter.h" | 7 #include "chrome/common/extensions/features/chrome_channel_feature_filter.h" |
8 #include "chrome/common/extensions/features/feature_channel.h" | 8 #include "chrome/common/extensions/features/feature_channel.h" |
9 #include "extensions/common/features/permission_feature.h" | 9 #include "extensions/common/features/permission_feature.h" |
10 #include "extensions/common/value_builder.h" | 10 #include "extensions/common/value_builder.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 using chrome::VersionInfo; | 13 using chrome::VersionInfo; |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
| 17 namespace { |
| 18 |
| 19 template <class FeatureClass> |
| 20 SimpleFeature* CreateFeature() { |
| 21 SimpleFeature* feature = new FeatureClass(); |
| 22 feature->AddFilter( |
| 23 scoped_ptr<SimpleFeatureFilter>(new ChromeChannelFeatureFilter(feature))); |
| 24 return feature; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
17 TEST(BaseFeatureProviderTest, ManifestFeatures) { | 29 TEST(BaseFeatureProviderTest, ManifestFeatures) { |
18 FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest"); | 30 FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest"); |
19 SimpleFeature* feature = | 31 SimpleFeature* feature = |
20 static_cast<SimpleFeature*>(provider->GetFeature("description")); | 32 static_cast<SimpleFeature*>(provider->GetFeature("description")); |
21 ASSERT_TRUE(feature); | 33 ASSERT_TRUE(feature); |
22 EXPECT_EQ(6u, feature->extension_types()->size()); | 34 EXPECT_EQ(6u, feature->extension_types()->size()); |
23 EXPECT_EQ(1u, feature->extension_types()->count(Manifest::TYPE_EXTENSION)); | 35 EXPECT_EQ(1u, feature->extension_types()->count(Manifest::TYPE_EXTENSION)); |
24 EXPECT_EQ(1u, | 36 EXPECT_EQ(1u, |
25 feature->extension_types()->count(Manifest::TYPE_LEGACY_PACKAGED_APP)); | 37 feature->extension_types()->count(Manifest::TYPE_LEGACY_PACKAGED_APP)); |
26 EXPECT_EQ(1u, | 38 EXPECT_EQ(1u, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature->IsAvailableToContext( | 103 EXPECT_EQ(Feature::NOT_FOUND_IN_WHITELIST, feature->IsAvailableToContext( |
92 extension.get(), Feature::UNSPECIFIED_CONTEXT).result()); | 104 extension.get(), Feature::UNSPECIFIED_CONTEXT).result()); |
93 | 105 |
94 feature = | 106 feature = |
95 static_cast<SimpleFeature*>(provider->GetFeature("clipboardWrite")); | 107 static_cast<SimpleFeature*>(provider->GetFeature("clipboardWrite")); |
96 ASSERT_TRUE(feature); | 108 ASSERT_TRUE(feature); |
97 EXPECT_EQ(Feature::NOT_PRESENT, feature->IsAvailableToContext( | 109 EXPECT_EQ(Feature::NOT_PRESENT, feature->IsAvailableToContext( |
98 extension.get(), Feature::UNSPECIFIED_CONTEXT).result()); | 110 extension.get(), Feature::UNSPECIFIED_CONTEXT).result()); |
99 } | 111 } |
100 | 112 |
101 SimpleFeature* CreatePermissionFeature() { | |
102 SimpleFeature* feature = new PermissionFeature(); | |
103 feature->AddFilter( | |
104 scoped_ptr<SimpleFeatureFilter>(new ChromeChannelFeatureFilter(feature))); | |
105 return feature; | |
106 } | |
107 | |
108 TEST(BaseFeatureProviderTest, Validation) { | 113 TEST(BaseFeatureProviderTest, Validation) { |
109 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 114 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
110 | 115 |
111 base::DictionaryValue* feature1 = new base::DictionaryValue(); | 116 base::DictionaryValue* feature1 = new base::DictionaryValue(); |
112 feature1->SetString("channel", "trunk"); | 117 feature1->SetString("channel", "trunk"); |
113 value->Set("feature1", feature1); | 118 value->Set("feature1", feature1); |
114 | 119 |
115 base::DictionaryValue* feature2 = new base::DictionaryValue(); | 120 base::DictionaryValue* feature2 = new base::DictionaryValue(); |
116 feature2->SetString("channel", "trunk"); | 121 feature2->SetString("channel", "trunk"); |
117 base::ListValue* extension_types = new base::ListValue(); | 122 base::ListValue* extension_types = new base::ListValue(); |
118 extension_types->Append(new base::StringValue("extension")); | 123 extension_types->Append(new base::StringValue("extension")); |
119 feature2->Set("extension_types", extension_types); | 124 feature2->Set("extension_types", extension_types); |
120 base::ListValue* contexts = new base::ListValue(); | 125 base::ListValue* contexts = new base::ListValue(); |
121 contexts->Append(new base::StringValue("blessed_extension")); | 126 contexts->Append(new base::StringValue("blessed_extension")); |
122 feature2->Set("contexts", contexts); | 127 feature2->Set("contexts", contexts); |
123 value->Set("feature2", feature2); | 128 value->Set("feature2", feature2); |
124 | 129 |
125 scoped_ptr<BaseFeatureProvider> provider( | 130 scoped_ptr<BaseFeatureProvider> provider( |
126 new BaseFeatureProvider(*value, CreatePermissionFeature)); | 131 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
127 | 132 |
128 // feature1 won't validate because it lacks an extension type. | 133 // feature1 won't validate because it lacks an extension type. |
129 EXPECT_FALSE(provider->GetFeature("feature1")); | 134 EXPECT_FALSE(provider->GetFeature("feature1")); |
130 | 135 |
131 // If we add one, it works. | 136 // If we add one, it works. |
132 feature1->Set("extension_types", extension_types->DeepCopy()); | 137 feature1->Set("extension_types", extension_types->DeepCopy()); |
133 provider.reset(new BaseFeatureProvider(*value, CreatePermissionFeature)); | 138 provider.reset( |
| 139 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
134 EXPECT_TRUE(provider->GetFeature("feature1")); | 140 EXPECT_TRUE(provider->GetFeature("feature1")); |
135 | 141 |
136 // Remove the channel, and feature1 won't validate. | 142 // Remove the channel, and feature1 won't validate. |
137 feature1->Remove("channel", NULL); | 143 feature1->Remove("channel", NULL); |
138 provider.reset(new BaseFeatureProvider(*value, CreatePermissionFeature)); | 144 provider.reset( |
| 145 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
139 EXPECT_FALSE(provider->GetFeature("feature1")); | 146 EXPECT_FALSE(provider->GetFeature("feature1")); |
140 | 147 |
141 // feature2 won't validate because of the presence of "contexts". | 148 // feature2 won't validate because of the presence of "contexts". |
142 EXPECT_FALSE(provider->GetFeature("feature2")); | 149 EXPECT_FALSE(provider->GetFeature("feature2")); |
143 | 150 |
144 // If we remove it, it works. | 151 // If we remove it, it works. |
145 feature2->Remove("contexts", NULL); | 152 feature2->Remove("contexts", NULL); |
146 provider.reset(new BaseFeatureProvider(*value, CreatePermissionFeature)); | 153 provider.reset( |
| 154 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
147 EXPECT_TRUE(provider->GetFeature("feature2")); | 155 EXPECT_TRUE(provider->GetFeature("feature2")); |
148 } | 156 } |
149 | 157 |
150 TEST(BaseFeatureProviderTest, ComplexFeatures) { | 158 TEST(BaseFeatureProviderTest, ComplexFeatures) { |
151 scoped_ptr<base::DictionaryValue> rule( | 159 scoped_ptr<base::DictionaryValue> rule( |
152 DictionaryBuilder() | 160 DictionaryBuilder() |
153 .Set("feature1", ListBuilder() | 161 .Set("feature1", ListBuilder() |
154 .Append(DictionaryBuilder() | 162 .Append(DictionaryBuilder() |
155 .Set("channel", "beta") | 163 .Set("channel", "beta") |
156 .Set("extension_types", ListBuilder() | 164 .Set("extension_types", ListBuilder() |
157 .Append("extension"))) | 165 .Append("extension"))) |
158 .Append(DictionaryBuilder() | 166 .Append(DictionaryBuilder() |
159 .Set("channel", "beta") | 167 .Set("channel", "beta") |
160 .Set("extension_types", ListBuilder() | 168 .Set("extension_types", ListBuilder() |
161 .Append("legacy_packaged_app")))) | 169 .Append("legacy_packaged_app")))) |
162 .Build()); | 170 .Build()); |
163 | 171 |
164 scoped_ptr<BaseFeatureProvider> provider( | 172 scoped_ptr<BaseFeatureProvider> provider( |
165 new BaseFeatureProvider(*rule, NULL)); | 173 new BaseFeatureProvider(*rule, CreateFeature<SimpleFeature>)); |
166 | 174 |
167 Feature* feature = provider->GetFeature("feature1"); | 175 Feature* feature = provider->GetFeature("feature1"); |
168 EXPECT_TRUE(feature); | 176 EXPECT_TRUE(feature); |
169 | 177 |
170 // Make sure both rules are applied correctly. | 178 // Make sure both rules are applied correctly. |
171 { | 179 { |
172 ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_BETA); | 180 ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_BETA); |
173 EXPECT_EQ( | 181 EXPECT_EQ( |
174 Feature::IS_AVAILABLE, | 182 Feature::IS_AVAILABLE, |
175 feature->IsAvailableToManifest("1", | 183 feature->IsAvailableToManifest("1", |
(...skipping 18 matching lines...) Expand all Loading... |
194 EXPECT_NE( | 202 EXPECT_NE( |
195 Feature::IS_AVAILABLE, | 203 Feature::IS_AVAILABLE, |
196 feature->IsAvailableToManifest("2", | 204 feature->IsAvailableToManifest("2", |
197 Manifest::TYPE_LEGACY_PACKAGED_APP, | 205 Manifest::TYPE_LEGACY_PACKAGED_APP, |
198 Manifest::INVALID_LOCATION, | 206 Manifest::INVALID_LOCATION, |
199 Feature::UNSPECIFIED_PLATFORM).result()); | 207 Feature::UNSPECIFIED_PLATFORM).result()); |
200 } | 208 } |
201 } | 209 } |
202 | 210 |
203 } // namespace extensions | 211 } // namespace extensions |
OLD | NEW |