OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TEST_SUITE_H_ | 5 #ifndef BASE_TEST_SUITE_H_ |
6 #define BASE_TEST_SUITE_H_ | 6 #define BASE_TEST_SUITE_H_ |
7 | 7 |
8 // Defines a basic test suite framework for running gtest based tests. You can | 8 // Defines a basic test suite framework for running gtest based tests. You can |
9 // instantiate this class in your main function and call its Run method to run | 9 // instantiate this class in your main function and call its Run method to run |
10 // any gtest based tests that are linked into your executable. | 10 // any gtest based tests that are linked into your executable. |
11 | 11 |
12 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/debug_on_start.h" | 14 #include "base/debug_on_start.h" |
15 #include "base/icu_util.h" | 15 #include "base/icu_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/multiprocess_test.h" | 17 #include "base/multiprocess_test.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
21 #include <windows.h> | 21 #include <windows.h> |
22 #include "base/multiprocess_test.h" | 22 #include "base/multiprocess_test.h" |
23 #elif defined(OS_LINUX) | 23 #elif defined(OS_LINUX) |
24 #include <dlfcn.h> | 24 #include <dlfcn.h> |
25 #include <gtk/gtk.h> | 25 #include <gtk/gtk.h> |
| 26 #elif defined(OS_MACOSX) |
| 27 #include <dlfcn.h> |
26 #endif | 28 #endif |
27 | 29 |
28 class TestSuite { | 30 class TestSuite { |
29 public: | 31 public: |
30 TestSuite(int argc, char** argv) { | 32 TestSuite(int argc, char** argv) { |
31 CommandLine::SetArgcArgv(argc, argv); | 33 CommandLine::SetArgcArgv(argc, argv); |
32 testing::InitGoogleTest(&argc, argv); | 34 testing::InitGoogleTest(&argc, argv); |
33 #if defined(OS_LINUX) | 35 #if defined(OS_LINUX) |
34 gtk_init_check(&argc, &argv); | 36 gtk_init_check(&argc, &argv); |
35 #endif | 37 #endif |
36 } | 38 } |
37 | 39 |
38 virtual ~TestSuite() {} | 40 virtual ~TestSuite() {} |
39 | 41 |
40 int Run() { | 42 int Run() { |
41 Initialize(); | 43 Initialize(); |
42 #if defined(OS_WIN) || defined(OS_LINUX) | |
43 std::wstring client_func = CommandLine().GetSwitchValue(kRunClientProcess); | 44 std::wstring client_func = CommandLine().GetSwitchValue(kRunClientProcess); |
44 // Check to see if we are being run as a client process. | 45 // Check to see if we are being run as a client process. |
45 if (!client_func.empty()) { | 46 if (!client_func.empty()) { |
46 // Convert our function name to a usable string for GetProcAddress. | 47 // Convert our function name to a usable string for GetProcAddress. |
47 std::string func_name(client_func.begin(), client_func.end()); | 48 std::string func_name(client_func.begin(), client_func.end()); |
48 | 49 |
49 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
50 // Get our module handle and search for an exported function | 51 // Get our module handle and search for an exported function |
51 // which we can use as our client main. | 52 // which we can use as our client main. |
52 MultiProcessTest::ChildFunctionPtr func = | 53 MultiProcessTest::ChildFunctionPtr func = |
53 reinterpret_cast<MultiProcessTest::ChildFunctionPtr>( | 54 reinterpret_cast<MultiProcessTest::ChildFunctionPtr>( |
54 GetProcAddress(GetModuleHandle(NULL), func_name.c_str())); | 55 GetProcAddress(GetModuleHandle(NULL), func_name.c_str())); |
55 #elif defined(OS_LINUX) | 56 #elif defined(OS_LINUX) |
56 void* exobj = dlopen(0, RTLD_LAZY); | 57 void* exobj = dlopen(0, RTLD_LAZY); |
57 MultiProcessTest::ChildFunctionPtr func = | 58 MultiProcessTest::ChildFunctionPtr func = |
58 reinterpret_cast<MultiProcessTest::ChildFunctionPtr>( | 59 reinterpret_cast<MultiProcessTest::ChildFunctionPtr>( |
59 dlsym(exobj, func_name.c_str())); | 60 dlsym(exobj, func_name.c_str())); |
60 #endif // defined(OS_LINUX) | 61 #elif defined(OS_MACOSX) |
| 62 MultiProcessTest::ChildFunctionPtr func = |
| 63 reinterpret_cast<MultiProcessTest::ChildFunctionPtr>( |
| 64 dlsym(RTLD_SELF, func_name.c_str())); |
| 65 #endif // defined(OS_MACOSX) |
61 | 66 |
62 if (func) | 67 if (func) |
63 return (*func)(); | 68 return (*func)(); |
64 return -1; | 69 return -1; |
65 } | 70 } |
66 #endif // defined(OS_WIN) || defined(OS_LINUX) | |
67 int result = RUN_ALL_TESTS(); | 71 int result = RUN_ALL_TESTS(); |
68 | 72 |
69 Shutdown(); | 73 Shutdown(); |
70 return result; | 74 return result; |
71 } | 75 } |
72 | 76 |
73 protected: | 77 protected: |
74 // All fatal log messages (e.g. DCHECK failures) imply unit test failures | 78 // All fatal log messages (e.g. DCHECK failures) imply unit test failures |
75 static void UnitTestAssertHandler(const std::string& str) { | 79 static void UnitTestAssertHandler(const std::string& str) { |
76 FAIL() << str; | 80 FAIL() << str; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 virtual void Shutdown() { | 113 virtual void Shutdown() { |
110 } | 114 } |
111 | 115 |
112 // Make sure that we setup an AtExitManager so Singleton objects will be | 116 // Make sure that we setup an AtExitManager so Singleton objects will be |
113 // destroyed. | 117 // destroyed. |
114 base::AtExitManager at_exit_manager_; | 118 base::AtExitManager at_exit_manager_; |
115 }; | 119 }; |
116 | 120 |
117 #endif // BASE_TEST_SUITE_H_ | 121 #endif // BASE_TEST_SUITE_H_ |
118 | 122 |
OLD | NEW |