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_
.SetTickClockForTesting(
15 scoped_ptr
<base::TickClock
>(tick_clock_
));
16 AdvanceTimeInSeconds(1);
18 ~WallClockTimeSourceTest() override
{}
20 void AdvanceTimeInSeconds(int seconds
) {
21 tick_clock_
->Advance(base::TimeDelta::FromSeconds(seconds
));
24 int CurrentMediaTimeInSeconds() {
25 return time_source_
.CurrentMediaTime().InSeconds();
28 void SetMediaTimeInSeconds(int seconds
) {
29 return time_source_
.SetMediaTime(base::TimeDelta::FromSeconds(seconds
));
32 bool IsWallClockNowForMediaTimeInSeconds(int seconds
) {
33 return tick_clock_
->NowTicks() ==
34 time_source_
.GetWallClockTime(base::TimeDelta::FromSeconds(seconds
));
37 bool IsWallClockNullForMediaTimeInSeconds(int seconds
) {
38 return time_source_
.GetWallClockTime(base::TimeDelta::FromSeconds(seconds
))
43 WallClockTimeSource time_source_
;
44 base::SimpleTestTickClock
* tick_clock_
; // Owned by |time_source_|.
46 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest
);
49 TEST_F(WallClockTimeSourceTest
, InitialTimeIsZero
) {
50 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
51 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0));
54 TEST_F(WallClockTimeSourceTest
, InitialTimeIsNotTicking
) {
55 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
56 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0));
57 AdvanceTimeInSeconds(100);
58 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
59 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0));
62 TEST_F(WallClockTimeSourceTest
, InitialPlaybackRateIsOne
) {
63 time_source_
.StartTicking();
65 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
66 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
67 AdvanceTimeInSeconds(100);
68 EXPECT_EQ(100, CurrentMediaTimeInSeconds());
69 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(100));
72 TEST_F(WallClockTimeSourceTest
, SetMediaTime
) {
73 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
74 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0));
75 SetMediaTimeInSeconds(10);
76 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
77 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(10));
80 TEST_F(WallClockTimeSourceTest
, SetPlaybackRate
) {
81 time_source_
.StartTicking();
83 time_source_
.SetPlaybackRate(0.5);
84 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
85 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
86 AdvanceTimeInSeconds(10);
87 EXPECT_EQ(5, CurrentMediaTimeInSeconds());
88 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5));
90 time_source_
.SetPlaybackRate(2);
91 EXPECT_EQ(5, CurrentMediaTimeInSeconds());
92 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5));
93 AdvanceTimeInSeconds(10);
94 EXPECT_EQ(25, CurrentMediaTimeInSeconds());
95 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(25));
98 TEST_F(WallClockTimeSourceTest
, StopTicking
) {
99 time_source_
.StartTicking();
101 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
102 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
103 AdvanceTimeInSeconds(10);
104 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
105 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(10));
107 time_source_
.StopTicking();
109 AdvanceTimeInSeconds(10);
110 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
111 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(10));