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/logging.h"
6 #include "base/test/simple_test_tick_clock.h"
7 #include "media/base/time_delta_interpolator.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 class TimeDeltaInterpolatorTest
: public ::testing::Test
{
14 TimeDeltaInterpolatorTest() : interpolator_(&test_tick_clock_
) {}
17 void AdvanceSystemTime(base::TimeDelta delta
) {
18 test_tick_clock_
.Advance(delta
);
21 base::SimpleTestTickClock test_tick_clock_
;
22 TimeDeltaInterpolator interpolator_
;
25 TEST_F(TimeDeltaInterpolatorTest
, Created
) {
26 const base::TimeDelta kExpected
= base::TimeDelta::FromSeconds(0);
27 EXPECT_EQ(kExpected
, interpolator_
.GetInterpolatedTime());
30 TEST_F(TimeDeltaInterpolatorTest
, StartInterpolating_NormalSpeed
) {
31 const base::TimeDelta kZero
;
32 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(2);
34 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
35 AdvanceSystemTime(kTimeToAdvance
);
36 EXPECT_EQ(kTimeToAdvance
, interpolator_
.GetInterpolatedTime());
39 TEST_F(TimeDeltaInterpolatorTest
, StartInterpolating_DoubleSpeed
) {
40 const base::TimeDelta kZero
;
41 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(5);
43 interpolator_
.SetPlaybackRate(2.0);
44 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
45 AdvanceSystemTime(kTimeToAdvance
);
46 EXPECT_EQ(2 * kTimeToAdvance
, interpolator_
.GetInterpolatedTime());
49 TEST_F(TimeDeltaInterpolatorTest
, StartInterpolating_HalfSpeed
) {
50 const base::TimeDelta kZero
;
51 const base::TimeDelta kTimeToAdvance
= base::TimeDelta::FromSeconds(4);
53 interpolator_
.SetPlaybackRate(0.5);
54 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
55 AdvanceSystemTime(kTimeToAdvance
);
56 EXPECT_EQ(kTimeToAdvance
/ 2, interpolator_
.GetInterpolatedTime());
59 TEST_F(TimeDeltaInterpolatorTest
, StartInterpolating_ZeroSpeed
) {
60 // We'll play for 2 seconds at normal speed, 4 seconds at zero speed, and 8
61 // seconds at normal speed.
62 const base::TimeDelta kZero
;
63 const base::TimeDelta kPlayDuration1
= base::TimeDelta::FromSeconds(2);
64 const base::TimeDelta kPlayDuration2
= base::TimeDelta::FromSeconds(4);
65 const base::TimeDelta kPlayDuration3
= base::TimeDelta::FromSeconds(8);
66 const base::TimeDelta kExpected
= kPlayDuration1
+ kPlayDuration3
;
68 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
70 AdvanceSystemTime(kPlayDuration1
);
71 interpolator_
.SetPlaybackRate(0.0);
72 AdvanceSystemTime(kPlayDuration2
);
73 interpolator_
.SetPlaybackRate(1.0);
74 AdvanceSystemTime(kPlayDuration3
);
76 EXPECT_EQ(kExpected
, interpolator_
.GetInterpolatedTime());
79 TEST_F(TimeDeltaInterpolatorTest
, StartInterpolating_MultiSpeed
) {
80 // We'll play for 2 seconds at half speed, 4 seconds at normal speed, and 8
81 // seconds at double speed.
82 const base::TimeDelta kZero
;
83 const base::TimeDelta kPlayDuration1
= base::TimeDelta::FromSeconds(2);
84 const base::TimeDelta kPlayDuration2
= base::TimeDelta::FromSeconds(4);
85 const base::TimeDelta kPlayDuration3
= base::TimeDelta::FromSeconds(8);
86 const base::TimeDelta kExpected
=
87 kPlayDuration1
/ 2 + kPlayDuration2
+ 2 * kPlayDuration3
;
89 interpolator_
.SetPlaybackRate(0.5);
90 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
91 AdvanceSystemTime(kPlayDuration1
);
93 interpolator_
.SetPlaybackRate(1.0);
94 AdvanceSystemTime(kPlayDuration2
);
96 interpolator_
.SetPlaybackRate(2.0);
97 AdvanceSystemTime(kPlayDuration3
);
98 EXPECT_EQ(kExpected
, interpolator_
.GetInterpolatedTime());
101 TEST_F(TimeDeltaInterpolatorTest
, StopInterpolating
) {
102 const base::TimeDelta kZero
;
103 const base::TimeDelta kPlayDuration
= base::TimeDelta::FromSeconds(4);
104 const base::TimeDelta kPauseDuration
= base::TimeDelta::FromSeconds(20);
105 const base::TimeDelta kExpectedFirstPause
= kPlayDuration
;
106 const base::TimeDelta kExpectedSecondPause
= 2 * kPlayDuration
;
108 // Play for 4 seconds.
109 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
110 AdvanceSystemTime(kPlayDuration
);
112 // Pause for 20 seconds.
113 EXPECT_EQ(kExpectedFirstPause
, interpolator_
.StopInterpolating());
114 EXPECT_EQ(kExpectedFirstPause
, interpolator_
.GetInterpolatedTime());
115 AdvanceSystemTime(kPauseDuration
);
116 EXPECT_EQ(kExpectedFirstPause
, interpolator_
.GetInterpolatedTime());
118 // Play again for 4 more seconds.
119 EXPECT_EQ(kExpectedFirstPause
, interpolator_
.StartInterpolating());
120 AdvanceSystemTime(kPlayDuration
);
121 EXPECT_EQ(kExpectedSecondPause
, interpolator_
.StopInterpolating());
122 EXPECT_EQ(kExpectedSecondPause
, interpolator_
.GetInterpolatedTime());
125 TEST_F(TimeDeltaInterpolatorTest
, SetBounds_Stopped
) {
126 const base::TimeDelta kFirstTime
= base::TimeDelta::FromSeconds(4);
127 const base::TimeDelta kSecondTime
= base::TimeDelta::FromSeconds(16);
128 const base::TimeDelta kArbitraryMaxTime
= base::TimeDelta::FromSeconds(100);
130 interpolator_
.SetBounds(kFirstTime
, kArbitraryMaxTime
);
131 EXPECT_EQ(kFirstTime
, interpolator_
.GetInterpolatedTime());
132 interpolator_
.SetBounds(kSecondTime
, kArbitraryMaxTime
);
133 EXPECT_EQ(kSecondTime
, interpolator_
.GetInterpolatedTime());
136 TEST_F(TimeDeltaInterpolatorTest
, SetBounds_Started
) {
137 // We'll play for 4 seconds, then set the time to 12, then play for 4 more
139 const base::TimeDelta kZero
;
140 const base::TimeDelta kPlayDuration
= base::TimeDelta::FromSeconds(4);
141 const base::TimeDelta kUpdatedTime
= base::TimeDelta::FromSeconds(12);
142 const base::TimeDelta kArbitraryMaxTime
= base::TimeDelta::FromSeconds(100);
143 const base::TimeDelta kExpected
= kUpdatedTime
+ kPlayDuration
;
145 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
146 AdvanceSystemTime(kPlayDuration
);
148 interpolator_
.SetBounds(kUpdatedTime
, kArbitraryMaxTime
);
149 AdvanceSystemTime(kPlayDuration
);
150 EXPECT_EQ(kExpected
, interpolator_
.GetInterpolatedTime());
153 TEST_F(TimeDeltaInterpolatorTest
, SetUpperBound
) {
154 const base::TimeDelta kZero
;
155 const base::TimeDelta kTimeInterval
= base::TimeDelta::FromSeconds(4);
156 const base::TimeDelta kMaxTime
= base::TimeDelta::FromSeconds(6);
158 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
159 interpolator_
.SetUpperBound(kMaxTime
);
160 AdvanceSystemTime(kTimeInterval
);
161 EXPECT_EQ(kTimeInterval
, interpolator_
.GetInterpolatedTime());
163 AdvanceSystemTime(kTimeInterval
);
164 EXPECT_EQ(kMaxTime
, interpolator_
.GetInterpolatedTime());
166 AdvanceSystemTime(kTimeInterval
);
167 EXPECT_EQ(kMaxTime
, interpolator_
.GetInterpolatedTime());
170 TEST_F(TimeDeltaInterpolatorTest
, SetUpperBound_MultipleTimes
) {
171 const base::TimeDelta kZero
;
172 const base::TimeDelta kTimeInterval
= base::TimeDelta::FromSeconds(4);
173 const base::TimeDelta kMaxTime0
= base::TimeDelta::FromSeconds(120);
174 const base::TimeDelta kMaxTime1
= base::TimeDelta::FromSeconds(6);
175 const base::TimeDelta kMaxTime2
= base::TimeDelta::FromSeconds(12);
177 EXPECT_EQ(kZero
, interpolator_
.StartInterpolating());
178 interpolator_
.SetUpperBound(kMaxTime0
);
179 AdvanceSystemTime(kTimeInterval
);
180 EXPECT_EQ(kTimeInterval
, interpolator_
.GetInterpolatedTime());
182 interpolator_
.SetUpperBound(kMaxTime1
);
183 AdvanceSystemTime(kTimeInterval
);
184 EXPECT_EQ(kMaxTime1
, interpolator_
.GetInterpolatedTime());
186 AdvanceSystemTime(kTimeInterval
);
187 EXPECT_EQ(kMaxTime1
, interpolator_
.GetInterpolatedTime());
189 interpolator_
.SetUpperBound(kMaxTime2
);
190 EXPECT_EQ(kMaxTime1
, interpolator_
.GetInterpolatedTime());
192 AdvanceSystemTime(kTimeInterval
);
193 EXPECT_EQ(kMaxTime1
+ kTimeInterval
, interpolator_
.GetInterpolatedTime());
195 AdvanceSystemTime(kTimeInterval
);
196 EXPECT_EQ(kMaxTime2
, interpolator_
.GetInterpolatedTime());