1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "multiprocess_func_list.h"
9 // Helper functions to maintain mapping of "test name"->test func.
10 // The information is accessed via a global map.
11 namespace multi_process_function_list {
15 ChildProcessTestRunner g_test_runner = nullptr;
17 struct ProcessFunctions {
18 ProcessFunctions() : main(NULL), setup(NULL) {}
19 ProcessFunctions(TestMainFunctionPtr main, SetupFunctionPtr setup)
23 TestMainFunctionPtr main;
24 SetupFunctionPtr setup;
27 typedef std::map<std::string, ProcessFunctions> MultiProcessTestMap;
29 // Retrieve a reference to the global 'func name' -> func ptr map.
30 MultiProcessTestMap& GetMultiprocessFuncMap() {
31 static MultiProcessTestMap test_name_to_func_ptr_map;
32 return test_name_to_func_ptr_map;
37 AppendMultiProcessTest::AppendMultiProcessTest(
38 std::string test_name,
39 TestMainFunctionPtr main_func_ptr,
40 SetupFunctionPtr setup_func_ptr) {
41 GetMultiprocessFuncMap()[test_name] =
42 ProcessFunctions(main_func_ptr, setup_func_ptr);
45 void SetChildProcessTestRunner(ChildProcessTestRunner runner) {
46 g_test_runner = runner;
49 int InvokeChildProcessTest(const std::string& test_name) {
51 return g_test_runner(test_name);
53 return InvokeChildProcessTestMain(test_name);
56 int InvokeChildProcessTestMain(const std::string& test_name) {
57 MultiProcessTestMap& func_lookup_table = GetMultiprocessFuncMap();
58 MultiProcessTestMap::iterator it = func_lookup_table.find(test_name);
59 if (it != func_lookup_table.end()) {
60 const ProcessFunctions& process_functions = it->second;
61 if (process_functions.setup)
62 (*process_functions.setup)();
63 if (process_functions.main)
64 return (*process_functions.main)();
70 } // namespace multi_process_function_list