| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "extensions/common/features/base_feature_provider.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/stl_util.h" | |
| 13 #include "extensions/common/extension_builder.h" | |
| 14 #include "extensions/common/features/feature.h" | |
| 15 #include "extensions/common/features/simple_feature.h" | |
| 16 #include "extensions/common/manifest.h" | |
| 17 #include "extensions/common/value_builder.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace extensions { | |
| 21 | |
| 22 // Tests that a real manifest feature is available for the correct types of | |
| 23 // extensions and apps. | |
| 24 TEST(BaseFeatureProviderTest, ManifestFeatureTypes) { | |
| 25 // NOTE: This feature cannot have multiple rules, otherwise it is not a | |
| 26 // SimpleFeature. | |
| 27 const SimpleFeature* feature = static_cast<const SimpleFeature*>( | |
| 28 FeatureProvider::GetManifestFeature("description")); | |
| 29 ASSERT_TRUE(feature); | |
| 30 const std::vector<Manifest::Type>& extension_types = | |
| 31 feature->extension_types(); | |
| 32 EXPECT_EQ(6u, extension_types.size()); | |
| 33 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_EXTENSION)); | |
| 34 EXPECT_EQ( | |
| 35 1, base::STLCount(extension_types, Manifest::TYPE_LEGACY_PACKAGED_APP)); | |
| 36 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_PLATFORM_APP)); | |
| 37 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_HOSTED_APP)); | |
| 38 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_THEME)); | |
| 39 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_SHARED_MODULE)); | |
| 40 } | |
| 41 | |
| 42 // Tests that real manifest features have the correct availability for an | |
| 43 // extension. | |
| 44 TEST(BaseFeatureProviderTest, ManifestFeatureAvailability) { | |
| 45 const FeatureProvider* provider = BaseFeatureProvider::GetByName("manifest"); | |
| 46 | |
| 47 scoped_refptr<const Extension> extension = | |
| 48 ExtensionBuilder() | |
| 49 .SetManifest(DictionaryBuilder() | |
| 50 .Set("name", "test extension") | |
| 51 .Set("version", "1") | |
| 52 .Set("description", "hello there") | |
| 53 .Build()) | |
| 54 .Build(); | |
| 55 ASSERT_TRUE(extension.get()); | |
| 56 | |
| 57 Feature* feature = provider->GetFeature("description"); | |
| 58 EXPECT_EQ(Feature::IS_AVAILABLE, | |
| 59 feature->IsAvailableToContext(extension.get(), | |
| 60 Feature::UNSPECIFIED_CONTEXT, | |
| 61 GURL()).result()); | |
| 62 | |
| 63 // This is a generic extension, so an app-only feature isn't allowed. | |
| 64 feature = provider->GetFeature("app.background"); | |
| 65 ASSERT_TRUE(feature); | |
| 66 EXPECT_EQ(Feature::INVALID_TYPE, | |
| 67 feature->IsAvailableToContext(extension.get(), | |
| 68 Feature::UNSPECIFIED_CONTEXT, | |
| 69 GURL()).result()); | |
| 70 | |
| 71 // A feature not listed in the manifest isn't allowed. | |
| 72 feature = provider->GetFeature("background"); | |
| 73 ASSERT_TRUE(feature); | |
| 74 EXPECT_EQ(Feature::NOT_PRESENT, | |
| 75 feature->IsAvailableToContext(extension.get(), | |
| 76 Feature::UNSPECIFIED_CONTEXT, | |
| 77 GURL()).result()); | |
| 78 } | |
| 79 | |
| 80 // Tests that a real permission feature is available for the correct types of | |
| 81 // extensions and apps. | |
| 82 TEST(BaseFeatureProviderTest, PermissionFeatureTypes) { | |
| 83 // NOTE: This feature cannot have multiple rules, otherwise it is not a | |
| 84 // SimpleFeature. | |
| 85 const SimpleFeature* feature = static_cast<const SimpleFeature*>( | |
| 86 BaseFeatureProvider::GetPermissionFeature("power")); | |
| 87 ASSERT_TRUE(feature); | |
| 88 const std::vector<Manifest::Type>& extension_types = | |
| 89 feature->extension_types(); | |
| 90 EXPECT_EQ(3u, extension_types.size()); | |
| 91 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_EXTENSION)); | |
| 92 EXPECT_EQ( | |
| 93 1, base::STLCount(extension_types, Manifest::TYPE_LEGACY_PACKAGED_APP)); | |
| 94 EXPECT_EQ(1, base::STLCount(extension_types, Manifest::TYPE_PLATFORM_APP)); | |
| 95 } | |
| 96 | |
| 97 // Tests that real permission features have the correct availability for an app. | |
| 98 TEST(BaseFeatureProviderTest, PermissionFeatureAvailability) { | |
| 99 const FeatureProvider* provider = | |
| 100 BaseFeatureProvider::GetByName("permission"); | |
| 101 | |
| 102 scoped_refptr<const Extension> app = | |
| 103 ExtensionBuilder() | |
| 104 .SetManifest( | |
| 105 DictionaryBuilder() | |
| 106 .Set("name", "test app") | |
| 107 .Set("version", "1") | |
| 108 .Set("app", | |
| 109 DictionaryBuilder() | |
| 110 .Set("background", | |
| 111 DictionaryBuilder() | |
| 112 .Set("scripts", ListBuilder() | |
| 113 .Append("background.js") | |
| 114 .Build()) | |
| 115 .Build()) | |
| 116 .Build()) | |
| 117 .Set("permissions", ListBuilder().Append("power").Build()) | |
| 118 .Build()) | |
| 119 .Build(); | |
| 120 ASSERT_TRUE(app.get()); | |
| 121 ASSERT_TRUE(app->is_platform_app()); | |
| 122 | |
| 123 // A permission requested in the manifest is available. | |
| 124 Feature* feature = provider->GetFeature("power"); | |
| 125 EXPECT_EQ( | |
| 126 Feature::IS_AVAILABLE, | |
| 127 feature->IsAvailableToContext( | |
| 128 app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result()); | |
| 129 | |
| 130 // A permission only available to whitelisted extensions returns availability | |
| 131 // NOT_FOUND_IN_WHITELIST. | |
| 132 feature = provider->GetFeature("bluetoothPrivate"); | |
| 133 ASSERT_TRUE(feature); | |
| 134 EXPECT_EQ( | |
| 135 Feature::NOT_FOUND_IN_WHITELIST, | |
| 136 feature->IsAvailableToContext( | |
| 137 app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result()); | |
| 138 | |
| 139 // A permission that isn't part of the manifest returns NOT_PRESENT. | |
| 140 feature = provider->GetFeature("serial"); | |
| 141 ASSERT_TRUE(feature); | |
| 142 EXPECT_EQ( | |
| 143 Feature::NOT_PRESENT, | |
| 144 feature->IsAvailableToContext( | |
| 145 app.get(), Feature::UNSPECIFIED_CONTEXT, GURL()).result()); | |
| 146 } | |
| 147 | |
| 148 } // namespace extensions | |
| OLD | NEW |