blob: 7170163346a3ccfbf377d8c4f6a6478544dfbd03 [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"
Artem Titov694b74b2020-02-20 23:52:26 +010018#include "absl/memory/memory.h"
Tommiec3ba732020-05-17 14:33:40 +020019#include "absl/strings/match.h"
Artem Titov694b74b2020-02-20 23:52:26 +010020#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/checks.h"
Yves Gerey050e38f2019-08-28 13:24:00 +020022#include "rtc_base/event_tracer.h"
Artem Titov40a7a352018-10-15 15:25:34 +020023#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 13:01:58 +020024#include "rtc_base/ssl_adapter.h"
25#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 15:25:34 +020026#include "rtc_base/thread.h"
27#include "system_wrappers/include/field_trial.h"
28#include "system_wrappers/include/metrics.h"
29#include "test/field_trial.h"
Artem Titov40a7a352018-10-15 15:25:34 +020030#include "test/gtest.h"
Artem Titov40a7a352018-10-15 15:25:34 +020031#include "test/testsupport/perf_test.h"
Patrik Höglund844600e2019-10-15 12:19:36 +020032#include "test/testsupport/resources_dir_flag.h"
Artem Titov40a7a352018-10-15 15:25:34 +020033
34#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 15:25:34 +020036#endif
37
38#if defined(WEBRTC_IOS)
39#include "test/ios/test_support.h"
40
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020041ABSL_FLAG(std::string,
42 NSTreatUnknownArgumentsAsOpen,
43 "",
44 "Intentionally ignored flag intended for iOS simulator.");
45ABSL_FLAG(std::string,
46 ApplePersistenceIgnoreState,
47 "",
48 "Intentionally ignored flag intended for iOS simulator.");
Patrik Höglund34288272020-03-19 08:51:35 +010049
50// This is the cousin of isolated_script_test_perf_output, but we can't dictate
51// where to write on iOS so the semantics of this flag are a bit different.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020052ABSL_FLAG(
53 bool,
Patrik Höglund34288272020-03-19 08:51:35 +010054 write_perf_output_on_ios,
Artem Titov40a7a352018-10-15 15:25:34 +020055 false,
Patrik Höglund34288272020-03-19 08:51:35 +010056 "Store the perf results in Documents/perftest_result.pb in the format "
57 "described by histogram.proto in "
58 "https://chromium.googlesource.com/catapult/.");
Artem Titov40a7a352018-10-15 15:25:34 +020059
60#else
61
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020062ABSL_FLAG(
63 std::string,
Artem Titov40a7a352018-10-15 15:25:34 +020064 isolated_script_test_perf_output,
65 "",
Patrik Höglund34288272020-03-19 08:51:35 +010066 "Path where the perf results should be stored in proto format described "
67 "described by histogram.proto in "
68 "https://chromium.googlesource.com/catapult/.");
69
Artem Titov694b74b2020-02-20 23:52:26 +010070#endif
Artem Titov40a7a352018-10-15 15:25:34 +020071
Artem Titov087be5c2019-09-12 20:30:54 +020072constexpr char kPlotAllMetrics[] = "all";
73ABSL_FLAG(std::vector<std::string>,
74 plot,
75 {},
76 "List of metrics that should be exported for plotting (if they are "
77 "available). Example: psnr,ssim,encode_time. To plot all available "
78 " metrics pass 'all' as flag value");
79
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020080ABSL_FLAG(bool, logs, true, "print logs to stderr");
81ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020082
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020083ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 13:24:00 +020084 trace_event,
85 "",
86 "Path to collect trace events (json file) for chrome://tracing. "
87 "If not set, events aren't captured.");
88
89ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020090 force_fieldtrials,
91 "",
92 "Field trials control experimental feature code which can be forced. "
93 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
94 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 15:25:34 +020095
96namespace webrtc {
97
98namespace {
99
100class TestMainImpl : public TestMain {
101 public:
Tommi9b7232a2020-05-15 10:09:27 +0200102 // In order to set up a fresh rtc::Thread state for each test and avoid
103 // accidentally carrying over pending tasks that might be sent from one test
104 // and executed while another test is running, we inject a TestListener
105 // that sets up a new rtc::Thread instance for the main thread, per test.
106 class TestListener : public ::testing::EmptyTestEventListener {
107 public:
108 TestListener() = default;
109
110 private:
Tommiec3ba732020-05-17 14:33:40 +0200111 bool IsDeathTest(const char* test_case_name, const char* test_name) {
112 // Workaround to avoid wrapping the main thread when we run death tests.
113 // The approach we take for detecting death tests is essentially the same
114 // as gtest does internally. Gtest does this:
115 //
116 // static const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*";
117 // ::testing::internal::UnitTestOptions::MatchesFilter(
118 // test_case_name, kDeathTestCaseFilter);
119 //
120 // Our approach is a little more straight forward.
121 if (absl::EndsWith(test_case_name, "DeathTest"))
122 return true;
123
124 return absl::EndsWith(test_name, "DeathTest");
125 }
126
Tommi9b7232a2020-05-15 10:09:27 +0200127 void OnTestStart(const ::testing::TestInfo& test_info) override {
Tommiec3ba732020-05-17 14:33:40 +0200128 if (!IsDeathTest(test_info.test_suite_name(), test_info.name())) {
129 // Ensure that main thread gets wrapped as an rtc::Thread.
130 // TODO(bugs.webrtc.org/9714): It might be better to avoid wrapping the
131 // main thread, or leave it to individual tests that need it. But as
132 // long as we have automatic thread wrapping, we need this to avoid that
133 // some other random thread (which one depending on which tests are run)
134 // gets automatically wrapped.
135 thread_ = rtc::Thread::CreateWithSocketServer();
136 thread_->WrapCurrent();
137 RTC_DCHECK_EQ(rtc::Thread::Current(), thread_.get());
138 } else {
139 RTC_LOG(LS_INFO) << "No thread auto wrap for death test.";
140 }
Tommi9b7232a2020-05-15 10:09:27 +0200141 }
142
143 void OnTestEnd(const ::testing::TestInfo& test_info) override {
144 // Terminate the message loop. Note that if the test failed to clean
145 // up pending messages, this may execute part of the test. Ideally we
146 // should print a warning message here, or even fail the test if it leaks.
Tommiec3ba732020-05-17 14:33:40 +0200147 if (thread_) {
148 thread_->Quit(); // Signal quit.
149 thread_->Run(); // Flush + process Quit signal.
150 thread_->UnwrapCurrent();
151 thread_ = nullptr;
152 }
Tommi9b7232a2020-05-15 10:09:27 +0200153 }
154
155 std::unique_ptr<rtc::Thread> thread_;
156 };
157
Artem Titovbcb42f12020-08-11 12:19:18 +0200158 int Init(int* argc, char* argv[]) override { return Init(); }
Artem Titov40a7a352018-10-15 15:25:34 +0200159
Artem Titovbcb42f12020-08-11 12:19:18 +0200160 int Init() override {
Patrik Höglund2bc1ea02019-10-16 10:24:35 +0200161 // Make sure we always pull in the --resources_dir flag, even if the test
162 // binary doesn't link with fileutils (downstream expects all test mains to
163 // have this flag).
164 (void)absl::GetFlag(FLAGS_resources_dir);
Patrik Höglund2f283702019-10-14 10:12:18 +0200165
Artem Titov40a7a352018-10-15 15:25:34 +0200166 // Default to LS_INFO, even for release builds to provide better test
167 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +0200168 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
169 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
170
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200171 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100172 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
173
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200174 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
175 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100176
Artem Titov40a7a352018-10-15 15:25:34 +0200177 // InitFieldTrialsFromString stores the char*, so the char array must
178 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200179 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
180 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200181 webrtc::metrics::Enable();
182
183#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200184 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200185#endif
186
Niels Möller04a3cc12019-05-21 13:01:58 +0200187 // Initialize SSL which are used by several tests.
188 rtc::InitializeSSL();
189 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
190
Tommi9b7232a2020-05-15 10:09:27 +0200191 ::testing::UnitTest::GetInstance()->listeners().Append(new TestListener());
Yves Gerey050e38f2019-08-28 13:24:00 +0200192
Artem Titov40a7a352018-10-15 15:25:34 +0200193 return 0;
194 }
195
196 int Run(int argc, char* argv[]) override {
Yves Gerey071d0252020-01-28 20:07:19 +0100197 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
198 const bool capture_events = !trace_event_path.empty();
199 if (capture_events) {
200 rtc::tracing::SetupInternalTracer();
201 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
202 }
203
Artem Titov694b74b2020-02-20 23:52:26 +0100204 absl::optional<std::vector<std::string>> metrics_to_plot =
205 absl::GetFlag(FLAGS_plot);
206
207 if (metrics_to_plot->empty()) {
208 metrics_to_plot = absl::nullopt;
209 } else {
210 if (metrics_to_plot->size() == 1 &&
211 (*metrics_to_plot)[0] == kPlotAllMetrics) {
212 metrics_to_plot->clear();
213 }
214 }
215
Artem Titov40a7a352018-10-15 15:25:34 +0200216#if defined(WEBRTC_IOS)
217 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Patrik Höglund34288272020-03-19 08:51:35 +0100218 absl::GetFlag(FLAGS_write_perf_output_on_ios),
Artem Titov694b74b2020-02-20 23:52:26 +0100219 metrics_to_plot);
Artem Titov40a7a352018-10-15 15:25:34 +0200220 rtc::test::RunTestsFromIOSApp();
Yves Gerey071d0252020-01-28 20:07:19 +0100221 int exit_code = 0;
Artem Titov40a7a352018-10-15 15:25:34 +0200222#else
223 int exit_code = RUN_ALL_TESTS();
224
Patrik Höglunda7a01732020-03-27 19:22:36 +0100225 std::string perf_output_file =
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200226 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Patrik Höglunda7a01732020-03-27 19:22:36 +0100227 if (!perf_output_file.empty()) {
228 if (!webrtc::test::WritePerfResults(perf_output_file)) {
Patrik Höglundb8e69ef2020-03-12 09:39:40 +0100229 return 1;
230 }
Artem Titov40a7a352018-10-15 15:25:34 +0200231 }
Artem Titov694b74b2020-02-20 23:52:26 +0100232 if (metrics_to_plot) {
233 webrtc::test::PrintPlottableResults(*metrics_to_plot);
Artem Titov087be5c2019-09-12 20:30:54 +0200234 }
Yves Gerey071d0252020-01-28 20:07:19 +0100235#endif
236
237 if (capture_events) {
238 rtc::tracing::StopInternalCapture();
239 }
Artem Titov40a7a352018-10-15 15:25:34 +0200240
Yves Gerey53347b72018-10-19 15:04:04 +0200241#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
242 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
243 defined(UNDEFINED_SANITIZER)
244 // We want the test flagged as failed only for sanitizer defects,
245 // in which case the sanitizer will override exit code with 66.
Yves Gerey071d0252020-01-28 20:07:19 +0100246 exit_code = 0;
Yves Gerey53347b72018-10-19 15:04:04 +0200247#endif
248
Artem Titov40a7a352018-10-15 15:25:34 +0200249 return exit_code;
Artem Titov40a7a352018-10-15 15:25:34 +0200250 }
251
252 ~TestMainImpl() override = default;
253
254 private:
255#if defined(WEBRTC_WIN)
256 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
257#endif
258};
259
260} // namespace
261
262std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200263 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200264}
265
266} // namespace webrtc