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"
11 #include "base/stl_util.h"
12 #include "extensions/common/extension_builder.h"
13 #include "extensions/common/features/feature.h"
14 #include "extensions/common/features/simple_feature.h"
15 #include "extensions/common/manifest.h"
16 #include "extensions/common/value_builder.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace extensions
{
21 // Tests that a real manifest feature is available for the correct types of
22 // extensions and apps.
23 TEST(BaseFeatureProviderTest
, ManifestFeatureTypes
) {
24 // NOTE: This feature cannot have multiple rules, otherwise it is not a
26 const SimpleFeature
* feature
= static_cast<const SimpleFeature
*>(
27 FeatureProvider::GetManifestFeature("description"));
29 const std::vector
<Manifest::Type
>* extension_types
=
30 feature
->extension_types();
31 EXPECT_EQ(6u, extension_types
->size());
32 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_EXTENSION
));
34 STLCount(*(extension_types
), Manifest::TYPE_LEGACY_PACKAGED_APP
));
35 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_PLATFORM_APP
));
36 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_HOSTED_APP
));
37 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_THEME
));
38 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_SHARED_MODULE
));
41 // Tests that real manifest features have the correct availability for an
43 TEST(BaseFeatureProviderTest
, ManifestFeatureAvailability
) {
44 const FeatureProvider
* provider
= BaseFeatureProvider::GetByName("manifest");
46 scoped_refptr
<const Extension
> extension
=
48 .SetManifest(DictionaryBuilder()
49 .Set("name", "test extension")
51 .Set("description", "hello there"))
53 ASSERT_TRUE(extension
.get());
55 Feature
* feature
= provider
->GetFeature("description");
56 EXPECT_EQ(Feature::IS_AVAILABLE
,
57 feature
->IsAvailableToContext(extension
.get(),
58 Feature::UNSPECIFIED_CONTEXT
,
61 // This is a generic extension, so an app-only feature isn't allowed.
62 feature
= provider
->GetFeature("app.background");
64 EXPECT_EQ(Feature::INVALID_TYPE
,
65 feature
->IsAvailableToContext(extension
.get(),
66 Feature::UNSPECIFIED_CONTEXT
,
69 // A feature not listed in the manifest isn't allowed.
70 feature
= provider
->GetFeature("background");
72 EXPECT_EQ(Feature::NOT_PRESENT
,
73 feature
->IsAvailableToContext(extension
.get(),
74 Feature::UNSPECIFIED_CONTEXT
,
78 // Tests that a real permission feature is available for the correct types of
79 // extensions and apps.
80 TEST(BaseFeatureProviderTest
, PermissionFeatureTypes
) {
81 // NOTE: This feature cannot have multiple rules, otherwise it is not a
83 const SimpleFeature
* feature
= static_cast<const SimpleFeature
*>(
84 BaseFeatureProvider::GetPermissionFeature("power"));
86 const std::vector
<Manifest::Type
>* extension_types
=
87 feature
->extension_types();
88 EXPECT_EQ(3u, extension_types
->size());
89 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_EXTENSION
));
91 STLCount(*(extension_types
), Manifest::TYPE_LEGACY_PACKAGED_APP
));
92 EXPECT_EQ(1, STLCount(*(extension_types
), Manifest::TYPE_PLATFORM_APP
));
95 // Tests that real permission features have the correct availability for an app.
96 TEST(BaseFeatureProviderTest
, PermissionFeatureAvailability
) {
97 const FeatureProvider
* provider
=
98 BaseFeatureProvider::GetByName("permission");
100 scoped_refptr
<const Extension
> app
=
102 .SetManifest(DictionaryBuilder()
103 .Set("name", "test app")
106 DictionaryBuilder().Set(
108 DictionaryBuilder().Set(
110 ListBuilder().Append("background.js"))))
111 .Set("permissions", ListBuilder().Append("power")))
113 ASSERT_TRUE(app
.get());
114 ASSERT_TRUE(app
->is_platform_app());
116 // A permission requested in the manifest is available.
117 Feature
* feature
= provider
->GetFeature("power");
119 Feature::IS_AVAILABLE
,
120 feature
->IsAvailableToContext(
121 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
123 // A permission only available to whitelisted extensions returns availability
124 // NOT_FOUND_IN_WHITELIST.
125 feature
= provider
->GetFeature("bluetoothPrivate");
126 ASSERT_TRUE(feature
);
128 Feature::NOT_FOUND_IN_WHITELIST
,
129 feature
->IsAvailableToContext(
130 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
132 // A permission that isn't part of the manifest returns NOT_PRESENT.
133 feature
= provider
->GetFeature("serial");
134 ASSERT_TRUE(feature
);
136 Feature::NOT_PRESENT
,
137 feature
->IsAvailableToContext(
138 app
.get(), Feature::UNSPECIFIED_CONTEXT
, GURL()).result());
141 } // namespace extensions