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_timeout_listener.h"
14#include "gtest/gtest.h"
15
16#ifdef OS_IOS
17#include <asl.h>
18#endif // OS_IOS
19
20std::optional<fml::TimeDelta> GetTestTimeoutFromArgs(int argc, char** argv) {
21 const auto command_line = fml::CommandLineFromArgcArgv(argc, argv);
22
23 std::string timeout_seconds;
24 if (!command_line.GetOptionValue("timeout", &timeout_seconds)) {
25 // No timeout specified. Default to 30s.
26 return fml::TimeDelta::FromSeconds(30u);
27 }
28
29 const auto seconds = std::stoi(timeout_seconds);
30
31 if (seconds < 1) {
32 return std::nullopt;
33 }
34
35 return fml::TimeDelta::FromSeconds(seconds);
36}
37
38int main(int argc, char** argv) {
39 fml::InstallCrashHandler();
40#ifdef OS_IOS
41 asl_log_descriptor(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO,
42 ASL_LOG_DESCRIPTOR_WRITE);
43 asl_log_descriptor(NULL, NULL, ASL_LEVEL_ERR, STDERR_FILENO,
44 ASL_LOG_DESCRIPTOR_WRITE);
45#endif // OS_IOS
46
47 ::testing::InitGoogleTest(&argc, argv);
48
49 // Check if the user has specified a timeout.
50 const auto timeout = GetTestTimeoutFromArgs(argc, argv);
51 if (!timeout.has_value()) {
52 FML_LOG(INFO) << "Timeouts disabled via a command line flag.";
53 return RUN_ALL_TESTS();
54 }
55
56 // Check if the user is debugging the process.
57 if (flutter::testing::GetDebuggerStatus() ==
58 flutter::testing::DebuggerStatus::kAttached) {
59 FML_LOG(INFO) << "Debugger is attached. Suspending test timeouts.";
60 return RUN_ALL_TESTS();
61 }
62
63 auto timeout_listener =
64 new flutter::testing::TestTimeoutListener(timeout.value());
65 auto& listeners = ::testing::UnitTest::GetInstance()->listeners();
66 listeners.Append(timeout_listener);
67 auto result = RUN_ALL_TESTS();
68 delete listeners.Release(timeout_listener);
69 return result;
70}
71