blob: dd5198ce86da1a8be37ad1fda8471f38aef9873a [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"
20#include "io/StringStream.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070021#include "io/ZipArchive.h"
yd6b83292018-04-11 09:54:56 -070022#include "java/AnnotationProcessor.h"
23#include "test/Test.h"
24
25namespace aapt {
26
Ryan Mitchell833a1a62018-07-10 13:51:36 -070027int TestCompile(const std::string& path, const std::string& outDir, bool legacy,
28 StdErrDiagnostics& diag) {
yd6b83292018-04-11 09:54:56 -070029 std::vector<android::StringPiece> args;
30 args.push_back(path);
31 args.push_back("-o");
32 args.push_back(outDir);
yd6b83292018-04-11 09:54:56 -070033 if (legacy) {
34 args.push_back("--legacy");
35 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -070036 return CompileCommand(&diag).Execute(args, &std::cerr);
yd6b83292018-04-11 09:54:56 -070037}
38
39TEST(CompilerTest, MultiplePeriods) {
40 StdErrDiagnostics diag;
41 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
42 const std::string kResDir = android::base::Dirname(android::base::GetExecutablePath())
43 + "/integration-tests/CompileTest/res";
44
45 // Resource files without periods in the file name should not throw errors
46 const std::string path0 = kResDir + "/values/values.xml";
47 const std::string path0_out = kResDir + "/values_values.arsc.flat";
48
49 remove(path0_out.c_str());
50 ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0);
51 ASSERT_EQ(remove(path0_out.c_str()), 0);
52 ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0);
53 ASSERT_EQ(remove(path0_out.c_str()), 0);
54
55 const std::string path1 = kResDir + "/drawable/image.png";
56 const std::string path1_out = kResDir + "/drawable_image.png.flat";
57 remove(path1_out.c_str());
58 ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0);
59 ASSERT_EQ(remove(path1_out.c_str()), 0);
60 ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0);
61 ASSERT_EQ(remove(path1_out.c_str()), 0);
62
63 const std::string path2 = kResDir + "/drawable/image.9.png";
64 const std::string path2_out = kResDir + "/drawable_image.9.png.flat";
65 remove(path2_out.c_str());
66 ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0);
67 ASSERT_EQ(remove(path2_out.c_str()), 0);
68 ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0);
69 ASSERT_EQ(remove(path2_out.c_str()), 0);
70
71 // Resource files with periods in the file name should fail on non-legacy compilations
72 const std::string path3 = kResDir + "/values/values.all.xml";
73 const std::string path3_out = kResDir + "/values_values.all.arsc.flat";
74 remove(path3_out.c_str());
75 ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0);
76 ASSERT_NE(remove(path3_out.c_str()), 0);
77 ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0);
78 ASSERT_EQ(remove(path3_out.c_str()), 0);
79
80 const std::string path4 = kResDir + "/drawable/image.small.png";
81 const std::string path4_out = (kResDir + std::string("/drawable_image.small.png.flat")).c_str();
82 remove(path4_out.c_str());
83 ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0);
84 ASSERT_NE(remove(path4_out.c_str()), 0);
85 ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0);
86 ASSERT_EQ(remove(path4_out.c_str()), 0);
87
88 const std::string path5 = kResDir + "/drawable/image.small.9.png";
89 const std::string path5_out = (kResDir + std::string("/drawable_image.small.9.png.flat")).c_str();
90 remove(path5_out.c_str());
91 ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0);
92 ASSERT_NE(remove(path5_out.c_str()), 0);
93 ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0);
94 ASSERT_EQ(remove(path5_out.c_str()), 0);
95}
96
Ryan Mitchellf3649d62018-08-02 16:16:45 -070097TEST(CompilerTest, DirInput) {
98 StdErrDiagnostics diag;
99 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
100 const std::string kResDir = android::base::Dirname(android::base::GetExecutablePath())
101 + "/integration-tests/CompileTest/DirInput/res";
102 const std::string kOutputFlata = android::base::Dirname(android::base::GetExecutablePath())
103 + "/integration-tests/CompileTest/DirInput/compiled.flata";
104 remove(kOutputFlata.c_str());
105
106 std::vector<android::StringPiece> args;
107 args.push_back("--dir");
108 args.push_back(kResDir);
109 args.push_back("-o");
110 args.push_back(kOutputFlata);
111 ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
112
113 // Check for the presence of the compiled files
114 std::string err;
115 std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err);
116 ASSERT_NE(zip, nullptr) << err;
117 ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr);
118 ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr);
119 ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr);
120 ASSERT_EQ(remove(kOutputFlata.c_str()), 0);
121}
122
123TEST(CompilerTest, ZipInput) {
124 StdErrDiagnostics diag;
125 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
126 const std::string kResZip = android::base::Dirname(android::base::GetExecutablePath())
127 + "/integration-tests/CompileTest/ZipInput/res.zip";
128 const std::string kOutputFlata = android::base::Dirname(android::base::GetExecutablePath())
129 + "/integration-tests/CompileTest/ZipInput/compiled.flata";
130 remove(kOutputFlata.c_str());
131
132 std::vector<android::StringPiece> args;
133 args.push_back("--zip");
134 args.push_back(kResZip);
135 args.push_back("-o");
136 args.push_back(kOutputFlata);
137 ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
138
139 // Check for the presence of the compiled files
140 std::string err;
141 std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err);
142 ASSERT_NE(zip, nullptr) << err;
143 ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr);
144 ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr);
145 ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr);
146 ASSERT_EQ(remove(kOutputFlata.c_str()), 0);
147}
148
149} // namespace aapt