Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 14 | #include <memory> |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 17 | #include "absl/flags/flag.h" |
| 18 | #include "absl/flags/parse.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 20 | #include "rtc_base/event_tracer.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
Niels Möller | 04a3cc1 | 2019-05-21 11:01:58 | [diff] [blame] | 22 | #include "rtc_base/ssl_adapter.h" |
| 23 | #include "rtc_base/ssl_stream_adapter.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 24 | #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" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 30 | #include "test/testsupport/perf_test.h" |
Patrik Höglund | 844600e | 2019-10-15 10:19:36 | [diff] [blame] | 31 | #include "test/testsupport/resources_dir_flag.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 32 | |
| 33 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 34 | #include "rtc_base/win32_socket_init.h" |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | #if defined(WEBRTC_IOS) |
| 38 | #include "test/ios/test_support.h" |
| 39 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 40 | ABSL_FLAG(std::string, |
| 41 | NSTreatUnknownArgumentsAsOpen, |
| 42 | "", |
| 43 | "Intentionally ignored flag intended for iOS simulator."); |
| 44 | ABSL_FLAG(std::string, |
| 45 | ApplePersistenceIgnoreState, |
| 46 | "", |
| 47 | "Intentionally ignored flag intended for iOS simulator."); |
| 48 | ABSL_FLAG( |
| 49 | bool, |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 50 | 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 Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 59 | ABSL_FLAG(std::string, |
| 60 | isolated_script_test_output, |
| 61 | "", |
| 62 | "Path to output an empty JSON file which Chromium infra requires."); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 63 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 64 | ABSL_FLAG( |
| 65 | std::string, |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 66 | 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 | |
Artem Titov | 087be5c | 2019-09-12 18:30:54 | [diff] [blame] | 73 | constexpr char kPlotAllMetrics[] = "all"; |
| 74 | ABSL_FLAG(std::vector<std::string>, |
| 75 | plot, |
| 76 | {}, |
| 77 | "List of metrics that should be exported for plotting (if they are " |
| 78 | "available). Example: psnr,ssim,encode_time. To plot all available " |
| 79 | " metrics pass 'all' as flag value"); |
| 80 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 81 | #endif |
| 82 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 83 | ABSL_FLAG(bool, logs, true, "print logs to stderr"); |
| 84 | ABSL_FLAG(bool, verbose, false, "verbose logs to stderr"); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 85 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 86 | ABSL_FLAG(std::string, |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 87 | trace_event, |
| 88 | "", |
| 89 | "Path to collect trace events (json file) for chrome://tracing. " |
| 90 | "If not set, events aren't captured."); |
| 91 | |
| 92 | ABSL_FLAG(std::string, |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 93 | force_fieldtrials, |
| 94 | "", |
| 95 | "Field trials control experimental feature code which can be forced. " |
| 96 | "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/" |
| 97 | " will assign the group Enable to field trial WebRTC-FooFeature."); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 98 | |
| 99 | namespace webrtc { |
| 100 | |
| 101 | namespace { |
| 102 | |
| 103 | class TestMainImpl : public TestMain { |
| 104 | public: |
Artem Titov | b5541a0 | 2018-10-17 15:37:47 | [diff] [blame] | 105 | int Init(int* argc, char* argv[]) override { |
| 106 | ::testing::InitGoogleMock(argc, argv); |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 107 | absl::ParseCommandLine(*argc, argv); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 108 | |
Patrik Höglund | 2bc1ea0 | 2019-10-16 08:24:35 | [diff] [blame] | 109 | // Make sure we always pull in the --resources_dir flag, even if the test |
| 110 | // binary doesn't link with fileutils (downstream expects all test mains to |
| 111 | // have this flag). |
| 112 | (void)absl::GetFlag(FLAGS_resources_dir); |
Patrik Höglund | 2f28370 | 2019-10-14 08:12:18 | [diff] [blame] | 113 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 114 | // Default to LS_INFO, even for release builds to provide better test |
| 115 | // logging. |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 116 | if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) |
| 117 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 118 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 119 | if (absl::GetFlag(FLAGS_verbose)) |
Niels Möller | d0def19 | 2018-12-21 10:28:45 | [diff] [blame] | 120 | rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE); |
| 121 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 122 | rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) || |
| 123 | absl::GetFlag(FLAGS_verbose)); |
Niels Möller | d0def19 | 2018-12-21 10:28:45 | [diff] [blame] | 124 | |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 125 | std::string trace_event_path = absl::GetFlag(FLAGS_trace_event); |
| 126 | const bool capture_events = !trace_event_path.empty(); |
| 127 | if (capture_events) { |
| 128 | rtc::tracing::SetupInternalTracer(); |
| 129 | rtc::tracing::StartInternalCapture(trace_event_path.c_str()); |
| 130 | } |
| 131 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 132 | // InitFieldTrialsFromString stores the char*, so the char array must |
| 133 | // outlive the application. |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 134 | field_trials_ = absl::GetFlag(FLAGS_force_fieldtrials); |
| 135 | webrtc::field_trial::InitFieldTrialsFromString(field_trials_.c_str()); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 136 | webrtc::metrics::Enable(); |
| 137 | |
| 138 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 139 | winsock_init_ = std::make_unique<rtc::WinsockInitializer>(); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 140 | #endif |
| 141 | |
Niels Möller | 04a3cc1 | 2019-05-21 11:01:58 | [diff] [blame] | 142 | // Initialize SSL which are used by several tests. |
| 143 | rtc::InitializeSSL(); |
| 144 | rtc::SSLStreamAdapter::EnableTimeCallbackForTesting(); |
| 145 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 146 | // Ensure that main thread gets wrapped as an rtc::Thread. |
| 147 | // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main |
| 148 | // thread, or leave it to individual tests that need it. But as long as we |
| 149 | // have automatic thread wrapping, we need this to avoid that some other |
| 150 | // random thread (which one depending on which tests are run) gets |
| 151 | // automatically wrapped. |
| 152 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 153 | RTC_CHECK(rtc::Thread::Current()); |
Yves Gerey | 050e38f | 2019-08-28 11:24:00 | [diff] [blame] | 154 | |
| 155 | if (capture_events) { |
| 156 | rtc::tracing::StopInternalCapture(); |
| 157 | } |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | int Run(int argc, char* argv[]) override { |
| 162 | #if defined(WEBRTC_IOS) |
| 163 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 164 | absl::GetFlag(FLAGS_save_chartjson_result)); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 165 | rtc::test::RunTestsFromIOSApp(); |
| 166 | return 0; |
| 167 | #else |
| 168 | int exit_code = RUN_ALL_TESTS(); |
| 169 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 170 | std::string chartjson_result_file = |
| 171 | absl::GetFlag(FLAGS_isolated_script_test_perf_output); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 172 | if (!chartjson_result_file.empty()) { |
| 173 | webrtc::test::WritePerfResults(chartjson_result_file); |
| 174 | } |
Artem Titov | 087be5c | 2019-09-12 18:30:54 | [diff] [blame] | 175 | std::vector<std::string> metrics_to_plot = absl::GetFlag(FLAGS_plot); |
| 176 | if (!metrics_to_plot.empty()) { |
| 177 | if (metrics_to_plot.size() == 1 && |
| 178 | metrics_to_plot[0] == kPlotAllMetrics) { |
| 179 | metrics_to_plot.clear(); |
| 180 | } |
| 181 | webrtc::test::PrintPlottableResults(metrics_to_plot); |
| 182 | } |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 183 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 184 | std::string result_filename = |
| 185 | absl::GetFlag(FLAGS_isolated_script_test_output); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 186 | if (!result_filename.empty()) { |
| 187 | std::ofstream result_file(result_filename); |
| 188 | result_file << "{\"version\": 3}"; |
| 189 | result_file.close(); |
| 190 | } |
| 191 | |
Yves Gerey | 53347b7 | 2018-10-19 13:04:04 | [diff] [blame] | 192 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 193 | defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 194 | defined(UNDEFINED_SANITIZER) |
| 195 | // We want the test flagged as failed only for sanitizer defects, |
| 196 | // in which case the sanitizer will override exit code with 66. |
| 197 | return 0; |
| 198 | #endif |
| 199 | |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 200 | return exit_code; |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | ~TestMainImpl() override = default; |
| 205 | |
| 206 | private: |
| 207 | #if defined(WEBRTC_WIN) |
| 208 | std::unique_ptr<rtc::WinsockInitializer> winsock_init_; |
| 209 | #endif |
| 210 | }; |
| 211 | |
| 212 | } // namespace |
| 213 | |
| 214 | std::unique_ptr<TestMain> TestMain::Create() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 215 | return std::make_unique<TestMainImpl>(); |
Artem Titov | 40a7a35 | 2018-10-15 13:25:34 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace webrtc |