y | d6b8329 | 2018-04-11 09:54:56 -0700 | [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 | #include "Compile.h" |
| 18 | |
| 19 | #include "android-base/file.h" |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
| 21 | #include "android-base/utf8.h" |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 22 | #include "format/proto/ProtoDeserialize.h" |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 23 | #include "io/StringStream.h" |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 24 | #include "io/ZipArchive.h" |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 25 | #include "java/AnnotationProcessor.h" |
| 26 | #include "test/Test.h" |
| 27 | |
| 28 | namespace aapt { |
| 29 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 30 | using CompilerTest = CommandTestFixture; |
| 31 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 32 | std::string BuildPath(std::vector<std::string> args) { |
| 33 | std::string out; |
| 34 | if (args.empty()) { |
| 35 | return out; |
| 36 | } |
| 37 | out = args[0]; |
| 38 | for (int i = 1; i < args.size(); i++) { |
| 39 | file::AppendPath(&out, args[i]); |
| 40 | } |
| 41 | return out; |
| 42 | } |
| 43 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 44 | int TestCompile(const std::string& path, const std::string& outDir, bool legacy, |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 45 | StdErrDiagnostics& diag) { |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 46 | std::vector<android::StringPiece> args; |
| 47 | args.push_back(path); |
| 48 | args.push_back("-o"); |
| 49 | args.push_back(outDir); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 50 | if (legacy) { |
| 51 | args.push_back("--legacy"); |
| 52 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 53 | return CompileCommand(&diag).Execute(args, &std::cerr); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 56 | TEST_F(CompilerTest, MultiplePeriods) { |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 57 | StdErrDiagnostics diag; |
| 58 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 59 | const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()), |
| 60 | "integration-tests", "CompileTest", "res"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 61 | const std::string kOutDir = testing::TempDir(); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 62 | |
| 63 | // Resource files without periods in the file name should not throw errors |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 64 | const std::string path0 = BuildPath({kResDir, "values", "values.xml"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 65 | const std::string path0_out = BuildPath({kOutDir, "values_values.arsc.flat"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 66 | ::android::base::utf8::unlink(path0_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 67 | ASSERT_EQ(TestCompile(path0, kOutDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 68 | ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 69 | ASSERT_EQ(TestCompile(path0, kOutDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 70 | ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 71 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 72 | const std::string path1 = BuildPath({kResDir, "drawable", "image.png"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 73 | const std::string path1_out = BuildPath({kOutDir, "drawable_image.png.flat"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 74 | ::android::base::utf8::unlink(path1_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 75 | ASSERT_EQ(TestCompile(path1, kOutDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 76 | ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 77 | ASSERT_EQ(TestCompile(path1, kOutDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 78 | ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 79 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 80 | const std::string path2 = BuildPath({kResDir, "drawable", "image.9.png"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 81 | const std::string path2_out = BuildPath({kOutDir, "drawable_image.9.png.flat"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 82 | ::android::base::utf8::unlink(path2_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 83 | ASSERT_EQ(TestCompile(path2, kOutDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 84 | ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 85 | ASSERT_EQ(TestCompile(path2, kOutDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 86 | ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 87 | |
| 88 | // Resource files with periods in the file name should fail on non-legacy compilations |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 89 | const std::string path3 = BuildPath({kResDir, "values", "values.all.xml"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 90 | const std::string path3_out = BuildPath({kOutDir, "values_values.all.arsc.flat"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 91 | ::android::base::utf8::unlink(path3_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 92 | ASSERT_NE(TestCompile(path3, kOutDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 93 | ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 94 | ASSERT_EQ(TestCompile(path3, kOutDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 95 | ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 96 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 97 | const std::string path4 = BuildPath({kResDir, "drawable", "image.small.png"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 98 | const std::string path4_out = BuildPath({kOutDir, "drawable_image.small.png.flat"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 99 | ::android::base::utf8::unlink(path4_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 100 | ASSERT_NE(TestCompile(path4, kOutDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 101 | ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 102 | ASSERT_EQ(TestCompile(path4, kOutDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 103 | ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 104 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 105 | const std::string path5 = BuildPath({kResDir, "drawable", "image.small.9.png"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 106 | const std::string path5_out = BuildPath({kOutDir, "drawable_image.small.9.png.flat"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 107 | ::android::base::utf8::unlink(path5_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 108 | ASSERT_NE(TestCompile(path5, kOutDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 109 | ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 110 | ASSERT_EQ(TestCompile(path5, kOutDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 111 | ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 114 | TEST_F(CompilerTest, DirInput) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 115 | StdErrDiagnostics diag; |
| 116 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 117 | const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()), |
| 118 | "integration-tests", "CompileTest", "DirInput", "res"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 119 | const std::string kOutputFlata = BuildPath({testing::TempDir(), "compiled.flata"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 120 | ::android::base::utf8::unlink(kOutputFlata.c_str()); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 121 | |
| 122 | std::vector<android::StringPiece> args; |
| 123 | args.push_back("--dir"); |
| 124 | args.push_back(kResDir); |
| 125 | args.push_back("-o"); |
| 126 | args.push_back(kOutputFlata); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 127 | args.push_back("-v"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 128 | ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0); |
| 129 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 130 | { |
| 131 | // Check for the presence of the compiled files |
| 132 | std::string err; |
| 133 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err); |
| 134 | ASSERT_NE(zip, nullptr) << err; |
| 135 | ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr); |
| 136 | ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr); |
| 137 | ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr); |
| 138 | } |
| 139 | ASSERT_EQ(::android::base::utf8::unlink(kOutputFlata.c_str()), 0); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 142 | TEST_F(CompilerTest, ZipInput) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 143 | StdErrDiagnostics diag; |
| 144 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 145 | const std::string kResZip = |
| 146 | BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests", |
| 147 | "CompileTest", "ZipInput", "res.zip"}); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 148 | const std::string kOutputFlata = BuildPath({testing::TempDir(), "compiled.flata"}); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 149 | |
| 150 | ::android::base::utf8::unlink(kOutputFlata.c_str()); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 151 | |
| 152 | std::vector<android::StringPiece> args; |
| 153 | args.push_back("--zip"); |
| 154 | args.push_back(kResZip); |
| 155 | args.push_back("-o"); |
| 156 | args.push_back(kOutputFlata); |
| 157 | ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0); |
| 158 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 159 | { |
| 160 | // Check for the presence of the compiled files |
| 161 | std::string err; |
| 162 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err); |
| 163 | ASSERT_NE(zip, nullptr) << err; |
| 164 | ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr); |
| 165 | ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr); |
| 166 | ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr); |
| 167 | } |
| 168 | ASSERT_EQ(::android::base::utf8::unlink(kOutputFlata.c_str()), 0); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 171 | /* |
| 172 | * This tests the "protection" from pseudo-translation of |
| 173 | * non-translatable files (starting with 'donotranslate') |
| 174 | * and strings (with the translatable="false" attribute) |
| 175 | * |
| 176 | * We check 4 string files, 2 translatable, and 2 not (based on file name) |
| 177 | * Each file contains 2 strings, one translatable, one not (attribute based) |
| 178 | * Each of these files are compiled and linked into one .apk, then we load the |
| 179 | * strings from the apk and check if there are pseudo-translated strings. |
| 180 | */ |
| 181 | |
| 182 | // Using 000 and 111 because they are not changed by pseudo-translation, |
| 183 | // making our life easier. |
| 184 | constexpr static const char sTranslatableXmlContent[] = |
| 185 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| 186 | "<resources>" |
| 187 | " <string name=\"normal\">000</string>" |
| 188 | " <string name=\"non_translatable\" translatable=\"false\">111</string>" |
| 189 | "</resources>"; |
| 190 | |
| 191 | static void AssertTranslations(CommandTestFixture *ctf, std::string file_name, |
| 192 | std::vector<std::string> expected) { |
| 193 | |
| 194 | StdErrDiagnostics diag; |
| 195 | |
| 196 | const std::string source_file = ctf->GetTestPath("/res/values/" + file_name + ".xml"); |
| 197 | const std::string compiled_files_dir = ctf->GetTestPath("/compiled_" + file_name); |
| 198 | const std::string out_apk = ctf->GetTestPath("/" + file_name + ".apk"); |
| 199 | |
Ryan Mitchell | 81dfca0 | 2019-06-07 10:20:27 -0700 | [diff] [blame] | 200 | ctf->WriteFile(source_file, sTranslatableXmlContent); |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 201 | CHECK(file::mkdirs(compiled_files_dir.data())); |
| 202 | |
| 203 | ASSERT_EQ(CompileCommand(&diag).Execute({ |
| 204 | source_file, |
| 205 | "-o", compiled_files_dir, |
| 206 | "-v", |
| 207 | "--pseudo-localize" |
| 208 | }, &std::cerr), 0); |
| 209 | |
| 210 | ASSERT_TRUE(ctf->Link({ |
| 211 | "--manifest", ctf->GetDefaultManifest(), |
| 212 | "-o", out_apk |
| 213 | }, compiled_files_dir, &diag)); |
| 214 | |
| 215 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 216 | ASSERT_NE(apk, nullptr); |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 217 | |
| 218 | ResourceTable* table = apk->GetResourceTable(); |
| 219 | ASSERT_NE(table, nullptr); |
| 220 | table->string_pool.Sort(); |
| 221 | |
| 222 | const std::vector<std::unique_ptr<StringPool::Entry>>& pool_strings = |
| 223 | table->string_pool.strings(); |
| 224 | |
| 225 | // The actual / expected vectors have the same size |
| 226 | const size_t pool_size = pool_strings.size(); |
| 227 | ASSERT_EQ(pool_size, expected.size()); |
| 228 | |
| 229 | for (size_t i = 0; i < pool_size; i++) { |
| 230 | std::string actual = pool_strings[i]->value; |
| 231 | ASSERT_EQ(actual, expected[i]); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | TEST_F(CompilerTest, DoNotTranslateTest) { |
| 236 | // The first string (000) is translatable, the second is not |
| 237 | // ar-XB uses "\u200F\u202E...\u202C\u200F" |
| 238 | std::vector<std::string> expected_translatable = { |
| 239 | "000", "111", // default locale |
| 240 | "[000 one]", // en-XA |
| 241 | "\xE2\x80\x8F\xE2\x80\xAE" "000" "\xE2\x80\xAC\xE2\x80\x8F", // ar-XB |
| 242 | }; |
| 243 | AssertTranslations(this, "foo", expected_translatable); |
| 244 | AssertTranslations(this, "foo_donottranslate", expected_translatable); |
| 245 | |
| 246 | // No translatable strings because these are non-translatable files |
| 247 | std::vector<std::string> expected_not_translatable = { |
| 248 | "000", "111", // default locale |
| 249 | }; |
| 250 | AssertTranslations(this, "donottranslate", expected_not_translatable); |
| 251 | AssertTranslations(this, "donottranslate_foo", expected_not_translatable); |
| 252 | } |
| 253 | |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 254 | TEST_F(CompilerTest, RelativePathTest) { |
| 255 | StdErrDiagnostics diag; |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 256 | const std::string res_path = |
| 257 | BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests", |
| 258 | "CompileTest", "res"}); |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 259 | |
| 260 | const std::string path_values_colors = GetTestPath("values/colors.xml"); |
| 261 | WriteFile(path_values_colors, "<resources>" |
| 262 | "<color name=\"color_one\">#008577</color>" |
| 263 | "</resources>"); |
| 264 | |
| 265 | const std::string path_layout_layout_one = GetTestPath("layout/layout_one.xml"); |
| 266 | WriteFile(path_layout_layout_one, "<LinearLayout " |
| 267 | "xmlns:android=\"http://schemas.android.com/apk/res/android\">" |
| 268 | "<TextBox android:id=\"@+id/text_one\" android:background=\"@color/color_one\"/>" |
| 269 | "</LinearLayout>"); |
| 270 | |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 271 | const std::string compiled_files_dir = |
| 272 | BuildPath({testing::TempDir(), "integration-tests", "CompileTest", "compiled"}); |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 273 | CHECK(file::mkdirs(compiled_files_dir.data())); |
| 274 | |
| 275 | const std::string path_values_colors_out = |
| 276 | BuildPath({compiled_files_dir,"values_colors.arsc.flat"}); |
| 277 | const std::string path_layout_layout_one_out = |
| 278 | BuildPath({compiled_files_dir, "layout_layout_one.flat"}); |
| 279 | ::android::base::utf8::unlink(path_values_colors_out.c_str()); |
| 280 | ::android::base::utf8::unlink(path_layout_layout_one_out.c_str()); |
weisu | 90d3fb6 | 2021-12-12 22:24:52 +0000 | [diff] [blame] | 281 | const std::string apk_path = |
| 282 | BuildPath({testing::TempDir(), "integration-tests", "CompileTest", "out.apk"}); |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 283 | |
| 284 | const std::string source_set_res = BuildPath({"main", "res"}); |
| 285 | const std::string relative_path_values_colors = |
| 286 | BuildPath({source_set_res, "values", "colors.xml"}); |
| 287 | const std::string relative_path_layout_layout_one = |
| 288 | BuildPath({source_set_res, "layout", "layout_one.xml"}); |
| 289 | |
| 290 | CompileCommand(&diag).Execute({ |
| 291 | path_values_colors, |
| 292 | "-o", |
| 293 | compiled_files_dir, |
| 294 | "--source-path", |
| 295 | relative_path_values_colors}, |
| 296 | &std::cerr); |
| 297 | |
| 298 | CompileCommand(&diag).Execute({ |
| 299 | path_layout_layout_one, |
| 300 | "-o", |
| 301 | compiled_files_dir, |
| 302 | "--source-path", |
| 303 | relative_path_layout_layout_one}, |
| 304 | &std::cerr); |
| 305 | |
| 306 | std::ifstream ifs_values(path_values_colors_out); |
| 307 | std::string content_values((std::istreambuf_iterator<char>(ifs_values)), |
| 308 | (std::istreambuf_iterator<char>())); |
| 309 | ASSERT_NE(content_values.find(relative_path_values_colors), -1); |
| 310 | ASSERT_EQ(content_values.find(path_values_colors), -1); |
| 311 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 312 | ASSERT_TRUE(Link({"-o", apk_path, |
| 313 | "--manifest", GetDefaultManifest(), |
| 314 | "--proto-format"}, |
| 315 | compiled_files_dir, &diag)); |
lukeedgar | 19b8ebf | 2020-06-23 10:43:42 +0100 | [diff] [blame] | 316 | |
| 317 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(apk_path, &diag); |
| 318 | ResourceTable* resource_table = apk.get()->GetResourceTable(); |
| 319 | const std::vector<std::unique_ptr<StringPool::Entry>>& pool_strings = |
| 320 | resource_table->string_pool.strings(); |
| 321 | |
| 322 | ASSERT_EQ(pool_strings.size(), 2); |
| 323 | ASSERT_EQ(pool_strings[0]->value, "res/layout/layout_one.xml"); |
| 324 | ASSERT_EQ(pool_strings[1]->value, "res/layout-v1/layout_one.xml"); |
| 325 | |
| 326 | // Check resources.pb contains relative sources. |
| 327 | io::IFile* proto_file = |
| 328 | apk.get()->GetFileCollection()->FindFile("resources.pb"); |
| 329 | std::unique_ptr<io::InputStream> proto_stream = proto_file->OpenInputStream(); |
| 330 | io::ProtoInputStreamReader proto_reader(proto_stream.get()); |
| 331 | pb::ResourceTable pb_table; |
| 332 | proto_reader.ReadMessage(&pb_table); |
| 333 | |
| 334 | const std::string pool_strings_proto = pb_table.source_pool().data(); |
| 335 | |
| 336 | ASSERT_NE(pool_strings_proto.find(relative_path_values_colors), -1); |
| 337 | ASSERT_NE(pool_strings_proto.find(relative_path_layout_layout_one), -1); |
| 338 | } |
| 339 | |
Mihai Nita | d1a6521 | 2019-03-26 13:47:45 -0700 | [diff] [blame] | 340 | } // namespace aapt |