timer_sampling_event_source_unittest.cc 991 B

123456789101112131415161718192021222324252627282930
  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 "base/power_monitor/timer_sampling_event_source.h"
  5. #include "base/test/bind.h"
  6. #include "base/test/task_environment.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace base {
  9. TEST(TimerSamplingEventSourceTest, Basic) {
  10. constexpr TimeDelta kDelay = Seconds(1);
  11. int num_callbacks = 0;
  12. test::SingleThreadTaskEnvironment task_environment(
  13. test::TaskEnvironment::TimeSource::MOCK_TIME);
  14. TimerSamplingEventSource source(kDelay);
  15. EXPECT_TRUE(source.Start(BindLambdaForTesting([&]() { ++num_callbacks; })));
  16. EXPECT_EQ(0, num_callbacks);
  17. task_environment.FastForwardBy(kDelay / 2);
  18. EXPECT_EQ(0, num_callbacks);
  19. task_environment.FastForwardBy(kDelay / 2);
  20. EXPECT_EQ(1, num_callbacks);
  21. task_environment.FastForwardBy(kDelay * 10);
  22. EXPECT_EQ(11, num_callbacks);
  23. }
  24. } // namespace base