blob: a276eb6d03c260705c121cc6e135de3f9d4c04cc [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>
14#include <string>
15
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020016#include "absl/flags/flag.h"
17#include "absl/flags/parse.h"
Mirko Bonadeibc6a06c2018-10-19 08:43:53 +020018#include "absl/memory/memory.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
73#endif
74
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020075ABSL_FLAG(bool, logs, true, "print logs to stderr");
76ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020077
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020078ABSL_FLAG(std::string,
Yves Gerey050e38f2019-08-28 13:24:00 +020079 trace_event,
80 "",
81 "Path to collect trace events (json file) for chrome://tracing. "
82 "If not set, events aren't captured.");
83
84ABSL_FLAG(std::string,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020085 force_fieldtrials,
86 "",
87 "Field trials control experimental feature code which can be forced. "
88 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
89 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 15:25:34 +020090
91namespace webrtc {
92
93namespace {
94
95class TestMainImpl : public TestMain {
96 public:
Artem Titovb5541a02018-10-17 17:37:47 +020097 int Init(int* argc, char* argv[]) override {
98 ::testing::InitGoogleMock(argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020099 absl::ParseCommandLine(*argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +0200100
101 // Default to LS_INFO, even for release builds to provide better test
102 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +0200103 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
104 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
105
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200106 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100107 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
108
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200109 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
110 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100111
Yves Gerey050e38f2019-08-28 13:24:00 +0200112 std::string trace_event_path = absl::GetFlag(FLAGS_trace_event);
113 const bool capture_events = !trace_event_path.empty();
114 if (capture_events) {
115 rtc::tracing::SetupInternalTracer();
116 rtc::tracing::StartInternalCapture(trace_event_path.c_str());
117 }
118
Artem Titov40a7a352018-10-15 15:25:34 +0200119 // TODO(bugs.webrtc.org/9792): we need to reference something from
120 // fileutils.h so that our downstream hack where we replace fileutils.cc
121 // works. Otherwise the downstream flag implementation will take over and
122 // botch the flag introduced by the hack. Remove this awful thing once the
123 // downstream implementation has been eliminated.
124 (void)webrtc::test::JoinFilename("horrible", "hack");
125
Artem Titov40a7a352018-10-15 15:25:34 +0200126 // InitFieldTrialsFromString stores the char*, so the char array must
127 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200128 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
129 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200130 webrtc::metrics::Enable();
131
132#if defined(WEBRTC_WIN)
133 winsock_init_ = absl::make_unique<rtc::WinsockInitializer>();
134#endif
135
Niels Möller04a3cc12019-05-21 13:01:58 +0200136 // Initialize SSL which are used by several tests.
137 rtc::InitializeSSL();
138 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
139
Artem Titov40a7a352018-10-15 15:25:34 +0200140 // Ensure that main thread gets wrapped as an rtc::Thread.
141 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
142 // thread, or leave it to individual tests that need it. But as long as we
143 // have automatic thread wrapping, we need this to avoid that some other
144 // random thread (which one depending on which tests are run) gets
145 // automatically wrapped.
146 rtc::ThreadManager::Instance()->WrapCurrentThread();
147 RTC_CHECK(rtc::Thread::Current());
Yves Gerey050e38f2019-08-28 13:24:00 +0200148
149 if (capture_events) {
150 rtc::tracing::StopInternalCapture();
151 }
Artem Titov40a7a352018-10-15 15:25:34 +0200152 return 0;
153 }
154
155 int Run(int argc, char* argv[]) override {
156#if defined(WEBRTC_IOS)
157 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200158 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200159 rtc::test::RunTestsFromIOSApp();
160 return 0;
161#else
162 int exit_code = RUN_ALL_TESTS();
163
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200164 std::string chartjson_result_file =
165 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200166 if (!chartjson_result_file.empty()) {
167 webrtc::test::WritePerfResults(chartjson_result_file);
168 }
169
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200170 std::string result_filename =
171 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200172 if (!result_filename.empty()) {
173 std::ofstream result_file(result_filename);
174 result_file << "{\"version\": 3}";
175 result_file.close();
176 }
177
Yves Gerey53347b72018-10-19 15:04:04 +0200178#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
179 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
180 defined(UNDEFINED_SANITIZER)
181 // We want the test flagged as failed only for sanitizer defects,
182 // in which case the sanitizer will override exit code with 66.
183 return 0;
184#endif
185
Artem Titov40a7a352018-10-15 15:25:34 +0200186 return exit_code;
187#endif
188 }
189
190 ~TestMainImpl() override = default;
191
192 private:
193#if defined(WEBRTC_WIN)
194 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
195#endif
196};
197
198} // namespace
199
200std::unique_ptr<TestMain> TestMain::Create() {
201 return absl::make_unique<TestMainImpl>();
202}
203
204} // namespace webrtc