blob: f5e02341f3f63d88ecdcb20ad1f00cb557337431 [file] [log] [blame]
Artem Titov40a7a352018-10-15 13:25:341/*
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 15:06:1814#include <memory>
Artem Titov40a7a352018-10-15 13:25:3415#include <string>
16
Mirko Bonadei2ab97f62019-07-18 11:44:1217#include "absl/flags/flag.h"
18#include "absl/flags/parse.h"
Artem Titov694b74b2020-02-20 22:52:2619#include "absl/memory/memory.h"
Tommiec3ba732020-05-17 12:33:4020#include "absl/strings/match.h"
Artem Titov694b74b2020-02-20 22:52:2621#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 15:47:4922#include "rtc_base/checks.h"
Yves Gerey050e38f2019-08-28 11:24:0023#include "rtc_base/event_tracer.h"
Artem Titov40a7a352018-10-15 13:25:3424#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 11:01:5825#include "rtc_base/ssl_adapter.h"
26#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 13:25:3427#include "rtc_base/thread.h"
28#include "system_wrappers/include/field_trial.h"
29#include "system_wrappers/include/metrics.h"
30#include "test/field_trial.h"
31#include "test/gmock.h"
32#include "test/gtest.h"
Artem Titov40a7a352018-10-15 13:25:3433#include "test/testsupport/perf_test.h"
Patrik Höglund844600e2019-10-15 10:19:3634#include "test/testsupport/resources_dir_flag.h"
Artem Titov40a7a352018-10-15 13:25:3435
36#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 17:11:0037#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 13:25:3438#endif
39
40#if defined(WEBRTC_IOS)
41#include "test/ios/test_support.h"
42
Mirko Bonadei2ab97f62019-07-18 11:44:1243ABSL_FLAG(std::string,
44 NSTreatUnknownArgumentsAsOpen,
45 "",
46 "Intentionally ignored flag intended for iOS simulator.");
47ABSL_FLAG(std::string,
48 ApplePersistenceIgnoreState,
49 "",
50 "Intentionally ignored flag intended for iOS simulator.");
Patrik Höglund34288272020-03-19 07:51:3551
52// This is the cousin of isolated_script_test_perf_output, but we can't dictate
53// where to write on iOS so the semantics of this flag are a bit different.
Mirko Bonadei2ab97f62019-07-18 11:44:1254ABSL_FLAG(
55 bool,
Patrik Höglund34288272020-03-19 07:51:3556 write_perf_output_on_ios,
Artem Titov40a7a352018-10-15 13:25:3457 false,
Patrik Höglund34288272020-03-19 07:51:3558 "Store the perf results in Documents/perftest_result.pb in the format "
59 "described by histogram.proto in "
60 "https://chromium.googlesource.com/catapult/.");
Artem Titov40a7a352018-10-15 13:25:3461
62#else
63
Mirko Bonadei2ab97f62019-07-18 11:44:1264ABSL_FLAG(
65 std::string,
Artem Titov40a7a352018-10-15 13:25:3466 isolated_script_test_perf_output,
67 "",
Patrik Höglund34288272020-03-19 07:51:3568 "Path where the perf results should be stored in proto format described "
69 "described by histogram.proto in "
70 "https://chromium.googlesource.com/catapult/.");
71
Artem Titov694b74b2020-02-20 22:52:2672#endif
Artem Titov40a7a352018-10-15 13:25:3473
Artem Titov087be5c2019-09-12 18:30:5474constexpr char kPlotAllMetrics[] = "all";
75ABSL_FLAG(std::vector<std::string>,
76 plot,
77 {},
78 "List of metrics that should be exported for plotting (if they are "
79 "available). Example: psnr,ssim,encode_time. To plot all available "
80 " metrics pass 'all' as flag value");
81
Mirko Bonadei2ab97f62019-07-18 11:44:1282ABSL_FLAG(bool, logs, true, "print logs to stderr");
83ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 13:25:3484
Mirko Bonadei2ab97f62019-07-18 11:44:1285ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 11:24:0086 trace_event,
87 "",
88 "Path to collect trace events (json file) for chrome://tracing. "
89 "If not set, events aren't captured.");
90
91ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 11:44:1292 force_fieldtrials,
93 "",
94 "Field trials control experimental feature code which can be forced. "
95 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
96 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 13:25:3497
98namespace webrtc {
99
100namespace {
101
102class TestMainImpl : public TestMain {
103 public:
Tommi9b7232a2020-05-15 08:09:27104 // In order to set up a fresh rtc::Thread state for each test and avoid
105 // accidentally carrying over pending tasks that might be sent from one test
106 // and executed while another test is running, we inject a TestListener
107 // that sets up a new rtc::Thread instance for the main thread, per test.
108 class TestListener : public ::testing::EmptyTestEventListener {
109 public:
110 TestListener() = default;
111
112 private:
Tommiec3ba732020-05-17 12:33:40113 bool IsDeathTest(const char* test_case_name, const char* test_name) {
114 // Workaround to avoid wrapping the main thread when we run death tests.
115 // The approach we take for detecting death tests is essentially the same
116 // as gtest does internally. Gtest does this:
117 //
118 // static const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*";
119 // ::testing::internal::UnitTestOptions::MatchesFilter(
120 // test_case_name, kDeathTestCaseFilter);
121 //
122 // Our approach is a little more straight forward.
123 if (absl::EndsWith(test_case_name, "DeathTest"))
124 return true;
125
126 return absl::EndsWith(test_name, "DeathTest");
127 }
128
Tommi9b7232a2020-05-15 08:09:27129 void OnTestStart(const ::testing::TestInfo& test_info) override {
Tommiec3ba732020-05-17 12:33:40130 if (!IsDeathTest(test_info.test_suite_name(), test_info.name())) {
131 // Ensure that main thread gets wrapped as an rtc::Thread.
132 // TODO(bugs.webrtc.org/9714): It might be better to avoid wrapping the
133 // main thread, or leave it to individual tests that need it. But as
134 // long as we have automatic thread wrapping, we need this to avoid that
135 // some other random thread (which one depending on which tests are run)
136 // gets automatically wrapped.
137 thread_ = rtc::Thread::CreateWithSocketServer();
138 thread_->WrapCurrent();
139 RTC_DCHECK_EQ(rtc::Thread::Current(), thread_.get());
140 } else {
141 RTC_LOG(LS_INFO) << "No thread auto wrap for death test.";
142 }
Tommi9b7232a2020-05-15 08:09:27143 }
144
145 void OnTestEnd(const ::testing::TestInfo& test_info) override {
146 // Terminate the message loop. Note that if the test failed to clean
147 // up pending messages, this may execute part of the test. Ideally we
148 // should print a warning message here, or even fail the test if it leaks.
Tommiec3ba732020-05-17 12:33:40149 if (thread_) {
150 thread_->Quit(); // Signal quit.
151 thread_->Run(); // Flush + process Quit signal.
152 thread_->UnwrapCurrent();
153 thread_ = nullptr;
154 }
Tommi9b7232a2020-05-15 08:09:27155 }
156
157 std::unique_ptr<rtc::Thread> thread_;
158 };
159
Artem Titovb5541a02018-10-17 15:37:47160 int Init(int* argc, char* argv[]) override {
161 ::testing::InitGoogleMock(argc, argv);
Mirko Bonadei2ab97f62019-07-18 11:44:12162 absl::ParseCommandLine(*argc, argv);
Artem Titov40a7a352018-10-15 13:25:34163
Patrik Höglund2bc1ea02019-10-16 08:24:35164 // Make sure we always pull in the --resources_dir flag, even if the test
165 // binary doesn't link with fileutils (downstream expects all test mains to
166 // have this flag).
167 (void)absl::GetFlag(FLAGS_resources_dir);
Patrik Höglund2f283702019-10-14 08:12:18168
Artem Titov40a7a352018-10-15 13:25:34169 // Default to LS_INFO, even for release builds to provide better test
170 // logging.
Artem Titov40a7a352018-10-15 13:25:34171 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
172 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
173
Mirko Bonadei2ab97f62019-07-18 11:44:12174 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 10:28:45175 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
176
Mirko Bonadei2ab97f62019-07-18 11:44:12177 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
178 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 10:28:45179
Artem Titov40a7a352018-10-15 13:25:34180 // InitFieldTrialsFromString stores the char*, so the char array must
181 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 11:44:12182 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
183 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 13:25:34184 webrtc::metrics::Enable();
185
186#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 15:06:18187 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 13:25:34188#endif
189
Niels Möller04a3cc12019-05-21 11:01:58190 // Initialize SSL which are used by several tests.
191 rtc::InitializeSSL();
192 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
193
Tommi9b7232a2020-05-15 08:09:27194 ::testing::UnitTest::GetInstance()->listeners().Append(new TestListener());
Yves Gerey050e38f2019-08-28 11:24:00195
Artem Titov40a7a352018-10-15 13:25:34196 return 0;
197 }
198
199 int Run(int argc, char* argv[]) override {
Yves Gerey071d0252020-01-28 19:07:19200 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
201 const bool capture_events = !trace_event_path.empty();
202 if (capture_events) {
203 rtc::tracing::SetupInternalTracer();
204 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
205 }
206
Artem Titov694b74b2020-02-20 22:52:26207 absl::optional<std::vector<std::string>> metrics_to_plot =
208 absl::GetFlag(FLAGS_plot);
209
210 if (metrics_to_plot->empty()) {
211 metrics_to_plot = absl::nullopt;
212 } else {
213 if (metrics_to_plot->size() == 1 &&
214 (*metrics_to_plot)[0] == kPlotAllMetrics) {
215 metrics_to_plot->clear();
216 }
217 }
218
Artem Titov40a7a352018-10-15 13:25:34219#if defined(WEBRTC_IOS)
220 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Patrik Höglund34288272020-03-19 07:51:35221 absl::GetFlag(FLAGS_write_perf_output_on_ios),
Artem Titov694b74b2020-02-20 22:52:26222 metrics_to_plot);
Artem Titov40a7a352018-10-15 13:25:34223 rtc::test::RunTestsFromIOSApp();
Yves Gerey071d0252020-01-28 19:07:19224 int exit_code = 0;
Artem Titov40a7a352018-10-15 13:25:34225#else
226 int exit_code = RUN_ALL_TESTS();
227
Patrik Höglunda7a01732020-03-27 18:22:36228 std::string perf_output_file =
Mirko Bonadei2ab97f62019-07-18 11:44:12229 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Patrik Höglunda7a01732020-03-27 18:22:36230 if (!perf_output_file.empty()) {
231 if (!webrtc::test::WritePerfResults(perf_output_file)) {
Patrik Höglundb8e69ef2020-03-12 08:39:40232 return 1;
233 }
Artem Titov40a7a352018-10-15 13:25:34234 }
Artem Titov694b74b2020-02-20 22:52:26235 if (metrics_to_plot) {
236 webrtc::test::PrintPlottableResults(*metrics_to_plot);
Artem Titov087be5c2019-09-12 18:30:54237 }
Yves Gerey071d0252020-01-28 19:07:19238#endif
239
240 if (capture_events) {
241 rtc::tracing::StopInternalCapture();
242 }
Artem Titov40a7a352018-10-15 13:25:34243
Yves Gerey53347b72018-10-19 13:04:04244#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
245 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
246 defined(UNDEFINED_SANITIZER)
247 // We want the test flagged as failed only for sanitizer defects,
248 // in which case the sanitizer will override exit code with 66.
Yves Gerey071d0252020-01-28 19:07:19249 exit_code = 0;
Yves Gerey53347b72018-10-19 13:04:04250#endif
251
Artem Titov40a7a352018-10-15 13:25:34252 return exit_code;
Artem Titov40a7a352018-10-15 13:25:34253 }
254
255 ~TestMainImpl() override = default;
256
257 private:
258#if defined(WEBRTC_WIN)
259 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
260#endif
261};
262
263} // namespace
264
265std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 15:06:18266 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 13:25:34267}
268
269} // namespace webrtc