blob: 59352caefd83fe3e8425a10c8bd55de05b54c6a2 [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"
Artem Titov40a7a352018-10-15 15:25:34 +020031#include "test/testsupport/perf_test.h"
32
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
109 // Default to LS_INFO, even for release builds to provide better test
110 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +0200111 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
112 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
113
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200114 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100115 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
116
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200117 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
118 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100119
Yves Gerey050e38f2019-08-28 13:24:00 +0200120 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
121 const bool capture_events = !trace_event_path.empty();
122 if (capture_events) {
123 rtc::tracing::SetupInternalTracer();
124 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
125 }
126
Artem Titov40a7a352018-10-15 15:25:34 +0200127 // InitFieldTrialsFromString stores the char*, so the char array must
128 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200129 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
130 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200131 webrtc::metrics::Enable();
132
133#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200134 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200135#endif
136
Niels Möller04a3cc12019-05-21 13:01:58 +0200137 // Initialize SSL which are used by several tests.
138 rtc::InitializeSSL();
139 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
140
Artem Titov40a7a352018-10-15 15:25:34 +0200141 // Ensure that main thread gets wrapped as an rtc::Thread.
142 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
143 // thread, or leave it to individual tests that need it. But as long as we
144 // have automatic thread wrapping, we need this to avoid that some other
145 // random thread (which one depending on which tests are run) gets
146 // automatically wrapped.
147 rtc::ThreadManager::Instance()->WrapCurrentThread();
148 RTC_CHECK(rtc::Thread::Current());
Yves Gerey050e38f2019-08-28 13:24:00 +0200149
150 if (capture_events) {
151 rtc::tracing::StopInternalCapture();
152 }
Artem Titov40a7a352018-10-15 15:25:34 +0200153 return 0;
154 }
155
156 int Run(int argc, char* argv[]) override {
157#if defined(WEBRTC_IOS)
158 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200159 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200160 rtc::test::RunTestsFromIOSApp();
161 return 0;
162#else
163 int exit_code = RUN_ALL_TESTS();
164
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200165 std::string chartjson_result_file =
166 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200167 if (!chartjson_result_file.empty()) {
168 webrtc::test::WritePerfResults(chartjson_result_file);
169 }
Artem Titov087be5c2019-09-12 20:30:54 +0200170 std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot);
171 if (!metrics_to_plot.empty()) {
172 if (metrics_to_plot.size() == 1 &&
173 metrics_to_plot[0] == kPlotAllMetrics) {
174 metrics_to_plot.clear();
175 }
176 webrtc::test::PrintPlottableResults(metrics_to_plot);
177 }
Artem Titov40a7a352018-10-15 15:25:34 +0200178
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200179 std::string result_filename =
180 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200181 if (!result_filename.empty()) {
182 std::ofstream result_file(result_filename);
183 result_file << "{\"version\": 3}";
184 result_file.close();
185 }
186
Yves Gerey53347b72018-10-19 15:04:04 +0200187#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
188 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
189 defined(UNDEFINED_SANITIZER)
190 // We want the test flagged as failed only for sanitizer defects,
191 // in which case the sanitizer will override exit code with 66.
192 return 0;
193#endif
194
Artem Titov40a7a352018-10-15 15:25:34 +0200195 return exit_code;
196#endif
197 }
198
199 ~TestMainImpl() override = default;
200
201 private:
202#if defined(WEBRTC_WIN)
203 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
204#endif
205};
206
207} // namespace
208
209std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200210 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200211}
212
213} // namespace webrtc