| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/simple_test_tick_clock.h" | 5 #include "base/test/simple_test_tick_clock.h" |
| 6 #include "media/base/wall_clock_time_source.h" | 6 #include "media/base/wall_clock_time_source.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| 11 class WallClockTimeSourceTest : public testing::Test { | 11 class WallClockTimeSourceTest : public testing::Test { |
| 12 public: | 12 public: |
| 13 WallClockTimeSourceTest() : tick_clock_(new base::SimpleTestTickClock()) { | 13 WallClockTimeSourceTest() : tick_clock_(new base::SimpleTestTickClock()) { |
| 14 time_source_.SetTickClockForTesting( | 14 time_source_.SetTickClockForTesting( |
| 15 scoped_ptr<base::TickClock>(tick_clock_)); | 15 scoped_ptr<base::TickClock>(tick_clock_)); |
| 16 AdvanceTimeInSeconds(1); |
| 16 } | 17 } |
| 17 ~WallClockTimeSourceTest() override {} | 18 ~WallClockTimeSourceTest() override {} |
| 18 | 19 |
| 19 void AdvanceTimeInSeconds(int seconds) { | 20 void AdvanceTimeInSeconds(int seconds) { |
| 20 tick_clock_->Advance(base::TimeDelta::FromSeconds(seconds)); | 21 tick_clock_->Advance(base::TimeDelta::FromSeconds(seconds)); |
| 21 } | 22 } |
| 22 | 23 |
| 23 int CurrentMediaTimeInSeconds() { | 24 int CurrentMediaTimeInSeconds() { |
| 24 return time_source_.CurrentMediaTime().InSeconds(); | 25 return time_source_.CurrentMediaTime().InSeconds(); |
| 25 } | 26 } |
| 26 | 27 |
| 27 void SetMediaTimeInSeconds(int seconds) { | 28 void SetMediaTimeInSeconds(int seconds) { |
| 28 return time_source_.SetMediaTime(base::TimeDelta::FromSeconds(seconds)); | 29 return time_source_.SetMediaTime(base::TimeDelta::FromSeconds(seconds)); |
| 29 } | 30 } |
| 30 | 31 |
| 32 bool IsWallClockNowForMediaTimeInSeconds(int seconds) { |
| 33 return tick_clock_->NowTicks() == |
| 34 time_source_.GetWallClockTime(base::TimeDelta::FromSeconds(seconds)); |
| 35 } |
| 36 |
| 37 bool IsWallClockNullForMediaTimeInSeconds(int seconds) { |
| 38 return time_source_.GetWallClockTime(base::TimeDelta::FromSeconds(seconds)) |
| 39 .is_null(); |
| 40 } |
| 41 |
| 42 protected: |
| 31 WallClockTimeSource time_source_; | 43 WallClockTimeSource time_source_; |
| 32 | |
| 33 private: | |
| 34 base::SimpleTestTickClock* tick_clock_; // Owned by |time_source_|. | 44 base::SimpleTestTickClock* tick_clock_; // Owned by |time_source_|. |
| 35 | 45 |
| 36 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest); | 46 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest); |
| 37 }; | 47 }; |
| 38 | 48 |
| 39 TEST_F(WallClockTimeSourceTest, InitialTimeIsZero) { | 49 TEST_F(WallClockTimeSourceTest, InitialTimeIsZero) { |
| 40 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 50 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 51 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); |
| 41 } | 52 } |
| 42 | 53 |
| 43 TEST_F(WallClockTimeSourceTest, InitialTimeIsNotTicking) { | 54 TEST_F(WallClockTimeSourceTest, InitialTimeIsNotTicking) { |
| 44 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 55 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 56 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); |
| 45 AdvanceTimeInSeconds(100); | 57 AdvanceTimeInSeconds(100); |
| 46 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 58 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 59 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); |
| 47 } | 60 } |
| 48 | 61 |
| 49 TEST_F(WallClockTimeSourceTest, InitialPlaybackRateIsOne) { | 62 TEST_F(WallClockTimeSourceTest, InitialPlaybackRateIsOne) { |
| 50 time_source_.StartTicking(); | 63 time_source_.StartTicking(); |
| 51 | 64 |
| 52 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 65 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 66 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0)); |
| 53 AdvanceTimeInSeconds(100); | 67 AdvanceTimeInSeconds(100); |
| 54 EXPECT_EQ(100, CurrentMediaTimeInSeconds()); | 68 EXPECT_EQ(100, CurrentMediaTimeInSeconds()); |
| 69 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(100)); |
| 55 } | 70 } |
| 56 | 71 |
| 57 TEST_F(WallClockTimeSourceTest, SetMediaTime) { | 72 TEST_F(WallClockTimeSourceTest, SetMediaTime) { |
| 58 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 73 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 74 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); |
| 59 SetMediaTimeInSeconds(10); | 75 SetMediaTimeInSeconds(10); |
| 60 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); | 76 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); |
| 77 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(10)); |
| 61 } | 78 } |
| 62 | 79 |
| 63 TEST_F(WallClockTimeSourceTest, SetPlaybackRate) { | 80 TEST_F(WallClockTimeSourceTest, SetPlaybackRate) { |
| 64 time_source_.StartTicking(); | 81 time_source_.StartTicking(); |
| 65 | 82 |
| 66 time_source_.SetPlaybackRate(0.5); | 83 time_source_.SetPlaybackRate(0.5); |
| 67 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 84 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 85 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0)); |
| 68 AdvanceTimeInSeconds(10); | 86 AdvanceTimeInSeconds(10); |
| 69 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); | 87 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); |
| 88 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5)); |
| 70 | 89 |
| 71 time_source_.SetPlaybackRate(2); | 90 time_source_.SetPlaybackRate(2); |
| 72 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); | 91 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); |
| 92 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5)); |
| 73 AdvanceTimeInSeconds(10); | 93 AdvanceTimeInSeconds(10); |
| 74 EXPECT_EQ(25, CurrentMediaTimeInSeconds()); | 94 EXPECT_EQ(25, CurrentMediaTimeInSeconds()); |
| 95 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(25)); |
| 75 } | 96 } |
| 76 | 97 |
| 77 TEST_F(WallClockTimeSourceTest, StopTicking) { | 98 TEST_F(WallClockTimeSourceTest, StopTicking) { |
| 78 time_source_.StartTicking(); | 99 time_source_.StartTicking(); |
| 79 | 100 |
| 80 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 101 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 102 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0)); |
| 81 AdvanceTimeInSeconds(10); | 103 AdvanceTimeInSeconds(10); |
| 82 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); | 104 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); |
| 105 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(10)); |
| 83 | 106 |
| 84 time_source_.StopTicking(); | 107 time_source_.StopTicking(); |
| 85 | 108 |
| 86 AdvanceTimeInSeconds(10); | 109 AdvanceTimeInSeconds(10); |
| 87 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); | 110 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); |
| 111 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(10)); |
| 88 } | 112 } |
| 89 | 113 |
| 90 } // namespace media | 114 } // namespace media |
| OLD | NEW |