blob: 52939afd69088a8e4a343036b263c9d72b55365e [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 Bonadeibc6a06c2018-10-19 08:43:53 +020016#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "rtc_base/checks.h"
Artem Titov40a7a352018-10-15 15:25:34 +020018#include "rtc_base/flags.h"
19#include "rtc_base/logging.h"
Niels Möller04a3cc12019-05-21 13:01:58 +020020#include "rtc_base/ssl_adapter.h"
21#include "rtc_base/ssl_stream_adapter.h"
Artem Titov40a7a352018-10-15 15:25:34 +020022#include "rtc_base/thread.h"
23#include "system_wrappers/include/field_trial.h"
24#include "system_wrappers/include/metrics.h"
25#include "test/field_trial.h"
26#include "test/gmock.h"
27#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "test/testsupport/file_utils.h"
Artem Titov40a7a352018-10-15 15:25:34 +020029#include "test/testsupport/perf_test.h"
30
31#if defined(WEBRTC_WIN)
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/win32_socket_init.h"
Artem Titov40a7a352018-10-15 15:25:34 +020033#endif
34
35#if defined(WEBRTC_IOS)
36#include "test/ios/test_support.h"
37
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020038WEBRTC_DEFINE_string(NSTreatUnknownArgumentsAsOpen,
39 "",
40 "Intentionally ignored flag intended for iOS simulator.");
41WEBRTC_DEFINE_string(ApplePersistenceIgnoreState,
42 "",
43 "Intentionally ignored flag intended for iOS simulator.");
44WEBRTC_DEFINE_bool(
Artem Titov40a7a352018-10-15 15:25:34 +020045 save_chartjson_result,
46 false,
47 "Store the perf results in Documents/perf_result.json in the format "
48 "described by "
49 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
50 "data-format.md.");
51
52#else
53
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020054WEBRTC_DEFINE_string(
Artem Titov40a7a352018-10-15 15:25:34 +020055 isolated_script_test_output,
56 "",
57 "Path to output an empty JSON file which Chromium infra requires.");
58
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020059WEBRTC_DEFINE_string(
Artem Titov40a7a352018-10-15 15:25:34 +020060 isolated_script_test_perf_output,
61 "",
62 "Path where the perf results should be stored in the JSON format described "
63 "by "
64 "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/"
65 "data-format.md.");
66
67#endif
68
Anton Sukhanov8b096a02019-05-23 14:42:41 -070069WEBRTC_DEFINE_bool(logs, true, "print logs to stderr");
Niels Möllerd0def192018-12-21 11:28:45 +010070WEBRTC_DEFINE_bool(verbose, false, "verbose logs to stderr");
Artem Titov40a7a352018-10-15 15:25:34 +020071
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020072WEBRTC_DEFINE_string(
Artem Titov40a7a352018-10-15 15:25:34 +020073 force_fieldtrials,
74 "",
75 "Field trials control experimental feature code which can be forced. "
76 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
77 " will assign the group Enable to field trial WebRTC-FooFeature.");
78
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020079WEBRTC_DEFINE_bool(help, false, "Print this message.");
Artem Titov40a7a352018-10-15 15:25:34 +020080
81namespace webrtc {
82
83namespace {
84
85class TestMainImpl : public TestMain {
86 public:
Artem Titovb5541a02018-10-17 17:37:47 +020087 int Init(int* argc, char* argv[]) override {
88 ::testing::InitGoogleMock(argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +020089
90 // Default to LS_INFO, even for release builds to provide better test
91 // logging.
Artem Titov40a7a352018-10-15 15:25:34 +020092 if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO)
93 rtc::LogMessage::LogToDebug(rtc::LS_INFO);
94
Artem Titovb5541a02018-10-17 17:37:47 +020095 if (rtc::FlagList::SetFlagsFromCommandLine(argc, argv, false)) {
Artem Titov40a7a352018-10-15 15:25:34 +020096 return 1;
97 }
98 if (FLAG_help) {
99 rtc::FlagList::Print(nullptr, false);
100 return 0;
101 }
102
Niels Möllerd0def192018-12-21 11:28:45 +0100103 if (FLAG_verbose)
104 rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
105
106 rtc::LogMessage::SetLogToStderr(FLAG_logs || FLAG_verbose);
107
Artem Titov40a7a352018-10-15 15:25:34 +0200108 // TODO(bugs.webrtc.org/9792): we need to reference something from
109 // fileutils.h so that our downstream hack where we replace fileutils.cc
110 // works. Otherwise the downstream flag implementation will take over and
111 // botch the flag introduced by the hack. Remove this awful thing once the
112 // downstream implementation has been eliminated.
113 (void)webrtc::test::JoinFilename("horrible", "hack");
114
Artem Titov40a7a352018-10-15 15:25:34 +0200115 // InitFieldTrialsFromString stores the char*, so the char array must
116 // outlive the application.
117 webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
118 webrtc::metrics::Enable();
119
120#if defined(WEBRTC_WIN)
121 winsock_init_ = absl::make_unique<rtc::WinsockInitializer>();
122#endif
123
Niels Möller04a3cc12019-05-21 13:01:58 +0200124 // Initialize SSL which are used by several tests.
125 rtc::InitializeSSL();
126 rtc::SSLStreamAdapter::EnableTimeCallbackForTesting();
127
Artem Titov40a7a352018-10-15 15:25:34 +0200128 // Ensure that main thread gets wrapped as an rtc::Thread.
129 // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main
130 // thread, or leave it to individual tests that need it. But as long as we
131 // have automatic thread wrapping, we need this to avoid that some other
132 // random thread (which one depending on which tests are run) gets
133 // automatically wrapped.
134 rtc::ThreadManager::Instance()->WrapCurrentThread();
135 RTC_CHECK(rtc::Thread::Current());
136 return 0;
137 }
138
139 int Run(int argc, char* argv[]) override {
140#if defined(WEBRTC_IOS)
141 rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv,
142 FLAG_save_chartjson_result);
143 rtc::test::RunTestsFromIOSApp();
144 return 0;
145#else
146 int exit_code = RUN_ALL_TESTS();
147
148 std::string chartjson_result_file = FLAG_isolated_script_test_perf_output;
149 if (!chartjson_result_file.empty()) {
150 webrtc::test::WritePerfResults(chartjson_result_file);
151 }
152
153 std::string result_filename = FLAG_isolated_script_test_output;
154 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