ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / common / features / base_feature_provider_unittest.cc
blob8f4a3ba3ca35f2c808f5bcef34378fa1683a8295
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 <set>
8 #include <string>
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
23 // SimpleFeature.
24 const SimpleFeature* feature = static_cast<const SimpleFeature*>(
25 FeatureProvider::GetManifestFeature("description"));
26 ASSERT_TRUE(feature);
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
38 // extension.
39 TEST(BaseFeatureProviderTest, ManifestFeatureAvailability) {
40 const FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest");
42 scoped_refptr<const Extension> extension =
43 ExtensionBuilder()
44 .SetManifest(DictionaryBuilder()
45 .Set("name", "test extension")
46 .Set("version", "1")
47 .Set("description", "hello there"))
48 .Build();
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,
55 GURL()).result());
57 // This is a generic extension, so an app-only feature isn't allowed.
58 feature = provider->GetFeature("app.background");
59 ASSERT_TRUE(feature);
60 EXPECT_EQ(Feature::INVALID_TYPE,
61 feature->IsAvailableToContext(extension.get(),
62 Feature::UNSPECIFIED_CONTEXT,
63 GURL()).result());
65 // A feature not listed in the manifest isn't allowed.
66 feature = provider->GetFeature("background");
67 ASSERT_TRUE(feature);
68 EXPECT_EQ(Feature::NOT_PRESENT,
69 feature->IsAvailableToContext(extension.get(),
70 Feature::UNSPECIFIED_CONTEXT,
71 GURL()).result());
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
78 // SimpleFeature.
79 const SimpleFeature* feature = static_cast<const SimpleFeature*>(
80 BaseFeatureProvider::GetPermissionFeature("power"));
81 ASSERT_TRUE(feature);
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 =
95 ExtensionBuilder()
96 .SetManifest(DictionaryBuilder()
97 .Set("name", "test app")
98 .Set("version", "1")
99 .Set("app",
100 DictionaryBuilder().Set(
101 "background",
102 DictionaryBuilder().Set(
103 "scripts",
104 ListBuilder().Append("background.js"))))
105 .Set("permissions", ListBuilder().Append("power")))
106 .Build();
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");
112 EXPECT_EQ(
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);
121 EXPECT_EQ(
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);
129 EXPECT_EQ(
130 Feature::NOT_PRESENT,
131 feature->IsAvailableToContext(
132 app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result());
135 } // namespace extensions