summaryrefslogtreecommitdiffstats
path: root/media/base/clock_unittest.cc
blob: 3bf05996c62127d7a92eb58409be1269ef9cf9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/clock.h"
#include "media/base/clock.h"
#include "testing/gmock/include/gmock/gmock.h"

using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrictMock;

namespace base {

// Provide a stream output operator so we can use EXPECT_EQ(...) with TimeDelta.
//
// TODO(scherkus): move this into the testing package.
static std::ostream& operator<<(std::ostream& stream, const TimeDelta& time) {
  return (stream << time.ToInternalValue());
}

}  // namespace

namespace media {

static const int kDurationInSeconds = 120;

class ClockTest : public ::testing::Test {
 public:
  ClockTest() : clock_(&test_tick_clock_) {
    SetDuration();
  }

 protected:
  void SetDuration() {
    const base::TimeDelta kDuration =
        base::TimeDelta::FromSeconds(kDurationInSeconds);
    clock_.SetDuration(kDuration);
    EXPECT_EQ(kDuration, clock_.Duration());
  }

  void AdvanceSystemTime(base::TimeDelta delta) {
    test_tick_clock_.Advance(delta);
  }

  base::SimpleTestTickClock test_tick_clock_;
  Clock clock_;
  base::TimeDelta time_elapsed_;
};

TEST_F(ClockTest, Created) {
  const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(0);
  EXPECT_EQ(kExpected, clock_.Elapsed());
}

TEST_F(ClockTest, Play_NormalSpeed) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeToAdvance = base::TimeDelta::FromSeconds(2);

  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(kTimeToAdvance);
  EXPECT_EQ(kTimeToAdvance, clock_.Elapsed());
}

TEST_F(ClockTest, Play_DoubleSpeed) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeToAdvance = base::TimeDelta::FromSeconds(5);

  clock_.SetPlaybackRate(2.0f);
  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(kTimeToAdvance);
  EXPECT_EQ(2 * kTimeToAdvance, clock_.Elapsed());
}

TEST_F(ClockTest, Play_HalfSpeed) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeToAdvance = base::TimeDelta::FromSeconds(4);

  clock_.SetPlaybackRate(0.5f);
  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(kTimeToAdvance);
  EXPECT_EQ(kTimeToAdvance / 2, clock_.Elapsed());
}

TEST_F(ClockTest, Play_ZeroSpeed) {
  // We'll play for 2 seconds at normal speed, 4 seconds at zero speed, and 8
  // seconds at normal speed.
  const base::TimeDelta kZero;
  const base::TimeDelta kPlayDuration1 = base::TimeDelta::FromSeconds(2);
  const base::TimeDelta kPlayDuration2 = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kPlayDuration3 = base::TimeDelta::FromSeconds(8);
  const base::TimeDelta kExpected = kPlayDuration1 + kPlayDuration3;

  EXPECT_EQ(kZero, clock_.Play());

  AdvanceSystemTime(kPlayDuration1);
  clock_.SetPlaybackRate(0.0f);
  AdvanceSystemTime(kPlayDuration2);
  clock_.SetPlaybackRate(1.0f);
  AdvanceSystemTime(kPlayDuration3);

  EXPECT_EQ(kExpected, clock_.Elapsed());
}

TEST_F(ClockTest, Play_MultiSpeed) {
  // We'll play for 2 seconds at half speed, 4 seconds at normal speed, and 8
  // seconds at double speed.
  const base::TimeDelta kZero;
  const base::TimeDelta kPlayDuration1 = base::TimeDelta::FromSeconds(2);
  const base::TimeDelta kPlayDuration2 = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kPlayDuration3 = base::TimeDelta::FromSeconds(8);
  const base::TimeDelta kExpected =
      kPlayDuration1 / 2 + kPlayDuration2 + 2 * kPlayDuration3;

  clock_.SetPlaybackRate(0.5f);
  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(kPlayDuration1);

  clock_.SetPlaybackRate(1.0f);
  AdvanceSystemTime(kPlayDuration2);

  clock_.SetPlaybackRate(2.0f);
  AdvanceSystemTime(kPlayDuration3);
  EXPECT_EQ(kExpected, clock_.Elapsed());
}

