Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / testing / multiprocess_func_list.cc
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.
4
5 #include "multiprocess_func_list.h"
6
7 #include <map>
8
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 {
12
13 namespace {
14
15 ChildProcessTestRunner g_test_runner = nullptr;
16
17 struct ProcessFunctions {
18   ProcessFunctions() : main(NULL), setup(NULL) {}
19   ProcessFunctions(TestMainFunctionPtr main, SetupFunctionPtr setup)
20       : main(main),
21         setup(setup) {
22   }
23   TestMainFunctionPtr main;
24   SetupFunctionPtr setup;
25 };
26
27 typedef std::map<std::string, ProcessFunctions> MultiProcessTestMap;
28
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;
33 }
34
35 }  // namespace
36
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);
43 }
44
45 void SetChildProcessTestRunner(ChildProcessTestRunner runner) {
46   g_test_runner = runner;
47 }
48
49 int InvokeChildProcessTest(const std::string& test_name) {
50   if (g_test_runner) {
51     return g_test_runner(test_name);
52   }
53   return InvokeChildProcessTestMain(test_name);
54 }
55
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)();
65   }
66
67   return -1;
68 }
69
70 }  // namespace multi_process_function_list