1 // Copyright 2014 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.
5 #include "base/test/simple_test_tick_clock.h"
6 #include "media/base/wall_clock_time_source.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 class WallClockTimeSourceTest
: public testing::Test
{
13 WallClockTimeSourceTest() : tick_clock_(new base::SimpleTestTickClock()) {
14 time_source_
.set_tick_clock_for_testing(tick_clock_
.get());
15 AdvanceTimeInSeconds(1);
17 ~WallClockTimeSourceTest() override
{}
19 void AdvanceTimeInSeconds(int seconds
) {
20 tick_clock_
->Advance(base::TimeDelta::FromSeconds(seconds
));
23 int CurrentMediaTimeInSeconds() {
24 return time_source_
.CurrentMediaTime().InSeconds();
27 void SetMediaTimeInSeconds(int seconds
) {
28 return time_source_
.SetMediaTime(base::TimeDelta::FromSeconds(seconds
));
31 bool IsWallClockNowForMediaTimeInSeconds(int seconds
) {
32 std::vector
<base::TimeTicks
> wall_clock_times
;
33 EXPECT_TRUE(time_source_
.GetWallClockTimes(
34 std::vector
<base::TimeDelta
>(1, base::TimeDelta::FromSeconds(seconds
)),
36 return tick_clock_
->NowTicks() == wall_clock_times
.front();
39 bool IsTimeStopped() {
40 std::vector
<base::TimeTicks
> wall_clock_times
;
41 // Convert any random value, it shouldn't matter for this call.
42 const bool time_stopped
= !time_source_
.GetWallClockTimes(
43 std::vector
<base::TimeDelta
>(1, base::TimeDelta::FromSeconds(1)),
45 EXPECT_EQ(time_stopped
, wall_clock_times
.empty());
50 WallClockTimeSource time_source_
;
51 scoped_ptr
<base::SimpleTestTickClock
> tick_clock_
;
53 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest
);
56 TEST_F(WallClockTimeSourceTest
, InitialTimeIsZero
) {
57 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
58 EXPECT_TRUE(IsTimeStopped());
61 TEST_F(WallClockTimeSourceTest
, InitialTimeIsNotTicking
) {
62 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
63 EXPECT_TRUE(IsTimeStopped());
64 AdvanceTimeInSeconds(100);
65 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
66 EXPECT_TRUE(IsTimeStopped());
69 TEST_F(WallClockTimeSourceTest
, InitialPlaybackRateIsOne
) {
70 time_source_
.StartTicking();
72 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
73 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
74 AdvanceTimeInSeconds(100);
75 EXPECT_EQ(100, CurrentMediaTimeInSeconds());
76 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(100));
79 TEST_F(WallClockTimeSourceTest
, SetMediaTime
) {
80 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
81 EXPECT_TRUE(IsTimeStopped());
82 SetMediaTimeInSeconds(10);
83 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
84 EXPECT_TRUE(IsTimeStopped());
87 TEST_F(WallClockTimeSourceTest
, SetPlaybackRate
) {
88 time_source_
.StartTicking();
90 time_source_
.SetPlaybackRate(0.5);
91 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
92 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
93 AdvanceTimeInSeconds(10);
94 EXPECT_EQ(5, CurrentMediaTimeInSeconds());
95 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5));
97 time_source_
.SetPlaybackRate(2);
98 EXPECT_EQ(5, CurrentMediaTimeInSeconds());
99 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5));
100 AdvanceTimeInSeconds(10);
101 EXPECT_EQ(25, CurrentMediaTimeInSeconds());
102 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(25));
105 TEST_F(WallClockTimeSourceTest
, StopTicking
) {
106 time_source_
.StartTicking();
108 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
109 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
110 AdvanceTimeInSeconds(10);
111 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
112 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(10));
114 time_source_
.StopTicking();
116 AdvanceTimeInSeconds(10);
117 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
118 EXPECT_TRUE(IsTimeStopped());