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