Add --testdata flag for easier testing

Instead of hardcoding or assuming a path for testdata,
allow the testdata path to be specified via the command
line.
Test: make libandroidfw_tests

Change-Id: Ideae880b21c157b70a11bb5a90a94556771aead6
diff --git a/libs/androidfw/tests/TestHelpers.cpp b/libs/androidfw/tests/TestHelpers.cpp
index 3d1d5f5..702ee5c 100644
--- a/libs/androidfw/tests/TestHelpers.cpp
+++ b/libs/androidfw/tests/TestHelpers.cpp
@@ -16,26 +16,23 @@
 
 #include "TestHelpers.h"
 
-#include <androidfw/ResourceTypes.h>
-#include <gtest/gtest.h>
 #include <unistd.h>
-#include <utils/String8.h>
 
-std::string TestSourceDir() {
-  const char* dir = getenv("ANDROID_BUILD_TOP");
-  LOG_ALWAYS_FATAL_IF(dir == nullptr, "Environment variable ANDROID_BUILD_TOP must be set");
-  std::string testdir = std::string(dir) + "/frameworks/base/libs/androidfw/tests/data";
-
-  // Check that the directory exists.
-  struct stat filestat;
-  LOG_ALWAYS_FATAL_IF(stat(testdir.c_str(), &filestat) != 0, "test data path '%s' does not exist",
-                      testdir.c_str());
-  return testdir;
-}
+#include "android-base/logging.h"
 
 namespace android {
 
-::testing::AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id,
+static std::string sTestDataPath;
+
+void SetTestDataPath(const std::string& path) { sTestDataPath = path; }
+
+const std::string& GetTestDataPath() {
+  CHECK(!sTestDataPath.empty()) << "no test data path set.";
+  return sTestDataPath;
+}
+
+::testing::AssertionResult IsStringEqual(const ResTable& table,
+                                         uint32_t resource_id,
                                          const char* expected_str) {
   Res_value val;
   ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG);
@@ -49,7 +46,8 @@
 
   const ResStringPool* pool = table.getTableStringBlock(block);
   if (pool == NULL) {
-    return ::testing::AssertionFailure() << "table has no string pool for block " << block;
+    return ::testing::AssertionFailure()
+           << "table has no string pool for block " << block;
   }
 
   const String8 actual_str = pool->string8ObjectAt(val.data);