Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1032)

Side by Side Diff: chrome/common/extensions/manifest_unittest.cc

Issue 12093036: Move Extension Location and Type enums to Manifest, and move InstallWarning to its own file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/common/extensions/manifest.h" 5 #include "chrome/common/extensions/manifest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 10
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/common/extensions/extension_manifest_constants.h" 14 #include "chrome/common/extensions/extension_manifest_constants.h"
15 #include "chrome/common/extensions/features/feature.h" 15 #include "chrome/common/extensions/features/feature.h"
16 #include "chrome/common/extensions/features/simple_feature.h" 16 #include "chrome/common/extensions/features/simple_feature.h"
17 #include "extensions/common/error_utils.h" 17 #include "extensions/common/error_utils.h"
18 #include "extensions/common/install_warning.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 namespace errors = extension_manifest_errors; 21 namespace errors = extension_manifest_errors;
21 namespace keys = extension_manifest_keys; 22 namespace keys = extension_manifest_keys;
22 23
23 namespace extensions { 24 namespace extensions {
24 25
25 class ManifestTest : public testing::Test { 26 class ManifestTest : public testing::Test {
26 public: 27 public:
27 ManifestTest() : default_value_("test") {} 28 ManifestTest() : default_value_("test") {}
28 29
29 protected: 30 protected:
30 void AssertType(Manifest* manifest, Extension::Type type) { 31 void AssertType(Manifest* manifest, Manifest::Type type) {
31 EXPECT_EQ(type, manifest->type()); 32 EXPECT_EQ(type, manifest->type());
32 EXPECT_EQ(type == Extension::TYPE_THEME, manifest->is_theme()); 33 EXPECT_EQ(type == Manifest::TYPE_THEME, manifest->is_theme());
33 EXPECT_EQ(type == Extension::TYPE_PLATFORM_APP, 34 EXPECT_EQ(type == Manifest::TYPE_PLATFORM_APP,
34 manifest->is_platform_app()); 35 manifest->is_platform_app());
35 EXPECT_EQ(type == Extension::TYPE_LEGACY_PACKAGED_APP, 36 EXPECT_EQ(type == Manifest::TYPE_LEGACY_PACKAGED_APP,
36 manifest->is_legacy_packaged_app()); 37 manifest->is_legacy_packaged_app());
37 EXPECT_EQ(type == Extension::TYPE_HOSTED_APP, manifest->is_hosted_app()); 38 EXPECT_EQ(type == Manifest::TYPE_HOSTED_APP, manifest->is_hosted_app());
38 } 39 }
39 40
40 // Helper function that replaces the Manifest held by |manifest| with a copy 41 // Helper function that replaces the Manifest held by |manifest| with a copy
41 // with its |key| changed to |value|. If |value| is NULL, then |key| will 42 // with its |key| changed to |value|. If |value| is NULL, then |key| will
42 // instead be deleted. 43 // instead be deleted.
43 void MutateManifest( 44 void MutateManifest(
44 scoped_ptr<Manifest>* manifest, const std::string& key, Value* value) { 45 scoped_ptr<Manifest>* manifest, const std::string& key, Value* value) {
45 scoped_ptr<DictionaryValue> manifest_value( 46 scoped_ptr<DictionaryValue> manifest_value(
46 manifest->get()->value()->DeepCopy()); 47 manifest->get()->value()->DeepCopy());
47 if (value) 48 if (value)
48 manifest_value->Set(key, value); 49 manifest_value->Set(key, value);
49 else 50 else
50 manifest_value->Remove(key, NULL); 51 manifest_value->Remove(key, NULL);
51 manifest->reset(new Manifest(Extension::INTERNAL, manifest_value.Pass())); 52 manifest->reset(new Manifest(Manifest::INTERNAL, manifest_value.Pass()));
52 } 53 }
53 54
54 std::string default_value_; 55 std::string default_value_;
55 }; 56 };
56 57
57 // Verifies that extensions can access the correct keys. 58 // Verifies that extensions can access the correct keys.
58 TEST_F(ManifestTest, Extension) { 59 TEST_F(ManifestTest, Extension) {
59 scoped_ptr<DictionaryValue> manifest_value(new DictionaryValue()); 60 scoped_ptr<DictionaryValue> manifest_value(new DictionaryValue());
60 manifest_value->SetString(keys::kName, "extension"); 61 manifest_value->SetString(keys::kName, "extension");
61 manifest_value->SetString(keys::kVersion, "1"); 62 manifest_value->SetString(keys::kVersion, "1");
62 // Only supported in manifest_version=1. 63 // Only supported in manifest_version=1.
63 manifest_value->SetString(keys::kBackgroundPageLegacy, "bg.html"); 64 manifest_value->SetString(keys::kBackgroundPageLegacy, "bg.html");
64 manifest_value->SetString("unknown_key", "foo"); 65 manifest_value->SetString("unknown_key", "foo");
65 66
66 scoped_ptr<Manifest> manifest( 67 scoped_ptr<Manifest> manifest(
67 new Manifest(Extension::INTERNAL, manifest_value.Pass())); 68 new Manifest(Manifest::INTERNAL, manifest_value.Pass()));
68 std::string error; 69 std::string error;
69 Extension::InstallWarningVector warnings; 70 std::vector<InstallWarning> warnings;
70 manifest->ValidateManifest(&error, &warnings); 71 manifest->ValidateManifest(&error, &warnings);
71 EXPECT_TRUE(error.empty()); 72 EXPECT_TRUE(error.empty());
72 ASSERT_EQ(1u, warnings.size()); 73 ASSERT_EQ(1u, warnings.size());
73 AssertType(manifest.get(), Extension::TYPE_EXTENSION); 74 AssertType(manifest.get(), Manifest::TYPE_EXTENSION);
74 75
75 // The known key 'background_page' should be accessible. 76 // The known key 'background_page' should be accessible.
76 std::string value; 77 std::string value;
77 EXPECT_TRUE(manifest->GetString(keys::kBackgroundPageLegacy, &value)); 78 EXPECT_TRUE(manifest->GetString(keys::kBackgroundPageLegacy, &value));
78 EXPECT_EQ("bg.html", value); 79 EXPECT_EQ("bg.html", value);
79 80
80 // The unknown key 'unknown_key' should be accesible. 81 // The unknown key 'unknown_key' should be accesible.
81 value.clear(); 82 value.clear();
82 EXPECT_TRUE(manifest->GetString("unknown_key", &value)); 83 EXPECT_TRUE(manifest->GetString("unknown_key", &value));
83 EXPECT_EQ("foo", value); 84 EXPECT_EQ("foo", value);
(...skipping 28 matching lines...) Expand all
112 EXPECT_FALSE(manifest->Equals(manifest2.get())); 113 EXPECT_FALSE(manifest->Equals(manifest2.get()));
113 } 114 }
114 115
115 // Verifies that key restriction based on type works. 116 // Verifies that key restriction based on type works.
116 TEST_F(ManifestTest, ExtensionTypes) { 117 TEST_F(ManifestTest, ExtensionTypes) {
117 scoped_ptr<DictionaryValue> value(new DictionaryValue()); 118 scoped_ptr<DictionaryValue> value(new DictionaryValue());
118 value->SetString(keys::kName, "extension"); 119 value->SetString(keys::kName, "extension");
119 value->SetString(keys::kVersion, "1"); 120 value->SetString(keys::kVersion, "1");
120 121
121 scoped_ptr<Manifest> manifest( 122 scoped_ptr<Manifest> manifest(
122 new Manifest(Extension::INTERNAL, value.Pass())); 123 new Manifest(Manifest::INTERNAL, value.Pass()));
123 std::string error; 124 std::string error;
124 Extension::InstallWarningVector warnings; 125 std::vector<InstallWarning> warnings;
125 manifest->ValidateManifest(&error, &warnings); 126 manifest->ValidateManifest(&error, &warnings);
126 EXPECT_TRUE(error.empty()); 127 EXPECT_TRUE(error.empty());
127 EXPECT_TRUE(warnings.empty()); 128 EXPECT_TRUE(warnings.empty());
128 129
129 // By default, the type is Extension. 130 // By default, the type is Extension.
130 AssertType(manifest.get(), Extension::TYPE_EXTENSION); 131 AssertType(manifest.get(), Manifest::TYPE_EXTENSION);
131 132
132 // Theme. 133 // Theme.
133 MutateManifest( 134 MutateManifest(
134 &manifest, keys::kTheme, new DictionaryValue()); 135 &manifest, keys::kTheme, new DictionaryValue());
135 AssertType(manifest.get(), Extension::TYPE_THEME); 136 AssertType(manifest.get(), Manifest::TYPE_THEME);
136 MutateManifest( 137 MutateManifest(
137 &manifest, keys::kTheme, NULL); 138 &manifest, keys::kTheme, NULL);
138 139
139 // Packaged app. 140 // Packaged app.
140 MutateManifest( 141 MutateManifest(
141 &manifest, keys::kApp, new DictionaryValue()); 142 &manifest, keys::kApp, new DictionaryValue());
142 AssertType(manifest.get(), Extension::TYPE_LEGACY_PACKAGED_APP); 143 AssertType(manifest.get(), Manifest::TYPE_LEGACY_PACKAGED_APP);
143 144
144 // Platform app. 145 // Platform app.
145 MutateManifest( 146 MutateManifest(
146 &manifest, keys::kPlatformAppBackground, new DictionaryValue()); 147 &manifest, keys::kPlatformAppBackground, new DictionaryValue());
147 AssertType(manifest.get(), Extension::TYPE_PLATFORM_APP); 148 AssertType(manifest.get(), Manifest::TYPE_PLATFORM_APP);
148 MutateManifest( 149 MutateManifest(
149 &manifest, keys::kPlatformAppBackground, NULL); 150 &manifest, keys::kPlatformAppBackground, NULL);
150 151
151 // Hosted app. 152 // Hosted app.
152 MutateManifest( 153 MutateManifest(
153 &manifest, keys::kWebURLs, new ListValue()); 154 &manifest, keys::kWebURLs, new ListValue());
154 AssertType(manifest.get(), Extension::TYPE_HOSTED_APP); 155 AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP);
155 MutateManifest( 156 MutateManifest(
156 &manifest, keys::kWebURLs, NULL); 157 &manifest, keys::kWebURLs, NULL);
157 MutateManifest( 158 MutateManifest(
158 &manifest, keys::kLaunchWebURL, Value::CreateStringValue("foo")); 159 &manifest, keys::kLaunchWebURL, Value::CreateStringValue("foo"));
159 AssertType(manifest.get(), Extension::TYPE_HOSTED_APP); 160 AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP);
160 MutateManifest( 161 MutateManifest(
161 &manifest, keys::kLaunchWebURL, NULL); 162 &manifest, keys::kLaunchWebURL, NULL);
162 }; 163 };
163 164
164 // Verifies that the getters filter restricted keys. 165 // Verifies that the getters filter restricted keys.
165 TEST_F(ManifestTest, RestrictedKeys) { 166 TEST_F(ManifestTest, RestrictedKeys) {
166 scoped_ptr<DictionaryValue> value(new DictionaryValue()); 167 scoped_ptr<DictionaryValue> value(new DictionaryValue());
167 value->SetString(keys::kName, "extension"); 168 value->SetString(keys::kName, "extension");
168 value->SetString(keys::kVersion, "1"); 169 value->SetString(keys::kVersion, "1");
169 170
170 scoped_ptr<Manifest> manifest( 171 scoped_ptr<Manifest> manifest(
171 new Manifest(Extension::INTERNAL, value.Pass())); 172 new Manifest(Manifest::INTERNAL, value.Pass()));
172 std::string error; 173 std::string error;
173 Extension::InstallWarningVector warnings; 174 std::vector<InstallWarning> warnings;
174 manifest->ValidateManifest(&error, &warnings); 175 manifest->ValidateManifest(&error, &warnings);
175 EXPECT_TRUE(error.empty()); 176 EXPECT_TRUE(error.empty());
176 EXPECT_TRUE(warnings.empty()); 177 EXPECT_TRUE(warnings.empty());
177 178
178 // Platform apps cannot have a "page_action" key. 179 // Platform apps cannot have a "page_action" key.
179 MutateManifest( 180 MutateManifest(
180 &manifest, keys::kPageAction, new DictionaryValue()); 181 &manifest, keys::kPageAction, new DictionaryValue());
181 AssertType(manifest.get(), Extension::TYPE_EXTENSION); 182 AssertType(manifest.get(), Manifest::TYPE_EXTENSION);
182 base::Value* output = NULL; 183 base::Value* output = NULL;
183 EXPECT_TRUE(manifest->HasKey(keys::kPageAction)); 184 EXPECT_TRUE(manifest->HasKey(keys::kPageAction));
184 EXPECT_TRUE(manifest->Get(keys::kPageAction, &output)); 185 EXPECT_TRUE(manifest->Get(keys::kPageAction, &output));
185 186
186 MutateManifest( 187 MutateManifest(
187 &manifest, keys::kPlatformAppBackground, new DictionaryValue()); 188 &manifest, keys::kPlatformAppBackground, new DictionaryValue());
188 AssertType(manifest.get(), Extension::TYPE_PLATFORM_APP); 189 AssertType(manifest.get(), Manifest::TYPE_PLATFORM_APP);
189 EXPECT_FALSE(manifest->HasKey(keys::kPageAction)); 190 EXPECT_FALSE(manifest->HasKey(keys::kPageAction));
190 EXPECT_FALSE(manifest->Get(keys::kPageAction, &output)); 191 EXPECT_FALSE(manifest->Get(keys::kPageAction, &output));
191 MutateManifest( 192 MutateManifest(
192 &manifest, keys::kPlatformAppBackground, NULL); 193 &manifest, keys::kPlatformAppBackground, NULL);
193 194
194 // "commands" is restricted to manifest_version >= 2. 195 // "commands" is restricted to manifest_version >= 2.
195 { 196 {
196 // ... and dev channel, for now. 197 // ... and dev channel, for now.
197 Feature::ScopedCurrentChannel dev_channel_scope( 198 Feature::ScopedCurrentChannel dev_channel_scope(
198 chrome::VersionInfo::CHANNEL_DEV); 199 chrome::VersionInfo::CHANNEL_DEV);
199 200
200 MutateManifest( 201 MutateManifest(
201 &manifest, keys::kCommands, new DictionaryValue()); 202 &manifest, keys::kCommands, new DictionaryValue());
202 EXPECT_FALSE(manifest->HasKey(keys::kCommands)); 203 EXPECT_FALSE(manifest->HasKey(keys::kCommands));
203 EXPECT_FALSE(manifest->Get(keys::kCommands, &output)); 204 EXPECT_FALSE(manifest->Get(keys::kCommands, &output));
204 205
205 MutateManifest( 206 MutateManifest(
206 &manifest, keys::kManifestVersion, Value::CreateIntegerValue(2)); 207 &manifest, keys::kManifestVersion, Value::CreateIntegerValue(2));
207 EXPECT_TRUE(manifest->HasKey(keys::kCommands)); 208 EXPECT_TRUE(manifest->HasKey(keys::kCommands));
208 EXPECT_TRUE(manifest->Get(keys::kCommands, &output)); 209 EXPECT_TRUE(manifest->Get(keys::kCommands, &output));
209 } 210 }
210 }; 211 };
211 212
212 } // namespace extensions 213 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698