blob: df74182850766abd298db59141b1f7f81bdac70c [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"
Artem Titov40a7a352018-10-15 15:25:34 +020030#include "test/testsupport/perf_test.h"
Patrik Höglund844600e2019-10-15 12:19:36 +020031#include "test/testsupport/resources_dir_flag.h"
Artem Titov40a7a352018-10-15 15:25:34 +020032
33#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 15:25:34 +020035#endif
36
37#if defined(WEBRTC_IOS)
38#include "test/ios/test_support.h"
39
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020040ABSL_FLAG(std::string,
41 NSTreatUnknownArgumentsAsOpen,
42 "",
43 "Intentionally ignored flag intended for iOS simulator.");
44ABSL_FLAG(std::string,
45 ApplePersistenceIgnoreState,
46 "",
47 "Intentionally ignored flag intended for iOS simulator.");
48ABSL_FLAG(
49 bool,
Artem Titov40a7a352018-10-15 15:25:34 +020050 save_chartjson_result,
51 false,
52 "Store the perf results in Documents/perf_result.json in the format "
53 "described by "
54 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
55 "data-format.md.");
56
57#else
58
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020059ABSL_FLAG(std::string,
60 isolated_script_test_output,
61 "",
62 "Path to output an empty JSON file which Chromium infra requires.");
Artem Titov40a7a352018-10-15 15:25:34 +020063
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020064ABSL_FLAG(
65 std::string,
Artem Titov40a7a352018-10-15 15:25:34 +020066 isolated_script_test_perf_output,
67 "",
68 "Path where the perf results should be stored in the JSON format described "
69 "by "
70 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
71 "data-format.md.");
72
Artem Titov087be5c2019-09-12 20:30:54 +020073constexpr char kPlotAllMetrics[] = "all";
74ABSL_FLAG(std::vector<std::string>,
75 plot,
76 {},
77 "List of metrics that should be exported for plotting (if they are "
78 "available). Example: psnr,ssim,encode_time. To plot all available "
79 " metrics pass 'all' as flag value");
80
Artem Titov40a7a352018-10-15 15:25:34 +020081#endif
82
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020083ABSL_FLAG(bool, logs, true, "print logs to stderr");
84ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020085
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020086ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 13:24:00 +020087 trace_event,
88 "",
89 "Path to collect trace events (json file) for chrome://tracing. "
90 "If not set, events aren't captured.");
91
92ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020093 force_fieldtrials,
94 "",
95 "Field trials control experimental feature code which can be forced. "
96 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
97 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 15:25:34 +020098
99namespace webrtc {
100
101namespace {
102
103class TestMainImpl : public TestMain {
104 public:
Artem Titovb5541a02018-10-17 17:37:47 +0200105 int Init(int* argc, char* argv[]) override {
106 ::testing::InitGoogleMock(argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200107 absl::ParseCommandLine(*argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +0200108
Patrik Höglund2bc1ea02019-10-16 10:24:35 +0200109 // Make sure we always pull in the --resources_dir flag, even if the test
110 // binary doesn't link with fileutils (downstream expects all test mains to
111 // have this flag).
112 (void)absl::GetFlag(FLAGS_resources_dir);
Patrik Höglund2f283702019-10-14 10:12:18 +0200113
Artem Titov40a7a352018-10-15 15:25:34 +0200114 // Default to LS_INFO, even for release builds to provide better test
115 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +0200116 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
117 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
118
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200119 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100120 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
121
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200122 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
123 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100124
Yves Gerey050e38f2019-08-28 13:24:00 +0200125 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
126 const bool capture_events = !trace_event_path.empty();
127 if (capture_events) {
128 rtc::tracing::SetupInternalTracer();
129 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
130 }
131
Artem Titov40a7a352018-10-15 15:25:34 +0200132 // InitFieldTrialsFromString stores the char*, so the char array must
133 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200134 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
135 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200136 webrtc::metrics::Enable();
137
138#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200139 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200140#endif
141
Niels Möller04a3cc12019-05-21 13:01:58 +0200142 // Initialize SSL which are used by several tests.
143 rtc::InitializeSSL();
144 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
145
Artem Titov40a7a352018-10-15 15:25:34 +0200146 // Ensure that main thread gets wrapped as an rtc::Thread.
147 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
148 // thread, or leave it to individual tests that need it. But as long as we
149 // have automatic thread wrapping, we need this to avoid that some other
150 // random thread (which one depending on which tests are run) gets
151 // automatically wrapped.
152 rtc::ThreadManager::Instance()->WrapCurrentThread();
153 RTC_CHECK(rtc::Thread::Current());
Yves Gerey050e38f2019-08-28 13:24:00 +0200154
155 if (capture_events) {
156 rtc::tracing::StopInternalCapture();
157 }
Artem Titov40a7a352018-10-15 15:25:34 +0200158 return 0;
159 }
160
161 int Run(int argc, char* argv[]) override {
162#if defined(WEBRTC_IOS)
163 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200164 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200165 rtc::test::RunTestsFromIOSApp();
166 return 0;
167#else
168 int exit_code = RUN_ALL_TESTS();
169
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200170 std::string chartjson_result_file =
171 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200172 if (!chartjson_result_file.empty()) {
173 webrtc::test::WritePerfResults(chartjson_result_file);
174 }
Artem Titov087be5c2019-09-12 20:30:54 +0200175 std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot);
176 if (!metrics_to_plot.empty()) {
177 if (metrics_to_plot.size() == 1 &&
178 metrics_to_plot[0] == kPlotAllMetrics) {
179 metrics_to_plot.clear();
180 }
181 webrtc::test::PrintPlottableResults(metrics_to_plot);
182 }
Artem Titov40a7a352018-10-15 15:25:34 +0200183
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200184 std::string result_filename =
185 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200186 if (!result_filename.empty()) {
187 std::ofstream result_file(result_filename);
188 result_file << "{\"version\": 3}";
189 result_file.close();
190 }
191
Yves Gerey53347b72018-10-19 15:04:04 +0200192#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
193 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
194 defined(UNDEFINED_SANITIZER)
195 // We want the test flagged as failed only for sanitizer defects,
196 // in which case the sanitizer will override exit code with 66.
197 return 0;
198#endif
199
Artem Titov40a7a352018-10-15 15:25:34 +0200200 return exit_code;
201#endif
202 }
203
204 ~TestMainImpl() override = default;
205
206 private:
207#if defined(WEBRTC_WIN)
208 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
209#endif
210};
211
212} // namespace
213
214std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200215 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200216}
217
218} // namespace webrtc