blob: bb41e6530f16a252e3db5e34cab43c3d1c8fea4e [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
Patrik Höglundf83d0ef2019-10-04 10:07:38 +0000127 // TODO(bugs.webrtc.org/9792): we need to reference something from
128 // fileutils.h so that our downstream hack where we replace fileutils.cc
129 // works. Otherwise the downstream flag implementation will take over and
130 // botch the flag introduced by the hack. Remove this awful thing once the
131 // downstream implementation has been eliminated.
132 (void)webrtc::test::JoinFilename("horrible", "hack");
133
Artem Titov40a7a352018-10-15 15:25:34 +0200134 // InitFieldTrialsFromString stores the char*, so the char array must
135 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200136 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
137 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200138 webrtc::metrics::Enable();
139
140#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200141 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200142#endif
143
Niels Möller04a3cc12019-05-21 13:01:58 +0200144 // Initialize SSL which are used by several tests.
145 rtc::InitializeSSL();
146 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
147
Artem Titov40a7a352018-10-15 15:25:34 +0200148 // Ensure that main thread gets wrapped as an rtc::Thread.
149 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
150 // thread, or leave it to individual tests that need it. But as long as we
151 // have automatic thread wrapping, we need this to avoid that some other
152 // random thread (which one depending on which tests are run) gets
153 // automatically wrapped.
154 rtc::ThreadManager::Instance()->WrapCurrentThread();
155 RTC_CHECK(rtc::Thread::Current());
Yves Gerey050e38f2019-08-28 13:24:00 +0200156
157 if (capture_events) {
158 rtc::tracing::StopInternalCapture();
159 }
Artem Titov40a7a352018-10-15 15:25:34 +0200160 return 0;
161 }
162
163 int Run(int argc, char* argv[]) override {
164#if defined(WEBRTC_IOS)
165 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200166 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200167 rtc::test::RunTestsFromIOSApp();
168 return 0;
169#else
170 int exit_code = RUN_ALL_TESTS();
171
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200172 std::string chartjson_result_file =
173 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200174 if (!chartjson_result_file.empty()) {
175 webrtc::test::WritePerfResults(chartjson_result_file);
176 }
Artem Titov087be5c2019-09-12 20:30:54 +0200177 std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot);
178 if (!metrics_to_plot.empty()) {
179 if (metrics_to_plot.size() == 1 &&
180 metrics_to_plot[0] == kPlotAllMetrics) {
181 metrics_to_plot.clear();
182 }
183 webrtc::test::PrintPlottableResults(metrics_to_plot);
184 }
Artem Titov40a7a352018-10-15 15:25:34 +0200185
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200186 std::string result_filename =
187 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200188 if (!result_filename.empty()) {
189 std::ofstream result_file(result_filename);
190 result_file << "{\"version\": 3}";
191 result_file.close();
192 }
193
Yves Gerey53347b72018-10-19 15:04:04 +0200194#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
195 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
196 defined(UNDEFINED_SANITIZER)
197 // We want the test flagged as failed only for sanitizer defects,
198 // in which case the sanitizer will override exit code with 66.
199 return 0;
200#endif
201
Artem Titov40a7a352018-10-15 15:25:34 +0200202 return exit_code;
203#endif
204 }
205
206 ~TestMainImpl() override = default;
207
208 private:
209#if defined(WEBRTC_WIN)
210 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
211#endif
212};
213
214} // namespace
215
216std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200217 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200218}
219
220} // namespace webrtc