Move --resources_dir to its right place.

We needed a hack in test_main_lib.cc to ensure fileutils were always
linked with test binaries downstream. When I removed the hack, it
broke the binaries that were _not_ using fileutils because a certain
bazel rule expects to be able to pass the flag to all test binaries.

The solution is to move the flag to test_main_lib.cc. This is the
right place for it since it's apparently in the contract of a WebRTC
test binary to support this flag. We then have to pass the value
down to the override, which is why I add a new function for that.
I leave the flag unimplemented in OSS because no one is using it
here anyway. It will be implemented downstream.

Bug: webrtc:9792
Change-Id: I21b3deb43bf0cd56d6aa2622dc5519370a0307a9
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/156568
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Patrik Höglund <phoglund@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29474}
diff --git a/test/BUILD.gn b/test/BUILD.gn
index f2632d3..ff13621 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -253,6 +253,7 @@
 
     deps = [
       ":field_trial",
+      ":fileutils_override_api",
       ":perf_test",
       ":test_support",
       "../rtc_base",
diff --git a/test/test_main_lib.cc b/test/test_main_lib.cc
index bb41e65..1650d2a 100644
--- a/test/test_main_lib.cc
+++ b/test/test_main_lib.cc
@@ -28,6 +28,7 @@
 #include "test/gmock.h"
 #include "test/gtest.h"
 #include "test/testsupport/file_utils.h"
+#include "test/testsupport/file_utils_override.h"
 #include "test/testsupport/perf_test.h"
 
 #if defined(WEBRTC_WIN)
@@ -80,6 +81,14 @@
 
 #endif
 
+ABSL_FLAG(std::string,
+          resources_dir,
+          "",
+          "Where to look for the runtime dependencies. If not specified, we "
+          "will use a reasonable default depending on where we are running. "
+          "This flag is useful if we copy over test resources to a phone and "
+          "need to tell the tests where their resources are.");
+
 ABSL_FLAG(bool, logs, true, "print logs to stderr");
 ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
 
@@ -106,6 +115,10 @@
     ::testing::InitGoogleMock(argc, argv);
     absl::ParseCommandLine(*argc, argv);
 
+    std::string resources_dir = absl::GetFlag(FLAGS_resources_dir);
+    if (!resources_dir.empty())
+      test::internal::OverrideResourcesDir(resources_dir);
+
     // Default to LS_INFO, even for release builds to provide better test
     // logging.
     if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
diff --git a/test/testsupport/file_utils_override.cc b/test/testsupport/file_utils_override.cc
index 0980691..3106d0f 100644
--- a/test/testsupport/file_utils_override.cc
+++ b/test/testsupport/file_utils_override.cc
@@ -150,6 +150,12 @@
 #endif
 }
 
+void OverrideResourcesDir(const std::string& resources_dir) {
+  RTC_CHECK(false)
+      << "Setting the resource dir is not supported in open-source "
+         "tests.";
+}
+
 }  // namespace internal
 }  // namespace test
 }  // namespace webrtc
diff --git a/test/testsupport/file_utils_override.h b/test/testsupport/file_utils_override.h
index a72cd80..235c839 100644
--- a/test/testsupport/file_utils_override.h
+++ b/test/testsupport/file_utils_override.h
@@ -38,8 +38,7 @@
 // directory.
 std::string WorkingDir();
 
-// Returns a path to a resource file in [project-root]/resources/ dir.
-// Returns an absolute path
+// Returns a full path to a resource file in the resources_dir dir.
 //
 // Arguments:
 //    name - Name of the resource file. If a plain filename (no directory path)
@@ -49,6 +48,15 @@
 //    extension - File extension, without the dot, i.e. "bmp" or "yuv".
 std::string ResourcePath(const std::string& name, const std::string& extension);
 
+// Overrides the root resource dir. See ResourcePath for more information.
+//
+// This is not necessary in most cases, but it can be if we're running on
+// phones and copy over resources to some custom location.
+//
+// Arguments:
+//    resources_dir - Where resources are located.
+void OverrideResourcesDir(const std::string& resources_dir);
+
 }  // namespace internal
 }  // namespace test
 }  // namespace webrtc