blob: c0c05cda35e71673d930bea5fcf80f470c83d10a [file] [log] [blame]
yd6b83292018-04-11 09:54:56 -07001/*
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 Mitchell0ce89732018-10-03 09:20:57 -070020#include "android-base/stringprintf.h"
21#include "android-base/utf8.h"
22
yd6b83292018-04-11 09:54:56 -070023#include "io/StringStream.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070024#include "io/ZipArchive.h"
yd6b83292018-04-11 09:54:56 -070025#include "java/AnnotationProcessor.h"
26#include "test/Test.h"
27
28namespace aapt {
29
Ryan Mitchell0ce89732018-10-03 09:20:57 -070030std::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 Mitchell833a1a62018-07-10 13:51:36 -070042int TestCompile(const std::string& path, const std::string& outDir, bool legacy,
Ryan Mitchell0ce89732018-10-03 09:20:57 -070043 StdErrDiagnostics& diag) {
yd6b83292018-04-11 09:54:56 -070044 std::vector<android::StringPiece> args;
45 args.push_back(path);
46 args.push_back("-o");
47 args.push_back(outDir);
yd6b83292018-04-11 09:54:56 -070048 if (legacy) {
49 args.push_back("--legacy");
50 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070051 return CompileCommand(&diag).Execute(args, &std::cerr);
yd6b83292018-04-11 09:54:56 -070052}
53
54TEST(CompilerTest, MultiplePeriods) {
55 StdErrDiagnostics diag;
56 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Ryan Mitchell0ce89732018-10-03 09:20:57 -070057 const std::string kResDir = BuildPath({android::base::Dirname(android::base::GetExecutablePath()),
58 "integration-tests", "CompileTest", "res"});
yd6b83292018-04-11 09:54:56 -070059
60 // Resource files without periods in the file name should not throw errors
Ryan Mitchell0ce89732018-10-03 09:20:57 -070061 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());
yd6b83292018-04-11 09:54:56 -070064 ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070065 ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070066 ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070067 ASSERT_EQ(::android::base::utf8::unlink(path0_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070068
Ryan Mitchell0ce89732018-10-03 09:20:57 -070069 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());
yd6b83292018-04-11 09:54:56 -070072 ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070073 ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070074 ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070075 ASSERT_EQ(::android::base::utf8::unlink(path1_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070076
Ryan Mitchell0ce89732018-10-03 09:20:57 -070077 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());
yd6b83292018-04-11 09:54:56 -070080 ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070081 ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070082 ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070083 ASSERT_EQ(::android::base::utf8::unlink(path2_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070084
85 // Resource files with periods in the file name should fail on non-legacy compilations
Ryan Mitchell0ce89732018-10-03 09:20:57 -070086 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());
yd6b83292018-04-11 09:54:56 -070089 ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070090 ASSERT_NE(::android::base::utf8::unlink(path3_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070091 ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070092 ASSERT_EQ(::android::base::utf8::unlink(path3_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070093
Ryan Mitchell0ce89732018-10-03 09:20:57 -070094 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());
yd6b83292018-04-11 09:54:56 -070097 ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -070098 ASSERT_NE(::android::base::utf8::unlink(path4_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -070099 ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700100 ASSERT_EQ(::android::base::utf8::unlink(path4_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700101
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700102 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());
yd6b83292018-04-11 09:54:56 -0700105 ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700106 ASSERT_NE(::android::base::utf8::unlink(path5_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700107 ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0);
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700108 ASSERT_EQ(::android::base::utf8::unlink(path5_out.c_str()), 0);
yd6b83292018-04-11 09:54:56 -0700109}
110
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700111TEST(CompilerTest, DirInput) {
112 StdErrDiagnostics diag;
113 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700114 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 Mitchellf3649d62018-08-02 16:16:45 -0700120
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 Mitchell0ce89732018-10-03 09:20:57 -0700126 args.push_back("-v");
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700127 ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
128
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700129 {
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 Mitchellf3649d62018-08-02 16:16:45 -0700139}
140
141TEST(CompilerTest, ZipInput) {
142 StdErrDiagnostics diag;
143 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700144 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 Mitchellf3649d62018-08-02 16:16:45 -0700152
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 Mitchell0ce89732018-10-03 09:20:57 -0700160 {
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 Mitchellf3649d62018-08-02 16:16:45 -0700170}
171
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700172} // namespace aapt