blob: 5127be34aab486197c452694ef9c752e87142784 [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
15constexpr TimeDelta kSleepDuration = TimeDelta::FromMilliseconds(20);
16
17}
18
19TEST(ElapsedTimerTest, Simple) {
20 ElapsedTimer timer;
21
22 PlatformThread::Sleep(kSleepDuration);
23 EXPECT_GE(timer.Elapsed(), kSleepDuration);
24
25 // Can call |Elapsed()| multiple times.
26 PlatformThread::Sleep(kSleepDuration);
27 EXPECT_GE(timer.Elapsed(), 2 * kSleepDuration);
28}
29
Gabriel Charetteef6cbc22019-08-02 06:24:5930TEST(ElapsedTimerTest, Mocked) {
31 ScopedMockElapsedTimersForTest mock_elapsed_timer;
32
33 ElapsedTimer timer;
34 EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
35
36 // Real-time doesn't matter.
37 PlatformThread::Sleep(kSleepDuration);
38 EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
39}
40
Benoît Lizé0af836f2019-05-15 09:44:4141class ElapsedThreadTimerTest : public ::testing::Test {
42 protected:
43 void SetUp() override {
44 if (ThreadTicks::IsSupported())
45 ThreadTicks::WaitUntilInitialized();
46 }
47};
48
49TEST_F(ElapsedThreadTimerTest, IsSupported) {
50 ElapsedThreadTimer timer;
51 if (!ThreadTicks::IsSupported()) {
52 EXPECT_FALSE(timer.is_supported());
53 EXPECT_EQ(TimeDelta(), timer.Elapsed());
54 } else {
55 EXPECT_TRUE(timer.is_supported());
56 }
57}
58
59TEST_F(ElapsedThreadTimerTest, Simple) {
60 if (!ThreadTicks::IsSupported())
61 return;
62
63 ElapsedThreadTimer timer;
64 EXPECT_TRUE(timer.is_supported());
65
66 // 1ms of work.
67 constexpr TimeDelta kLoopingTime = TimeDelta::FromMilliseconds(1);
68 const ThreadTicks start_ticks = ThreadTicks::Now();
69 while (ThreadTicks::Now() - start_ticks < kLoopingTime) {
70 }
71
72 EXPECT_GE(timer.Elapsed(), kLoopingTime);
73}
74
75TEST_F(ElapsedThreadTimerTest, DoesNotCountSleep) {
76 if (!ThreadTicks::IsSupported())
77 return;
78
79 ElapsedThreadTimer timer;
80 EXPECT_TRUE(timer.is_supported());
81
82 PlatformThread::Sleep(kSleepDuration);
83 // Sleep time is not accounted for.
84 EXPECT_LT(timer.Elapsed(), kSleepDuration);
85}
86
Gabriel Charetteef6cbc22019-08-02 06:24:5987TEST_F(ElapsedThreadTimerTest, Mocked) {
88 if (!ThreadTicks::IsSupported())
89 return;
90
91 ScopedMockElapsedTimersForTest mock_elapsed_timer;
92
93 ElapsedThreadTimer timer;
94 EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
95
96 // Real-time doesn't matter.
97 PlatformThread::Sleep(kSleepDuration);
98 EXPECT_EQ(timer.Elapsed(), ScopedMockElapsedTimersForTest::kMockElapsedTime);
99}
100
Benoît Lizé0af836f2019-05-15 09:44:41101} // namespace base