| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <iostream> |
| 6 | #include <optional> |
| 7 | #include <string> |
| 8 | |
| 9 | #include "flutter/fml/backtrace.h" |
| 10 | #include "flutter/fml/build_config.h" |
| 11 | #include "flutter/fml/command_line.h" |
| 12 | #include "flutter/testing/debugger_detection.h" |
| 13 | #include "flutter/testing/test_args.h" |
| 14 | #include "flutter/testing/test_timeout_listener.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | #ifdef FML_OS_IOS |
| 18 | #include <asl.h> |
| 19 | #endif // FML_OS_IOS |
| 20 | |
| 21 | std::optional<fml::TimeDelta> GetTestTimeout() { |
| 22 | const auto& command_line = flutter::testing::GetArgsForProcess(); |
| 23 | |
| 24 | std::string timeout_seconds; |
| 25 | if (!command_line.GetOptionValue(name: "timeout" , value: &timeout_seconds)) { |
| 26 | // No timeout specified. Default to 300s. |
| 27 | return fml::TimeDelta::FromSeconds(seconds: 300u); |
| 28 | } |
| 29 | |
| 30 | const auto seconds = std::stoi(str: timeout_seconds); |
| 31 | |
| 32 | if (seconds < 1) { |
| 33 | return std::nullopt; |
| 34 | } |
| 35 | |
| 36 | return fml::TimeDelta::FromSeconds(seconds); |
| 37 | } |
| 38 | |
| 39 | int main(int argc, char** argv) { |
| 40 | fml::InstallCrashHandler(); |
| 41 | |
| 42 | flutter::testing::SetArgsForProcess(argc, argv); |
| 43 | |
| 44 | #ifdef FML_OS_IOS |
| 45 | asl_log_descriptor(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO, |
| 46 | ASL_LOG_DESCRIPTOR_WRITE); |
| 47 | asl_log_descriptor(NULL, NULL, ASL_LEVEL_ERR, STDERR_FILENO, |
| 48 | ASL_LOG_DESCRIPTOR_WRITE); |
| 49 | #endif // FML_OS_IOS |
| 50 | |
| 51 | ::testing::InitGoogleTest(argc: &argc, argv); |
| 52 | |
| 53 | // Check if the user has specified a timeout. |
| 54 | const auto timeout = GetTestTimeout(); |
| 55 | if (!timeout.has_value()) { |
| 56 | FML_LOG(INFO) << "Timeouts disabled via a command line flag." ; |
| 57 | return RUN_ALL_TESTS(); |
| 58 | } |
| 59 | |
| 60 | // Check if the user is debugging the process. |
| 61 | if (flutter::testing::GetDebuggerStatus() == |
| 62 | flutter::testing::DebuggerStatus::kAttached) { |
| 63 | FML_LOG(INFO) << "Debugger is attached. Suspending test timeouts." ; |
| 64 | return RUN_ALL_TESTS(); |
| 65 | } |
| 66 | |
| 67 | auto timeout_listener = |
| 68 | new flutter::testing::TestTimeoutListener(timeout.value()); |
| 69 | auto& listeners = ::testing::UnitTest::GetInstance()->listeners(); |
| 70 | listeners.Append(listener: timeout_listener); |
| 71 | auto result = RUN_ALL_TESTS(); |
| 72 | delete listeners.Release(listener: timeout_listener); |
| 73 | return result; |
| 74 | } |
| 75 | |