license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
evanm@google.com | 60d2d96 | 2008-08-12 08:25:03 +0900 | [diff] [blame] | 5 | #ifndef BASE_TEST_SUITE_H_ |
| 6 | #define BASE_TEST_SUITE_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
| 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 |
| 10 | // any gtest based tests that are linked into your executable. |
| 11 | |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | #include "base/command_line.h" |
| 15 | #include "base/debug_on_start.h" |
| 16 | #include "base/icu_util.h" |
| 17 | #include "base/logging.h" |
| 18 | #include "base/message_loop.h" |
darin@google.com | 12d40bb | 2008-08-20 03:36:23 +0900 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
| 21 | #if defined(OS_WIN) |
| 22 | #include <windows.h> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 23 | #include "base/multiprocess_test.h" |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 24 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 25 | |
| 26 | class TestSuite { |
| 27 | public: |
| 28 | TestSuite(int argc, char** argv) { |
| 29 | testing::InitGoogleTest(&argc, argv); |
| 30 | } |
| 31 | |
| 32 | virtual ~TestSuite() { |
| 33 | // Flush any remaining messages. This ensures that any accumulated Task |
| 34 | // objects get destroyed before we exit, which avoids noise in purify |
| 35 | // leak-test results. |
darin@google.com | a2ac02b | 2008-08-15 13:32:57 +0900 | [diff] [blame] | 36 | message_loop_.RunAllPending(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int Run() { |
| 40 | Initialize(); |
| 41 | |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 42 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 43 | // Check to see if we are being run as a client process. |
| 44 | std::wstring client_func = |
| 45 | parsed_command_line_.GetSwitchValue(kRunClientProcess); |
| 46 | if (!client_func.empty()) { |
| 47 | // Convert our function name to a usable string for GetProcAddress. |
| 48 | std::string func_name(client_func.begin(), client_func.end()); |
| 49 | |
| 50 | // Get our module handle and search for an exported function |
| 51 | // which we can use as our client main. |
| 52 | MultiProcessTest::ChildFunctionPtr func = |
| 53 | reinterpret_cast<MultiProcessTest::ChildFunctionPtr>( |
| 54 | GetProcAddress(GetModuleHandle(NULL), func_name.c_str())); |
| 55 | if (func) |
| 56 | return func(); |
| 57 | return -1; |
| 58 | } |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 59 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 60 | return RUN_ALL_TESTS(); |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | // All fatal log messages (e.g. DCHECK failures) imply unit test failures |
| 65 | static void UnitTestAssertHandler(const std::string& str) { |
| 66 | FAIL() << str; |
| 67 | } |
| 68 | |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 69 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 70 | // Disable crash dialogs so that it doesn't gum up the buildbot |
| 71 | virtual void SuppressErrorDialogs() { |
| 72 | UINT new_flags = SEM_FAILCRITICALERRORS | |
| 73 | SEM_NOGPFAULTERRORBOX | |
| 74 | SEM_NOOPENFILEERRORBOX; |
| 75 | |
evanm@google.com | 60d2d96 | 2008-08-12 08:25:03 +0900 | [diff] [blame] | 76 | // Preserve existing error mode, as discussed at |
| 77 | // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 78 | UINT existing_flags = SetErrorMode(new_flags); |
| 79 | SetErrorMode(existing_flags | new_flags); |
| 80 | } |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 81 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 82 | |
| 83 | virtual void Initialize() { |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 84 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 85 | // In some cases, we do not want to see standard error dialogs. |
| 86 | if (!IsDebuggerPresent() && |
| 87 | !parsed_command_line_.HasSwitch(L"show-error-dialogs")) { |
| 88 | SuppressErrorDialogs(); |
| 89 | logging::SetLogAssertHandler(UnitTestAssertHandler); |
| 90 | } |
pinkerton@google.com | 0c0e01c | 2008-08-09 05:46:21 +0900 | [diff] [blame] | 91 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 92 | |
| 93 | icu_util::Initialize(); |
| 94 | } |
| 95 | |
| 96 | CommandLine parsed_command_line_; |
| 97 | MessageLoop message_loop_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 98 | }; |
| 99 | |
evanm@google.com | 60d2d96 | 2008-08-12 08:25:03 +0900 | [diff] [blame] | 100 | #endif // BASE_TEST_SUITE_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 101 | |