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

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

Issue 9150008: Introduce background.scripts feature for extension manifests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ready to land Created 8 years, 11 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/extension_file_util.h" 5 #include "chrome/common/extensions/extension_file_util.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_value_serializer.h" 8 #include "base/json/json_value_serializer.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h" 10 #include "base/scoped_temp_dir.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/extensions/extension.h" 14 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_constants.h" 15 #include "chrome/common/extensions/extension_constants.h"
16 #include "grit/generated_resources.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/base/l10n/l10n_util.h"
17 19
18 namespace keys = extension_manifest_keys; 20 namespace keys = extension_manifest_keys;
19 21
20 #if defined(OS_WIN) 22 #if defined(OS_WIN)
21 // http://crbug.com/106381 23 // http://crbug.com/106381
22 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect 24 #define InstallUninstallGarbageCollect DISABLED_InstallUninstallGarbageCollect
23 #endif 25 #endif
24 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) { 26 TEST(ExtensionFileUtil, InstallUninstallGarbageCollect) {
25 ScopedTempDir temp; 27 ScopedTempDir temp;
26 ASSERT_TRUE(temp.CreateUniqueTempDir()); 28 ASSERT_TRUE(temp.CreateUniqueTempDir());
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 FilePath actual_path = 255 FilePath actual_path =
254 extension_file_util::ExtensionURLToRelativeFilePath(url); 256 extension_file_util::ExtensionURLToRelativeFilePath(url);
255 EXPECT_FALSE(actual_path.IsAbsolute()) << 257 EXPECT_FALSE(actual_path.IsAbsolute()) <<
256 " For the path " << actual_path.value(); 258 " For the path " << actual_path.value();
257 EXPECT_EQ(expected_path.value(), actual_path.value()) << 259 EXPECT_EQ(expected_path.value(), actual_path.value()) <<
258 " For the path " << url; 260 " For the path " << url;
259 } 261 }
260 } 262 }
261 263
262 static scoped_refptr<Extension> LoadExtensionManifest( 264 static scoped_refptr<Extension> LoadExtensionManifest(
265 DictionaryValue* manifest,
266 const FilePath& manifest_dir,
267 Extension::Location location,
268 int extra_flags,
269 std::string* error) {
270 scoped_refptr<Extension> extension = Extension::Create(
271 manifest_dir, location, *manifest, extra_flags, error);
272 return extension;
273 }
274
275 static scoped_refptr<Extension> LoadExtensionManifest(
263 const std::string& manifest_value, 276 const std::string& manifest_value,
264 const FilePath& manifest_dir, 277 const FilePath& manifest_dir,
265 Extension::Location location, 278 Extension::Location location,
266 int extra_flags, 279 int extra_flags,
267 std::string* error) { 280 std::string* error) {
268 JSONStringValueSerializer serializer(manifest_value); 281 JSONStringValueSerializer serializer(manifest_value);
269 scoped_ptr<Value> result(serializer.Deserialize(NULL, error)); 282 Value* result = serializer.Deserialize(NULL, error);
270 if (!result.get()) 283 if (!result)
271 return NULL; 284 return NULL;
272 285 CHECK_EQ(Value::TYPE_DICTIONARY, result->GetType());
273 scoped_refptr<Extension> extension = Extension::Create( 286 return LoadExtensionManifest(static_cast<DictionaryValue*>(result),
274 manifest_dir, location, *static_cast<DictionaryValue*>(result.get()), 287 manifest_dir,
275 extra_flags, error); 288 location,
276 return extension; 289 extra_flags,
290 error);
277 } 291 }
278 292
279 #if defined(OS_WIN) 293 #if defined(OS_WIN)
280 // http://crbug.com/108279 294 // http://crbug.com/108279
281 #define ValidateThemeUTF8 DISABLED_ValidateThemeUTF8 295 #define ValidateThemeUTF8 DISABLED_ValidateThemeUTF8
282 #endif 296 #endif
283 TEST(ExtensionFileUtil, ValidateThemeUTF8) { 297 TEST(ExtensionFileUtil, ValidateThemeUTF8) {
284 ScopedTempDir temp; 298 ScopedTempDir temp;
285 ASSERT_TRUE(temp.CreateUniqueTempDir()); 299 ASSERT_TRUE(temp.CreateUniqueTempDir());
286 300
(...skipping 10 matching lines...) Expand all
297 "}", non_ascii_file.c_str()); 311 "}", non_ascii_file.c_str());
298 std::string error; 312 std::string error;
299 scoped_refptr<Extension> extension = LoadExtensionManifest( 313 scoped_refptr<Extension> extension = LoadExtensionManifest(
300 kManifest, temp.path(), Extension::LOAD, 0, &error); 314 kManifest, temp.path(), Extension::LOAD, 0, &error);
301 ASSERT_TRUE(extension.get()) << error; 315 ASSERT_TRUE(extension.get()) << error;
302 316
303 EXPECT_TRUE(extension_file_util::ValidateExtension(extension, &error)) << 317 EXPECT_TRUE(extension_file_util::ValidateExtension(extension, &error)) <<
304 error; 318 error;
305 } 319 }
306 320
321 TEST(ExtensionFileUtil, BackgroundScriptsMustExist) {
322 ScopedTempDir temp;
323 ASSERT_TRUE(temp.CreateUniqueTempDir());
324
325 scoped_ptr<DictionaryValue> value(new DictionaryValue());
326 value->SetString("name", "test");
327 value->SetString("version", "1");
328 value->SetInteger("manifest_version", 1);
329
330 ListValue* scripts = new ListValue();
331 scripts->Append(Value::CreateStringValue("foo.js"));
332 value->Set("background.scripts", scripts);
333
334 std::string error;
335 scoped_refptr<Extension> extension = LoadExtensionManifest(
336 value.get(), temp.path(), Extension::LOAD, 0, &error);
337 ASSERT_TRUE(extension.get()) << error;
338
339 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error));
340 EXPECT_EQ(l10n_util::GetStringFUTF8(
341 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, ASCIIToUTF16("foo.js")),
342 error);
343
344 scripts->Clear();
345 scripts->Append(Value::CreateStringValue("http://google.com/foo.js"));
346
347 extension = LoadExtensionManifest(value.get(), temp.path(), Extension::LOAD,
348 0, &error);
349 ASSERT_TRUE(extension.get()) << error;
350
351 EXPECT_FALSE(extension_file_util::ValidateExtension(extension, &error));
352 EXPECT_EQ(l10n_util::GetStringFUTF8(
353 IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED,
354 ASCIIToUTF16("http://google.com/foo.js")),
355 error);
356 }
357
307 // TODO(aa): More tests as motivation allows. Maybe steal some from 358 // TODO(aa): More tests as motivation allows. Maybe steal some from
308 // ExtensionService? Many of them could probably be tested here without the 359 // ExtensionService? Many of them could probably be tested here without the
309 // MessageLoop shenanigans. 360 // MessageLoop shenanigans.
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension_file_util.cc ('k') | chrome/common/extensions/extension_manifests_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698