Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | namespace aapt { |
| 31 | |
| 32 | class TestDirectoryFixture : public ::testing::Test { |
| 33 | public: |
| 34 | TestDirectoryFixture() = default; |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 35 | ~TestDirectoryFixture() override = default; |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 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. |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 44 | android::StringPiece GetTestDirectory() { |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 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. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 51 | std::string GetTestPath(android::StringPiece path) { |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 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. |
Ryan Mitchell | 81dfca0 | 2019-06-07 10:20:27 -0700 | [diff] [blame] | 61 | void WriteFile(const std::string& path, const std::string& contents); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | std::string temp_dir_; |
| 65 | DISALLOW_COPY_AND_ASSIGN(TestDirectoryFixture); |
| 66 | }; |
| 67 | |
| 68 | class CommandTestFixture : public TestDirectoryFixture { |
| 69 | public: |
| 70 | CommandTestFixture() = default; |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 71 | ~CommandTestFixture() override = default; |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 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, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 76 | android::StringPiece flat_out_dir, android::IDiagnostics* diag); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 77 | |
Ryan Mitchell | 81dfca0 | 2019-06-07 10:20:27 -0700 | [diff] [blame] | 78 | // Executes the link command with the specified arguments. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 79 | bool Link(const std::vector<std::string>& args, android::IDiagnostics* diag); |
Ryan Mitchell | 81dfca0 | 2019-06-07 10:20:27 -0700 | [diff] [blame] | 80 | |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 81 | // 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. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 83 | bool Link(const std::vector<std::string>& args, android::StringPiece flat_dir, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 84 | android::IDiagnostics* diag); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 85 | |
| 86 | // Creates a minimal android manifest within the test directory and returns the file path. |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 87 | std::string GetDefaultManifest(const char* package_name = kDefaultPackageName); |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 88 | |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 89 | // Returns pointer to data inside APK files |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 90 | std::unique_ptr<io::IData> OpenFileAsData(LoadedApk* apk, android::StringPiece path); |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 91 | |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 92 | // Asserts that loading the tree from the specified file in the apk succeeds. |
Winson | b7be793 | 2019-01-23 11:10:52 -0800 | [diff] [blame] | 93 | void AssertLoadXml(LoadedApk* apk, const io::IData* data, |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 94 | android::ResXMLTree* out_tree); |
| 95 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 96 | static const char* kDefaultPackageName; |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 97 | private: |
| 98 | DISALLOW_COPY_AND_ASSIGN(CommandTestFixture); |
| 99 | }; |
| 100 | |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 101 | struct ManifestBuilder { |
| 102 | explicit ManifestBuilder(CommandTestFixture* fixture); |
| 103 | ManifestBuilder& AddContents(const std::string& contents); |
| 104 | ManifestBuilder& SetPackageName(const std::string& package_name); |
| 105 | std::string Build(const std::string& file_path); |
| 106 | std::string Build(); |
| 107 | |
| 108 | private: |
| 109 | CommandTestFixture* fixture_; |
| 110 | std::string package_name_ = CommandTestFixture::kDefaultPackageName; |
| 111 | std::string contents_; |
| 112 | }; |
| 113 | |
| 114 | struct LinkCommandBuilder { |
| 115 | explicit LinkCommandBuilder(CommandTestFixture* fixture); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 116 | LinkCommandBuilder& AddCompiledResDir(const std::string& dir, android::IDiagnostics* diag); |
Ryan Mitchell | 5855de7 | 2021-02-24 14:39:13 -0800 | [diff] [blame] | 117 | LinkCommandBuilder& AddFlag(const std::string& flag); |
| 118 | LinkCommandBuilder& AddParameter(const std::string& param, const std::string& value); |
| 119 | LinkCommandBuilder& SetManifestFile(const std::string& manifest_path); |
| 120 | std::vector<std::string> Build(const std::string& out_apk_path); |
| 121 | |
| 122 | private: |
| 123 | CommandTestFixture* fixture_; |
| 124 | std::vector<std::string> args_; |
| 125 | bool manifest_supplied_ = false; |
| 126 | }; |
| 127 | |
Ryan Mitchell | 479fa39 | 2019-01-02 17:15:39 -0800 | [diff] [blame] | 128 | } // namespace aapt |
| 129 | |
| 130 | #endif // AAPT_TEST_FIXTURE_H |