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" |
| 27 | |
| 28 | namespace aapt { |
| 29 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 30 | std::string BuildPath(std::vector<std::string> args) { |
| 31 | std::string out; |
| 32 | if (args.empty()) { |
| 33 | return out; |
| 34 | } |
| 35 | out = args[0]; |
| 36 | for (int i = 1; i < args.size(); i++) { |
| 37 | file::AppendPath(&out, args[i]); |
| 38 | } |
| 39 | return out; |
| 40 | } |
| 41 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 42 | int TestCompile(const std::string& path, const std::string& outDir, bool legacy, |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 43 | StdErrDiagnostics& diag) { |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 44 | std::vector<android::StringPiece> args; |
| 45 | args.push_back(path); |
| 46 | args.push_back("-o"); |
| 47 | args.push_back(outDir); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 48 | if (legacy) { |
| 49 | args.push_back("--legacy"); |
| 50 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 51 | return CompileCommand(&diag).Execute(args, &std::cerr); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(CompilerTest, MultiplePeriods) { |
| 55 | StdErrDiagnostics diag; |
| 56 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 57 | const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()), |
| 58 | "integration-tests", "CompileTest", "res"}); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 59 | |
| 60 | // Resource files without periods in the file name should not throw errors |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 61 | const std::string path0 = BuildPath({kResDir, "values", "values.xml"}); |
| 62 | const std::string path0_out = BuildPath({kResDir, "values_values.arsc.flat"}); |
| 63 | ::android::base::utf8::unlink(path0_out.c_str()); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 64 | ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 65 | ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 66 | ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 67 | ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 68 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 69 | const std::string path1 = BuildPath({kResDir, "drawable", "image.png"}); |
| 70 | const std::string path1_out = BuildPath({kResDir, "drawable_image.png.flat"}); |
| 71 | ::android::base::utf8::unlink(path1_out.c_str()); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 72 | ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 73 | ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 74 | ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 75 | ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 76 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 77 | const std::string path2 = BuildPath({kResDir, "drawable", "image.9.png"}); |
| 78 | const std::string path2_out = BuildPath({kResDir, "drawable_image.9.png.flat"}); |
| 79 | ::android::base::utf8::unlink(path2_out.c_str()); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 80 | ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 81 | ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 82 | ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 83 | ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 84 | |
| 85 | // 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] | 86 | const std::string path3 = BuildPath({kResDir, "values", "values.all.xml"}); |
| 87 | const std::string path3_out = BuildPath({kResDir, "values_values.all.arsc.flat"}); |
| 88 | ::android::base::utf8::unlink(path3_out.c_str()); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 89 | ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 90 | ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 91 | ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 92 | ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 93 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 94 | const std::string path4 = BuildPath({kResDir, "drawable", "image.small.png"}); |
| 95 | const std::string path4_out = BuildPath({kResDir, "drawable_image.small.png.flat"}); |
| 96 | ::android::base::utf8::unlink(path4_out.c_str()); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 97 | ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 98 | ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 99 | ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 100 | ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 101 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 102 | const std::string path5 = BuildPath({kResDir, "drawable", "image.small.9.png"}); |
| 103 | const std::string path5_out = BuildPath({kResDir, "drawable_image.small.9.png.flat"}); |
| 104 | ::android::base::utf8::unlink(path5_out.c_str()); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 105 | ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 106 | ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 107 | ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 108 | ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0); |
y | d6b8329 | 2018-04-11 09:54:56 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 111 | TEST(CompilerTest, DirInput) { |
| 112 | StdErrDiagnostics diag; |
| 113 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 114 | const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()), |
| 115 | "integration-tests", "CompileTest", "DirInput", "res"}); |
| 116 | const std::string kOutputFlata = |
| 117 | BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests", |
| 118 | "CompileTest", "DirInput", "compiled.flata"}); |
| 119 | ::android::base::utf8::unlink(kOutputFlata.c_str()); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 120 | |
| 121 | std::vector<android::StringPiece> args; |
| 122 | args.push_back("--dir"); |
| 123 | args.push_back(kResDir); |
| 124 | args.push_back("-o"); |
| 125 | args.push_back(kOutputFlata); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 126 | args.push_back("-v"); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 127 | ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0); |
| 128 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 129 | { |
| 130 | // Check for the presence of the compiled files |
| 131 | std::string err; |
| 132 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err); |
| 133 | ASSERT_NE(zip, nullptr) << err; |
| 134 | ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr); |
| 135 | ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr); |
| 136 | ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr); |
| 137 | } |
| 138 | ASSERT_EQ(::android::base::utf8::unlink(kOutputFlata.c_str()), 0); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | TEST(CompilerTest, ZipInput) { |
| 142 | StdErrDiagnostics diag; |
| 143 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 144 | const std::string kResZip = |
| 145 | BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests", |
| 146 | "CompileTest", "ZipInput", "res.zip"}); |
| 147 | const std::string kOutputFlata = |
| 148 | BuildPath({android::base::Dirname(android::base::GetExecutablePath()), "integration-tests", |
| 149 | "CompileTest", "ZipInput", "compiled.flata"}); |
| 150 | |
| 151 | ::android::base::utf8::unlink(kOutputFlata.c_str()); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 152 | |
| 153 | std::vector<android::StringPiece> args; |
| 154 | args.push_back("--zip"); |
| 155 | args.push_back(kResZip); |
| 156 | args.push_back("-o"); |
| 157 | args.push_back(kOutputFlata); |
| 158 | ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0); |
| 159 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 160 | { |
| 161 | // Check for the presence of the compiled files |
| 162 | std::string err; |
| 163 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err); |
| 164 | ASSERT_NE(zip, nullptr) << err; |
| 165 | ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr); |
| 166 | ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr); |
| 167 | ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr); |
| 168 | } |
| 169 | ASSERT_EQ(::android::base::utf8::unlink(kOutputFlata.c_str()), 0); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 172 | } // namespace aapt |