1 // Copyright (c) 2012 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 CONTENT_PUBLIC_TEST_BROWSER_TEST_H_
6 #define CONTENT_PUBLIC_TEST_BROWSER_TEST_H_
7 
8 // We only want to use InProcessBrowserTest in test targets which properly
9 // isolate each test case by running each test in a separate process.
10 // This way if a test hangs the test launcher can reliably terminate it.
11 //
12 // InProcessBrowserTest cannot be run more than once in the same address space
13 // anyway - otherwise the second test crashes.
14 #if !defined(HAS_OUT_OF_PROC_TEST_RUNNER)
15 #error Can't reliably terminate hanging event tests without OOP test runner.
16 #endif
17 
18 #include "base/compiler_specific.h"
19 #include "base/macros.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 
22 #define IN_PROC_BROWSER_TEST_(                                               \
23     test_case_name, test_name, parent_class, parent_id)                      \
24   class GTEST_TEST_CLASS_NAME_(test_case_name, test_name)                    \
25       : public parent_class {                                                \
26    public:                                                                   \
27     GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}                   \
28                                                                              \
29    protected:                                                                \
30     void RunTestOnMainThread() override;                                     \
31                                                                              \
32    private:                                                                  \
33     void TestBody() override {}                                              \
34     static ::testing::TestInfo* const test_info_;                            \
35     GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_case_name,   \
36                                                            test_name));      \
37   };                                                                         \
38                                                                              \
39   ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name,          \
40                                                     test_name)::test_info_ = \
41       ::testing::internal::MakeAndRegisterTestInfo(                          \
42           #test_case_name,                                                   \
43           #test_name,                                                        \
44           "",                                                                \
45           "",                                                                \
46           ::testing::internal::CodeLocation(__FILE__, __LINE__),             \
47           (parent_id),                                                       \
48           parent_class::SetUpTestCase,                                       \
49           parent_class::TearDownTestCase,                                    \
50           new ::testing::internal::TestFactoryImpl<GTEST_TEST_CLASS_NAME_(   \
51               test_case_name, test_name)>);                                  \
52   void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::RunTestOnMainThread()
53 
54 #define IN_PROC_BROWSER_TEST_F(test_fixture, test_name)\
55   IN_PROC_BROWSER_TEST_(test_fixture, test_name, test_fixture,\
56                     ::testing::internal::GetTypeId<test_fixture>())
57 
58 #define IN_PROC_BROWSER_TEST_P_(test_case_name, test_name)                     \
59   class GTEST_TEST_CLASS_NAME_(test_case_name, test_name)                      \
60       : public test_case_name {                                                \
61    public:                                                                     \
62     GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}                     \
63                                                                                \
64    protected:                                                                  \
65     void RunTestOnMainThread() override;                                       \
66                                                                                \
67    private:                                                                    \
68     void TestBody() override {}                                                \
69     static int AddToRegistry() {                                               \
70       ::testing::UnitTest::GetInstance()                                       \
71           ->parameterized_test_registry()                                      \
72           .GetTestCasePatternHolder<test_case_name>(                           \
73                #test_case_name,                                                \
74                ::testing::internal::CodeLocation(__FILE__, __LINE__))          \
75           ->AddTestPattern(                                                    \
76               #test_case_name,                                                 \
77               #test_name,                                                      \
78               new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \
79                   test_case_name, test_name)>());                              \
80       return 0;                                                                \
81     }                                                                          \
82     static int gtest_registering_dummy_;                                       \
83     GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_case_name,     \
84                                                            test_name));        \
85   };                                                                           \
86   int GTEST_TEST_CLASS_NAME_(test_case_name,                                   \
87                              test_name)::gtest_registering_dummy_ =            \
88       GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry();      \
89   void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::RunTestOnMainThread()
90 
91 // Wrap the real macro with an outer macro to ensure that the parameters are
92 // evaluated (e.g., if |test_name| is prefixed with MAYBE_).
93 #define IN_PROC_BROWSER_TEST_P(test_case_name, test_name) \
94   IN_PROC_BROWSER_TEST_P_(test_case_name, test_name)
95 
96 #endif  // CONTENT_PUBLIC_TEST_BROWSER_TEST_H_
97