Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [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> |
| 14 | #include <string> |
| 15 | |
Mirko Bonadei | bc6a06c | 2018-10-19 08:43:53 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 17 | #include "rtc_base/flags.h" |
| 18 | #include "rtc_base/logging.h" |
| 19 | #include "rtc_base/thread.h" |
| 20 | #include "system_wrappers/include/field_trial.h" |
| 21 | #include "system_wrappers/include/metrics.h" |
| 22 | #include "test/field_trial.h" |
| 23 | #include "test/gmock.h" |
| 24 | #include "test/gtest.h" |
| 25 | #include "test/testsupport/fileutils.h" |
| 26 | #include "test/testsupport/perf_test.h" |
| 27 | |
| 28 | #if defined(WEBRTC_WIN) |
| 29 | #include "rtc_base/win32socketinit.h" |
| 30 | #endif |
| 31 | |
| 32 | #if defined(WEBRTC_IOS) |
| 33 | #include "test/ios/test_support.h" |
| 34 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 35 | WEBRTC_DEFINE_string(NSTreatUnknownArgumentsAsOpen, |
| 36 | "", |
| 37 | "Intentionally ignored flag intended for iOS simulator."); |
| 38 | WEBRTC_DEFINE_string(ApplePersistenceIgnoreState, |
| 39 | "", |
| 40 | "Intentionally ignored flag intended for iOS simulator."); |
| 41 | WEBRTC_DEFINE_bool( |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 42 | save_chartjson_result, |
| 43 | false, |
| 44 | "Store the perf results in Documents/perf_result.json in the format " |
| 45 | "described by " |
| 46 | "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/" |
| 47 | "data-format.md."); |
| 48 | |
| 49 | #else |
| 50 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 51 | WEBRTC_DEFINE_string( |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 52 | isolated_script_test_output, |
| 53 | "", |
| 54 | "Path to output an empty JSON file which Chromium infra requires."); |
| 55 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 56 | WEBRTC_DEFINE_string( |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 57 | isolated_script_test_perf_output, |
| 58 | "", |
| 59 | "Path where the perf results should be stored in the JSON format described " |
| 60 | "by " |
| 61 | "https://github.com/catapult-project/catapult/blob/master/dashboard/docs/" |
| 62 | "data-format.md."); |
| 63 | |
| 64 | #endif |
| 65 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 66 | WEBRTC_DEFINE_bool(logs, false, "print logs to stderr"); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 67 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 68 | WEBRTC_DEFINE_string( |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 69 | force_fieldtrials, |
| 70 | "", |
| 71 | "Field trials control experimental feature code which can be forced. " |
| 72 | "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/" |
| 73 | " will assign the group Enable to field trial WebRTC-FooFeature."); |
| 74 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 75 | WEBRTC_DEFINE_bool(help, false, "Print this message."); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 76 | |
| 77 | namespace webrtc { |
| 78 | |
| 79 | namespace { |
| 80 | |
| 81 | class TestMainImpl : public TestMain { |
| 82 | public: |
Artem Titov | b5541a0 | 2018-10-17 17:37:47 +0200 | [diff] [blame] | 83 | int Init(int* argc, char* argv[]) override { |
| 84 | ::testing::InitGoogleMock(argc, argv); |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 85 | |
| 86 | // Default to LS_INFO, even for release builds to provide better test |
| 87 | // logging. |
| 88 | // TODO(pbos): Consider adding a command-line override. |
| 89 | if (rtc::LogMessage::GetLogToDebug() > rtc::LS_INFO) |
| 90 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 91 | |
Artem Titov | b5541a0 | 2018-10-17 17:37:47 +0200 | [diff] [blame] | 92 | if (rtc::FlagList::SetFlagsFromCommandLine(argc, argv, false)) { |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 93 | return 1; |
| 94 | } |
| 95 | if (FLAG_help) { |
| 96 | rtc::FlagList::Print(nullptr, false); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | // TODO(bugs.webrtc.org/9792): we need to reference something from |
| 101 | // fileutils.h so that our downstream hack where we replace fileutils.cc |
| 102 | // works. Otherwise the downstream flag implementation will take over and |
| 103 | // botch the flag introduced by the hack. Remove this awful thing once the |
| 104 | // downstream implementation has been eliminated. |
| 105 | (void)webrtc::test::JoinFilename("horrible", "hack"); |
| 106 | |
| 107 | webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials); |
| 108 | // InitFieldTrialsFromString stores the char*, so the char array must |
| 109 | // outlive the application. |
| 110 | webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials); |
| 111 | webrtc::metrics::Enable(); |
| 112 | |
| 113 | #if defined(WEBRTC_WIN) |
| 114 | winsock_init_ = absl::make_unique<rtc::WinsockInitializer>(); |
| 115 | #endif |
| 116 | |
| 117 | rtc::LogMessage::SetLogToStderr(FLAG_logs); |
| 118 | |
| 119 | // Ensure that main thread gets wrapped as an rtc::Thread. |
| 120 | // TODO(bugs.webrt.org/9714): It might be better to avoid wrapping the main |
| 121 | // thread, or leave it to individual tests that need it. But as long as we |
| 122 | // have automatic thread wrapping, we need this to avoid that some other |
| 123 | // random thread (which one depending on which tests are run) gets |
| 124 | // automatically wrapped. |
| 125 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 126 | RTC_CHECK(rtc::Thread::Current()); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int Run(int argc, char* argv[]) override { |
| 131 | #if defined(WEBRTC_IOS) |
| 132 | rtc::test::InitTestSuite(RUN_ALL_TESTS, argc, argv, |
| 133 | FLAG_save_chartjson_result); |
| 134 | rtc::test::RunTestsFromIOSApp(); |
| 135 | return 0; |
| 136 | #else |
| 137 | int exit_code = RUN_ALL_TESTS(); |
| 138 | |
| 139 | std::string chartjson_result_file = FLAG_isolated_script_test_perf_output; |
| 140 | if (!chartjson_result_file.empty()) { |
| 141 | webrtc::test::WritePerfResults(chartjson_result_file); |
| 142 | } |
| 143 | |
| 144 | std::string result_filename = FLAG_isolated_script_test_output; |
| 145 | if (!result_filename.empty()) { |
| 146 | std::ofstream result_file(result_filename); |
| 147 | result_file << "{\"version\": 3}"; |
| 148 | result_file.close(); |
| 149 | } |
| 150 | |
Yves Gerey | 53347b7 | 2018-10-19 15:04:04 +0200 | [diff] [blame] | 151 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 152 | defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \ |
| 153 | defined(UNDEFINED_SANITIZER) |
| 154 | // We want the test flagged as failed only for sanitizer defects, |
| 155 | // in which case the sanitizer will override exit code with 66. |
| 156 | return 0; |
| 157 | #endif |
| 158 | |
Artem Titov | 40a7a35 | 2018-10-15 15:25:34 +0200 | [diff] [blame] | 159 | return exit_code; |
| 160 | #endif |
| 161 | } |
| 162 | |
| 163 | ~TestMainImpl() override = default; |
| 164 | |
| 165 | private: |
| 166 | #if defined(WEBRTC_WIN) |
| 167 | std::unique_ptr<rtc::WinsockInitializer> winsock_init_; |
| 168 | #endif |
| 169 | }; |
| 170 | |
| 171 | } // namespace |
| 172 | |
| 173 | std::unique_ptr<TestMain> TestMain::Create() { |
| 174 | return absl::make_unique<TestMainImpl>(); |
| 175 | } |
| 176 | |
| 177 | } // namespace webrtc |