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 "chrome/common/extensions/features/chrome_channel_feature_filter.h" | 5 #include "chrome/common/extensions/features/chrome_channel_feature_filter.h" |
6 | 6 |
| 7 #include <memory> |
7 #include <string> | 8 #include <string> |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/common/extensions/features/feature_channel.h" | 13 #include "chrome/common/extensions/features/feature_channel.h" |
14 #include "components/version_info/version_info.h" | 14 #include "components/version_info/version_info.h" |
15 #include "extensions/common/features/base_feature_provider.h" | 15 #include "extensions/common/features/base_feature_provider.h" |
16 #include "extensions/common/features/complex_feature.h" | 16 #include "extensions/common/features/complex_feature.h" |
17 #include "extensions/common/features/permission_feature.h" | 17 #include "extensions/common/features/permission_feature.h" |
18 #include "extensions/common/features/simple_feature.h" | 18 #include "extensions/common/features/simple_feature.h" |
19 #include "extensions/common/value_builder.h" | 19 #include "extensions/common/value_builder.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 namespace { | 23 namespace { |
24 | 24 |
25 template <class FeatureClass> | 25 template <class FeatureClass> |
26 SimpleFeature* CreateFeature() { | 26 SimpleFeature* CreateFeature() { |
27 SimpleFeature* feature = new FeatureClass(); | 27 SimpleFeature* feature = new FeatureClass(); |
28 feature->AddFilter( | 28 feature->AddFilter(std::unique_ptr<SimpleFeatureFilter>( |
29 scoped_ptr<SimpleFeatureFilter>(new ChromeChannelFeatureFilter(feature))); | 29 new ChromeChannelFeatureFilter(feature))); |
30 return feature; | 30 return feature; |
31 } | 31 } |
32 | 32 |
33 Feature::AvailabilityResult IsAvailableInChannel( | 33 Feature::AvailabilityResult IsAvailableInChannel( |
34 const std::string& channel, | 34 const std::string& channel, |
35 version_info::Channel channel_for_testing) { | 35 version_info::Channel channel_for_testing) { |
36 ScopedCurrentChannel current_channel(channel_for_testing); | 36 ScopedCurrentChannel current_channel(channel_for_testing); |
37 | 37 |
38 SimpleFeature feature; | 38 SimpleFeature feature; |
39 feature.AddFilter(scoped_ptr<SimpleFeatureFilter>( | 39 feature.AddFilter(std::unique_ptr<SimpleFeatureFilter>( |
40 new ChromeChannelFeatureFilter(&feature))); | 40 new ChromeChannelFeatureFilter(&feature))); |
41 | 41 |
42 base::DictionaryValue feature_value; | 42 base::DictionaryValue feature_value; |
43 feature_value.SetString("channel", channel); | 43 feature_value.SetString("channel", channel); |
44 feature.Parse(&feature_value); | 44 feature.Parse(&feature_value); |
45 | 45 |
46 return feature.IsAvailableToManifest("random-extension", | 46 return feature.IsAvailableToManifest("random-extension", |
47 Manifest::TYPE_UNKNOWN, | 47 Manifest::TYPE_UNKNOWN, |
48 Manifest::INVALID_LOCATION, | 48 Manifest::INVALID_LOCATION, |
49 -1, | 49 -1, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | 123 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, |
124 IsAvailableInChannel("trunk", version_info::Channel::DEV)); | 124 IsAvailableInChannel("trunk", version_info::Channel::DEV)); |
125 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | 125 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, |
126 IsAvailableInChannel("trunk", version_info::Channel::BETA)); | 126 IsAvailableInChannel("trunk", version_info::Channel::BETA)); |
127 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, | 127 EXPECT_EQ(Feature::UNSUPPORTED_CHANNEL, |
128 IsAvailableInChannel("trunk", version_info::Channel::STABLE)); | 128 IsAvailableInChannel("trunk", version_info::Channel::STABLE)); |
129 } | 129 } |
130 | 130 |
131 // Tests the validation of features with channel entries. | 131 // Tests the validation of features with channel entries. |
132 TEST_F(ChromeChannelFeatureFilterTest, FeatureValidation) { | 132 TEST_F(ChromeChannelFeatureFilterTest, FeatureValidation) { |
133 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 133 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
134 | 134 |
135 base::DictionaryValue* feature1 = new base::DictionaryValue(); | 135 base::DictionaryValue* feature1 = new base::DictionaryValue(); |
136 feature1->SetString("channel", "trunk"); | 136 feature1->SetString("channel", "trunk"); |
137 value->Set("feature1", feature1); | 137 value->Set("feature1", feature1); |
138 | 138 |
139 base::DictionaryValue* feature2 = new base::DictionaryValue(); | 139 base::DictionaryValue* feature2 = new base::DictionaryValue(); |
140 feature2->SetString("channel", "trunk"); | 140 feature2->SetString("channel", "trunk"); |
141 base::ListValue* extension_types = new base::ListValue(); | 141 base::ListValue* extension_types = new base::ListValue(); |
142 extension_types->Append(new base::StringValue("extension")); | 142 extension_types->Append(new base::StringValue("extension")); |
143 feature2->Set("extension_types", extension_types); | 143 feature2->Set("extension_types", extension_types); |
144 base::ListValue* contexts = new base::ListValue(); | 144 base::ListValue* contexts = new base::ListValue(); |
145 contexts->Append(new base::StringValue("blessed_extension")); | 145 contexts->Append(new base::StringValue("blessed_extension")); |
146 feature2->Set("contexts", contexts); | 146 feature2->Set("contexts", contexts); |
147 value->Set("feature2", feature2); | 147 value->Set("feature2", feature2); |
148 | 148 |
149 scoped_ptr<BaseFeatureProvider> provider( | 149 std::unique_ptr<BaseFeatureProvider> provider( |
150 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); | 150 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
151 | 151 |
152 // feature1 won't validate because it lacks an extension type. | 152 // feature1 won't validate because it lacks an extension type. |
153 EXPECT_FALSE(provider->GetFeature("feature1")); | 153 EXPECT_FALSE(provider->GetFeature("feature1")); |
154 | 154 |
155 // If we add one, it works. | 155 // If we add one, it works. |
156 feature1->Set("extension_types", extension_types->DeepCopy()); | 156 feature1->Set("extension_types", extension_types->DeepCopy()); |
157 provider.reset( | 157 provider.reset( |
158 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); | 158 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
159 EXPECT_TRUE(provider->GetFeature("feature1")); | 159 EXPECT_TRUE(provider->GetFeature("feature1")); |
160 | 160 |
161 // Remove the channel, and feature1 won't validate. | 161 // Remove the channel, and feature1 won't validate. |
162 feature1->Remove("channel", NULL); | 162 feature1->Remove("channel", NULL); |
163 provider.reset( | 163 provider.reset( |
164 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); | 164 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
165 EXPECT_FALSE(provider->GetFeature("feature1")); | 165 EXPECT_FALSE(provider->GetFeature("feature1")); |
166 | 166 |
167 // feature2 won't validate because of the presence of "contexts". | 167 // feature2 won't validate because of the presence of "contexts". |
168 EXPECT_FALSE(provider->GetFeature("feature2")); | 168 EXPECT_FALSE(provider->GetFeature("feature2")); |
169 | 169 |
170 // If we remove it, it works. | 170 // If we remove it, it works. |
171 feature2->Remove("contexts", NULL); | 171 feature2->Remove("contexts", NULL); |
172 provider.reset( | 172 provider.reset( |
173 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); | 173 new BaseFeatureProvider(*value, CreateFeature<PermissionFeature>)); |
174 EXPECT_TRUE(provider->GetFeature("feature2")); | 174 EXPECT_TRUE(provider->GetFeature("feature2")); |
175 } | 175 } |
176 | 176 |
177 // Tests simple feature availability across channels. | 177 // Tests simple feature availability across channels. |
178 TEST_F(ChromeChannelFeatureFilterTest, SimpleFeatureAvailability) { | 178 TEST_F(ChromeChannelFeatureFilterTest, SimpleFeatureAvailability) { |
179 scoped_ptr<base::DictionaryValue> rule( | 179 std::unique_ptr<base::DictionaryValue> rule( |
180 DictionaryBuilder() | 180 DictionaryBuilder() |
181 .Set("feature1", | 181 .Set("feature1", |
182 ListBuilder() | 182 ListBuilder() |
183 .Append(DictionaryBuilder() | 183 .Append(DictionaryBuilder() |
184 .Set("channel", "beta") | 184 .Set("channel", "beta") |
185 .Set("extension_types", | 185 .Set("extension_types", |
186 ListBuilder().Append("extension").Build()) | 186 ListBuilder().Append("extension").Build()) |
187 .Build()) | 187 .Build()) |
188 .Append(DictionaryBuilder() | 188 .Append(DictionaryBuilder() |
189 .Set("channel", "beta") | 189 .Set("channel", "beta") |
190 .Set("extension_types", | 190 .Set("extension_types", |
191 ListBuilder() | 191 ListBuilder() |
192 .Append("legacy_packaged_app") | 192 .Append("legacy_packaged_app") |
193 .Build()) | 193 .Build()) |
194 .Build()) | 194 .Build()) |
195 .Build()) | 195 .Build()) |
196 .Build()); | 196 .Build()); |
197 | 197 |
198 scoped_ptr<BaseFeatureProvider> provider( | 198 std::unique_ptr<BaseFeatureProvider> provider( |
199 new BaseFeatureProvider(*rule, CreateFeature<SimpleFeature>)); | 199 new BaseFeatureProvider(*rule, CreateFeature<SimpleFeature>)); |
200 | 200 |
201 Feature* feature = provider->GetFeature("feature1"); | 201 Feature* feature = provider->GetFeature("feature1"); |
202 EXPECT_TRUE(feature); | 202 EXPECT_TRUE(feature); |
203 | 203 |
204 // Make sure both rules are applied correctly. | 204 // Make sure both rules are applied correctly. |
205 { | 205 { |
206 ScopedCurrentChannel current_channel(version_info::Channel::BETA); | 206 ScopedCurrentChannel current_channel(version_info::Channel::BETA); |
207 EXPECT_EQ( | 207 EXPECT_EQ( |
208 Feature::IS_AVAILABLE, | 208 Feature::IS_AVAILABLE, |
(...skipping 20 matching lines...) Expand all Loading... |
229 Feature::IS_AVAILABLE, | 229 Feature::IS_AVAILABLE, |
230 feature->IsAvailableToManifest("2", | 230 feature->IsAvailableToManifest("2", |
231 Manifest::TYPE_LEGACY_PACKAGED_APP, | 231 Manifest::TYPE_LEGACY_PACKAGED_APP, |
232 Manifest::INVALID_LOCATION, | 232 Manifest::INVALID_LOCATION, |
233 Feature::UNSPECIFIED_PLATFORM).result()); | 233 Feature::UNSPECIFIED_PLATFORM).result()); |
234 } | 234 } |
235 } | 235 } |
236 | 236 |
237 // Tests complex feature availability across channels. | 237 // Tests complex feature availability across channels. |
238 TEST_F(ChromeChannelFeatureFilterTest, ComplexFeatureAvailability) { | 238 TEST_F(ChromeChannelFeatureFilterTest, ComplexFeatureAvailability) { |
239 scoped_ptr<ComplexFeature::FeatureList> features( | 239 std::unique_ptr<ComplexFeature::FeatureList> features( |
240 new ComplexFeature::FeatureList()); | 240 new ComplexFeature::FeatureList()); |
241 | 241 |
242 // Rule: "extension", channel trunk. | 242 // Rule: "extension", channel trunk. |
243 scoped_ptr<SimpleFeature> simple_feature(CreateFeature<SimpleFeature>()); | 243 std::unique_ptr<SimpleFeature> simple_feature(CreateFeature<SimpleFeature>()); |
244 scoped_ptr<base::DictionaryValue> rule( | 244 std::unique_ptr<base::DictionaryValue> rule( |
245 DictionaryBuilder() | 245 DictionaryBuilder() |
246 .Set("channel", "trunk") | 246 .Set("channel", "trunk") |
247 .Set("extension_types", ListBuilder().Append("extension").Build()) | 247 .Set("extension_types", ListBuilder().Append("extension").Build()) |
248 .Build()); | 248 .Build()); |
249 simple_feature->Parse(rule.get()); | 249 simple_feature->Parse(rule.get()); |
250 features->push_back(std::move(simple_feature)); | 250 features->push_back(std::move(simple_feature)); |
251 | 251 |
252 // Rule: "legacy_packaged_app", channel stable. | 252 // Rule: "legacy_packaged_app", channel stable. |
253 simple_feature.reset(CreateFeature<SimpleFeature>()); | 253 simple_feature.reset(CreateFeature<SimpleFeature>()); |
254 rule = DictionaryBuilder() | 254 rule = DictionaryBuilder() |
255 .Set("channel", "stable") | 255 .Set("channel", "stable") |
256 .Set("extension_types", | 256 .Set("extension_types", |
257 ListBuilder().Append("legacy_packaged_app").Build()) | 257 ListBuilder().Append("legacy_packaged_app").Build()) |
258 .Build(); | 258 .Build(); |
259 simple_feature->Parse(rule.get()); | 259 simple_feature->Parse(rule.get()); |
260 features->push_back(std::move(simple_feature)); | 260 features->push_back(std::move(simple_feature)); |
261 | 261 |
262 scoped_ptr<ComplexFeature> feature(new ComplexFeature(std::move(features))); | 262 std::unique_ptr<ComplexFeature> feature( |
| 263 new ComplexFeature(std::move(features))); |
263 | 264 |
264 // Test match 1st rule. | 265 // Test match 1st rule. |
265 { | 266 { |
266 ScopedCurrentChannel current_channel(version_info::Channel::UNKNOWN); | 267 ScopedCurrentChannel current_channel(version_info::Channel::UNKNOWN); |
267 EXPECT_EQ( | 268 EXPECT_EQ( |
268 Feature::IS_AVAILABLE, | 269 Feature::IS_AVAILABLE, |
269 feature->IsAvailableToManifest("1", | 270 feature->IsAvailableToManifest("1", |
270 Manifest::TYPE_EXTENSION, | 271 Manifest::TYPE_EXTENSION, |
271 Manifest::INVALID_LOCATION, | 272 Manifest::INVALID_LOCATION, |
272 Feature::UNSPECIFIED_PLATFORM, | 273 Feature::UNSPECIFIED_PLATFORM, |
(...skipping 19 matching lines...) Expand all Loading... |
292 Feature::IS_AVAILABLE, | 293 Feature::IS_AVAILABLE, |
293 feature->IsAvailableToManifest("1", | 294 feature->IsAvailableToManifest("1", |
294 Manifest::TYPE_EXTENSION, | 295 Manifest::TYPE_EXTENSION, |
295 Manifest::INVALID_LOCATION, | 296 Manifest::INVALID_LOCATION, |
296 Feature::UNSPECIFIED_PLATFORM, | 297 Feature::UNSPECIFIED_PLATFORM, |
297 Feature::GetCurrentPlatform()).result()); | 298 Feature::GetCurrentPlatform()).result()); |
298 } | 299 } |
299 } | 300 } |
300 | 301 |
301 } // namespace extensions | 302 } // namespace extensions |
OLD | NEW |