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

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

Issue 1880143002: Convert chrome/common to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 8 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
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 "extensions/common/manifest.h" 5 #include "extensions/common/manifest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <memory>
8 #include <set> 9 #include <set>
9 #include <string> 10 #include <string>
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "extensions/common/error_utils.h" 15 #include "extensions/common/error_utils.h"
16 #include "extensions/common/features/feature.h" 16 #include "extensions/common/features/feature.h"
17 #include "extensions/common/features/simple_feature.h" 17 #include "extensions/common/features/simple_feature.h"
18 #include "extensions/common/install_warning.h" 18 #include "extensions/common/install_warning.h"
19 #include "extensions/common/manifest_constants.h" 19 #include "extensions/common/manifest_constants.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace extensions { 22 namespace extensions {
(...skipping 15 matching lines...) Expand all
38 EXPECT_EQ(type == Manifest::TYPE_LEGACY_PACKAGED_APP, 38 EXPECT_EQ(type == Manifest::TYPE_LEGACY_PACKAGED_APP,
39 manifest->is_legacy_packaged_app()); 39 manifest->is_legacy_packaged_app());
40 EXPECT_EQ(type == Manifest::TYPE_HOSTED_APP, manifest->is_hosted_app()); 40 EXPECT_EQ(type == Manifest::TYPE_HOSTED_APP, manifest->is_hosted_app());
41 EXPECT_EQ(type == Manifest::TYPE_SHARED_MODULE, 41 EXPECT_EQ(type == Manifest::TYPE_SHARED_MODULE,
42 manifest->is_shared_module()); 42 manifest->is_shared_module());
43 } 43 }
44 44
45 // Helper function that replaces the Manifest held by |manifest| with a copy 45 // Helper function that replaces the Manifest held by |manifest| with a copy
46 // with its |key| changed to |value|. If |value| is NULL, then |key| will 46 // with its |key| changed to |value|. If |value| is NULL, then |key| will
47 // instead be deleted. 47 // instead be deleted.
48 void MutateManifest(scoped_ptr<Manifest>* manifest, 48 void MutateManifest(std::unique_ptr<Manifest>* manifest,
49 const std::string& key, 49 const std::string& key,
50 base::Value* value) { 50 base::Value* value) {
51 scoped_ptr<base::DictionaryValue> manifest_value( 51 std::unique_ptr<base::DictionaryValue> manifest_value(
52 manifest->get()->value()->DeepCopy()); 52 manifest->get()->value()->DeepCopy());
53 if (value) 53 if (value)
54 manifest_value->Set(key, value); 54 manifest_value->Set(key, value);
55 else 55 else
56 manifest_value->Remove(key, NULL); 56 manifest_value->Remove(key, NULL);
57 manifest->reset( 57 manifest->reset(
58 new Manifest(Manifest::INTERNAL, std::move(manifest_value))); 58 new Manifest(Manifest::INTERNAL, std::move(manifest_value)));
59 } 59 }
60 60
61 std::string default_value_; 61 std::string default_value_;
62 }; 62 };
63 63
64 // Verifies that extensions can access the correct keys. 64 // Verifies that extensions can access the correct keys.
65 TEST_F(ManifestUnitTest, Extension) { 65 TEST_F(ManifestUnitTest, Extension) {
66 scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue()); 66 std::unique_ptr<base::DictionaryValue> manifest_value(
67 new base::DictionaryValue());
67 manifest_value->SetString(keys::kName, "extension"); 68 manifest_value->SetString(keys::kName, "extension");
68 manifest_value->SetString(keys::kVersion, "1"); 69 manifest_value->SetString(keys::kVersion, "1");
69 // Only supported in manifest_version=1. 70 // Only supported in manifest_version=1.
70 manifest_value->SetString(keys::kBackgroundPageLegacy, "bg.html"); 71 manifest_value->SetString(keys::kBackgroundPageLegacy, "bg.html");
71 manifest_value->SetString("unknown_key", "foo"); 72 manifest_value->SetString("unknown_key", "foo");
72 73
73 scoped_ptr<Manifest> manifest( 74 std::unique_ptr<Manifest> manifest(
74 new Manifest(Manifest::INTERNAL, std::move(manifest_value))); 75 new Manifest(Manifest::INTERNAL, std::move(manifest_value)));
75 std::string error; 76 std::string error;
76 std::vector<InstallWarning> warnings; 77 std::vector<InstallWarning> warnings;
77 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); 78 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings));
78 EXPECT_TRUE(error.empty()); 79 EXPECT_TRUE(error.empty());
79 ASSERT_EQ(1u, warnings.size()); 80 ASSERT_EQ(1u, warnings.size());
80 AssertType(manifest.get(), Manifest::TYPE_EXTENSION); 81 AssertType(manifest.get(), Manifest::TYPE_EXTENSION);
81 82
82 // The known key 'background_page' should be accessible. 83 // The known key 'background_page' should be accessible.
83 std::string value; 84 std::string value;
(...skipping 20 matching lines...) Expand all
104 { 105 {
105 SimpleFeature feature; 106 SimpleFeature feature;
106 feature.set_name("background_page"); 107 feature.set_name("background_page");
107 feature.set_max_manifest_version(1); 108 feature.set_max_manifest_version(1);
108 EXPECT_EQ( 109 EXPECT_EQ(
109 "'background_page' requires manifest version of 1 or lower.", 110 "'background_page' requires manifest version of 1 or lower.",
110 warnings[0].message); 111 warnings[0].message);
111 } 112 }
112 113
113 // Test DeepCopy and Equals. 114 // Test DeepCopy and Equals.
114 scoped_ptr<Manifest> manifest2(manifest->DeepCopy()); 115 std::unique_ptr<Manifest> manifest2(manifest->DeepCopy());
115 EXPECT_TRUE(manifest->Equals(manifest2.get())); 116 EXPECT_TRUE(manifest->Equals(manifest2.get()));
116 EXPECT_TRUE(manifest2->Equals(manifest.get())); 117 EXPECT_TRUE(manifest2->Equals(manifest.get()));
117 MutateManifest( 118 MutateManifest(
118 &manifest, "foo", new base::StringValue("blah")); 119 &manifest, "foo", new base::StringValue("blah"));
119 EXPECT_FALSE(manifest->Equals(manifest2.get())); 120 EXPECT_FALSE(manifest->Equals(manifest2.get()));
120 } 121 }
121 122
122 // Verifies that key restriction based on type works. 123 // Verifies that key restriction based on type works.
123 TEST_F(ManifestUnitTest, ExtensionTypes) { 124 TEST_F(ManifestUnitTest, ExtensionTypes) {
124 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 125 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue());
125 value->SetString(keys::kName, "extension"); 126 value->SetString(keys::kName, "extension");
126 value->SetString(keys::kVersion, "1"); 127 value->SetString(keys::kVersion, "1");
127 128
128 scoped_ptr<Manifest> manifest( 129 std::unique_ptr<Manifest> manifest(
129 new Manifest(Manifest::INTERNAL, std::move(value))); 130 new Manifest(Manifest::INTERNAL, std::move(value)));
130 std::string error; 131 std::string error;
131 std::vector<InstallWarning> warnings; 132 std::vector<InstallWarning> warnings;
132 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); 133 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings));
133 EXPECT_TRUE(error.empty()); 134 EXPECT_TRUE(error.empty());
134 EXPECT_TRUE(warnings.empty()); 135 EXPECT_TRUE(warnings.empty());
135 136
136 // By default, the type is Extension. 137 // By default, the type is Extension.
137 AssertType(manifest.get(), Manifest::TYPE_EXTENSION); 138 AssertType(manifest.get(), Manifest::TYPE_EXTENSION);
138 139
(...skipping 27 matching lines...) Expand all
166 MutateManifest( 167 MutateManifest(
167 &manifest, keys::kWebURLs, new base::ListValue()); 168 &manifest, keys::kWebURLs, new base::ListValue());
168 AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP); 169 AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP);
169 MutateManifest( 170 MutateManifest(
170 &manifest, keys::kWebURLs, NULL); 171 &manifest, keys::kWebURLs, NULL);
171 MutateManifest( 172 MutateManifest(
172 &manifest, keys::kLaunchWebURL, new base::StringValue("foo")); 173 &manifest, keys::kLaunchWebURL, new base::StringValue("foo"));
173 AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP); 174 AssertType(manifest.get(), Manifest::TYPE_HOSTED_APP);
174 MutateManifest( 175 MutateManifest(
175 &manifest, keys::kLaunchWebURL, NULL); 176 &manifest, keys::kLaunchWebURL, NULL);
176 }; 177 }
177 178
178 // Verifies that the getters filter restricted keys. 179 // Verifies that the getters filter restricted keys.
179 TEST_F(ManifestUnitTest, RestrictedKeys) { 180 TEST_F(ManifestUnitTest, RestrictedKeys) {
180 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); 181 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue());
181 value->SetString(keys::kName, "extension"); 182 value->SetString(keys::kName, "extension");
182 value->SetString(keys::kVersion, "1"); 183 value->SetString(keys::kVersion, "1");
183 184
184 scoped_ptr<Manifest> manifest( 185 std::unique_ptr<Manifest> manifest(
185 new Manifest(Manifest::INTERNAL, std::move(value))); 186 new Manifest(Manifest::INTERNAL, std::move(value)));
186 std::string error; 187 std::string error;
187 std::vector<InstallWarning> warnings; 188 std::vector<InstallWarning> warnings;
188 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); 189 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings));
189 EXPECT_TRUE(error.empty()); 190 EXPECT_TRUE(error.empty());
190 EXPECT_TRUE(warnings.empty()); 191 EXPECT_TRUE(warnings.empty());
191 192
192 // "Commands" requires manifest version 2. 193 // "Commands" requires manifest version 2.
193 const base::Value* output = NULL; 194 const base::Value* output = NULL;
194 MutateManifest( 195 MutateManifest(
(...skipping 17 matching lines...) Expand all
212 &manifest, keys::kPlatformAppBackground, new base::DictionaryValue()); 213 &manifest, keys::kPlatformAppBackground, new base::DictionaryValue());
213 AssertType(manifest.get(), Manifest::TYPE_PLATFORM_APP); 214 AssertType(manifest.get(), Manifest::TYPE_PLATFORM_APP);
214 EXPECT_FALSE(manifest->HasKey(keys::kPageAction)); 215 EXPECT_FALSE(manifest->HasKey(keys::kPageAction));
215 EXPECT_FALSE(manifest->Get(keys::kPageAction, &output)); 216 EXPECT_FALSE(manifest->Get(keys::kPageAction, &output));
216 MutateManifest( 217 MutateManifest(
217 &manifest, keys::kPlatformAppBackground, NULL); 218 &manifest, keys::kPlatformAppBackground, NULL);
218 219
219 // Platform apps also can't have a "Commands" key. 220 // Platform apps also can't have a "Commands" key.
220 EXPECT_FALSE(manifest->HasKey(keys::kCommands)); 221 EXPECT_FALSE(manifest->HasKey(keys::kCommands));
221 EXPECT_FALSE(manifest->Get(keys::kCommands, &output)); 222 EXPECT_FALSE(manifest->Get(keys::kCommands, &output));
222 }; 223 }
223 224
224 } // namespace extensions 225 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698