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

media / base / time_delta_interpolator_unittest.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/time_delta_interpolator.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class TimeDeltaInterpolatorTest : public ::testing::Test {
 public:
  TimeDeltaInterpolatorTest() : interpolator_(&test_tick_clock_) {
    interpolator_.SetPlaybackRate(1.0);
  }

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

  base::SimpleTestTickClock test_tick_clock_;
  TimeDeltaInterpolator interpolator_;
};

TEST_F(TimeDeltaInterpolatorTest, Created) {
  const base::TimeDelta kExpected = base::Seconds(0);
  EXPECT_EQ(kExpected, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, StartInterpolating_NormalSpeed) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeToAdvance = base::Seconds(2);

  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  AdvanceSystemTime(kTimeToAdvance);
  EXPECT_EQ(kTimeToAdvance, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, StartInterpolating_DoubleSpeed) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeToAdvance = base::Seconds(5);

  interpolator_.SetPlaybackRate(2.0);
  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  AdvanceSystemTime(kTimeToAdvance);
  EXPECT_EQ(2 * kTimeToAdvance, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, StartInterpolating_HalfSpeed) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeToAdvance = base::Seconds(4);

  interpolator_.SetPlaybackRate(0.5);
  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  AdvanceSystemTime(kTimeToAdvance);
  EXPECT_EQ(kTimeToAdvance / 2, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, StartInterpolating_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::Seconds(2);
  const base::TimeDelta kPlayDuration2 = base::Seconds(4);
  const base::TimeDelta kPlayDuration3 = base::Seconds(8);
  const base::TimeDelta kExpected = kPlayDuration1 + kPlayDuration3;

  EXPECT_EQ(kZero, interpolator_.StartInterpolating());

  AdvanceSystemTime(kPlayDuration1);
  interpolator_.SetPlaybackRate(0.0);
  AdvanceSystemTime(kPlayDuration2);
  interpolator_.SetPlaybackRate(1.0);
  AdvanceSystemTime(kPlayDuration3);

  EXPECT_EQ(kExpected, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, StartInterpolating_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::Seconds(2);
  const base::TimeDelta kPlayDuration2 = base::Seconds(4);
  const base::TimeDelta kPlayDuration3 = base::Seconds(8);
  const base::TimeDelta kExpected =
      kPlayDuration1 / 2 + kPlayDuration2 + 2 * kPlayDuration3;

  interpolator_.SetPlaybackRate(0.5);
  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  AdvanceSystemTime(kPlayDuration1);

  interpolator_.SetPlaybackRate(1.0);
  AdvanceSystemTime(kPlayDuration2);

  interpolator_.SetPlaybackRate(2.0);
  AdvanceSystemTime(kPlayDuration3);
  EXPECT_EQ(kExpected, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, StopInterpolating) {
  const base::TimeDelta kZero;
  const base::TimeDelta kPlayDuration = base::Seconds(4);
  const base::TimeDelta kPauseDuration = base::Seconds(20);
  const base::TimeDelta kExpectedFirstPause = kPlayDuration;
  const base::TimeDelta kExpectedSecondPause = 2 * kPlayDuration;

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

  // Pause for 20 seconds.
  EXPECT_EQ(kExpectedFirstPause, interpolator_.StopInterpolating());
  EXPECT_EQ(kExpectedFirstPause, interpolator_.GetInterpolatedTime());
  AdvanceSystemTime(kPauseDuration);
  EXPECT_EQ(kExpectedFirstPause, interpolator_.GetInterpolatedTime());

  // Play again for 4 more seconds.
  EXPECT_EQ(kExpectedFirstPause, interpolator_.StartInterpolating());
  AdvanceSystemTime(kPlayDuration);
  EXPECT_EQ(kExpectedSecondPause, interpolator_.StopInterpolating());
  EXPECT_EQ(kExpectedSecondPause, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, SetBounds_Stopped) {
  const base::TimeDelta kFirstTime = base::Seconds(4);
  const base::TimeDelta kSecondTime = base::Seconds(16);
  const base::TimeDelta kArbitraryMaxTime = base::Seconds(100);

  interpolator_.SetBounds(kFirstTime, kArbitraryMaxTime,
                          test_tick_clock_.NowTicks());
  EXPECT_EQ(kFirstTime, interpolator_.GetInterpolatedTime());
  interpolator_.SetBounds(kSecondTime, kArbitraryMaxTime,
                          test_tick_clock_.NowTicks());
  EXPECT_EQ(kSecondTime, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, SetBounds_Started) {
  // 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::Seconds(4);
  const base::TimeDelta kUpdatedTime = base::Seconds(12);
  const base::TimeDelta kArbitraryMaxTime = base::Seconds(100);
  const base::TimeDelta kExpected = kUpdatedTime + kPlayDuration;

  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  AdvanceSystemTime(kPlayDuration);

  interpolator_.SetBounds(kUpdatedTime, kArbitraryMaxTime,
                          test_tick_clock_.NowTicks());
  AdvanceSystemTime(kPlayDuration);
  EXPECT_EQ(kExpected, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, SetUpperBound) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeInterval = base::Seconds(4);
  const base::TimeDelta kMaxTime = base::Seconds(6);

  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  interpolator_.SetUpperBound(kMaxTime);
  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kTimeInterval, interpolator_.GetInterpolatedTime());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime, interpolator_.GetInterpolatedTime());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime, interpolator_.GetInterpolatedTime());
}

TEST_F(TimeDeltaInterpolatorTest, SetUpperBound_MultipleTimes) {
  const base::TimeDelta kZero;
  const base::TimeDelta kTimeInterval = base::Seconds(4);
  const base::TimeDelta kMaxTime0 = base::Seconds(120);
  const base::TimeDelta kMaxTime1 = base::Seconds(6);
  const base::TimeDelta kMaxTime2 = base::Seconds(12);

  EXPECT_EQ(kZero, interpolator_.StartInterpolating());
  interpolator_.SetUpperBound(kMaxTime0);
  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kTimeInterval, interpolator_.GetInterpolatedTime());

  interpolator_.SetUpperBound(kMaxTime1);
  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime1, interpolator_.GetInterpolatedTime());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime1, interpolator_.GetInterpolatedTime());

  interpolator_.SetUpperBound(kMaxTime2);
  EXPECT_EQ(kMaxTime1, interpolator_.GetInterpolatedTime());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime1 + kTimeInterval, interpolator_.GetInterpolatedTime());

  AdvanceSystemTime(kTimeInterval);
  EXPECT_EQ(kMaxTime2, interpolator_.GetInterpolatedTime());
}

}  // namespace media