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 | |
| 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. |
| 14 | static 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.com | dbff4f5 | 2008-08-19 01:00:38 +0900 | [diff] [blame] | 23 | // kRunClientProcess switch, it will deal with it. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 24 | // 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 | // |
| 29 | class 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.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 59 | |