blob: 61403b7b0a6d7d1683b1a1ca57608e8b610e4d80 [file] [log] [blame]
Ryan Mitchell479fa392019-01-02 17:15:39 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef AAPT_TEST_FIXTURE_H
18#define AAPT_TEST_FIXTURE_H
19
20#include "android-base/file.h"
21#include "android-base/macros.h"
22#include "androidfw/StringPiece.h"
23#include "gmock/gmock.h"
24#include "gtest/gtest.h"
25
26#include "io/Util.h"
27#include "util/Files.h"
28#include "LoadedApk.h"
29
30namespace aapt {
31
32class TestDirectoryFixture : public ::testing::Test {
33 public:
34 TestDirectoryFixture() = default;
Ryan Mitchell5855de72021-02-24 14:39:13 -080035 ~TestDirectoryFixture() override = default;
Ryan Mitchell479fa392019-01-02 17:15:39 -080036
37 // Creates the test directory or clears its contents if it contains previously created files.
38 void SetUp() override;
39
40 // Clears the contents of the test directory.
41 void TearDown() override;
42
43 // Retrieve the test directory of the fixture.
Ryan Mitchell5855de72021-02-24 14:39:13 -080044 android::StringPiece GetTestDirectory() {
Ryan Mitchell479fa392019-01-02 17:15:39 -080045 return temp_dir_;
46 }
47
48 // Retrieves the absolute path of the specified relative path in the test directory. Directories
49 // should be separated using forward slashes ('/'), and these slashes will be translated to
50 // backslashes when running Windows tests.
Ryan Mitchell5855de72021-02-24 14:39:13 -080051 std::string GetTestPath(const android::StringPiece& path) {
Ryan Mitchell479fa392019-01-02 17:15:39 -080052 std::string base = temp_dir_;
53 for (android::StringPiece part : util::Split(path, '/')) {
54 file::AppendPath(&base, part);
55 }
56 return base;
57 }
58
59 // Creates a file with the specified contents, creates any intermediate directories in the
60 // process. The file path must be an absolute path within the test directory.
Ryan Mitchell81dfca02019-06-07 10:20:27 -070061 void WriteFile(const std::string& path, const std::string& contents);
Ryan Mitchell479fa392019-01-02 17:15:39 -080062
63 private:
64 std::string temp_dir_;
65 DISALLOW_COPY_AND_ASSIGN(TestDirectoryFixture);
66};
67
68class CommandTestFixture : public TestDirectoryFixture {
69 public:
70 CommandTestFixture() = default;
Ryan Mitchell5855de72021-02-24 14:39:13 -080071 ~CommandTestFixture() override = default;
Ryan Mitchell479fa392019-01-02 17:15:39 -080072
73 // Wries the contents of the file to the specified path. The file is compiled and the flattened
74 // file is written to the out directory.
75 bool CompileFile(const std::string& path, const std::string& contents,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000076 const android::StringPiece& flat_out_dir, android::IDiagnostics* diag);
Ryan Mitchell479fa392019-01-02 17:15:39 -080077
Ryan Mitchell81dfca02019-06-07 10:20:27 -070078 // Executes the link command with the specified arguments.
Jeremy Meyer56f36e82022-05-20 20:35:42 +000079 bool Link(const std::vector<std::string>& args, android::IDiagnostics* diag);
Ryan Mitchell81dfca02019-06-07 10:20:27 -070080
Ryan Mitchell479fa392019-01-02 17:15:39 -080081 // Executes the link command with the specified arguments. The flattened files residing in the
82 // flat directory will be added to the link command as file arguments.
83 bool Link(const std::vector<std::string>& args, const android::StringPiece& flat_dir,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000084 android::IDiagnostics* diag);
Ryan Mitchell479fa392019-01-02 17:15:39 -080085
86 // Creates a minimal android manifest within the test directory and returns the file path.
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080087 std::string GetDefaultManifest(const char* package_name = kDefaultPackageName);
Ryan Mitchell479fa392019-01-02 17:15:39 -080088
Winsonb7be7932019-01-23 11:10:52 -080089 // Returns pointer to data inside APK files
90 std::unique_ptr<io::IData> OpenFileAsData(LoadedApk* apk,
91 const android::StringPiece& path);
92
Ryan Mitchell479fa392019-01-02 17:15:39 -080093 // Asserts that loading the tree from the specified file in the apk succeeds.
Winsonb7be7932019-01-23 11:10:52 -080094 void AssertLoadXml(LoadedApk* apk, const io::IData* data,
Ryan Mitchell479fa392019-01-02 17:15:39 -080095 android::ResXMLTree* out_tree);
96
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080097 static const char* kDefaultPackageName;
Ryan Mitchell479fa392019-01-02 17:15:39 -080098 private:
99 DISALLOW_COPY_AND_ASSIGN(CommandTestFixture);
100};
101
Ryan Mitchell5855de72021-02-24 14:39:13 -0800102struct ManifestBuilder {
103 explicit ManifestBuilder(CommandTestFixture* fixture);
104 ManifestBuilder& AddContents(const std::string& contents);
105 ManifestBuilder& SetPackageName(const std::string& package_name);
106 std::string Build(const std::string& file_path);
107 std::string Build();
108
109 private:
110 CommandTestFixture* fixture_;
111 std::string package_name_ = CommandTestFixture::kDefaultPackageName;
112 std::string contents_;
113};
114
115struct LinkCommandBuilder {
116 explicit LinkCommandBuilder(CommandTestFixture* fixture);
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000117 LinkCommandBuilder& AddCompiledResDir(const std::string& dir, android::IDiagnostics* diag);
Ryan Mitchell5855de72021-02-24 14:39:13 -0800118 LinkCommandBuilder& AddFlag(const std::string& flag);
119 LinkCommandBuilder& AddParameter(const std::string& param, const std::string& value);
120 LinkCommandBuilder& SetManifestFile(const std::string& manifest_path);
121 std::vector<std::string> Build(const std::string& out_apk_path);
122
123 private:
124 CommandTestFixture* fixture_;
125 std::vector<std::string> args_;
126 bool manifest_supplied_ = false;
127};
128
Ryan Mitchell479fa392019-01-02 17:15:39 -0800129} // namespace aapt
130
131#endif // AAPT_TEST_FIXTURE_H