Ensure --webrtc_test_metrics_output_path is a file name on iOS.

Bug: b/237982523
Change-Id: I5671e311fe14d7bcdd389626b6e11245d19d62c4
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/286425
Reviewed-by: Jeremy Leconte <jleconte@google.com>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38819}
diff --git a/test/ios/test_support.mm b/test/ios/test_support.mm
index 8586f46..bb8532b 100644
--- a/test/ios/test_support.mm
+++ b/test/ios/test_support.mm
@@ -97,14 +97,14 @@
 
   int exitStatus = g_test_suite();
 
+  NSArray<NSString *> *outputDirectories =
+      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   std::vector<std::unique_ptr<webrtc::test::MetricsExporter>> exporters;
   if (g_export_perf_results_new_api) {
     exporters.push_back(std::make_unique<webrtc::test::StdoutMetricsExporter>());
     if (g_write_perf_output) {
       // Stores data into a proto file under the app's document directory.
       NSString *fileName = @"perftest-output.pb";
-      NSArray<NSString *> *outputDirectories =
-          NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       if ([outputDirectories count] != 0) {
         NSString *outputPath = [outputDirectories[0] stringByAppendingPathComponent:fileName];
 
@@ -113,8 +113,16 @@
       }
     }
     if (!g_webrtc_test_metrics_output_path.empty()) {
-      exporters.push_back(std::make_unique<webrtc::test::MetricsSetProtoFileExporter>(
-          webrtc::test::MetricsSetProtoFileExporter::Options(g_webrtc_test_metrics_output_path)));
+      RTC_CHECK_NE(g_webrtc_test_metrics_output_path.find('/'), std::string::npos)
+          << "On iOS, --webrtc_test_metrics_output_path must only be a file name.";
+      if ([outputDirectories count] != 0) {
+        NSString *fileName = [NSString stringWithCString:g_webrtc_test_metrics_output_path.c_str()
+                                                encoding:[NSString defaultCStringEncoding]];
+        NSString *outputPath = [outputDirectories[0] stringByAppendingPathComponent:fileName];
+        exporters.push_back(std::make_unique<webrtc::test::MetricsSetProtoFileExporter>(
+            webrtc::test::MetricsSetProtoFileExporter::Options(
+                [NSString stdStringForString:outputPath])));
+      }
     }
   } else {
     exporters.push_back(std::make_unique<webrtc::test::PrintResultProxyMetricsExporter>());
@@ -124,8 +132,6 @@
     if (g_write_perf_output) {
       // Stores data into a proto file under the app's document directory.
       NSString *fileName = @"perftest-output.pb";
-      NSArray<NSString *> *outputDirectories =
-          NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       if ([outputDirectories count] != 0) {
         NSString *outputPath = [outputDirectories[0] stringByAppendingPathComponent:fileName];
 
diff --git a/test/test_flags.cc b/test/test_flags.cc
index 5e8ba32..a0fff74 100644
--- a/test/test_flags.cc
+++ b/test/test_flags.cc
@@ -42,7 +42,8 @@
           "",
           "Path where the test perf metrics should be stored using "
           "api/test/metrics/metric.proto proto format. File will contain "
-          "MetricsSet as a root proto");
+          "MetricsSet as a root proto. On iOS, this MUST be a file name "
+          "and the file will be stored under NSDocumentDirectory.");
 
 ABSL_FLAG(bool,
           export_perf_results_new_api,