blob: 23a1c2b699002fa167771fbabcfd44db72e040fe [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"
Artem Titov40a7a352018-10-15 15:25:34 +020017#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
35DEFINE_string(NSTreatUnknownArgumentsAsOpen,
36 "",
37 "Intentionally ignored flag intended for iOS simulator.");
38DEFINE_string(ApplePersistenceIgnoreState,
39 "",
40 "Intentionally ignored flag intended for iOS simulator.");
41DEFINE_bool(
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
51DEFINE_string(
52 isolated_script_test_output,
53 "",
54 "Path to output an empty JSON file which Chromium infra requires.");
55
56DEFINE_string(
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
66DEFINE_bool(logs, false, "print logs to stderr");
67
68DEFINE_string(
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
75DEFINE_bool(help, false, "Print this message.");
76
77namespace webrtc {
78
79namespace {
80
81class TestMainImpl : public TestMain {
82 public:
Artem Titovb5541a02018-10-17 17:37:47 +020083 int Init(int* argc, char* argv[]) override {
84 ::testing::InitGoogleMock(argc, argv);
Artem Titov40a7a352018-10-15 15:25:34 +020085
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 Titovb5541a02018-10-17 17:37:47 +020092 if (rtc::FlagList::SetFlagsFromCommandLine(argc, argv, false)) {
Artem Titov40a7a352018-10-15 15:25:34 +020093 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
151 return exit_code;
152#endif
153 }
154
155 ~TestMainImpl() override = default;
156
157 private:
158#if defined(WEBRTC_WIN)
159 std::unique_ptr<rtc::WinsockInitializer> winsock_init_;
160#endif
161};
162
163} // namespace
164
165std::unique_ptr<TestMain> TestMain::Create() {
166 return absl::make_unique<TestMainImpl>();
167}
168
169} // namespace webrtc