Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "base/compiler_specific.h"
      6 #include "base/logging.h"
      7 #include "base/test/simple_test_tick_clock.h"
      8 #include "base/time/clock.h"
      9 #include "media/base/clock.h"
     10 #include "testing/gmock/include/gmock/gmock.h"
     11 
     12 using ::testing::InSequence;
     13 using ::testing::Return;
     14 using ::testing::StrictMock;
     15 
     16 namespace base {
     17 
     18 // Provide a stream output operator so we can use EXPECT_EQ(...) with TimeDelta.
     19 //
     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());
     23 }
     24 
     25 }  // namespace
     26 
     27 namespace media {
     28 
     29 static const int kDurationInSeconds = 120;
     30 
     31 class ClockTest : public ::testing::Test {
     32  public:
     33   ClockTest() : clock_(&test_tick_clock_) {
     34     SetDuration();
     35   }
     36 
     37  protected:
     38   void SetDuration() {
     39     const base::TimeDelta kDuration =
     40         base::TimeDelta::FromSeconds(kDurationInSeconds);
     41     clock_.SetDuration(kDuration);
     42     EXPECT_EQ(kDuration, clock_.Duration());
     43   }
     44 
     45   void AdvanceSystemTime(base::TimeDelta delta) {
     46     test_tick_clock_.Advance(delta);
     47   }
     48 
     49   base::SimpleTestTickClock test_tick_clock_;
     50   Clock clock_;
     51   base::TimeDelta time_elapsed_;
     52 };
     53 
     54 TEST_F(ClockTest, Created) {
     55   const base::TimeDelta kExpected = base::TimeDelta::FromSeconds(0);
     56   EXPECT_EQ(kExpected, clock_.Elapsed());
     57 }
     58 
     59 TEST_F(ClockTest, Play_NormalSpeed) {
     60   const base::TimeDelta kZero;
     61   const base::TimeDelta kTimeToAdvance = base::TimeDelta::FromSeconds(2);
     62 
     63   EXPECT_EQ(kZero, clock_.Play());
     64   AdvanceSystemTime(kTimeToAdvance);
     65   EXPECT_EQ(kTimeToAdvance, clock_.Elapsed());
     66 }
     67 
     68 TEST_F(ClockTest, Play_DoubleSpeed) {
     69   const base::TimeDelta kZero;
     70   const base::TimeDelta kTimeToAdvance = base::TimeDelta::FromSeconds(5);
     71 
     72   clock_.SetPlaybackRate(2.0f);
     73   EXPECT_EQ(kZero, clock_.Play());
     74   AdvanceSystemTime(kTimeToAdvance);
     75   EXPECT_EQ(2 * kTimeToAdvance, clock_.Elapsed());
     76 }
     77 
     78 TEST_F(ClockTest, Play_HalfSpeed) {
     79   const base::TimeDelta kZero;
     80   const base::TimeDelta kTimeToAdvance = base::TimeDelta::FromSeconds(4);
     81 
     82   clock_.SetPlaybackRate(0.5f);
     83   EXPECT_EQ(kZero, clock_.Play());
     84   AdvanceSystemTime(kTimeToAdvance);
     85   EXPECT_EQ(kTimeToAdvance / 2, clock_.Elapsed());
     86 }
     87 
     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;
     96 
     97   EXPECT_EQ(kZero, clock_.Play());
     98 
     99   AdvanceSystemTime(kPlayDuration1);
    100   clock_.SetPlaybackRate(0.0f);
    101   AdvanceSystemTime(kPlayDuration2);
    102   clock_.SetPlaybackRate(1.0f);
    103   AdvanceSystemTime(kPlayDuration3);
    104 
    105   EXPECT_EQ(kExpected, clock_.Elapsed());
    106 }
    107 
    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;
    117 
    118   clock_.SetPlaybackRate(0.5f);
    119   EXPECT_EQ(kZero, clock_.Play());
    120   AdvanceSystemTime(kPlayDuration1);
    121 
    122   clock_.SetPlaybackRate(1.0f);
    123   AdvanceSystemTime(kPlayDuration2);
    124 
    125   clock_.SetPlaybackRate(2.0f);
    126   AdvanceSystemTime(kPlayDuration3);
    127   EXPECT_EQ(kExpected, clock_.Elapsed());
    128 }
    129 
    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;
    136 
    137   // Play for 4 seconds.
    138   EXPECT_EQ(kZero, clock_.Play());
    139   AdvanceSystemTime(kPlayDuration);
    140 
    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());
    146 
    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());
    152 }
    153 
    154 TEST_F(ClockTest, SetTime_Paused) {
    155   const base::TimeDelta kFirstTime = base::TimeDelta::FromSeconds(4);
    156   const base::TimeDelta kSecondTime = base::TimeDelta::FromSeconds(16);
    157 
    158   clock_.SetTime(kFirstTime, clock_.Duration());
    159   EXPECT_EQ(kFirstTime, clock_.Elapsed());
    160   clock_.SetTime(kSecondTime, clock_.Duration());
    161   EXPECT_EQ(kSecondTime, clock_.Elapsed());
    162 }
    163 
    164 TEST_F(ClockTest, SetTime_Playing) {
    165   // We'll play for 4 seconds, then set the time to 12, then play for 4 more
    166   // seconds.
    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;
    171 
    172   EXPECT_EQ(kZero, clock_.Play());
    173   AdvanceSystemTime(kPlayDuration);
    174 
    175   clock_.SetTime(kUpdatedTime, clock_.Duration());
    176   AdvanceSystemTime(kPlayDuration);
    177   EXPECT_EQ(kExpected, clock_.Elapsed());
    178 }
    179 
    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);
    185 
    186   // Elapsed time should always be capped at the duration of the media.
    187   clock_.SetTime(kTimeOverDuration, kTimeOverDuration);
    188   EXPECT_EQ(kDuration, clock_.Elapsed());
    189 }
    190 
    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);
    197 
    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());
    202 
    203   // Manually set the time past the duration.
    204   clock_.SetTime(kTimeOverDuration, kTimeOverDuration);
    205   EXPECT_EQ(kDuration, clock_.Elapsed());
    206 }
    207 
    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);
    212 
    213   EXPECT_EQ(kZero, clock_.Play());
    214   clock_.SetMaxTime(kMaxTime);
    215   AdvanceSystemTime(kTimeInterval);
    216   EXPECT_EQ(kTimeInterval, clock_.Elapsed());
    217 
    218   AdvanceSystemTime(kTimeInterval);
    219   EXPECT_EQ(kMaxTime, clock_.Elapsed());
    220 
    221   AdvanceSystemTime(kTimeInterval);
    222   EXPECT_EQ(kMaxTime, clock_.Elapsed());
    223 }
    224 
    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);
    230 
    231   EXPECT_EQ(kZero, clock_.Play());
    232   clock_.SetMaxTime(clock_.Duration());
    233   AdvanceSystemTime(kTimeInterval);
    234   EXPECT_EQ(kTimeInterval, clock_.Elapsed());
    235 
    236   clock_.SetMaxTime(kMaxTime1);
    237   AdvanceSystemTime(kTimeInterval);
    238   EXPECT_EQ(kMaxTime1, clock_.Elapsed());
    239 
    240   AdvanceSystemTime(kTimeInterval);
    241   EXPECT_EQ(kMaxTime1, clock_.Elapsed());
    242 
    243   clock_.SetMaxTime(kMaxTime2);
    244   EXPECT_EQ(kMaxTime1, clock_.Elapsed());
    245 
    246   AdvanceSystemTime(kTimeInterval);
    247   EXPECT_EQ(kMaxTime1 + kTimeInterval, clock_.Elapsed());
    248 
    249   AdvanceSystemTime(kTimeInterval);
    250   EXPECT_EQ(kMaxTime2, clock_.Elapsed());
    251 }
    252 
    253 }  // namespace media
    254