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
3 // found in the LICENSE file.
5 #include "extensions/common/features/base_feature_provider.h"
10 #include "extensions/common/extension_builder.h"
11 #include "extensions/common/features/feature.h"
12 #include "extensions/common/features/simple_feature.h"
13 #include "extensions/common/manifest.h"
14 #include "extensions/common/value_builder.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace extensions
{
19 // Tests that a real manifest feature is available for the correct types of
20 // extensions and apps.
21 TEST(BaseFeatureProviderTest
, ManifestFeatureTypes
) {
22 const FeatureProvider
* provider
= BaseFeatureProvider::GetByName("manifest");
23 // NOTE: This feature cannot have multiple rules, otherwise it is not a
25 SimpleFeature
* feature
=
26 static_cast<SimpleFeature
*>(provider
->GetFeature("description"));
28 std::set
<Manifest::Type
>* extension_types
= feature
->extension_types();
29 EXPECT_EQ(6u, extension_types
->size());
30 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_EXTENSION
));
31 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_LEGACY_PACKAGED_APP
));
32 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_PLATFORM_APP
));
33 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_HOSTED_APP
));
34 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_THEME
));
35 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_SHARED_MODULE
));
38 // Tests that real manifest features have the correct availability for an
40 TEST(BaseFeatureProviderTest
, ManifestFeatureAvailability
) {
41 const FeatureProvider
* provider
= BaseFeatureProvider::GetByName("manifest");
43 scoped_refptr
<const Extension
> extension
=
45 .SetManifest(DictionaryBuilder()
46 .Set("name", "test extension")
48 .Set("description", "hello there"))
50 ASSERT_TRUE(extension
.get());
52 Feature
* feature
= provider
->GetFeature("description");
53 EXPECT_EQ(Feature::IS_AVAILABLE
,
54 feature
->IsAvailableToContext(extension
.get(),
55 Feature::UNSPECIFIED_CONTEXT
,
58 // This is a generic extension, so an app-only feature isn't allowed.
59 feature
= provider
->GetFeature("app.background");
61 EXPECT_EQ(Feature::INVALID_TYPE
,
62 feature
->IsAvailableToContext(extension
.get(),
63 Feature::UNSPECIFIED_CONTEXT
,
66 // A feature not listed in the manifest isn't allowed.
67 feature
= provider
->GetFeature("background");
69 EXPECT_EQ(Feature::NOT_PRESENT
,
70 feature
->IsAvailableToContext(extension
.get(),
71 Feature::UNSPECIFIED_CONTEXT
,
75 // Tests that a real permission feature is available for the correct types of
76 // extensions and apps.
77 TEST(BaseFeatureProviderTest
, PermissionFeatureTypes
) {
78 const FeatureProvider
* provider
=
79 BaseFeatureProvider::GetByName("permission");
80 // NOTE: This feature cannot have multiple rules, otherwise it is not a
82 SimpleFeature
* feature
=
83 static_cast<SimpleFeature
*>(provider
->GetFeature("power"));
85 std::set
<Manifest::Type
>* extension_types
= feature
->extension_types();
86 EXPECT_EQ(3u, extension_types
->size());
87 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_EXTENSION
));
88 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_LEGACY_PACKAGED_APP
));
89 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_PLATFORM_APP
));
92 // Tests that real permission features have the correct availability for an app.
93 TEST(BaseFeatureProviderTest
, PermissionFeatureAvailability
) {
94 const FeatureProvider
* provider
=
95 BaseFeatureProvider::GetByName("permission");
97 scoped_refptr
<const Extension
> app
=
99 .SetManifest(DictionaryBuilder()
100 .Set("name", "test app")
103 DictionaryBuilder().Set(
105 DictionaryBuilder().Set(
107 ListBuilder().Append("background.js"))))
108 .Set("permissions", ListBuilder().Append("power")))
110 ASSERT_TRUE(app
.get());
111 ASSERT_TRUE(app
->is_platform_app());
113 // A permission requested in the manifest is available.
114 Feature
* feature
= provider
->GetFeature("power");
116 Feature::IS_AVAILABLE
,
117 feature
->IsAvailableToContext(
118 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
120 // A permission only available to whitelisted extensions returns availability
121 // NOT_FOUND_IN_WHITELIST.
122 feature
= provider
->GetFeature("bluetoothPrivate");
123 ASSERT_TRUE(feature
);
125 Feature::NOT_FOUND_IN_WHITELIST
,
126 feature
->IsAvailableToContext(
127 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
129 // A permission that isn't part of the manifest returns NOT_PRESENT.
130 feature
= provider
->GetFeature("serial");
131 ASSERT_TRUE(feature
);
133 Feature::NOT_PRESENT
,
134 feature
->IsAvailableToContext(
135 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
138 } // namespace extensions