xref: /aosp_15_r20/external/cronet/base/timer/elapsed_timer_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 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 
5 #include "base/timer/elapsed_timer.h"
6 
7 #include "base/threading/platform_thread.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace base {
12 
13 namespace {
14 
15 constexpr TimeDelta kSleepDuration = Milliseconds(20);
16 }
17 
TEST(ElapsedTimerTest,Simple)18 TEST(ElapsedTimerTest, Simple) {
19   ElapsedTimer timer;
20 
21   PlatformThread::Sleep(kSleepDuration);
22   EXPECT_GE(timer.Elapsed(), kSleepDuration);
23 
24   // Can call |Elapsed()| multiple times.
25   PlatformThread::Sleep(kSleepDuration);
26   EXPECT_GE(timer.Elapsed(), 2 * kSleepDuration);
27 }
28 
TEST(ElapsedTimerTest,Mocked)29 TEST(ElapsedTimerTest, Mocked) {
30   ScopedMockElapsedTimersForTest mock_elapsed_timer;
31 
32   ElapsedTimer timer;
33   EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
34 
35   // Real-time doesn't matter.
36   PlatformThread::Sleep(kSleepDuration);
37   EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
38 }
39 
40 class ElapsedThreadTimerTest : public ::testing::Test {
41  protected:
SetUp()42   void SetUp() override {
43     if (ThreadTicks::IsSupported())
44       ThreadTicks::WaitUntilInitialized();
45   }
46 };
47 
TEST_F(ElapsedThreadTimerTest,IsSupported)48 TEST_F(ElapsedThreadTimerTest, IsSupported) {
49   ElapsedThreadTimer timer;
50   if (!ThreadTicks::IsSupported()) {
51     EXPECT_FALSE(timer.is_supported());
52     EXPECT_EQ(TimeDelta(), timer.Elapsed());
53   } else {
54     EXPECT_TRUE(timer.is_supported());
55   }
56 }
57 
TEST_F(ElapsedThreadTimerTest,Simple)58 TEST_F(ElapsedThreadTimerTest, Simple) {
59   if (!ThreadTicks::IsSupported())
60     return;
61 
62   ElapsedThreadTimer timer;
63   EXPECT_TRUE(timer.is_supported());
64 
65   // 1ms of work.
66   constexpr TimeDelta kLoopingTime = Milliseconds(1);
67   const ThreadTicks start_ticks = ThreadTicks::Now();
68   while (ThreadTicks::Now() - start_ticks < kLoopingTime) {
69   }
70 
71   EXPECT_GE(timer.Elapsed(), kLoopingTime);
72 }
73 
TEST_F(ElapsedThreadTimerTest,DoesNotCountSleep)74 TEST_F(ElapsedThreadTimerTest, DoesNotCountSleep) {
75   if (!ThreadTicks::IsSupported())
76     return;
77 
78   ElapsedThreadTimer timer;
79   EXPECT_TRUE(timer.is_supported());
80 
81   PlatformThread::Sleep(kSleepDuration);
82   // Sleep time is not accounted for.
83   EXPECT_LT(timer.Elapsed(), kSleepDuration);
84 }
85 
TEST_F(ElapsedThreadTimerTest,Mocked)86 TEST_F(ElapsedThreadTimerTest, Mocked) {
87   if (!ThreadTicks::IsSupported())
88     return;
89 
90   ScopedMockElapsedTimersForTest mock_elapsed_timer;
91 
92   ElapsedThreadTimer timer;
93   EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
94 
95   // Real-time doesn't matter.
96   PlatformThread::Sleep(kSleepDuration);
97   EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
98 }
99 
100 }  // namespace base
101