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. 4 5 #include "chrome/common/extensions/extension_test_util.h" 6 7 #include "base/files/file_path.h" 8 #include "base/json/json_file_value_serializer.h" 9 #include "base/path_service.h" 10 #include "base/values.h" 11 #include "chrome/common/chrome_paths.h" 12 #include "extensions/common/extension.h" 13 #include "extensions/common/extension_builder.h" 14 #include "extensions/common/manifest_constants.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 using extensions::DictionaryBuilder; 18 using extensions::Extension; 19 using extensions::ExtensionBuilder; 20 using extensions::ListBuilder; 21 using extensions::Manifest; 22 23 namespace extensions { 24 namespace extension_test_util { 25 26 ExtensionBuilder& BuildExtension(ExtensionBuilder& builder) { 27 return builder 28 .SetManifest(DictionaryBuilder() 29 .Set("name", "Test extension") 30 .Set("version", "1.0") 31 .Set("manifest_version", 2)); 32 } 33 34 ExtensionBuilder& BuildExtensionWithPermissions(ExtensionBuilder& builder, 35 ListBuilder& permissions) { 36 return 37 BuildExtension(builder) 38 .MergeManifest( 39 DictionaryBuilder().Set("permissions", permissions)); 40 } 41 42 } // namespace extension_test_util 43 } // namespace extensions 44 45 namespace extension_test_util { 46 47 scoped_refptr<Extension> CreateExtensionWithID(std::string id) { 48 base::DictionaryValue values; 49 values.SetString(extensions::manifest_keys::kName, "test"); 50 values.SetString(extensions::manifest_keys::kVersion, "0.1"); 51 std::string error; 52 return Extension::Create(base::FilePath(), extensions::Manifest::INTERNAL, 53 values, Extension::NO_FLAGS, id, &error); 54 } 55 56 scoped_refptr<Extension> LoadManifestUnchecked(const std::string& dir, 57 const std::string& test_file, 58 Manifest::Location location, 59 int extra_flags, 60 const std::string& id, 61 std::string* error) { 62 base::FilePath path; 63 PathService::Get(chrome::DIR_TEST_DATA, &path); 64 path = path.AppendASCII("extensions") 65 .AppendASCII(dir) 66 .AppendASCII(test_file); 67 68 JSONFileValueSerializer serializer(path); 69 scoped_ptr<base::Value> result(serializer.Deserialize(NULL, error)); 70 if (!result) 71 return NULL; 72 const base::DictionaryValue* dict; 73 CHECK(result->GetAsDictionary(&dict)); 74 75 scoped_refptr<Extension> extension = Extension::Create( 76 path.DirName(), location, *dict, extra_flags, id, error); 77 return extension; 78 } 79 80 scoped_refptr<Extension> LoadManifestUnchecked(const std::string& dir, 81 const std::string& test_file, 82 Manifest::Location location, 83 int extra_flags, 84 std::string* error) { 85 return LoadManifestUnchecked( 86 dir, test_file, location, extra_flags, std::string(), error); 87 } 88 89 scoped_refptr<Extension> LoadManifest(const std::string& dir, 90 const std::string& test_file, 91 Manifest::Location location, 92 int extra_flags) { 93 std::string error; 94 scoped_refptr<Extension> extension = 95 LoadManifestUnchecked(dir, test_file, location, extra_flags, &error); 96 97 EXPECT_TRUE(extension.get()) << test_file << ":" << error; 98 return extension; 99 } 100 101 scoped_refptr<Extension> LoadManifest(const std::string& dir, 102 const std::string& test_file, 103 int extra_flags) { 104 return LoadManifest(dir, test_file, Manifest::INVALID_LOCATION, extra_flags); 105 } 106 107 scoped_refptr<Extension> LoadManifestStrict(const std::string& dir, 108 const std::string& test_file) { 109 return LoadManifest(dir, test_file, Extension::NO_FLAGS); 110 } 111 112 scoped_refptr<Extension> LoadManifest(const std::string& dir, 113 const std::string& test_file) { 114 return LoadManifest(dir, test_file, Extension::NO_FLAGS); 115 } 116 117 } // namespace extension_test_util 118