lacros_test_helper.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2021 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 "chromeos/lacros/lacros_test_helper.h"
  5. #include "base/check.h"
  6. #include "chromeos/crosapi/mojom/test_controller.mojom-test-utils.h"
  7. #include "chromeos/startup/browser_init_params.h"
  8. namespace chromeos {
  9. namespace {
  10. base::Version GetAshVersion() {
  11. constexpr int min_mojo_version =
  12. crosapi::mojom::TestController::kGetAshVersionMinVersion;
  13. if (chromeos::LacrosService::Get()->GetInterfaceVersion(
  14. crosapi::mojom::TestController::Uuid_) < min_mojo_version) {
  15. return base::Version({0, 0, 0, 0});
  16. }
  17. std::string ash_version_str;
  18. crosapi::mojom::TestControllerAsyncWaiter async_waiter(
  19. chromeos::LacrosService::Get()
  20. ->GetRemote<crosapi::mojom::TestController>()
  21. .get());
  22. async_waiter.GetAshVersion(&ash_version_str);
  23. return base::Version(ash_version_str);
  24. }
  25. } // namespace
  26. ScopedDisableCrosapiForTesting::ScopedDisableCrosapiForTesting()
  27. : disable_crosapi_resetter_(
  28. &BrowserInitParams::disable_crosapi_for_testing_,
  29. true) {
  30. // Ensure that no instance exist, to prevent interference.
  31. CHECK(!LacrosService::Get());
  32. }
  33. // TODO(crbug.com/1196314): Ensure that no instance exist on destruction, too.
  34. // Currently, browser_tests' shutdown is an exception.
  35. ScopedDisableCrosapiForTesting::~ScopedDisableCrosapiForTesting() = default;
  36. ScopedLacrosServiceTestHelper::ScopedLacrosServiceTestHelper() = default;
  37. ScopedLacrosServiceTestHelper::~ScopedLacrosServiceTestHelper() = default;
  38. bool IsAshVersionAtLeastForTesting(base::Version required_version) {
  39. DCHECK(required_version.IsValid());
  40. DCHECK(LacrosService::Get());
  41. static base::Version cached_ash_version = GetAshVersion();
  42. DCHECK(cached_ash_version.IsValid());
  43. return (cached_ash_version >= required_version);
  44. }
  45. } // namespace chromeos