• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
20 #include "io/StringStream.h"
21 #include "java/AnnotationProcessor.h"
22 #include "test/Test.h"
23 
24 namespace aapt {
25 
TestCompile(std::string path,std::string outDir,bool legacy,StdErrDiagnostics & diag)26 int TestCompile(std::string path, std::string outDir, bool legacy, StdErrDiagnostics& diag) {
27   std::vector<android::StringPiece> args;
28   args.push_back(path);
29   args.push_back("-o");
30   args.push_back(outDir);
31   args.push_back("-v");
32   if (legacy) {
33     args.push_back("--legacy");
34   }
35   return aapt::Compile(args, &diag);
36 }
37 
TEST(CompilerTest,MultiplePeriods)38 TEST(CompilerTest, MultiplePeriods) {
39   StdErrDiagnostics diag;
40   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
41   const std::string kResDir = android::base::Dirname(android::base::GetExecutablePath())
42       + "/integration-tests/CompileTest/res";
43 
44   // Resource files without periods in the file name should not throw errors
45   const std::string path0 = kResDir + "/values/values.xml";
46   const std::string path0_out = kResDir + "/values_values.arsc.flat";
47 
48   remove(path0_out.c_str());
49   ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ false, diag), 0);
50   ASSERT_EQ(remove(path0_out.c_str()), 0);
51   ASSERT_EQ(TestCompile(path0, kResDir, /** legacy */ true, diag), 0);
52   ASSERT_EQ(remove(path0_out.c_str()), 0);
53 
54   const std::string path1 = kResDir + "/drawable/image.png";
55   const std::string path1_out = kResDir + "/drawable_image.png.flat";
56   remove(path1_out.c_str());
57   ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ false, diag), 0);
58   ASSERT_EQ(remove(path1_out.c_str()), 0);
59   ASSERT_EQ(TestCompile(path1, kResDir, /** legacy */ true, diag), 0);
60   ASSERT_EQ(remove(path1_out.c_str()), 0);
61 
62   const std::string path2 = kResDir + "/drawable/image.9.png";
63   const std::string path2_out = kResDir + "/drawable_image.9.png.flat";
64   remove(path2_out.c_str());
65   ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ false, diag), 0);
66   ASSERT_EQ(remove(path2_out.c_str()), 0);
67   ASSERT_EQ(TestCompile(path2, kResDir, /** legacy */ true, diag), 0);
68   ASSERT_EQ(remove(path2_out.c_str()), 0);
69 
70   // Resource files with periods in the file name should fail on non-legacy compilations
71   const std::string path3 = kResDir + "/values/values.all.xml";
72   const std::string path3_out = kResDir + "/values_values.all.arsc.flat";
73   remove(path3_out.c_str());
74   ASSERT_NE(TestCompile(path3, kResDir, /** legacy */ false, diag), 0);
75   ASSERT_NE(remove(path3_out.c_str()), 0);
76   ASSERT_EQ(TestCompile(path3, kResDir, /** legacy */ true, diag), 0);
77   ASSERT_EQ(remove(path3_out.c_str()), 0);
78 
79   const std::string path4 = kResDir + "/drawable/image.small.png";
80   const std::string path4_out = (kResDir + std::string("/drawable_image.small.png.flat")).c_str();
81   remove(path4_out.c_str());
82   ASSERT_NE(TestCompile(path4, kResDir, /** legacy */ false, diag), 0);
83   ASSERT_NE(remove(path4_out.c_str()), 0);
84   ASSERT_EQ(TestCompile(path4, kResDir, /** legacy */ true, diag), 0);
85   ASSERT_EQ(remove(path4_out.c_str()), 0);
86 
87   const std::string path5 = kResDir + "/drawable/image.small.9.png";
88   const std::string path5_out = (kResDir + std::string("/drawable_image.small.9.png.flat")).c_str();
89   remove(path5_out.c_str());
90   ASSERT_NE(TestCompile(path5, kResDir, /** legacy */ false, diag), 0);
91   ASSERT_NE(remove(path5_out.c_str()), 0);
92   ASSERT_EQ(TestCompile(path5, kResDir, /** legacy */ true, diag), 0);
93   ASSERT_EQ(remove(path5_out.c_str()), 0);
94 }
95 
96 }