blob: 178eda7c75d1977ab07b5eeb13123b992709315a [file] [log] [blame]
Artem Titov40a7a352018-10-15 15:25:34 +02001/*
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
11#include "test/test_main_lib.h"
12
13#include <fstream>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020014#include <memory>
Artem Titov40a7a352018-10-15 15:25:34 +020015#include <string>
16
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020017#include "absl/flags/flag.h"
18#include "absl/flags/parse.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/checks.h"
Yves Gerey050e38f2019-08-28 13:24:00 +020020#include "rtc_base/event_tracer.h"
Artem Titov40a7a352018-10-15 15:25:34 +020021#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 13:01:58 +020022#include "rtc_base/ssl_adapter.h"
23#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 15:25:34 +020024#include "rtc_base/thread.h"
25#include "system_wrappers/include/field_trial.h"
26#include "system_wrappers/include/metrics.h"
27#include "test/field_trial.h"
28#include "test/gmock.h"
29#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "test/testsupport/file_utils.h"
Patrik Höglund2f283702019-10-14 10:12:18 +020031#include "test/testsupport/file_utils_override.h"
Artem Titov40a7a352018-10-15 15:25:34 +020032#include "test/testsupport/perf_test.h"
Patrik Höglund844600e2019-10-15 12:19:36 +020033#include "test/testsupport/resources_dir_flag.h"
Artem Titov40a7a352018-10-15 15:25:34 +020034
35#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080036#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 15:25:34 +020037#endif
38
39#if defined(WEBRTC_IOS)
40#include "test/ios/test_support.h"
41
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020042ABSL_FLAG(std::string,
43 NSTreatUnknownArgumentsAsOpen,
44 "",
45 "Intentionally ignored flag intended for iOS simulator.");
46ABSL_FLAG(std::string,
47 ApplePersistenceIgnoreState,
48 "",
49 "Intentionally ignored flag intended for iOS simulator.");
50ABSL_FLAG(
51 bool,
Artem Titov40a7a352018-10-15 15:25:34 +020052 save_chartjson_result,
53 false,
54 "Store the perf results in Documents/perf_result.json in the format "
55 "described by "
56 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
57 "data-format.md.");
58
59#else
60
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020061ABSL_FLAG(std::string,
62 isolated_script_test_output,
63 "",
64 "Path to output an empty JSON file which Chromium infra requires.");
Artem Titov40a7a352018-10-15 15:25:34 +020065
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020066ABSL_FLAG(
67 std::string,
Artem Titov40a7a352018-10-15 15:25:34 +020068 isolated_script_test_perf_output,
69 "",
70 "Path where the perf results should be stored in the JSON format described "
71 "by "
72 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
73 "data-format.md.");
74
Artem Titov087be5c2019-09-12 20:30:54 +020075constexpr char kPlotAllMetrics[] = "all";
76ABSL_FLAG(std::vector<std::string>,
77 plot,
78 {},
79 "List of metrics that should be exported for plotting (if they are "
80 "available). Example: psnr,ssim,encode_time. To plot all available "
81 " metrics pass 'all' as flag value");
82
Artem Titov40a7a352018-10-15 15:25:34 +020083#endif
84
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020085ABSL_FLAG(bool, logs, true, "print logs to stderr");
86ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020087
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020088ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 13:24:00 +020089 trace_event,
90 "",
91 "Path to collect trace events (json file) for chrome://tracing. "
92 "If not set, events aren't captured.");
93
94ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020095 force_fieldtrials,
96 "",
97 "Field trials control experimental feature code which can be forced. "
98 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
99 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 15:25:34 +0200100
101namespace webrtc {
102
103namespace {
104
105class TestMainImpl : public TestMain {
106 public:
Artem Titovb5541a02018-10-17 17:37:47 +0200107 int Init(int* argc, char* argv[]) override {
108 ::testing::InitGoogleMock(argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200109 absl::ParseCommandLine(*argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +0200110
Patrik Höglund2f283702019-10-14 10:12:18 +0200111 std::string resources_dir = absl::GetFlag(FLAGS_resources_dir);
112 if (!resources_dir.empty())
113 test::internal::OverrideResourcesDir(resources_dir);
114
Artem Titov40a7a352018-10-15 15:25:34 +0200115 // Default to LS_INFO, even for release builds to provide better test
116 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +0200117 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
118 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
119
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200120 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100121 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
122
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200123 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
124 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100125
Yves Gerey050e38f2019-08-28 13:24:00 +0200126 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
127 const bool capture_events = !trace_event_path.empty();
128 if (capture_events) {
129 rtc::tracing::SetupInternalTracer();
130 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
131 }
132
Patrik Höglundf83d0ef2019-10-04 10:07:38 +0000133 // TODO(bugs.webrtc.org/9792): we need to reference something from
134 // fileutils.h so that our downstream hack where we replace fileutils.cc
135 // works. Otherwise the downstream flag implementation will take over and
136 // botch the flag introduced by the hack. Remove this awful thing once the
137 // downstream implementation has been eliminated.
138 (void)webrtc::test::JoinFilename("horrible", "hack");
139
Artem Titov40a7a352018-10-15 15:25:34 +0200140 // InitFieldTrialsFromString stores the char*, so the char array must
141 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200142 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
143 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200144 webrtc::metrics::Enable();
145
146#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200147 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200148#endif
149
Niels Möller04a3cc12019-05-21 13:01:58 +0200150 // Initialize SSL which are used by several tests.
151 rtc::InitializeSSL();
152 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
153
Artem Titov40a7a352018-10-15 15:25:34 +0200154 // Ensure that main thread gets wrapped as an rtc::Thread.
155 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
156 // thread, or leave it to individual tests that need it. But as long as we
157 // have automatic thread wrapping, we need this to avoid that some other
158 // random thread (which one depending on which tests are run) gets
159 // automatically wrapped.
160 rtc::ThreadManager::Instance()->WrapCurrentThread();
161 RTC_CHECK(rtc::Thread::Current());
Yves Gerey050e38f2019-08-28 13:24:00 +0200162
163 if (capture_events) {
164 rtc::tracing::StopInternalCapture();
165 }
Artem Titov40a7a352018-10-15 15:25:34 +0200166 return 0;
167 }
168
169 int Run(int argc, char* argv[]) override {
170#if defined(WEBRTC_IOS)
171 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200172 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200173 rtc::test::RunTestsFromIOSApp();
174 return 0;
175#else
176 int exit_code = RUN_ALL_TESTS();
177
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200178 std::string chartjson_result_file =
179 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200180 if (!chartjson_result_file.empty()) {
181 webrtc::test::WritePerfResults(chartjson_result_file);
182 }
Artem Titov087be5c2019-09-12 20:30:54 +0200183 std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot);
184 if (!metrics_to_plot.empty()) {
185 if (metrics_to_plot.size() == 1 &&
186 metrics_to_plot[0] == kPlotAllMetrics) {
187 metrics_to_plot.clear();
188 }
189 webrtc::test::PrintPlottableResults(metrics_to_plot);
190 }
Artem Titov40a7a352018-10-15 15:25:34 +0200191
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200192 std::string result_filename =
193 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200194 if (!result_filename.empty()) {
195 std::ofstream result_file(result_filename);
196 result_file << "{\"version\": 3}";
197 result_file.close();
198 }
199
Yves Gerey53347b72018-10-19 15:04:04 +0200200#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
201 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
202 defined(UNDEFINED_SANITIZER)
203 // We want the test flagged as failed only for sanitizer defects,
204 // in which case the sanitizer will override exit code with 66.
205 return 0;
206#endif
207
Artem Titov40a7a352018-10-15 15:25:34 +0200208 return exit_code;
209#endif
210 }
211
212 ~TestMainImpl() override = default;
213
214 private:
215#if defined(WEBRTC_WIN)
216 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
217#endif
218};
219
220} // namespace
221
222std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200223 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200224}
225
226} // namespace webrtc