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/logging.h"
6 #include "base/test/mock_time_provider.h"
7 #include "media/base/clock.h"
8 #include "testing/gmock/include/gmock/gmock.h"
10 using ::testing::InSequence
;
11 using ::testing::Return
;
12 using ::testing::StrictMock
;
16 // Provide a stream output operator so we can use EXPECT_EQ(...) with TimeDelta.
18 // TODO(scherkus): move this into the testing package.
19 static std::ostream
& operator<<(std::ostream
& stream
, const TimeDelta
& time
) {
20 return (stream
<< time
.ToInternalValue());
27 static const int kDurationInSeconds
= 120;
29 class ClockTest
: public ::testing::Test
{
32 : clock_(&base::MockTimeProvider::StaticNow
) {
34 EXPECT_CALL(mock_time_
, Now())
35 .WillRepeatedly(Return(base::Time::UnixEpoch()));
40 const base::TimeDelta kDuration
=
41 base::TimeDelta::FromSeconds(kDurationInSeconds
);
42 clock_
.SetDuration(kDuration
);
43 EXPECT_EQ(kDuration
, clock_
.Duration());
46 void AdvanceSystemTime(base::TimeDelta delta
) {
47 time_elapsed_
+= delta
;
48 EXPECT_CALL(mock_time_
, Now())
49 .WillRepeatedly(Return(base::Time::UnixEpoch() + time_elapsed_
));
53 StrictMock
<base::MockTimeProvider
> mock_time_
;
54 base::TimeDelta time_elapsed_
;
57 TEST_F(ClockTest
, Created
) {
58 const base::TimeDelta kExpected
= base::TimeDelta::FromSeconds(0);
59 EXPECT_EQ(kExpected
, clock_
.Elapsed());
62 TEST_F(ClockTest
, Play_NormalSpeed
) {
63 const base::TimeDelta kZero
;
64 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(2);
66 EXPECT_EQ(kZero
, clock_
.Play());
67 AdvanceSystemTime(kTimeToAdvance
);
68 EXPECT_EQ(kTimeToAdvance
, clock_
.Elapsed());
71 TEST_F(ClockTest
, Play_DoubleSpeed
) {
72 const base::TimeDelta kZero
;
73 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(5);
75 clock_
.SetPlaybackRate(2.0f
);
76 EXPECT_EQ(kZero
, clock_
.Play());
77 AdvanceSystemTime(kTimeToAdvance
);
78 EXPECT_EQ(2 * kTimeToAdvance
, clock_
.Elapsed());
81 TEST_F(ClockTest
, Play_HalfSpeed
) {
82 const base::TimeDelta kZero
;
83 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(4);
85 clock_
.SetPlaybackRate(0.5f
);
86 EXPECT_EQ(kZero
, clock_
.Play());
87 AdvanceSystemTime(kTimeToAdvance
);
88 EXPECT_EQ(kTimeToAdvance
/ 2, clock_
.Elapsed());
91 TEST_F(ClockTest
, Play_ZeroSpeed
) {
92 // We'll play for 2 seconds at normal speed, 4 seconds at zero speed, and 8
93 // seconds at normal speed.
94 const base::TimeDelta kZero
;
95 const base::TimeDelta kPlayDuration1
= base::TimeDelta::FromSeconds(2);
96 const base::TimeDelta kPlayDuration2
= base::TimeDelta::FromSeconds(4);
97 const base::TimeDelta kPlayDuration3
= base::TimeDelta::FromSeconds(8);
98 const base::TimeDelta kExpected
= kPlayDuration1
+ kPlayDuration3
;
100 EXPECT_EQ(kZero
, clock_
.Play());
102 AdvanceSystemTime(kPlayDuration1
);
103 clock_
.SetPlaybackRate(0.0f
);
104 AdvanceSystemTime(kPlayDuration2
);
105 clock_
.SetPlaybackRate(1.0f
);
106 AdvanceSystemTime(kPlayDuration3
);
108 EXPECT_EQ(kExpected
, clock_
.Elapsed());
111 TEST_F(ClockTest
, Play_MultiSpeed
) {
112 // We'll play for 2 seconds at half speed, 4 seconds at normal speed, and 8
113 // seconds at double speed.
114 const base::TimeDelta kZero
;
115 const base::TimeDelta kPlayDuration1
= base::TimeDelta::FromSeconds(2);
116 const base::TimeDelta kPlayDuration2
= base::TimeDelta::FromSeconds(4);
117 const base::TimeDelta kPlayDuration3
= base::TimeDelta::FromSeconds(8);
118 const base::TimeDelta kExpected
=
119 kPlayDuration1
/ 2 + kPlayDuration2
+ 2 * kPlayDuration3
;
121 clock_
.SetPlaybackRate(0.5f
);
122 EXPECT_EQ(kZero
, clock_
.Play());
123 AdvanceSystemTime(kPlayDuration1
);
125 clock_
.SetPlaybackRate(1.0f
);
126 AdvanceSystemTime(kPlayDuration2
);
128 clock_
.SetPlaybackRate(2.0f
);
129 AdvanceSystemTime(kPlayDuration3
);
130 EXPECT_EQ(kExpected
, clock_
.Elapsed());
133 TEST_F(ClockTest
, Pause
) {
134 const base::TimeDelta kZero
;
135 const base::TimeDelta kPlayDuration
= base::TimeDelta::FromSeconds(4);
136 const base::TimeDelta kPauseDuration
= base::TimeDelta::FromSeconds(20);
137 const base::TimeDelta kExpectedFirstPause
= kPlayDuration
;
138 const base::TimeDelta kExpectedSecondPause
= 2 * kPlayDuration
;
140 // Play for 4 seconds.
141 EXPECT_EQ(kZero
, clock_
.Play());
142 AdvanceSystemTime(kPlayDuration
);
144 // Pause for 20 seconds.
145 EXPECT_EQ(kExpectedFirstPause
, clock_
.Pause());
146 EXPECT_EQ(kExpectedFirstPause
, clock_
.Elapsed());
147 AdvanceSystemTime(kPauseDuration
);
148 EXPECT_EQ(kExpectedFirstPause
, clock_
.Elapsed());
150 // Play again for 4 more seconds.
151 EXPECT_EQ(kExpectedFirstPause
, clock_
.Play());
152 AdvanceSystemTime(kPlayDuration
);
153 EXPECT_EQ(kExpectedSecondPause
, clock_
.Pause());
154 EXPECT_EQ(kExpectedSecondPause
, clock_
.Elapsed());
157 TEST_F(ClockTest
, SetTime_Paused
) {
158 const base::TimeDelta kFirstTime
= base::TimeDelta::FromSeconds(4);
159 const base::TimeDelta kSecondTime
= base::TimeDelta::FromSeconds(16);
161 clock_
.SetTime(kFirstTime
, clock_
.Duration());
162 EXPECT_EQ(kFirstTime
, clock_
.Elapsed());
163 clock_
.SetTime(kSecondTime
, clock_
.Duration());
164 EXPECT_EQ(kSecondTime
, clock_
.Elapsed());
167 TEST_F(ClockTest
, SetTime_Playing
) {
168 // We'll play for 4 seconds, then set the time to 12, then play for 4 more
170 const base::TimeDelta kZero
;
171 const base::TimeDelta kPlayDuration
= base::TimeDelta::FromSeconds(4);
172 const base::TimeDelta kUpdatedTime
= base::TimeDelta::FromSeconds(12);
173 const base::TimeDelta kExpected
= kUpdatedTime
+ kPlayDuration
;
175 EXPECT_EQ(kZero
, clock_
.Play());
176 AdvanceSystemTime(kPlayDuration
);
178 clock_
.SetTime(kUpdatedTime
, clock_
.Duration());
179 AdvanceSystemTime(kPlayDuration
);
180 EXPECT_EQ(kExpected
, clock_
.Elapsed());
183 TEST_F(ClockTest
, CapAtMediaDuration_Paused
) {
184 const base::TimeDelta kDuration
=
185 base::TimeDelta::FromSeconds(kDurationInSeconds
);
186 const base::TimeDelta kTimeOverDuration
=
187 base::TimeDelta::FromSeconds(kDurationInSeconds
+ 4);
189 // Elapsed time should always be capped at the duration of the media.
190 clock_
.SetTime(kTimeOverDuration
, kTimeOverDuration
);
191 EXPECT_EQ(kDuration
, clock_
.Elapsed());
194 TEST_F(ClockTest
, CapAtMediaDuration_Playing
) {
195 const base::TimeDelta kZero
;
196 const base::TimeDelta kDuration
=
197 base::TimeDelta::FromSeconds(kDurationInSeconds
);
198 const base::TimeDelta kTimeOverDuration
=
199 base::TimeDelta::FromSeconds(kDurationInSeconds
+ 4);
201 // Play for twice as long as the duration of the media.
202 EXPECT_EQ(kZero
, clock_
.Play());
203 AdvanceSystemTime(2 * kDuration
);
204 EXPECT_EQ(kDuration
, clock_
.Elapsed());
206 // Manually set the time past the duration.
207 clock_
.SetTime(kTimeOverDuration
, kTimeOverDuration
);
208 EXPECT_EQ(kDuration
, clock_
.Elapsed());
211 TEST_F(ClockTest
, SetMaxTime
) {
212 const base::TimeDelta kZero
;
213 const base::TimeDelta kTimeInterval
= base::TimeDelta::FromSeconds(4);
214 const base::TimeDelta kMaxTime
= base::TimeDelta::FromSeconds(6);
216 EXPECT_EQ(kZero
, clock_
.Play());
217 clock_
.SetMaxTime(kMaxTime
);
218 AdvanceSystemTime(kTimeInterval
);
219 EXPECT_EQ(kTimeInterval
, clock_
.Elapsed());
221 AdvanceSystemTime(kTimeInterval
);
222 EXPECT_EQ(kMaxTime
, clock_
.Elapsed());
224 AdvanceSystemTime(kTimeInterval
);
225 EXPECT_EQ(kMaxTime
, clock_
.Elapsed());
228 TEST_F(ClockTest
, SetMaxTime_MultipleTimes
) {
229 const base::TimeDelta kZero
;
230 const base::TimeDelta kTimeInterval
= base::TimeDelta::FromSeconds(4);
231 const base::TimeDelta kMaxTime1
= base::TimeDelta::FromSeconds(6);
232 const base::TimeDelta kMaxTime2
= base::TimeDelta::FromSeconds(12);
234 EXPECT_EQ(kZero
, clock_
.Play());
235 clock_
.SetMaxTime(clock_
.Duration());
236 AdvanceSystemTime(kTimeInterval
);
237 EXPECT_EQ(kTimeInterval
, clock_
.Elapsed());
239 clock_
.SetMaxTime(kMaxTime1
);
240 AdvanceSystemTime(kTimeInterval
);
241 EXPECT_EQ(kMaxTime1
, clock_
.Elapsed());
243 AdvanceSystemTime(kTimeInterval
);
244 EXPECT_EQ(kMaxTime1
, clock_
.Elapsed());
246 clock_
.SetMaxTime(kMaxTime2
);
247 EXPECT_EQ(kMaxTime1
, clock_
.Elapsed());
249 AdvanceSystemTime(kTimeInterval
);
250 EXPECT_EQ(kMaxTime1
+ kTimeInterval
, clock_
.Elapsed());
252 AdvanceSystemTime(kTimeInterval
);
253 EXPECT_EQ(kMaxTime2
, clock_
.Elapsed());