blob: 3079c757f61a3a02be71c36a405fb663b0f4a26e [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;
35 virtual ~TestDirectoryFixture() = default;
36
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.
44 const android::StringPiece GetTestDirectory() {
45 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.
51 const std::string GetTestPath(const android::StringPiece& path) {
52 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.
61 bool WriteFile(const std::string& path, const std::string& contents);
62
63 private:
64 std::string temp_dir_;
65 DISALLOW_COPY_AND_ASSIGN(TestDirectoryFixture);
66};
67
68class CommandTestFixture : public TestDirectoryFixture {
69 public:
70 CommandTestFixture() = default;
71 virtual ~CommandTestFixture() = default;
72
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,
76 const android::StringPiece& flat_out_dir, IDiagnostics* diag);
77
78 // Executes the link command with the specified arguments. The flattened files residing in the
79 // flat directory will be added to the link command as file arguments.
80 bool Link(const std::vector<std::string>& args, const android::StringPiece& flat_dir,
81 IDiagnostics* diag);
82
83 // Creates a minimal android manifest within the test directory and returns the file path.
84 std::string GetDefaultManifest();
85
Winsonb7be7932019-01-23 11:10:52 -080086 // Returns pointer to data inside APK files
87 std::unique_ptr<io::IData> OpenFileAsData(LoadedApk* apk,
88 const android::StringPiece& path);
89
Ryan Mitchell479fa392019-01-02 17:15:39 -080090 // Asserts that loading the tree from the specified file in the apk succeeds.
Winsonb7be7932019-01-23 11:10:52 -080091 void AssertLoadXml(LoadedApk* apk, const io::IData* data,
Ryan Mitchell479fa392019-01-02 17:15:39 -080092 android::ResXMLTree* out_tree);
93
94 private:
95 DISALLOW_COPY_AND_ASSIGN(CommandTestFixture);
96};
97
98} // namespace aapt
99
100#endif // AAPT_TEST_FIXTURE_H