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 // NOTE: This feature cannot have multiple rules, otherwise it is not a
24 const SimpleFeature
* feature
= static_cast<const SimpleFeature
*>(
25 FeatureProvider::GetManifestFeature("description"));
27 const std::set
<Manifest::Type
>* extension_types
= feature
->extension_types();
28 EXPECT_EQ(6u, extension_types
->size());
29 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_EXTENSION
));
30 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_LEGACY_PACKAGED_APP
));
31 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_PLATFORM_APP
));
32 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_HOSTED_APP
));
33 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_THEME
));
34 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_SHARED_MODULE
));
37 // Tests that real manifest features have the correct availability for an
39 TEST(BaseFeatureProviderTest
, ManifestFeatureAvailability
) {
40 const FeatureProvider
* provider
= BaseFeatureProvider::GetByName("manifest");
42 scoped_refptr
<const Extension
> extension
=
44 .SetManifest(DictionaryBuilder()
45 .Set("name", "test extension")
47 .Set("description", "hello there"))
49 ASSERT_TRUE(extension
.get());
51 Feature
* feature
= provider
->GetFeature("description");
52 EXPECT_EQ(Feature::IS_AVAILABLE
,
53 feature
->IsAvailableToContext(extension
.get(),
54 Feature::UNSPECIFIED_CONTEXT
,
57 // This is a generic extension, so an app-only feature isn't allowed.
58 feature
= provider
->GetFeature("app.background");
60 EXPECT_EQ(Feature::INVALID_TYPE
,
61 feature
->IsAvailableToContext(extension
.get(),
62 Feature::UNSPECIFIED_CONTEXT
,
65 // A feature not listed in the manifest isn't allowed.
66 feature
= provider
->GetFeature("background");
68 EXPECT_EQ(Feature::NOT_PRESENT
,
69 feature
->IsAvailableToContext(extension
.get(),
70 Feature::UNSPECIFIED_CONTEXT
,
74 // Tests that a real permission feature is available for the correct types of
75 // extensions and apps.
76 TEST(BaseFeatureProviderTest
, PermissionFeatureTypes
) {
77 // NOTE: This feature cannot have multiple rules, otherwise it is not a
79 const SimpleFeature
* feature
= static_cast<const SimpleFeature
*>(
80 BaseFeatureProvider::GetPermissionFeature("power"));
82 const std::set
<Manifest::Type
>* extension_types
= feature
->extension_types();
83 EXPECT_EQ(3u, extension_types
->size());
84 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_EXTENSION
));
85 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_LEGACY_PACKAGED_APP
));
86 EXPECT_EQ(1u, extension_types
->count(Manifest::TYPE_PLATFORM_APP
));
89 // Tests that real permission features have the correct availability for an app.
90 TEST(BaseFeatureProviderTest
, PermissionFeatureAvailability
) {
91 const FeatureProvider
* provider
=
92 BaseFeatureProvider::GetByName("permission");
94 scoped_refptr
<const Extension
> app
=
96 .SetManifest(DictionaryBuilder()
97 .Set("name", "test app")
100 DictionaryBuilder().Set(
102 DictionaryBuilder().Set(
104 ListBuilder().Append("background.js"))))
105 .Set("permissions", ListBuilder().Append("power")))
107 ASSERT_TRUE(app
.get());
108 ASSERT_TRUE(app
->is_platform_app());
110 // A permission requested in the manifest is available.
111 Feature
* feature
= provider
->GetFeature("power");
113 Feature::IS_AVAILABLE
,
114 feature
->IsAvailableToContext(
115 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
117 // A permission only available to whitelisted extensions returns availability
118 // NOT_FOUND_IN_WHITELIST.
119 feature
= provider
->GetFeature("bluetoothPrivate");
120 ASSERT_TRUE(feature
);
122 Feature::NOT_FOUND_IN_WHITELIST
,
123 feature
->IsAvailableToContext(
124 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
126 // A permission that isn't part of the manifest returns NOT_PRESENT.
127 feature
= provider
->GetFeature("serial");
128 ASSERT_TRUE(feature
);
130 Feature::NOT_PRESENT
,
131 feature
->IsAvailableToContext(
132 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
135 } // namespace extensions