1 // Copyright (c) 2012 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/compiler_specific.h"
6 #include "base/logging.h"
7 #include "base/test/simple_test_clock.h"
8 #include "base/time/clock.h"
9 #include "media/base/clock.h"
10 #include "testing/gmock/include/gmock/gmock.h"
12 using ::testing::InSequence
;
13 using ::testing::Return
;
14 using ::testing::StrictMock
;
18 // Provide a stream output operator so we can use EXPECT_EQ(...) with TimeDelta.
20 // TODO(scherkus): move this into the testing package.
21 static std::ostream
& operator<<(std::ostream
& stream
, const TimeDelta
& time
) {
22 return (stream
<< time
.ToInternalValue());
29 static const int kDurationInSeconds
= 120;
31 class ClockTest
: public ::testing::Test
{
33 ClockTest() : clock_(&test_clock_
) {
39 const base::TimeDelta kDuration
=
40 base::TimeDelta::FromSeconds(kDurationInSeconds
);
41 clock_
.SetDuration(kDuration
);
42 EXPECT_EQ(kDuration
, clock_
.Duration());
45 void AdvanceSystemTime(base::TimeDelta delta
) {
46 test_clock_
.Advance(delta
);
49 base::SimpleTestClock test_clock_
;
51 base::TimeDelta time_elapsed_
;
54 TEST_F(ClockTest
, Created
) {
55 const base::TimeDelta kExpected
= base::TimeDelta::FromSeconds(0);
56 EXPECT_EQ(kExpected
, clock_
.Elapsed());
59 TEST_F(ClockTest
, Play_NormalSpeed
) {
60 const base::TimeDelta kZero
;
61 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(2);
63 EXPECT_EQ(kZero
, clock_
.Play());
64 AdvanceSystemTime(kTimeToAdvance
);
65 EXPECT_EQ(kTimeToAdvance
, clock_
.Elapsed());
68 TEST_F(ClockTest
, Play_DoubleSpeed
) {
69 const base::TimeDelta kZero
;
70 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(5);
72 clock_
.SetPlaybackRate(2.0f
);
73 EXPECT_EQ(kZero
, clock_
.Play());
74 AdvanceSystemTime(kTimeToAdvance
);
75 EXPECT_EQ(2 * kTimeToAdvance
, clock_
.Elapsed());
78 TEST_F(ClockTest
, Play_HalfSpeed
) {
79 const base::TimeDelta kZero
;
80 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(4);
82 clock_
.SetPlaybackRate(0.5f
);
83 EXPECT_EQ(kZero
, clock_
.Play());
84 AdvanceSystemTime(kTimeToAdvance
);
85 EXPECT_EQ(kTimeToAdvance
/ 2, clock_
.Elapsed());
88 TEST_F(ClockTest
, Play_ZeroSpeed
) {
89 // We'll play for 2 seconds at normal speed, 4 seconds at zero speed, and 8
90 // seconds at normal speed.
91 const base::TimeDelta kZero
;
92 const base::TimeDelta kPlayDuration1
= base::TimeDelta::FromSeconds(2);
93 const base::TimeDelta kPlayDuration2
= base::TimeDelta::FromSeconds(4);
94 const base::TimeDelta kPlayDuration3
= base::TimeDelta::FromSeconds(8);
95 const base::TimeDelta kExpected
= kPlayDuration1
+ kPlayDuration3
;
97 EXPECT_EQ(kZero
, clock_
.Play());
99 AdvanceSystemTime(kPlayDuration1
);
100 clock_
.SetPlaybackRate(0.0f
);
101 AdvanceSystemTime(kPlayDuration2
);
102 clock_
.SetPlaybackRate(1.0f
);
103 AdvanceSystemTime(kPlayDuration3
);
105 EXPECT_EQ(kExpected
, clock_
.Elapsed());
108 TEST_F(ClockTest
, Play_MultiSpeed
) {
109 // We'll play for 2 seconds at half speed, 4 seconds at normal speed, and 8
110 // seconds at double speed.
111 const base::TimeDelta kZero
;
112 const base::TimeDelta kPlayDuration1
= base::TimeDelta::FromSeconds(2);
113 const base::TimeDelta kPlayDuration2
= base::TimeDelta::FromSeconds(4);
114 const base::TimeDelta kPlayDuration3
= base::TimeDelta::FromSeconds(8);
115 const base::TimeDelta kExpected
=
116 kPlayDuration1
/ 2 + kPlayDuration2
+ 2 * kPlayDuration3
;
118 clock_
.SetPlaybackRate(0.5f
);
119 EXPECT_EQ(kZero
, clock_
.Play());
120 AdvanceSystemTime(kPlayDuration1
);
122 clock_
.SetPlaybackRate(1.0f
);
123 AdvanceSystemTime(kPlayDuration2
);
125 clock_
.SetPlaybackRate(2.0f
);
126 AdvanceSystemTime(kPlayDuration3
);
127 EXPECT_EQ(kExpected
, clock_
.Elapsed());
130 TEST_F(ClockTest
, Pause
) {
131 const base::TimeDelta kZero
;
132 const base::TimeDelta kPlayDuration
= base::TimeDelta::FromSeconds(4);
133 const base::TimeDelta kPauseDuration
= base::TimeDelta::FromSeconds(20);
134 const base::TimeDelta kExpectedFirstPause
= kPlayDuration
;
135 const base::TimeDelta kExpectedSecondPause
= 2 * kPlayDuration
;
137 // Play for 4 seconds.
138 EXPECT_EQ(kZero
, clock_
.Play());
139 AdvanceSystemTime(kPlayDuration
);
141 // Pause for 20 seconds.
142 EXPECT_EQ(kExpectedFirstPause
, clock_
.Pause());
143 EXPECT_EQ(kExpectedFirstPause
, clock_
.Elapsed());
144 AdvanceSystemTime(kPauseDuration
);
145 EXPECT_EQ(kExpectedFirstPause
, clock_
.Elapsed());
147 // Play again for 4 more seconds.
148 EXPECT_EQ(kExpectedFirstPause
, clock_
.Play());
149 AdvanceSystemTime(kPlayDuration
);
150 EXPECT_EQ(kExpectedSecondPause
, clock_
.Pause());
151 EXPECT_EQ(kExpectedSecondPause
, clock_
.Elapsed());
154 TEST_F(ClockTest
, SetTime_Paused
) {
155 const base::TimeDelta kFirstTime
= base::TimeDelta::FromSeconds(4);
156 const base::TimeDelta kSecondTime
= base::TimeDelta::FromSeconds(16);
158 clock_
.SetTime(kFirstTime
, clock_
.Duration());
159 EXPECT_EQ(kFirstTime
, clock_
.Elapsed());
160 clock_
.SetTime(kSecondTime
, clock_
.Duration());
161 EXPECT_EQ(kSecondTime
, clock_
.Elapsed());
164 TEST_F(ClockTest
, SetTime_Playing
) {
165 // We'll play for 4 seconds, then set the time to 12, then play for 4 more
167 const base::TimeDelta kZero
;
168 const base::TimeDelta kPlayDuration
= base::TimeDelta::FromSeconds(4);
169 const base::TimeDelta kUpdatedTime
= base::TimeDelta::FromSeconds(12);
170 const base::TimeDelta kExpected
= kUpdatedTime
+ kPlayDuration
;
172 EXPECT_EQ(kZero
, clock_
.Play());
173 AdvanceSystemTime(kPlayDuration
);
175 clock_
.SetTime(kUpdatedTime
, clock_
.Duration());
176 AdvanceSystemTime(kPlayDuration
);
177 EXPECT_EQ(kExpected
, clock_
.Elapsed());
180 TEST_F(ClockTest
, CapAtMediaDuration_Paused
) {
181 const base::TimeDelta kDuration
=
182 base::TimeDelta::FromSeconds(kDurationInSeconds
);
183 const base::TimeDelta kTimeOverDuration
=
184 base::TimeDelta::FromSeconds(kDurationInSeconds
+ 4);
186 // Elapsed time should always be capped at the duration of the media.
187 clock_
.SetTime(kTimeOverDuration
, kTimeOverDuration
);
188 EXPECT_EQ(kDuration
, clock_
.Elapsed());
191 TEST_F(ClockTest
, CapAtMediaDuration_Playing
) {
192 const base::TimeDelta kZero
;
193 const base::TimeDelta kDuration
=
194 base::TimeDelta::FromSeconds(kDurationInSeconds
);
195 const base::TimeDelta kTimeOverDuration
=
196 base::TimeDelta::FromSeconds(kDurationInSeconds
+ 4);
198 // Play for twice as long as the duration of the media.
199 EXPECT_EQ(kZero
, clock_
.Play());
200 AdvanceSystemTime(2 * kDuration
);
201 EXPECT_EQ(kDuration
, clock_
.Elapsed());
203 // Manually set the time past the duration.
204 clock_
.SetTime(kTimeOverDuration
, kTimeOverDuration
);
205 EXPECT_EQ(kDuration
, clock_
.Elapsed());
208 TEST_F(ClockTest
, SetMaxTime
) {
209 const base::TimeDelta kZero
;
210 const base::TimeDelta kTimeInterval
= base::TimeDelta::FromSeconds(4);
211 const base::TimeDelta kMaxTime
= base::TimeDelta::FromSeconds(6);
213 EXPECT_EQ(kZero
, clock_
.Play());
214 clock_
.SetMaxTime(kMaxTime
);
215 AdvanceSystemTime(kTimeInterval
);
216 EXPECT_EQ(kTimeInterval
, clock_
.Elapsed());
218 AdvanceSystemTime(kTimeInterval
);
219 EXPECT_EQ(kMaxTime
, clock_
.Elapsed());
221 AdvanceSystemTime(kTimeInterval
);
222 EXPECT_EQ(kMaxTime
, clock_
.Elapsed());
225 TEST_F(ClockTest
, SetMaxTime_MultipleTimes
) {
226 const base::TimeDelta kZero
;
227 const base::TimeDelta kTimeInterval
= base::TimeDelta::FromSeconds(4);
228 const base::TimeDelta kMaxTime1
= base::TimeDelta::FromSeconds(6);
229 const base::TimeDelta kMaxTime2
= base::TimeDelta::FromSeconds(12);
231 EXPECT_EQ(kZero
, clock_
.Play());
232 clock_
.SetMaxTime(clock_
.Duration());
233 AdvanceSystemTime(kTimeInterval
);
234 EXPECT_EQ(kTimeInterval
, clock_
.Elapsed());
236 clock_
.SetMaxTime(kMaxTime1
);
237 AdvanceSystemTime(kTimeInterval
);
238 EXPECT_EQ(kMaxTime1
, clock_
.Elapsed());
240 AdvanceSystemTime(kTimeInterval
);
241 EXPECT_EQ(kMaxTime1
, clock_
.Elapsed());
243 clock_
.SetMaxTime(kMaxTime2
);
244 EXPECT_EQ(kMaxTime1
, clock_
.Elapsed());
246 AdvanceSystemTime(kTimeInterval
);
247 EXPECT_EQ(kMaxTime1
+ kTimeInterval
, clock_
.Elapsed());
249 AdvanceSystemTime(kTimeInterval
);
250 EXPECT_EQ(kMaxTime2
, clock_
.Elapsed());