AAPT2: Compile --zip flag

Added a --zip flag similar to --dir that allows resources to be passed
into "aapt2 compile" using a zip file.

Also refactored Compile.cpp to be easier to mock and test in the future.

Bug: 74574557
Test: aapt2_tests
Change-Id: Idb90cb97e23a219525bdead38220cbf7bc6f3cab
diff --git a/tools/aapt2/cmd/Compile_test.cpp b/tools/aapt2/cmd/Compile_test.cpp
index d21addf..dd5198c 100644
--- a/tools/aapt2/cmd/Compile_test.cpp
+++ b/tools/aapt2/cmd/Compile_test.cpp
@@ -18,6 +18,7 @@
 
 #include "android-base/file.h"
 #include "io/StringStream.h"
+#include "io/ZipArchive.h"
 #include "java/AnnotationProcessor.h"
 #include "test/Test.h"
 
@@ -29,7 +30,6 @@
   args.push_back(path);
   args.push_back("-o");
   args.push_back(outDir);
-  args.push_back("-v");
   if (legacy) {
     args.push_back("--legacy");
   }
@@ -94,4 +94,56 @@
   ASSERT_EQ(remove(path5_out.c_str()), 0);
 }
 
-}
\ No newline at end of file
+TEST(CompilerTest, DirInput) {
+  StdErrDiagnostics diag;
+  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
+  const std::string kResDir = android::base::Dirname(android::base::GetExecutablePath())
+                            + "/integration-tests/CompileTest/DirInput/res";
+  const std::string kOutputFlata = android::base::Dirname(android::base::GetExecutablePath())
+                                 + "/integration-tests/CompileTest/DirInput/compiled.flata";
+  remove(kOutputFlata.c_str());
+
+  std::vector<android::StringPiece> args;
+  args.push_back("--dir");
+  args.push_back(kResDir);
+  args.push_back("-o");
+  args.push_back(kOutputFlata);
+  ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
+
+  // Check for the presence of the compiled files
+  std::string err;
+  std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err);
+  ASSERT_NE(zip, nullptr) << err;
+  ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr);
+  ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr);
+  ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr);
+  ASSERT_EQ(remove(kOutputFlata.c_str()), 0);
+}
+
+TEST(CompilerTest, ZipInput) {
+  StdErrDiagnostics diag;
+  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
+  const std::string kResZip = android::base::Dirname(android::base::GetExecutablePath())
+                            + "/integration-tests/CompileTest/ZipInput/res.zip";
+  const std::string kOutputFlata = android::base::Dirname(android::base::GetExecutablePath())
+                                 + "/integration-tests/CompileTest/ZipInput/compiled.flata";
+  remove(kOutputFlata.c_str());
+
+  std::vector<android::StringPiece> args;
+  args.push_back("--zip");
+  args.push_back(kResZip);
+  args.push_back("-o");
+  args.push_back(kOutputFlata);
+  ASSERT_EQ(CompileCommand(&diag).Execute(args, &std::cerr), 0);
+
+  // Check for the presence of the compiled files
+  std::string err;
+  std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(kOutputFlata, &err);
+  ASSERT_NE(zip, nullptr) << err;
+  ASSERT_NE(zip->FindFile("drawable_image.png.flat"), nullptr);
+  ASSERT_NE(zip->FindFile("layout_layout.xml.flat"), nullptr);
+  ASSERT_NE(zip->FindFile("values_values.arsc.flat"), nullptr);
+  ASSERT_EQ(remove(kOutputFlata.c_str()), 0);
+}
+
+} // namespace aapt
\ No newline at end of file