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