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

Side by Side Diff: chrome/browser/extensions/api/developer_private/extension_info_generator_unittest.cc

Issue 1871713002: Convert //chrome/browser/extensions from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and fix header 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/browser/extensions/api/developer_private/extension_info_generat or.h"
6
5 #include <utility> 7 #include <utility>
6 8
7 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
8 #include "base/json/json_file_value_serializer.h" 10 #include "base/json/json_file_value_serializer.h"
9 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h" 16 #include "build/build_config.h"
14 #include "chrome/browser/extensions/api/developer_private/extension_info_generat or.h"
15 #include "chrome/browser/extensions/api/developer_private/inspectable_views_find er.h" 17 #include "chrome/browser/extensions/api/developer_private/inspectable_views_find er.h"
16 #include "chrome/browser/extensions/error_console/error_console.h" 18 #include "chrome/browser/extensions/error_console/error_console.h"
17 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/extension_service_test_base.h" 20 #include "chrome/browser/extensions/extension_service_test_base.h"
19 #include "chrome/browser/extensions/extension_util.h" 21 #include "chrome/browser/extensions/extension_util.h"
20 #include "chrome/browser/extensions/permissions_updater.h" 22 #include "chrome/browser/extensions/permissions_updater.h"
21 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/common/extensions/api/developer_private.h" 24 #include "chrome/common/extensions/api/developer_private.h"
23 #include "chrome/common/pref_names.h" 25 #include "chrome/common/pref_names.h"
24 #include "components/crx_file/id_util.h" 26 #include "components/crx_file/id_util.h"
25 #include "extensions/browser/extension_registry.h" 27 #include "extensions/browser/extension_registry.h"
26 #include "extensions/common/constants.h" 28 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h" 29 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_builder.h" 30 #include "extensions/common/extension_builder.h"
29 #include "extensions/common/feature_switch.h" 31 #include "extensions/common/feature_switch.h"
30 #include "extensions/common/value_builder.h" 32 #include "extensions/common/value_builder.h"
31 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
32 34
33 namespace extensions { 35 namespace extensions {
34 36
35 namespace developer = api::developer_private; 37 namespace developer = api::developer_private;
36 38
37 namespace { 39 namespace {
38 40
39 const char kAllHostsPermission[] = "*://*/*"; 41 const char kAllHostsPermission[] = "*://*/*";
40 42
41 scoped_ptr<base::DictionaryValue> DeserializeJSONTestData( 43 std::unique_ptr<base::DictionaryValue> DeserializeJSONTestData(
42 const base::FilePath& path, 44 const base::FilePath& path,
43 std::string *error) { 45 std::string* error) {
44 JSONFileValueDeserializer deserializer(path); 46 JSONFileValueDeserializer deserializer(path);
45 return base::DictionaryValue::From(deserializer.Deserialize(nullptr, error)); 47 return base::DictionaryValue::From(deserializer.Deserialize(nullptr, error));
46 } 48 }
47 49
48 } // namespace 50 } // namespace
49 51
50 class ExtensionInfoGeneratorUnitTest : public ExtensionServiceTestBase { 52 class ExtensionInfoGeneratorUnitTest : public ExtensionServiceTestBase {
51 public: 53 public:
52 ExtensionInfoGeneratorUnitTest() {} 54 ExtensionInfoGeneratorUnitTest() {}
53 ~ExtensionInfoGeneratorUnitTest() override {} 55 ~ExtensionInfoGeneratorUnitTest() override {}
54 56
55 protected: 57 protected:
56 void SetUp() override { 58 void SetUp() override {
57 ExtensionServiceTestBase::SetUp(); 59 ExtensionServiceTestBase::SetUp();
58 InitializeEmptyExtensionService(); 60 InitializeEmptyExtensionService();
59 } 61 }
60 62
61 void OnInfosGenerated(scoped_ptr<developer::ExtensionInfo>* info_out, 63 void OnInfosGenerated(std::unique_ptr<developer::ExtensionInfo>* info_out,
62 ExtensionInfoGenerator::ExtensionInfoList list) { 64 ExtensionInfoGenerator::ExtensionInfoList list) {
63 EXPECT_EQ(1u, list.size()); 65 EXPECT_EQ(1u, list.size());
64 if (!list.empty()) 66 if (!list.empty())
65 info_out->reset(new developer::ExtensionInfo(std::move(list[0]))); 67 info_out->reset(new developer::ExtensionInfo(std::move(list[0])));
66 base::ResetAndReturn(&quit_closure_).Run(); 68 base::ResetAndReturn(&quit_closure_).Run();
67 } 69 }
68 70
69 scoped_ptr<developer::ExtensionInfo> GenerateExtensionInfo( 71 std::unique_ptr<developer::ExtensionInfo> GenerateExtensionInfo(
70 const std::string& extension_id) { 72 const std::string& extension_id) {
71 scoped_ptr<developer::ExtensionInfo> info; 73 std::unique_ptr<developer::ExtensionInfo> info;
72 base::RunLoop run_loop; 74 base::RunLoop run_loop;
73 quit_closure_ = run_loop.QuitClosure(); 75 quit_closure_ = run_loop.QuitClosure();
74 scoped_ptr<ExtensionInfoGenerator> generator( 76 std::unique_ptr<ExtensionInfoGenerator> generator(
75 new ExtensionInfoGenerator(browser_context())); 77 new ExtensionInfoGenerator(browser_context()));
76 generator->CreateExtensionInfo( 78 generator->CreateExtensionInfo(
77 extension_id, 79 extension_id,
78 base::Bind(&ExtensionInfoGeneratorUnitTest::OnInfosGenerated, 80 base::Bind(&ExtensionInfoGeneratorUnitTest::OnInfosGenerated,
79 base::Unretained(this), 81 base::Unretained(this),
80 base::Unretained(&info))); 82 base::Unretained(&info)));
81 run_loop.Run(); 83 run_loop.Run();
82 return info; 84 return info;
83 } 85 }
84 86
85 const scoped_refptr<const Extension> CreateExtension( 87 const scoped_refptr<const Extension> CreateExtension(
86 const std::string& name, 88 const std::string& name,
87 scoped_ptr<base::ListValue> permissions) { 89 std::unique_ptr<base::ListValue> permissions) {
88 const std::string kId = crx_file::id_util::GenerateId(name); 90 const std::string kId = crx_file::id_util::GenerateId(name);
89 scoped_refptr<const Extension> extension = 91 scoped_refptr<const Extension> extension =
90 ExtensionBuilder() 92 ExtensionBuilder()
91 .SetManifest(DictionaryBuilder() 93 .SetManifest(DictionaryBuilder()
92 .Set("name", name) 94 .Set("name", name)
93 .Set("description", "an extension") 95 .Set("description", "an extension")
94 .Set("manifest_version", 2) 96 .Set("manifest_version", 2)
95 .Set("version", "1.0.0") 97 .Set("version", "1.0.0")
96 .Set("permissions", std::move(permissions)) 98 .Set("permissions", std::move(permissions))
97 .Build()) 99 .Build())
98 .SetLocation(Manifest::INTERNAL) 100 .SetLocation(Manifest::INTERNAL)
99 .SetID(kId) 101 .SetID(kId)
100 .Build(); 102 .Build();
101 103
102 ExtensionRegistry::Get(profile())->AddEnabled(extension); 104 ExtensionRegistry::Get(profile())->AddEnabled(extension);
103 PermissionsUpdater(profile()).InitializePermissions(extension.get()); 105 PermissionsUpdater(profile()).InitializePermissions(extension.get());
104 return extension; 106 return extension;
105 } 107 }
106 108
107 scoped_ptr<developer::ExtensionInfo> CreateExtensionInfoFromPath( 109 std::unique_ptr<developer::ExtensionInfo> CreateExtensionInfoFromPath(
108 const base::FilePath& extension_path, 110 const base::FilePath& extension_path,
109 Manifest::Location location) { 111 Manifest::Location location) {
110 std::string error; 112 std::string error;
111 113
112 base::FilePath manifest_path = extension_path.Append(kManifestFilename); 114 base::FilePath manifest_path = extension_path.Append(kManifestFilename);
113 scoped_ptr<base::DictionaryValue> extension_data = 115 std::unique_ptr<base::DictionaryValue> extension_data =
114 DeserializeJSONTestData(manifest_path, &error); 116 DeserializeJSONTestData(manifest_path, &error);
115 EXPECT_EQ(std::string(), error); 117 EXPECT_EQ(std::string(), error);
116 118
117 scoped_refptr<Extension> extension(Extension::Create( 119 scoped_refptr<Extension> extension(Extension::Create(
118 extension_path, location, *extension_data, Extension::REQUIRE_KEY, 120 extension_path, location, *extension_data, Extension::REQUIRE_KEY,
119 &error)); 121 &error));
120 CHECK(extension.get()); 122 CHECK(extension.get());
121 service()->AddExtension(extension.get()); 123 service()->AddExtension(extension.get());
122 EXPECT_EQ(std::string(), error); 124 EXPECT_EQ(std::string(), error);
123 125
124 return GenerateExtensionInfo(extension->id()); 126 return GenerateExtensionInfo(extension->id());
125 } 127 }
126 128
127 void CompareExpectedAndActualOutput( 129 void CompareExpectedAndActualOutput(
128 const base::FilePath& extension_path, 130 const base::FilePath& extension_path,
129 InspectableViewsFinder::ViewList views, 131 InspectableViewsFinder::ViewList views,
130 const base::FilePath& expected_output_path) { 132 const base::FilePath& expected_output_path) {
131 std::string error; 133 std::string error;
132 scoped_ptr<base::DictionaryValue> expected_output_data( 134 std::unique_ptr<base::DictionaryValue> expected_output_data(
133 DeserializeJSONTestData(expected_output_path, &error)); 135 DeserializeJSONTestData(expected_output_path, &error));
134 EXPECT_EQ(std::string(), error); 136 EXPECT_EQ(std::string(), error);
135 137
136 // Produce test output. 138 // Produce test output.
137 scoped_ptr<developer::ExtensionInfo> info = 139 std::unique_ptr<developer::ExtensionInfo> info =
138 CreateExtensionInfoFromPath(extension_path, Manifest::INVALID_LOCATION); 140 CreateExtensionInfoFromPath(extension_path, Manifest::INVALID_LOCATION);
139 info->views = std::move(views); 141 info->views = std::move(views);
140 scoped_ptr<base::DictionaryValue> actual_output_data = info->ToValue(); 142 std::unique_ptr<base::DictionaryValue> actual_output_data = info->ToValue();
141 ASSERT_TRUE(actual_output_data); 143 ASSERT_TRUE(actual_output_data);
142 144
143 // Compare the outputs. 145 // Compare the outputs.
144 // Ignore unknown fields in the actual output data. 146 // Ignore unknown fields in the actual output data.
145 std::string paths_details = " - expected (" + 147 std::string paths_details = " - expected (" +
146 expected_output_path.MaybeAsASCII() + ") vs. actual (" + 148 expected_output_path.MaybeAsASCII() + ") vs. actual (" +
147 extension_path.MaybeAsASCII() + ")"; 149 extension_path.MaybeAsASCII() + ")";
148 std::string expected_string; 150 std::string expected_string;
149 std::string actual_string; 151 std::string actual_string;
150 for (base::DictionaryValue::Iterator field(*expected_output_data); 152 for (base::DictionaryValue::Iterator field(*expected_output_data);
(...skipping 23 matching lines...) Expand all
174 TEST_F(ExtensionInfoGeneratorUnitTest, BasicInfoTest) { 176 TEST_F(ExtensionInfoGeneratorUnitTest, BasicInfoTest) {
175 // Enable error console for testing. 177 // Enable error console for testing.
176 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT); 178 ResetThreadBundle(content::TestBrowserThreadBundle::DEFAULT);
177 FeatureSwitch::ScopedOverride error_console_override( 179 FeatureSwitch::ScopedOverride error_console_override(
178 FeatureSwitch::error_console(), true); 180 FeatureSwitch::error_console(), true);
179 profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); 181 profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true);
180 182
181 const char kName[] = "extension name"; 183 const char kName[] = "extension name";
182 const char kVersion[] = "1.0.0.1"; 184 const char kVersion[] = "1.0.0.1";
183 std::string id = crx_file::id_util::GenerateId("alpha"); 185 std::string id = crx_file::id_util::GenerateId("alpha");
184 scoped_ptr<base::DictionaryValue> manifest = 186 std::unique_ptr<base::DictionaryValue> manifest =
185 DictionaryBuilder() 187 DictionaryBuilder()
186 .Set("name", kName) 188 .Set("name", kName)
187 .Set("version", kVersion) 189 .Set("version", kVersion)
188 .Set("manifest_version", 2) 190 .Set("manifest_version", 2)
189 .Set("description", "an extension") 191 .Set("description", "an extension")
190 .Set("permissions", ListBuilder().Append("file://*/*").Build()) 192 .Set("permissions", ListBuilder().Append("file://*/*").Build())
191 .Build(); 193 .Build();
192 scoped_ptr<base::DictionaryValue> manifest_copy(manifest->DeepCopy()); 194 std::unique_ptr<base::DictionaryValue> manifest_copy(manifest->DeepCopy());
193 scoped_refptr<const Extension> extension = 195 scoped_refptr<const Extension> extension =
194 ExtensionBuilder() 196 ExtensionBuilder()
195 .SetManifest(std::move(manifest)) 197 .SetManifest(std::move(manifest))
196 .SetLocation(Manifest::UNPACKED) 198 .SetLocation(Manifest::UNPACKED)
197 .SetPath(data_dir()) 199 .SetPath(data_dir())
198 .SetID(id) 200 .SetID(id)
199 .Build(); 201 .Build();
200 service()->AddExtension(extension.get()); 202 service()->AddExtension(extension.get());
201 ErrorConsole* error_console = ErrorConsole::Get(profile()); 203 ErrorConsole* error_console = ErrorConsole::Get(profile());
202 error_console->ReportError( 204 error_console->ReportError(base::WrapUnique(new RuntimeError(
203 make_scoped_ptr(new RuntimeError( 205 extension->id(), false, base::UTF8ToUTF16("source"),
204 extension->id(), 206 base::UTF8ToUTF16("message"),
205 false, 207 StackTrace(1, StackFrame(1, 1, base::UTF8ToUTF16("source"),
206 base::UTF8ToUTF16("source"), 208 base::UTF8ToUTF16("function"))),
207 base::UTF8ToUTF16("message"), 209 GURL("url"), logging::LOG_ERROR, 1, 1)));
208 StackTrace(1, StackFrame(1, 210 error_console->ReportError(base::WrapUnique(
209 1, 211 new ManifestError(extension->id(), base::UTF8ToUTF16("message"),
210 base::UTF8ToUTF16("source"), 212 base::UTF8ToUTF16("key"), base::string16())));
211 base::UTF8ToUTF16("function"))), 213 error_console->ReportError(base::WrapUnique(new RuntimeError(
212 GURL("url"), 214 extension->id(), false, base::UTF8ToUTF16("source"),
213 logging::LOG_ERROR, 215 base::UTF8ToUTF16("message"),
214 1, 216 StackTrace(1, StackFrame(1, 1, base::UTF8ToUTF16("source"),
215 1))); 217 base::UTF8ToUTF16("function"))),
216 error_console->ReportError( 218 GURL("url"), logging::LOG_VERBOSE, 1, 1)));
217 make_scoped_ptr(new ManifestError(extension->id(),
218 base::UTF8ToUTF16("message"),
219 base::UTF8ToUTF16("key"),
220 base::string16())));
221 error_console->ReportError(
222 make_scoped_ptr(new RuntimeError(
223 extension->id(),
224 false,
225 base::UTF8ToUTF16("source"),
226 base::UTF8ToUTF16("message"),
227 StackTrace(1, StackFrame(1,
228 1,
229 base::UTF8ToUTF16("source"),
230 base::UTF8ToUTF16("function"))),
231 GURL("url"),
232 logging::LOG_VERBOSE,
233 1,
234 1)));
235 219
236 // It's not feasible to validate every field here, because that would be 220 // It's not feasible to validate every field here, because that would be
237 // a duplication of the logic in the method itself. Instead, test a handful 221 // a duplication of the logic in the method itself. Instead, test a handful
238 // of fields for sanity. 222 // of fields for sanity.
239 scoped_ptr<api::developer_private::ExtensionInfo> info = 223 std::unique_ptr<api::developer_private::ExtensionInfo> info =
240 GenerateExtensionInfo(extension->id()); 224 GenerateExtensionInfo(extension->id());
241 ASSERT_TRUE(info.get()); 225 ASSERT_TRUE(info.get());
242 EXPECT_EQ(kName, info->name); 226 EXPECT_EQ(kName, info->name);
243 EXPECT_EQ(id, info->id); 227 EXPECT_EQ(id, info->id);
244 EXPECT_EQ(kVersion, info->version); 228 EXPECT_EQ(kVersion, info->version);
245 EXPECT_EQ(info->location, developer::LOCATION_UNPACKED); 229 EXPECT_EQ(info->location, developer::LOCATION_UNPACKED);
246 ASSERT_TRUE(info->path); 230 ASSERT_TRUE(info->path);
247 EXPECT_EQ(data_dir(), base::FilePath::FromUTF8Unsafe(*info->path)); 231 EXPECT_EQ(data_dir(), base::FilePath::FromUTF8Unsafe(*info->path));
248 EXPECT_EQ(api::developer_private::EXTENSION_STATE_ENABLED, info->state); 232 EXPECT_EQ(api::developer_private::EXTENSION_STATE_ENABLED, info->state);
249 EXPECT_EQ(api::developer_private::EXTENSION_TYPE_EXTENSION, info->type); 233 EXPECT_EQ(api::developer_private::EXTENSION_TYPE_EXTENSION, info->type);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 CompareExpectedAndActualOutput(extension_path, 328 CompareExpectedAndActualOutput(extension_path,
345 InspectableViewsFinder::ViewList(), 329 InspectableViewsFinder::ViewList(),
346 expected_outputs_path.AppendASCII( 330 expected_outputs_path.AppendASCII(
347 "bjafgdebaacbbbecmhlhpofkepfkgcpa.json")); 331 "bjafgdebaacbbbecmhlhpofkepfkgcpa.json"));
348 } 332 }
349 333
350 // Test that the all_urls checkbox only shows up for extensions that want all 334 // Test that the all_urls checkbox only shows up for extensions that want all
351 // urls, and only when the switch is on. 335 // urls, and only when the switch is on.
352 TEST_F(ExtensionInfoGeneratorUnitTest, ExtensionInfoRunOnAllUrls) { 336 TEST_F(ExtensionInfoGeneratorUnitTest, ExtensionInfoRunOnAllUrls) {
353 // Start with the switch enabled. 337 // Start with the switch enabled.
354 scoped_ptr<FeatureSwitch::ScopedOverride> enable_scripts_switch( 338 std::unique_ptr<FeatureSwitch::ScopedOverride> enable_scripts_switch(
355 new FeatureSwitch::ScopedOverride( 339 new FeatureSwitch::ScopedOverride(FeatureSwitch::scripts_require_action(),
356 FeatureSwitch::scripts_require_action(), true)); 340 true));
357 // Two extensions - one with all urls, one without. 341 // Two extensions - one with all urls, one without.
358 scoped_refptr<const Extension> all_urls_extension = CreateExtension( 342 scoped_refptr<const Extension> all_urls_extension = CreateExtension(
359 "all_urls", ListBuilder().Append(kAllHostsPermission).Build()); 343 "all_urls", ListBuilder().Append(kAllHostsPermission).Build());
360 scoped_refptr<const Extension> no_urls_extension = 344 scoped_refptr<const Extension> no_urls_extension =
361 CreateExtension("no urls", ListBuilder().Build()); 345 CreateExtension("no urls", ListBuilder().Build());
362 346
363 scoped_ptr<developer::ExtensionInfo> info = 347 std::unique_ptr<developer::ExtensionInfo> info =
364 GenerateExtensionInfo(all_urls_extension->id()); 348 GenerateExtensionInfo(all_urls_extension->id());
365 349
366 // The extension should want all urls, but not currently have it. 350 // The extension should want all urls, but not currently have it.
367 EXPECT_TRUE(info->run_on_all_urls.is_enabled); 351 EXPECT_TRUE(info->run_on_all_urls.is_enabled);
368 EXPECT_FALSE(info->run_on_all_urls.is_active); 352 EXPECT_FALSE(info->run_on_all_urls.is_active);
369 353
370 // Give the extension all urls. 354 // Give the extension all urls.
371 util::SetAllowedScriptingOnAllUrls(all_urls_extension->id(), profile(), true); 355 util::SetAllowedScriptingOnAllUrls(all_urls_extension->id(), profile(), true);
372 356
373 // Now the extension should both want and have all urls. 357 // Now the extension should both want and have all urls.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 "all_urls_II", ListBuilder().Append(kAllHostsPermission).Build()); 390 "all_urls_II", ListBuilder().Append(kAllHostsPermission).Build());
407 391
408 // Even though the extension has all_urls permission, the checkbox shouldn't 392 // Even though the extension has all_urls permission, the checkbox shouldn't
409 // show up without the switch. 393 // show up without the switch.
410 info = GenerateExtensionInfo(all_urls_extension->id()); 394 info = GenerateExtensionInfo(all_urls_extension->id());
411 EXPECT_FALSE(info->run_on_all_urls.is_enabled); 395 EXPECT_FALSE(info->run_on_all_urls.is_enabled);
412 EXPECT_TRUE(info->run_on_all_urls.is_active); 396 EXPECT_TRUE(info->run_on_all_urls.is_active);
413 } 397 }
414 398
415 } // namespace extensions 399 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698