1// Copyright 2013 The Chromium 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#ifndef BASE_TEST_TEST_LAUNCHER_H_
6#define BASE_TEST_TEST_LAUNCHER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback_forward.h"
12#include "base/compiler_specific.h"
13#include "base/time/time.h"
14
15class CommandLine;
16
17namespace testing {
18class TestCase;
19class TestInfo;
20}
21
22namespace base {
23
24// Constants for GTest command-line flags.
25extern const char kGTestFilterFlag[];
26extern const char kGTestListTestsFlag[];
27extern const char kGTestRepeatFlag[];
28extern const char kGTestRunDisabledTestsFlag[];
29extern const char kGTestOutputFlag[];
30
31// Structure containing result of a single test.
32struct TestResult {
33  TestResult();
34
35  // Name of the test case (before the dot, e.g. "A" for test "A.B").
36  std::string test_case_name;
37
38  // Name of the test (after the dot, e.g. "B" for test "A.B").
39  std::string test_name;
40
41  // True if the test passed.
42  bool success;
43
44  // Time it took to run the test.
45  base::TimeDelta elapsed_time;
46};
47
48// Interface for use with LaunchTests that abstracts away exact details
49// which tests and how are run.
50class TestLauncherDelegate {
51 public:
52  // Called before a test is considered for running. If it returns false,
53  // the test is not run. If it returns true, the test will be run provided
54  // it is part of the current shard.
55  virtual bool ShouldRunTest(const testing::TestCase* test_case,
56                             const testing::TestInfo* test_info) = 0;
57
58  // Called to make the delegate run specified test. After the delegate
59  // finishes running the test (can do so asynchronously and out-of-order)
60  // it must call |callback| regardless of test success.
61  typedef base::Callback<void(const TestResult& result)> TestResultCallback;
62  virtual void RunTest(const testing::TestCase* test_case,
63                       const testing::TestInfo* test_info,
64                       const TestResultCallback& callback) = 0;
65
66  // If the delegate is running tests asynchronously, it must finish
67  // running all pending tests and call their callbacks before returning
68  // from this method.
69  virtual void RunRemainingTests() = 0;
70
71 protected:
72  virtual ~TestLauncherDelegate();
73};
74
75// Launches a child process (assumed to be gtest-based binary)
76// using |command_line|. If |wrapper| is not empty, it is prepended
77// to the final command line. If the child process is still running
78// after |timeout|, it is terminated and |*was_timeout| is set to true.
79int LaunchChildGTestProcess(const CommandLine& command_line,
80                            const std::string& wrapper,
81                            base::TimeDelta timeout,
82                            bool* was_timeout);
83
84// Launches GTest-based tests from the current executable
85// using |launcher_delegate|.
86int LaunchTests(TestLauncherDelegate* launcher_delegate,
87                int argc,
88                char** argv) WARN_UNUSED_RESULT;
89
90}  // namespace base
91
92#endif  // BASE_TEST_TEST_LAUNCHER_H_
93