1 // Copyright 2013 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.
5 #include "chrome/browser/extensions/test_extension_dir.h"
7 #include "base/files/file_util.h"
8 #include "base/json/json_writer.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/test/values_test_util.h"
12 #include "chrome/browser/extensions/extension_creator.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
17 TestExtensionDir::TestExtensionDir() {
18 EXPECT_TRUE(dir_
.CreateUniqueTempDir());
19 EXPECT_TRUE(crx_dir_
.CreateUniqueTempDir());
22 TestExtensionDir::~TestExtensionDir() {
25 void TestExtensionDir::WriteManifest(base::StringPiece manifest
) {
26 WriteFile(FILE_PATH_LITERAL("manifest.json"), manifest
);
29 void TestExtensionDir::WriteManifestWithSingleQuotes(
30 base::StringPiece manifest
) {
31 std::string double_quotes
;
32 base::ReplaceChars(manifest
.data(), "'", "\"", &double_quotes
);
33 WriteManifest(double_quotes
);
36 void TestExtensionDir::WriteFile(const base::FilePath::StringType
& filename
,
37 base::StringPiece contents
) {
39 base::checked_cast
<int>(contents
.size()),
41 dir_
.path().Append(filename
), contents
.data(), contents
.size()));
44 // This function packs the extension into a .crx, and returns the path to that
45 // .crx. Multiple calls to Pack() will produce extensions with the same ID.
46 base::FilePath
TestExtensionDir::Pack() {
47 ExtensionCreator creator
;
48 base::FilePath crx_path
=
49 crx_dir_
.path().Append(FILE_PATH_LITERAL("ext.crx"));
50 base::FilePath pem_path
=
51 crx_dir_
.path().Append(FILE_PATH_LITERAL("ext.pem"));
52 base::FilePath pem_in_path
, pem_out_path
;
53 if (base::PathExists(pem_path
))
54 pem_in_path
= pem_path
;
56 pem_out_path
= pem_path
;
57 if (!creator
.Run(dir_
.path(),
61 ExtensionCreator::kOverwriteCRX
)) {
63 << "ExtensionCreator::Run() failed: " << creator
.error_message();
64 return base::FilePath();
66 if (!base::PathExists(crx_path
)) {
67 ADD_FAILURE() << crx_path
.value() << " was not created.";
68 return base::FilePath();
73 } // namespace extensions