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