blob: 8a163ec17c1aa4d11a19f19c2903d054c38a531c [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.commit3f4a7322008-07-27 06:49:38 +09004
evanm@google.com60d2d962008-08-12 08:25:03 +09005#ifndef BASE_TEST_SUITE_H_
6#define BASE_TEST_SUITE_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
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.com0c0e01c2008-08-09 05:46:21 +090012#include "build/build_config.h"
13
initial.commit3f4a7322008-07-27 06:49:38 +090014#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.com12d40bb2008-08-20 03:36:23 +090019#include "testing/gtest/include/gtest/gtest.h"
20
21#if defined(OS_WIN)
22#include <windows.h>
initial.commit3f4a7322008-07-27 06:49:38 +090023#include "base/multiprocess_test.h"
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090024#endif
initial.commit3f4a7322008-07-27 06:49:38 +090025
26class 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.coma2ac02b2008-08-15 13:32:57 +090036 message_loop_.RunAllPending();
initial.commit3f4a7322008-07-27 06:49:38 +090037 }
38
39 int Run() {
40 Initialize();
41
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090042#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090043 // 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.com0c0e01c2008-08-09 05:46:21 +090059#endif
initial.commit3f4a7322008-07-27 06:49:38 +090060 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.com0c0e01c2008-08-09 05:46:21 +090069#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090070 // 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.com60d2d962008-08-12 08:25:03 +090076 // Preserve existing error mode, as discussed at
77 // http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
initial.commit3f4a7322008-07-27 06:49:38 +090078 UINT existing_flags = SetErrorMode(new_flags);
79 SetErrorMode(existing_flags | new_flags);
80 }
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090081#endif
initial.commit3f4a7322008-07-27 06:49:38 +090082
83 virtual void Initialize() {
pinkerton@google.com0c0e01c2008-08-09 05:46:21 +090084#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090085 // 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.com0c0e01c2008-08-09 05:46:21 +090091#endif
initial.commit3f4a7322008-07-27 06:49:38 +090092
93 icu_util::Initialize();
94 }
95
96 CommandLine parsed_command_line_;
97 MessageLoop message_loop_;
initial.commit3f4a7322008-07-27 06:49:38 +090098};
99
evanm@google.com60d2d962008-08-12 08:25:03 +0900100#endif // BASE_TEST_SUITE_H_
license.botf003cfe2008-08-24 09:55:55 +0900101