TEST_F(ClockTest, Pause) {
  const base::TimeDelta kZero;
  const base::TimeDelta kPlayDuration = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kPauseDuration = base::TimeDelta::FromSeconds(20);
  const base::TimeDelta kExpectedFirstPause = kPlayDuration;
  const base::TimeDelta kExpectedSecondPause = 2 * kPlayDuration;

  // Play for 4 seconds.
  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(kPlayDuration);

  // Pause for 20 seconds.
  EXPECT_EQ(kExpectedFirstPause, clock_.Pause());
  EXPECT_EQ(kExpectedFirstPause, clock_.Elapsed());
  AdvanceSystemTime(kPauseDuration);
  EXPECT_EQ(kExpectedFirstPause, clock_.Elapsed());

  // Play again for 4 more seconds.
  EXPECT_EQ(kExpectedFirstPause, clock_.Play());
  AdvanceSystemTime(kPlayDuration);
  EXPECT_EQ(kExpectedSecondPause, clock_.Pause());
  EXPECT_EQ(kExpectedSecondPause, clock_.Elapsed());
}

TEST_F(ClockTest, SetTime_Paused) {
  const base::TimeDelta kFirstTime = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kSecondTime = base::TimeDelta::FromSeconds(16);

  clock_.SetTime(kFirstTime, clock_.Duration());
  EXPECT_EQ(kFirstTime, clock_.Elapsed());
  clock_.SetTime(kSecondTime, clock_.Duration());
  EXPECT_EQ(kSecondTime, clock_.Elapsed());
}

TEST_F(ClockTest, SetTime_Playing) {
  // We'll play for 4 seconds, then set the time to 12, then play for 4 more
  // seconds.
  const base::TimeDelta kZero;
  const base::TimeDelta kPlayDuration = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kUpdatedTime = base::TimeDelta::FromSeconds(12);
  const base::TimeDelta kExpected = kUpdatedTime + kPlayDuration;

  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(kPlayDuration);

  clock_.SetTime(kUpdatedTime, clock_.Duration());
  AdvanceSystemTime(kPlayDuration);
  EXPECT_EQ(kExpected, clock_.Elapsed());
}

TEST_F(ClockTest, CapAtMediaDuration_Paused) {
  const base::TimeDelta kDuration =
      base::TimeDelta::FromSeconds(kDurationInSeconds);
  const base::TimeDelta kTimeOverDuration =
      base::TimeDelta::FromSeconds(kDurationInSeconds + 4);

  // Elapsed time should always be capped at the duration of the media.
  clock_.SetTime(kTimeOverDuration, kTimeOverDuration);
  EXPECT_EQ(kDuration, clock_.Elapsed());
}

TEST_F(ClockTest, CapAtMediaDuration_Playing) {
  const base::TimeDelta kZero;
  const base::TimeDelta kDuration =
      base::TimeDelta::FromSeconds(kDurationInSeconds);
  const base::TimeDelta kTimeOverDuration =
      base::TimeDelta::FromSeconds(kDurationInSeconds + 4);

  // Play for twice as long as the duration of the media.
  EXPECT_EQ(kZero, clock_.Play());
  AdvanceSystemTime(2 * kDuration);
  EXPECT_EQ(kDuration, clock_.Elapsed());

  // Manually set the time past the duration.
  clock_.SetTime(kTimeOverDuration, kTimeOverDuration);
  EXPECT_EQ(kDuration, clock_.Elapsed());
}

TEST_F(ClockTest, SetMaxTime) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeInterval = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kMaxTime = base::TimeDelta::FromSeconds(6);

  EXPECT_EQ(kZero, clock_.Play());
  clock_.SetMaxTime(kMaxTime);
  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kTimeInterval, clock_.Elapsed());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime, clock_.Elapsed());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime, clock_.Elapsed());
}

TEST_F(ClockTest, SetMaxTime_MultipleTimes) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeInterval = base::TimeDelta::FromSeconds(4);
  const base::TimeDelta kMaxTime1 = base::TimeDelta::FromSeconds(6);
  const base::TimeDelta kMaxTime2 = base::TimeDelta::FromSeconds(12);

  EXPECT_EQ(kZero, clock_.Play());
  clock_.SetMaxTime(clock_.Duration());
  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kTimeInterval, clock_.Elapsed());

  clock_.SetMaxTime(kMaxTime1);
  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime1, clock_.Elapsed());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime1, clock_.Elapsed());

  clock_.SetMaxTime(kMaxTime2);
  EXPECT_EQ(kMaxTime1, clock_.Elapsed());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime1 + kTimeInterval, clock_.Elapsed());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime2, clock_.Elapsed());
}

}  // namespace media