multiprocess_func_list.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "multiprocess_func_list.h"
  5. #include <map>
  6. // Helper functions to maintain mapping of "test name"->test func.
  7. // The information is accessed via a global map.
  8. namespace multi_process_function_list {
  9. namespace {
  10. ChildProcessTestRunner g_test_runner = nullptr;
  11. struct ProcessFunctions {
  12. ProcessFunctions() : main(NULL), setup(NULL) {}
  13. ProcessFunctions(TestMainFunctionPtr main, SetupFunctionPtr setup)
  14. : main(main),
  15. setup(setup) {
  16. }
  17. TestMainFunctionPtr main;
  18. SetupFunctionPtr setup;
  19. };
  20. typedef std::map<std::string, ProcessFunctions> MultiProcessTestMap;
  21. // Retrieve a reference to the global 'func name' -> func ptr map.
  22. MultiProcessTestMap& GetMultiprocessFuncMap() {
  23. static MultiProcessTestMap test_name_to_func_ptr_map;
  24. return test_name_to_func_ptr_map;
  25. }
  26. } // namespace
  27. AppendMultiProcessTest::AppendMultiProcessTest(
  28. std::string test_name,
  29. TestMainFunctionPtr main_func_ptr,
  30. SetupFunctionPtr setup_func_ptr) {
  31. GetMultiprocessFuncMap()[test_name] =
  32. ProcessFunctions(main_func_ptr, setup_func_ptr);
  33. }
  34. void SetChildProcessTestRunner(ChildProcessTestRunner runner) {
  35. g_test_runner = runner;
  36. }
  37. int InvokeChildProcessTest(const std::string& test_name) {
  38. if (g_test_runner) {
  39. return g_test_runner(test_name);
  40. }
  41. return InvokeChildProcessTestMain(test_name);
  42. }
  43. int InvokeChildProcessTestMain(const std::string& test_name) {
  44. MultiProcessTestMap& func_lookup_table = GetMultiprocessFuncMap();
  45. MultiProcessTestMap::iterator it = func_lookup_table.find(test_name);
  46. if (it != func_lookup_table.end()) {
  47. const ProcessFunctions& process_functions = it->second;
  48. if (process_functions.setup)
  49. (*process_functions.setup)();
  50. if (process_functions.main)
  51. return (*process_functions.main)();
  52. }
  53. return -1;
  54. }
  55. } // namespace multi_process_function_list