run_all_core_unittests.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2018 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.
  4. #include "base/base_switches.h"
  5. #include "base/bind.h"
  6. #include "base/command_line.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/logging.h"
  10. #include "base/test/launcher/unit_test_launcher.h"
  11. #include "base/test/scoped_feature_list.h"
  12. #include "base/test/test_suite.h"
  13. #include "build/build_config.h"
  14. #include "mojo/core/mojo_core_unittest.h"
  15. #include "mojo/public/c/system/core.h"
  16. #include "mojo/public/cpp/system/dynamic_library_support.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. base::FilePath GetMojoCoreLibraryPath() {
  19. #if BUILDFLAG(IS_FUCHSIA)
  20. return base::FilePath("libmojo_core.so");
  21. #else // BUILDFLAG(IS_FUCHSIA)
  22. #if BUILDFLAG(IS_WIN)
  23. const char kLibraryFilename[] = "mojo_core.dll";
  24. #else
  25. const char kLibraryFilename[] = "libmojo_core.so";
  26. #endif
  27. base::FilePath executable_dir =
  28. base::CommandLine::ForCurrentProcess()->GetProgram().DirName();
  29. if (executable_dir.IsAbsolute())
  30. return executable_dir.AppendASCII(kLibraryFilename);
  31. base::FilePath current_directory;
  32. CHECK(base::GetCurrentDirectory(&current_directory));
  33. return current_directory.Append(executable_dir).AppendASCII(kLibraryFilename);
  34. #endif // BUILDFLAG(IS_FUCHSIA)
  35. }
  36. int main(int argc, char** argv) {
  37. base::TestSuite test_suite(argc, argv);
  38. base::test::ScopedFeatureList feature_list;
  39. MojoInitializeFlags flags = MOJO_INITIALIZE_FLAG_NONE;
  40. const base::CommandLine& command_line =
  41. *base::CommandLine::ForCurrentProcess();
  42. if (!command_line.HasSwitch(switches::kTestChildProcess))
  43. flags |= MOJO_INITIALIZE_FLAG_AS_BROKER;
  44. absl::optional<base::FilePath> library_path;
  45. if (command_line.HasSwitch(switches::kMojoUseExplicitLibraryPath))
  46. library_path = GetMojoCoreLibraryPath();
  47. feature_list.InitFromCommandLine(
  48. command_line.GetSwitchValueASCII(switches::kEnableFeatures),
  49. command_line.GetSwitchValueASCII(switches::kDisableFeatures));
  50. if (command_line.HasSwitch(switches::kMojoLoadBeforeInit)) {
  51. CHECK_EQ(MOJO_RESULT_OK, mojo::LoadCoreLibrary(library_path));
  52. CHECK_EQ(MOJO_RESULT_OK, mojo::InitializeCoreLibrary(flags));
  53. } else {
  54. CHECK_EQ(MOJO_RESULT_OK,
  55. mojo::LoadAndInitializeCoreLibrary(library_path, flags));
  56. }
  57. int result = base::LaunchUnitTests(
  58. argc, argv,
  59. base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
  60. CHECK_EQ(MOJO_RESULT_OK, MojoShutdown(nullptr));
  61. return result;
  62. }