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/file_util.h"
8 #include "base/json/json_writer.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/test/values_test_util.h"
11 #include "chrome/browser/extensions/extension_creator.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace extensions
{
16 TestExtensionDir::TestExtensionDir() {
17 EXPECT_TRUE(dir_
.CreateUniqueTempDir());
18 EXPECT_TRUE(crx_dir_
.CreateUniqueTempDir());
21 TestExtensionDir::~TestExtensionDir() {
24 void TestExtensionDir::WriteManifest(base::StringPiece manifest
) {
25 // TODO(kalman): Write some more convenient way to specify a manifest than
26 // via JSON, which requires awkwardly escaping all quotes. E.g. add a feature
27 // to JSONReader that can parse '' literals rather than "".
28 WriteFile(FILE_PATH_LITERAL("manifest.json"), manifest
);
31 void TestExtensionDir::WriteFile(const base::FilePath::StringType
& filename
,
32 base::StringPiece contents
) {
34 base::checked_cast
<int>(contents
.size()),
36 dir_
.path().Append(filename
), contents
.data(), contents
.size()));
39 // This function packs the extension into a .crx, and returns the path to that
40 // .crx. Multiple calls to Pack() will produce extensions with the same ID.
41 base::FilePath
TestExtensionDir::Pack() {
42 ExtensionCreator creator
;
43 base::FilePath crx_path
=
44 crx_dir_
.path().Append(FILE_PATH_LITERAL("ext.crx"));
45 base::FilePath pem_path
=
46 crx_dir_
.path().Append(FILE_PATH_LITERAL("ext.pem"));
47 base::FilePath pem_in_path
, pem_out_path
;
48 if (base::PathExists(pem_path
))
49 pem_in_path
= pem_path
;
51 pem_out_path
= pem_path
;
52 if (!creator
.Run(dir_
.path(),
56 ExtensionCreator::kOverwriteCRX
)) {
58 << "ExtensionCreator::Run() failed: " << creator
.error_message();
59 return base::FilePath();
61 if (!base::PathExists(crx_path
)) {
62 ADD_FAILURE() << crx_path
.value() << " was not created.";
63 return base::FilePath();
68 } // namespace extensions