You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/test_main_lib.h"
  11. #include <fstream>
  12. #include <memory>
  13. #include <string>
  14. #include "absl/flags/flag.h"
  15. #include "absl/flags/parse.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/event_tracer.h"
  18. #include "rtc_base/logging.h"
  19. #include "rtc_base/ssl_adapter.h"
  20. #include "rtc_base/ssl_stream_adapter.h"
  21. #include "rtc_base/thread.h"
  22. #include "system_wrappers/include/field_trial.h"
  23. #include "system_wrappers/include/metrics.h"
  24. #include "test/field_trial.h"
  25. #include "test/gmock.h"
  26. #include "test/gtest.h"
  27. #include "test/testsupport/perf_test.h"
  28. #include "test/testsupport/resources_dir_flag.h"
  29. #if defined(WEBRTC_WIN)
  30. #include "rtc_base/win32_socket_init.h"
  31. #endif
  32. #if defined(WEBRTC_IOS)
  33. #include "test/ios/test_support.h"
  34. ABSL_FLAG(std::string,
  35. NSTreatUnknownArgumentsAsOpen,
  36. "",
  37. "Intentionally ignored flag intended for iOS simulator.");
  38. ABSL_FLAG(std::string,
  39. ApplePersistenceIgnoreState,
  40. "",
  41. "Intentionally ignored flag intended for iOS simulator.");
  42. ABSL_FLAG(
  43. bool,
  44. save_chartjson_result,
  45. false,
  46. "Store the perf results in Documents/perf_result.json in the format "
  47. "described by "
  48. "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
  49. "data-format.md.");
  50. #else
  51. ABSL_FLAG(std::string,
  52. isolated_script_test_output,
  53. "",
  54. "Path to output an empty JSON file which Chromium infra requires.");
  55. ABSL_FLAG(
  56. std::string,
  57. isolated_script_test_perf_output,
  58. "",
  59. "Path where the perf results should be stored in the JSON format described "
  60. "by "
  61. "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
  62. "data-format.md.");
  63. constexpr char kPlotAllMetrics[] = "all";
  64. ABSL_FLAG(std::vector<std::string>,
  65. plot,
  66. {},
  67. "List of metrics that should be exported for plotting (if they are "
  68. "available). Example: psnr,ssim,encode_time. To plot all available "
  69. " metrics pass 'all' as flag value");
  70. #endif
  71. ABSL_FLAG(bool, logs, true, "print logs to stderr");
  72. ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
  73. ABSL_FLAG(std::string,
  74. trace_event,
  75. "",
  76. "Path to collect trace events (json file) for chrome://tracing. "
  77. "If not set, events aren't captured.");
  78. ABSL_FLAG(std::string,
  79. force_fieldtrials,
  80. "",
  81. "Field trials control experimental feature code which can be forced. "
  82. "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
  83. " will assign the group Enable to field trial WebRTC-FooFeature.");
  84. namespace webrtc {
  85. namespace {
  86. class TestMainImpl : public TestMain {
  87. public:
  88. int Init(int* argc, char* argv[]) override {
  89. ::testing::InitGoogleMock(argc, argv);
  90. absl::ParseCommandLine(*argc, argv);
  91. // Make sure we always pull in the --resources_dir flag, even if the test
  92. // binary doesn't link with fileutils (downstream expects all test mains to
  93. // have this flag).
  94. (void)absl::GetFlag(FLAGS_resources_dir);
  95. // Default to LS_INFO, even for release builds to provide better test
  96. // logging.
  97. if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
  98. rtc::LogMessage::LogToDebug(rtc::LS_INFO);
  99. if (absl::GetFlag(FLAGS_verbose))
  100. rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
  101. rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
  102. absl::GetFlag(FLAGS_verbose));
  103. std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
  104. const bool capture_events = !trace_event_path.empty();
  105. if (capture_events) {
  106. rtc::tracing::SetupInternalTracer();
  107. rtc::tracing::StartInternalCapture(trace_event_path.c_str());
  108. }
  109. // InitFieldTrialsFromString stores the char*, so the char array must
  110. // outlive the application.
  111. field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
  112. webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
  113. webrtc::metrics::Enable();
  114. #if defined(WEBRTC_WIN)
  115. winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
  116. #endif
  117. // Initialize SSL which are used by several tests.
  118. rtc::InitializeSSL();
  119. rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
  120. // Ensure that main thread gets wrapped as an rtc::Thread.
  121. // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
  122. // thread, or leave it to individual tests that need it. But as long as we
  123. // have automatic thread wrapping, we need this to avoid that some other
  124. // random thread (which one depending on which tests are run) gets
  125. // automatically wrapped.
  126. rtc::ThreadManager::Instance()->WrapCurrentThread();
  127. RTC_CHECK(rtc::Thread::Current());
  128. if (capture_events) {
  129. rtc::tracing::StopInternalCapture();
  130. }
  131. return 0;
  132. }
  133. int Run(int argc, char* argv[]) override {
  134. #if defined(WEBRTC_IOS)
  135. rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
  136. absl::GetFlag(FLAGS_save_chartjson_result));
  137. rtc::test::RunTestsFromIOSApp();
  138. return 0;
  139. #else
  140. int exit_code = RUN_ALL_TESTS();
  141. std::string chartjson_result_file =
  142. absl::GetFlag(FLAGS_isolated_script_test_perf_output);
  143. if (!chartjson_result_file.empty()) {
  144. webrtc::test::WritePerfResults(chartjson_result_file);
  145. }
  146. std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot);
  147. if (!metrics_to_plot.empty()) {
  148. if (metrics_to_plot.size() == 1 &&
  149. metrics_to_plot[0] == kPlotAllMetrics) {
  150. metrics_to_plot.clear();
  151. }
  152. webrtc::test::PrintPlottableResults(metrics_to_plot);
  153. }
  154. std::string result_filename =
  155. absl::GetFlag(FLAGS_isolated_script_test_output);
  156. if (!result_filename.empty()) {
  157. std::ofstream result_file(result_filename);
  158. result_file << "{\"version\": 3}";
  159. result_file.close();
  160. }
  161. #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
  162. defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
  163. defined(UNDEFINED_SANITIZER)
  164. // We want the test flagged as failed only for sanitizer defects,
  165. // in which case the sanitizer will override exit code with 66.
  166. return 0;
  167. #endif
  168. return exit_code;
  169. #endif
  170. }
  171. ~TestMainImpl() override = default;
  172. private:
  173. #if defined(WEBRTC_WIN)
  174. std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
  175. #endif
  176. };
  177. } // namespace
  178. std::unique_ptr<TestMain> TestMain::Create() {
  179. return std::make_unique<TestMainImpl>();
  180. }
  181. } // namespace webrtc