blob: 02e0703116bfef3f8ce624aab3d3d96ddc591d34 [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"
Artem Titov40a7a352018-10-15 15:25:34 +020020#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 13:01:58 +020021#include "rtc_base/ssl_adapter.h"
22#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 15:25:34 +020023#include "rtc_base/thread.h"
24#include "system_wrappers/include/field_trial.h"
25#include "system_wrappers/include/metrics.h"
26#include "test/field_trial.h"
27#include "test/gmock.h"
28#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "test/testsupport/file_utils.h"
Artem Titov40a7a352018-10-15 15:25:34 +020030#include "test/testsupport/perf_test.h"
31
32#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 15:25:34 +020034#endif
35
36#if defined(WEBRTC_IOS)
37#include "test/ios/test_support.h"
38
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020039ABSL_FLAG(std::string,
40 NSTreatUnknownArgumentsAsOpen,
41 "",
42 "Intentionally ignored flag intended for iOS simulator.");
43ABSL_FLAG(std::string,
44 ApplePersistenceIgnoreState,
45 "",
46 "Intentionally ignored flag intended for iOS simulator.");
47ABSL_FLAG(
48 bool,
Artem Titov40a7a352018-10-15 15:25:34 +020049 save_chartjson_result,
50 false,
51 "Store the perf results in Documents/perf_result.json in the format "
52 "described by "
53 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
54 "data-format.md.");
55
56#else
57
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020058ABSL_FLAG(std::string,
59 isolated_script_test_output,
60 "",
61 "Path to output an empty JSON file which Chromium infra requires.");
Artem Titov40a7a352018-10-15 15:25:34 +020062
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020063ABSL_FLAG(
64 std::string,
Artem Titov40a7a352018-10-15 15:25:34 +020065 isolated_script_test_perf_output,
66 "",
67 "Path where the perf results should be stored in the JSON format described "
68 "by "
69 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
70 "data-format.md.");
71
72#endif
73
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020074ABSL_FLAG(bool, logs, true, "print logs to stderr");
75ABSL_FLAG(bool, verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020076
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020077ABSL_FLAG(std::string,
78 force_fieldtrials,
79 "",
80 "Field trials control experimental feature code which can be forced. "
81 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
82 " will assign the group Enable to field trial WebRTC-FooFeature.");
Artem Titov40a7a352018-10-15 15:25:34 +020083
84namespace webrtc {
85
86namespace {
87
88class TestMainImpl : public TestMain {
89 public:
Artem Titovb5541a02018-10-17 17:37:47 +020090 int Init(int* argc, char* argv[]) override {
91 ::testing::InitGoogleMock(argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020092 absl::ParseCommandLine(*argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +020093
94 // Default to LS_INFO, even for release builds to provide better test
95 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +020096 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
97 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
98
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020099 if (absl::GetFlag(FLAGS_verbose))
Niels Möllerd0def192018-12-21 11:28:45 +0100100 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
101
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200102 rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) ||
103 absl::GetFlag(FLAGS_verbose));
Niels Möllerd0def192018-12-21 11:28:45 +0100104
Artem Titov40a7a352018-10-15 15:25:34 +0200105 // TODO(bugs.webrtc.org/9792): we need to reference something from
106 // fileutils.h so that our downstream hack where we replace fileutils.cc
107 // works. Otherwise the downstream flag implementation will take over and
108 // botch the flag introduced by the hack. Remove this awful thing once the
109 // downstream implementation has been eliminated.
110 (void)webrtc::test::JoinFilename("horrible", "hack");
111
Artem Titov40a7a352018-10-15 15:25:34 +0200112 // InitFieldTrialsFromString stores the char*, so the char array must
113 // outlive the application.
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200114 field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials);
115 webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str());
Artem Titov40a7a352018-10-15 15:25:34 +0200116 webrtc::metrics::Enable();
117
118#if defined(WEBRTC_WIN)
119 winsock_init_ = absl::make_unique<rtc::WinsockInitializer>();
120#endif
121
Niels Möller04a3cc12019-05-21 13:01:58 +0200122 // Initialize SSL which are used by several tests.
123 rtc::InitializeSSL();
124 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
125
Artem Titov40a7a352018-10-15 15:25:34 +0200126 // Ensure that main thread gets wrapped as an rtc::Thread.
127 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
128 // thread, or leave it to individual tests that need it. But as long as we
129 // have automatic thread wrapping, we need this to avoid that some other
130 // random thread (which one depending on which tests are run) gets
131 // automatically wrapped.
132 rtc::ThreadManager::Instance()->WrapCurrentThread();
133 RTC_CHECK(rtc::Thread::Current());
134 return 0;
135 }
136
137 int Run(int argc, char* argv[]) override {
138#if defined(WEBRTC_IOS)
139 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200140 absl::GetFlag(FLAGS_save_chartjson_result));
Artem Titov40a7a352018-10-15 15:25:34 +0200141 rtc::test::RunTestsFromIOSApp();
142 return 0;
143#else
144 int exit_code = RUN_ALL_TESTS();
145
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200146 std::string chartjson_result_file =
147 absl::GetFlag(FLAGS_isolated_script_test_perf_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200148 if (!chartjson_result_file.empty()) {
149 webrtc::test::WritePerfResults(chartjson_result_file);
150 }
151
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200152 std::string result_filename =
153 absl::GetFlag(FLAGS_isolated_script_test_output);
Artem Titov40a7a352018-10-15 15:25:34 +0200154 if (!result_filename.empty()) {
155 std::ofstream result_file(result_filename);
156 result_file << "{\"version\": 3}";
157 result_file.close();
158 }
159
Yves Gerey53347b72018-10-19 15:04:04 +0200160#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
161 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
162 defined(UNDEFINED_SANITIZER)
163 // We want the test flagged as failed only for sanitizer defects,
164 // in which case the sanitizer will override exit code with 66.
165 return 0;
166#endif
167
Artem Titov40a7a352018-10-15 15:25:34 +0200168 return exit_code;
169#endif
170 }
171
172 ~TestMainImpl() override = default;
173
174 private:
175#if defined(WEBRTC_WIN)
176 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
177#endif
178};
179
180} // namespace
181
182std::unique_ptr<TestMain> TestMain::Create() {
183 return absl::make_unique<TestMainImpl>();
184}
185
186} // namespace webrtc