Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / extensions / common / features / base_feature_provider_unittest.cc
blobdc41366eb20ef5463f317de3bf290de63f21c6a9
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"
7 #include <algorithm>
8 #include <set>
9 #include <string>
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
25 // SimpleFeature.
26 const SimpleFeature* feature = static_cast<const SimpleFeature*>(
27 FeatureProvider::GetManifestFeature("description"));
28 ASSERT_TRUE(feature);
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));
33 EXPECT_EQ(1,
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
42 // extension.
43 TEST(BaseFeatureProviderTest, ManifestFeatureAvailability) {
44 const FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest");
46 scoped_refptr<const Extension> extension =
47 ExtensionBuilder()
48 .SetManifest(DictionaryBuilder()
49 .Set("name", "test extension")
50 .Set("version", "1")
51 .Set("description", "hello there"))
52 .Build();
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,
59 GURL()).result());
61 // This is a generic extension, so an app-only feature isn't allowed.
62 feature = provider->GetFeature("app.background");
63 ASSERT_TRUE(feature);
64 EXPECT_EQ(Feature::INVALID_TYPE,
65 feature->IsAvailableToContext(extension.get(),
66 Feature::UNSPECIFIED_CONTEXT,
67 GURL()).result());
69 // A feature not listed in the manifest isn't allowed.
70 feature = provider->GetFeature("background");
71 ASSERT_TRUE(feature);
72 EXPECT_EQ(Feature::NOT_PRESENT,
73 feature->IsAvailableToContext(extension.get(),
74 Feature::UNSPECIFIED_CONTEXT,
75 GURL()).result());
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
82 // SimpleFeature.
83 const SimpleFeature* feature = static_cast<const SimpleFeature*>(
84 BaseFeatureProvider::GetPermissionFeature("power"));
85 ASSERT_TRUE(feature);
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));
90 EXPECT_EQ(1,
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 =
101 ExtensionBuilder()
102 .SetManifest(DictionaryBuilder()
103 .Set("name", "test app")
104 .Set("version", "1")
105 .Set("app",
106 DictionaryBuilder().Set(
107 "background",
108 DictionaryBuilder().Set(
109 "scripts",
110 ListBuilder().Append("background.js"))))
111 .Set("permissions", ListBuilder().Append("power")))
112 .Build();
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");
118 EXPECT_EQ(
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);
127 EXPECT_EQ(
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);
135 EXPECT_EQ(
136 Feature::NOT_PRESENT,
137 feature->IsAvailableToContext(
138 app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
141 } // namespace extensions