Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / features / complex_feature_unittest.cc
blob38f9735f8b2d2262302f3efdfb65f2de8c864f32
1 // Copyright (c) 2012 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 "chrome/common/extensions/features/complex_feature.h"
7 #include "chrome/common/extensions/features/feature_channel.h"
8 #include "chrome/common/extensions/features/simple_feature.h"
9 #include "extensions/common/value_builder.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using chrome::VersionInfo;
13 using extensions::ComplexFeature;
14 using extensions::DictionaryBuilder;
15 using extensions::Feature;
16 using extensions::ListBuilder;
17 using extensions::Manifest;
18 using extensions::ScopedCurrentChannel;
19 using extensions::SimpleFeature;
21 namespace {
23 class ExtensionComplexFeatureTest : public testing::Test {
24 protected:
25 ExtensionComplexFeatureTest()
26 : current_channel_(VersionInfo::CHANNEL_UNKNOWN) {}
27 virtual ~ExtensionComplexFeatureTest() {}
29 private:
30 ScopedCurrentChannel current_channel_;
33 TEST_F(ExtensionComplexFeatureTest, MultipleRulesWhitelist) {
34 const std::string kIdFoo("fooabbbbccccddddeeeeffffgggghhhh");
35 const std::string kIdBar("barabbbbccccddddeeeeffffgggghhhh");
36 scoped_ptr<ComplexFeature::FeatureList> features(
37 new ComplexFeature::FeatureList());
39 // Rule: "extension", whitelist "foo".
40 scoped_ptr<SimpleFeature> simple_feature(new SimpleFeature());
41 scoped_ptr<base::DictionaryValue> rule(
42 DictionaryBuilder()
43 .Set("whitelist", ListBuilder().Append(kIdFoo))
44 .Set("extension_types", ListBuilder()
45 .Append("extension")).Build());
46 simple_feature->Parse(rule.get());
47 features->push_back(simple_feature.release());
49 // Rule: "legacy_packaged_app", whitelist "bar".
50 simple_feature.reset(new SimpleFeature());
51 rule = DictionaryBuilder()
52 .Set("whitelist", ListBuilder().Append(kIdBar))
53 .Set("extension_types", ListBuilder()
54 .Append("legacy_packaged_app")).Build();
55 simple_feature->Parse(rule.get());
56 features->push_back(simple_feature.release());
58 scoped_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
60 // Test match 1st rule.
61 EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
62 kIdFoo,
63 Manifest::TYPE_EXTENSION,
64 Feature::UNSPECIFIED_LOCATION,
65 Feature::UNSPECIFIED_PLATFORM,
66 Feature::GetCurrentPlatform()).result());
68 // Test match 2nd rule.
69 EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
70 kIdBar,
71 Manifest::TYPE_LEGACY_PACKAGED_APP,
72 Feature::UNSPECIFIED_LOCATION,
73 Feature::UNSPECIFIED_PLATFORM,
74 Feature::GetCurrentPlatform()).result());
76 // Test whitelist with wrong extension type.
77 EXPECT_NE(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
78 kIdBar,
79 Manifest::TYPE_EXTENSION,
80 Feature::UNSPECIFIED_LOCATION,
81 Feature::UNSPECIFIED_PLATFORM,
82 Feature::GetCurrentPlatform()).result());
83 EXPECT_NE(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(kIdFoo,
84 Manifest::TYPE_LEGACY_PACKAGED_APP,
85 Feature::UNSPECIFIED_LOCATION,
86 Feature::UNSPECIFIED_PLATFORM,
87 Feature::GetCurrentPlatform()).result());
90 TEST_F(ExtensionComplexFeatureTest, MultipleRulesChannels) {
91 scoped_ptr<ComplexFeature::FeatureList> features(
92 new ComplexFeature::FeatureList());
94 // Rule: "extension", channel trunk.
95 scoped_ptr<SimpleFeature> simple_feature(new SimpleFeature());
96 scoped_ptr<base::DictionaryValue> rule(
97 DictionaryBuilder()
98 .Set("channel", "trunk")
99 .Set("extension_types", ListBuilder().Append("extension")).Build());
100 simple_feature->Parse(rule.get());
101 features->push_back(simple_feature.release());
103 // Rule: "legacy_packaged_app", channel stable.
104 simple_feature.reset(new SimpleFeature());
105 rule = DictionaryBuilder()
106 .Set("channel", "stable")
107 .Set("extension_types", ListBuilder()
108 .Append("legacy_packaged_app")).Build();
109 simple_feature->Parse(rule.get());
110 features->push_back(simple_feature.release());
112 scoped_ptr<ComplexFeature> feature(new ComplexFeature(features.Pass()));
114 // Test match 1st rule.
116 ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_UNKNOWN);
117 EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
118 "1",
119 Manifest::TYPE_EXTENSION,
120 Feature::UNSPECIFIED_LOCATION,
121 Feature::UNSPECIFIED_PLATFORM,
122 Feature::GetCurrentPlatform()).result());
125 // Test match 2nd rule.
127 ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_BETA);
128 EXPECT_EQ(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
129 "2",
130 Manifest::TYPE_LEGACY_PACKAGED_APP,
131 Feature::UNSPECIFIED_LOCATION,
132 Feature::UNSPECIFIED_PLATFORM,
133 Feature::GetCurrentPlatform()).result());
136 // Test feature not available to extensions above channel unknown.
138 ScopedCurrentChannel current_channel(VersionInfo::CHANNEL_BETA);
139 EXPECT_NE(Feature::IS_AVAILABLE, feature->IsAvailableToManifest(
140 "1",
141 Manifest::TYPE_EXTENSION,
142 Feature::UNSPECIFIED_LOCATION,
143 Feature::UNSPECIFIED_PLATFORM,
144 Feature::GetCurrentPlatform()).result());
148 } // namespace