blob: 1650d2ac79fbf36f2d8e4c48bb26669a95f1ca9f [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"
Patrik Höglund2f283702019-10-14 10:12:18 +020031#include "test/testsupport/file_utils_override.h"
Artem Titov40a7a352018-10-15 15:25:34 +020032#include "test/testsupport/perf_test.h"
33
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.");
49ABSL_FLAG(
50 bool,
Artem Titov40a7a352018-10-15 15:25:34 +020051 save_chartjson_result,
52 false,
53 "Store the perf results in Documents/perf_result.json in the format "
54 "described by "
55 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
56 "data-format.md.");
57
58#else
59
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020060ABSL_FLAG(std::string,
61 isolated_script_test_output,
62 "",
63 "Path to output an empty JSON file which Chromium infra requires.");
Artem Titov40a7a352018-10-15 15:25:34 +020064
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020065ABSL_FLAG(
66 std::string,
Artem Titov40a7a352018-10-15 15:25:34 +020067 isolated_script_test_perf_output,
68 "",
69 "Path where the perf results should be stored in the JSON format described "
70 "by "
71 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
72 "data-format.md.");
73
Artem Titov087be5c2019-09-12 20:30:54 +020074constexpr 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
Artem Titov40a7a352018-10-15 15:25:34 +020082#endif
83
Patrik Höglund2f283702019-10-14 10:12:18 +020084ABSL_FLAG(std::string,
85 resources_dir,
86 "",
87 "Where to look for the runtime dependencies. If not specified, we "
88 "will use a reasonable default depending on where we are running. "
89 "This flag is useful if we copy over test resources to a phone and "
90 "need to tell the tests where their resources are.");
91
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020092ABSL_FLAG(bool, logs, true, "print logs to stderr");
93ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020094
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020095ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 13:24:00 +020096 trace_event,
97 "",
98 "Path to collect trace events (json file) for chrome://tracing. "
99 "If not set, events aren't captured.");
100
101ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200102 force_fieldtrials,
103 "",
104 "Field trials control experimental feature code which can be forced. "
105 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
106 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 15:25:34 +0200107
108namespace webrtc {
109
110namespace {
111
112class TestMainImpl : public TestMain {
113 public:
Artem Titovb5541a02018-10-17 17:37:47 +0200114 int Init(int* argc, char* argv[]) override {
115 ::testing::InitGoogleMock(argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200116 absl::ParseCommandLine(*argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +0200117
Patrik Höglund2f283702019-10-14 10:12:18 +0200118 std::string resources_dir = absl::GetFlag(FLAGS_resources_dir);
119 if (!resources_dir.empty())
120 test::internal::OverrideResourcesDir(resources_dir);
121
Artem Titov40a7a352018-10-15 15:25:34 +0200122 // Default to LS_INFO, even for release builds to provide better test
123 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +0200124 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
125 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
126
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200127 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100128 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
129
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200130 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
131 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100132
Yves Gerey050e38f2019-08-28 13:24:00 +0200133 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
134 const bool capture_events = !trace_event_path.empty();
135 if (capture_events) {
136 rtc::tracing::SetupInternalTracer();
137 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
138 }
139
Patrik Höglundf83d0ef2019-10-04 10:07:38 +0000140 // TODO(bugs.webrtc.org/9792): we need to reference something from
141 // fileutils.h so that our downstream hack where we replace fileutils.cc
142 // works. Otherwise the downstream flag implementation will take over and
143 // botch the flag introduced by the hack. Remove this awful thing once the
144 // downstream implementation has been eliminated.
145 (void)webrtc::test::JoinFilename("horrible", "hack");
146
Artem Titov40a7a352018-10-15 15:25:34 +0200147 // InitFieldTrialsFromString stores the char*, so the char array must
148 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200149 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
150 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200151 webrtc::metrics::Enable();
152
153#if defined(WEBRTC_WIN)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200154 winsock_init_ = std::make_unique<rtc::WinsockInitializer>();
Artem Titov40a7a352018-10-15 15:25:34 +0200155#endif
156
Niels Möller04a3cc12019-05-21 13:01:58 +0200157 // Initialize SSL which are used by several tests.
158 rtc::InitializeSSL();
159 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
160
Artem Titov40a7a352018-10-15 15:25:34 +0200161 // Ensure that main thread gets wrapped as an rtc::Thread.
162 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
163 // thread, or leave it to individual tests that need it. But as long as we
164 // have automatic thread wrapping, we need this to avoid that some other
165 // random thread (which one depending on which tests are run) gets
166 // automatically wrapped.
167 rtc::ThreadManager::Instance()->WrapCurrentThread();
168 RTC_CHECK(rtc::Thread::Current());
Yves Gerey050e38f2019-08-28 13:24:00 +0200169
170 if (capture_events) {
171 rtc::tracing::StopInternalCapture();
172 }
Artem Titov40a7a352018-10-15 15:25:34 +0200173 return 0;
174 }
175
176 int Run(int argc, char* argv[]) override {
177#if defined(WEBRTC_IOS)
178 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200179 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200180 rtc::test::RunTestsFromIOSApp();
181 return 0;
182#else
183 int exit_code = RUN_ALL_TESTS();
184
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200185 std::string chartjson_result_file =
186 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200187 if (!chartjson_result_file.empty()) {
188 webrtc::test::WritePerfResults(chartjson_result_file);
189 }
Artem Titov087be5c2019-09-12 20:30:54 +0200190 std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot);
191 if (!metrics_to_plot.empty()) {
192 if (metrics_to_plot.size() == 1 &&
193 metrics_to_plot[0] == kPlotAllMetrics) {
194 metrics_to_plot.clear();
195 }
196 webrtc::test::PrintPlottableResults(metrics_to_plot);
197 }
Artem Titov40a7a352018-10-15 15:25:34 +0200198
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200199 std::string result_filename =
200 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200201 if (!result_filename.empty()) {
202 std::ofstream result_file(result_filename);
203 result_file << "{\"version\": 3}";
204 result_file.close();
205 }
206
Yves Gerey53347b72018-10-19 15:04:04 +0200207#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
208 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
209 defined(UNDEFINED_SANITIZER)
210 // We want the test flagged as failed only for sanitizer defects,
211 // in which case the sanitizer will override exit code with 66.
212 return 0;
213#endif
214
Artem Titov40a7a352018-10-15 15:25:34 +0200215 return exit_code;
216#endif
217 }
218
219 ~TestMainImpl() override = default;
220
221 private:
222#if defined(WEBRTC_WIN)
223 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
224#endif
225};
226
227} // namespace
228
229std::unique_ptr<TestMain> TestMain::Create() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200230 return std::make_unique<TestMainImpl>();
Artem Titov40a7a352018-10-15 15:25:34 +0200231}
232
233} // namespace webrtc