native_timer_unittest.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 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/dbus/power/native_timer.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/memory/ptr_util.h"
  8. #include "base/run_loop.h"
  9. #include "base/test/task_environment.h"
  10. #include "chromeos/dbus/power/fake_power_manager_client.h"
  11. #include "chromeos/dbus/power/power_manager_client.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace chromeos {
  14. class NativeTimerTest : public testing::Test {
  15. public:
  16. NativeTimerTest()
  17. : task_environment_(base::test::TaskEnvironment::MainThreadType::IO,
  18. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  19. NativeTimerTest(const NativeTimerTest&) = delete;
  20. NativeTimerTest& operator=(const NativeTimerTest&) = delete;
  21. ~NativeTimerTest() override = default;
  22. // testing::Test:
  23. void SetUp() override {
  24. PowerManagerClient::InitializeFake();
  25. FakePowerManagerClient::Get()->set_tick_clock(
  26. task_environment_.GetMockTickClock());
  27. }
  28. void TearDown() override { PowerManagerClient::Shutdown(); }
  29. protected:
  30. // Returns true iff |Start| on |timer| succeeds and timer expiration occurs
  31. // too. The underlying fake power manager implementation expires the timer
  32. bool CheckStartTimerAndExpiration(NativeTimer* timer, base::TimeDelta delay) {
  33. base::RunLoop start_timer_loop;
  34. base::RunLoop expiration_loop;
  35. bool start_timer_result = false;
  36. bool expiration_result = false;
  37. timer->Start(task_environment_.GetMockTickClock()->NowTicks() + delay,
  38. base::BindOnce([](bool* result_out) { *result_out = true; },
  39. &expiration_result),
  40. base::BindOnce([](bool* result_out,
  41. bool result) { *result_out = result; },
  42. &start_timer_result));
  43. // Both starting the timer and timer firing should succeed.
  44. task_environment_.FastForwardBy(delay);
  45. if (!start_timer_result)
  46. return false;
  47. return expiration_result;
  48. }
  49. base::test::TaskEnvironment task_environment_;
  50. };
  51. TEST_F(NativeTimerTest, CheckCreateFailure) {
  52. // Create the timer. It queues async operations; enclose it in a run loop.
  53. // This should fail internally as an empty tag is provided.
  54. base::RunLoop create_timer_loop;
  55. NativeTimer timer("");
  56. create_timer_loop.RunUntilIdle();
  57. // Starting the timer should fail as timer creation failed.
  58. EXPECT_FALSE(CheckStartTimerAndExpiration(&timer, base::Milliseconds(1000)));
  59. }
  60. TEST_F(NativeTimerTest, CheckCreateAndStartTimer) {
  61. // Create the timer. It queues async operations; enclose it in a run loop.
  62. base::RunLoop create_timer_loop;
  63. NativeTimer timer("Assistant");
  64. create_timer_loop.RunUntilIdle();
  65. // Start timer and check if starting the timer and its expiration succeeded.
  66. EXPECT_TRUE(CheckStartTimerAndExpiration(&timer, base::Milliseconds(1000)));
  67. // Start another timer and check if starting the timer and its expiration
  68. // succeeded.
  69. EXPECT_TRUE(CheckStartTimerAndExpiration(&timer, base::Milliseconds(1000)));
  70. }
  71. } // namespace chromeos