blob: 04ede3bca27b01e8432cf5d7ddf5274b97771b31 [file] [log] [blame]
Benoît Lizé0af836f2019-05-15 09:44:411// 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
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
11namespace base {
12
13namespace {
14
Peter Kasting53fd6ee2021-10-05 20:40:4815constexpr TimeDelta kSleepDuration = Milliseconds(20);
Benoît Lizé0af836f2019-05-15 09:44:4116}
17
18TEST(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
Gabriel Charetteef6cbc22019-08-02 06:24:5929TEST(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
Benoît Lizé0af836f2019-05-15 09:44:4140class ElapsedThreadTimerTest : public ::testing::Test {
41 protected:
42 void SetUp() override {
43 if (ThreadTicks::IsSupported())
44 ThreadTicks::WaitUntilInitialized();
45 }
46};
47
48TEST_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
58TEST_F(ElapsedThreadTimerTest, Simple) {
59 if (!ThreadTicks::IsSupported())
60 return;
61
62 ElapsedThreadTimer timer;
63 EXPECT_TRUE(timer.is_supported());
64
65 // 1ms of work.
Peter Kasting53fd6ee2021-10-05 20:40:4866 constexpr TimeDelta kLoopingTime = Milliseconds(1);
Benoît Lizé0af836f2019-05-15 09:44:4167 const ThreadTicks start_ticks = ThreadTicks::Now();
68 while (ThreadTicks::Now() - start_ticks < kLoopingTime) {
69 }
70
71 EXPECT_GE(timer.Elapsed(), kLoopingTime);
72}
73
74TEST_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
Gabriel Charetteef6cbc22019-08-02 06:24:5986TEST_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
Benoît Lizé0af836f2019-05-15 09:44:41100} // namespace base