blob: 93bc5e76192c9a31104fb9d7498a3d9619a84794 [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
5#ifndef BASE_MULTIPROCESS_TEST_H__
6#define BASE_MULTIPROCESS_TEST_H__
7
8#include "base/command_line.h"
9#include "base/process_util.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12// Command line switch to invoke a child process rather than
13// to run the normal test suite.
14static const wchar_t kRunClientProcess[] = L"client";
15
16// A MultiProcessTest is a test class which makes it easier to
17// write a test which requires code running out of process.
18//
19// To create a multiprocess test simply follow these steps:
20//
21// 1) Derive your test from MultiProcessTest.
22// 2) Modify your mainline so that if it sees the
ericroman@google.comdbff4f52008-08-19 01:00:38 +090023// kRunClientProcess switch, it will deal with it.
initial.commit3f4a7322008-07-27 06:49:38 +090024// 3) Create a mainline function for the child processes
25// 4) Call SpawnChild("foo"), where "foo" is the name of
26// the function you wish to run in the child processes.
27// That's it!
28//
29class MultiProcessTest : public testing::Test {
30 public:
31 // Prototype function for a client function. Multi-process
32 // clients must provide a callback with this signature to run.
33 typedef int (__cdecl *ChildFunctionPtr)();
34
35 protected:
36 // Run a child process.
37 // 'procname' is the name of a function which the child will
38 // execute. It must be exported from this library in order to
39 // run.
40 //
41 // Example signature:
42 // extern "C" int __declspec(dllexport) FooBar() {
43 // // do client work here
44 // }
45 //
46 // Returns the handle to the child, or NULL on failure
47 HANDLE SpawnChild(const std::wstring& procname) {
48 std::wstring cl(GetCommandLineW());
49 CommandLine::AppendSwitchWithValue(&cl, kRunClientProcess, procname);
50 // TODO(darin): re-enable this once we have base/debug_util.h
51 //ProcessDebugFlags(&cl, DebugUtil::UNKNOWN, false);
52 HANDLE handle = NULL;
53 process_util::LaunchApp(cl, false, true, &handle);
54 return handle;
55 }
56};
57
58#endif // BASE_MULTIPROCESS_TEST_H__
license.botf003cfe2008-08-24 09:55:55 +090059