OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/api_feature.h" |
| 6 #include "extensions/common/features/complex_feature.h" |
| 7 #include "extensions/common/features/feature.h" |
| 8 #include "extensions/common/features/simple_feature.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "tools/json_schema_compiler/test/features_test.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 namespace { |
| 15 |
| 16 template <typename T> |
| 17 void ExpectVectorsEqual(std::vector<T> expected, |
| 18 std::vector<T> actual, |
| 19 const std::string& name) { |
| 20 std::sort(expected.begin(), expected.end()); |
| 21 std::sort(actual.begin(), actual.end()); |
| 22 EXPECT_EQ(expected, actual) << name; |
| 23 } |
| 24 |
| 25 SimpleFeature::Location kDefaultLocation = SimpleFeature::UNSPECIFIED_LOCATION; |
| 26 const int kDefaultMinVersion = 0; |
| 27 const int kDefaultMaxVersion = 0; |
| 28 const bool kDefaultAutoGrant = true; |
| 29 const bool kDefaultInternal = false; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 // A utility object for comparing a feature with its expected value. |
| 34 struct FeatureComparator { |
| 35 public: |
| 36 FeatureComparator(const std::string& name); |
| 37 ~FeatureComparator(); |
| 38 |
| 39 void CompareFeature(SimpleFeature* feature); |
| 40 |
| 41 std::string name; |
| 42 std::vector<std::string> blacklist; |
| 43 std::vector<std::string> whitelist; |
| 44 std::vector<std::string> dependencies; |
| 45 std::vector<Manifest::Type> extension_types; |
| 46 std::vector<Feature::Context> contexts; |
| 47 std::vector<Feature::Platform> platforms; |
| 48 URLPatternSet matches; |
| 49 SimpleFeature::Location location; |
| 50 int min_manifest_version; |
| 51 int max_manifest_version; |
| 52 bool component_extensions_auto_granted; |
| 53 std::string command_line_switch; |
| 54 std::unique_ptr<version_info::Channel> channel; |
| 55 bool internal; |
| 56 }; |
| 57 |
| 58 FeatureComparator::FeatureComparator(const std::string& name) |
| 59 : name(name), |
| 60 location(kDefaultLocation), |
| 61 min_manifest_version(kDefaultMinVersion), |
| 62 max_manifest_version(kDefaultMaxVersion), |
| 63 component_extensions_auto_granted(kDefaultAutoGrant), |
| 64 internal(kDefaultInternal) {} |
| 65 |
| 66 FeatureComparator::~FeatureComparator() {} |
| 67 |
| 68 void FeatureComparator::CompareFeature(SimpleFeature* feature) { |
| 69 ASSERT_TRUE(feature); |
| 70 EXPECT_EQ(name, feature->name()); |
| 71 ExpectVectorsEqual(blacklist, feature->blacklist(), name); |
| 72 ExpectVectorsEqual(whitelist, feature->whitelist(), name); |
| 73 ExpectVectorsEqual(dependencies, feature->dependencies(), name); |
| 74 ExpectVectorsEqual(extension_types, feature->extension_types(), name); |
| 75 ExpectVectorsEqual(contexts, feature->contexts(), name); |
| 76 ExpectVectorsEqual(platforms, feature->platforms(), name); |
| 77 EXPECT_EQ(matches, feature->matches()) << name; |
| 78 EXPECT_EQ(location, feature->location()) << name; |
| 79 EXPECT_EQ(min_manifest_version, feature->min_manifest_version()) << name; |
| 80 EXPECT_EQ(max_manifest_version, feature->max_manifest_version()) << name; |
| 81 EXPECT_EQ(component_extensions_auto_granted, |
| 82 feature->component_extensions_auto_granted()) |
| 83 << name; |
| 84 EXPECT_EQ(command_line_switch, feature->command_line_switch()) << name; |
| 85 ASSERT_EQ(channel.get() != nullptr, feature->has_channel()) << name; |
| 86 if (channel) |
| 87 EXPECT_EQ(*channel, feature->channel()) << name; |
| 88 EXPECT_EQ(internal, feature->IsInternal()) << name; |
| 89 } |
| 90 |
| 91 TEST(FeaturesGenerationTest, FeaturesTest) { |
| 92 TestAPIFeatureProvider provider; |
| 93 |
| 94 auto GetAPIFeature = [&provider](const std::string& name) { |
| 95 Feature* feature = provider.GetFeature(name); |
| 96 // Shame we can't test this more safely, but if our feature is declared as |
| 97 // the wrong class, things should blow up in a spectacular fashion. |
| 98 return static_cast<APIFeature*>(feature); |
| 99 }; |
| 100 |
| 101 // Check some simple features for accuracy. |
| 102 { |
| 103 APIFeature* feature = GetAPIFeature("alpha"); |
| 104 FeatureComparator comparator("alpha"); |
| 105 comparator.dependencies = {"permission:alpha"}; |
| 106 comparator.contexts = {Feature::BLESSED_EXTENSION_CONTEXT}; |
| 107 comparator.channel.reset( |
| 108 new version_info::Channel(version_info::Channel::STABLE)); |
| 109 comparator.CompareFeature(feature); |
| 110 } |
| 111 { |
| 112 APIFeature* feature = GetAPIFeature("beta"); |
| 113 FeatureComparator comparator("beta"); |
| 114 comparator.extension_types = {Manifest::TYPE_EXTENSION, |
| 115 Manifest::TYPE_PLATFORM_APP}; |
| 116 comparator.whitelist = {"aaa", "bbb"}; |
| 117 comparator.blacklist = {"zzz", "yyy"}; |
| 118 comparator.component_extensions_auto_granted = false; |
| 119 comparator.CompareFeature(feature); |
| 120 } |
| 121 { |
| 122 APIFeature* feature = GetAPIFeature("gamma"); |
| 123 FeatureComparator comparator("gamma"); |
| 124 comparator.channel.reset( |
| 125 new version_info::Channel(version_info::Channel::BETA)); |
| 126 comparator.platforms = {Feature::WIN_PLATFORM, Feature::MACOSX_PLATFORM}; |
| 127 comparator.contexts = {Feature::BLESSED_EXTENSION_CONTEXT}; |
| 128 comparator.extension_types = {Manifest::TYPE_EXTENSION}; |
| 129 comparator.internal = true; |
| 130 comparator.CompareFeature(feature); |
| 131 |
| 132 // A child feature should inherit all fields from its parent, except in the |
| 133 // case that it specifies its own value. Thus, we reuse |comparator|. |
| 134 feature = GetAPIFeature("gamma.child"); |
| 135 comparator.name = "gamma.child"; |
| 136 comparator.dependencies = {"permission:gamma.child"}; |
| 137 comparator.whitelist = {"ccc"}; |
| 138 comparator.platforms = {Feature::LINUX_PLATFORM}; |
| 139 comparator.CompareFeature(feature); |
| 140 } |
| 141 { |
| 142 // Features that specify 'noparent' should not inherit features from any |
| 143 // other feature. |
| 144 APIFeature* feature = GetAPIFeature("gamma.unparented"); |
| 145 FeatureComparator comparator("gamma.unparented"); |
| 146 comparator.blacklist = {"ddd"}; |
| 147 comparator.contexts = {Feature::UNBLESSED_EXTENSION_CONTEXT}; |
| 148 comparator.CompareFeature(feature); |
| 149 } |
| 150 { |
| 151 APIFeature* feature = GetAPIFeature("delta"); |
| 152 FeatureComparator comparator("delta"); |
| 153 comparator.contexts = {Feature::BLESSED_EXTENSION_CONTEXT, |
| 154 Feature::WEBUI_CONTEXT}; |
| 155 comparator.matches.AddPattern( |
| 156 URLPattern(URLPattern::SCHEME_ALL, "*://example.com/*")); |
| 157 comparator.CompareFeature(feature); |
| 158 } |
| 159 { |
| 160 // Omega is imported from a second .json file. |
| 161 APIFeature* feature = GetAPIFeature("omega"); |
| 162 FeatureComparator comparator("omega"); |
| 163 comparator.contexts = {Feature::WEB_PAGE_CONTEXT}; |
| 164 comparator.min_manifest_version = 2; |
| 165 comparator.CompareFeature(feature); |
| 166 } |
| 167 { |
| 168 // Features specifying 'nocompile' should not be generated at all. |
| 169 APIFeature* feature = GetAPIFeature("uncompiled"); |
| 170 EXPECT_FALSE(feature); |
| 171 } |
| 172 |
| 173 // Test complex features. |
| 174 { |
| 175 ComplexFeature* feature = |
| 176 static_cast<ComplexFeature*>(provider.GetFeature("complex")); |
| 177 ASSERT_TRUE(feature); |
| 178 EXPECT_EQ(2u, feature->features_.size()); |
| 179 // Find the default parent. This is a little tedious because it might not |
| 180 // be guaranteed that the default_parent is in a specific index, but it |
| 181 // specifies channel as 'stable'. |
| 182 SimpleFeature* default_parent = nullptr; |
| 183 SimpleFeature* other_parent = nullptr; |
| 184 { |
| 185 SimpleFeature* parent1 = |
| 186 static_cast<SimpleFeature*>(feature->features_[0].get()); |
| 187 SimpleFeature* parent2 = |
| 188 static_cast<SimpleFeature*>(feature->features_[1].get()); |
| 189 if (parent1->channel() == version_info::Channel::STABLE) { |
| 190 default_parent = parent1; |
| 191 other_parent = parent2; |
| 192 } else { |
| 193 other_parent = parent1; |
| 194 default_parent = parent2; |
| 195 } |
| 196 } |
| 197 { |
| 198 // Check the default parent. |
| 199 FeatureComparator comparator("complex"); |
| 200 comparator.channel.reset( |
| 201 new version_info::Channel(version_info::Channel::STABLE)); |
| 202 comparator.contexts = {Feature::BLESSED_EXTENSION_CONTEXT}; |
| 203 comparator.extension_types = {Manifest::TYPE_EXTENSION}; |
| 204 comparator.CompareFeature(default_parent); |
| 205 // Check the child of the complex feature. It should inherit its |
| 206 // properties from the default parent. |
| 207 APIFeature* child_feature = GetAPIFeature("complex.child"); |
| 208 comparator.name = "complex.child"; |
| 209 comparator.platforms = {Feature::WIN_PLATFORM}; |
| 210 comparator.dependencies = {"permission:complex.child"}; |
| 211 comparator.CompareFeature(child_feature); |
| 212 } |
| 213 { |
| 214 // Finally, check the branch of the complex feature. |
| 215 FeatureComparator comparator("complex"); |
| 216 comparator.channel.reset( |
| 217 new version_info::Channel(version_info::Channel::BETA)); |
| 218 comparator.contexts = {Feature::BLESSED_EXTENSION_CONTEXT}; |
| 219 comparator.extension_types = {Manifest::TYPE_EXTENSION}; |
| 220 comparator.whitelist = {"aaa"}; |
| 221 comparator.CompareFeature(other_parent); |
| 222 } |
| 223 } |
| 224 } |
| 225 |
| 226 } // namespace extensions |
OLD | NEW |