| 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); | |
| 17 } | 16 } |
| 18 ~WallClockTimeSourceTest() override {} | 17 ~WallClockTimeSourceTest() override {} |
| 19 | 18 |
| 20 void AdvanceTimeInSeconds(int seconds) { | 19 void AdvanceTimeInSeconds(int seconds) { |
| 21 tick_clock_->Advance(base::TimeDelta::FromSeconds(seconds)); | 20 tick_clock_->Advance(base::TimeDelta::FromSeconds(seconds)); |
| 22 } | 21 } |
| 23 | 22 |
| 24 int CurrentMediaTimeInSeconds() { | 23 int CurrentMediaTimeInSeconds() { |
| 25 return time_source_.CurrentMediaTime().InSeconds(); | 24 return time_source_.CurrentMediaTime().InSeconds(); |
| 26 } | 25 } |
| 27 | 26 |
| 28 void SetMediaTimeInSeconds(int seconds) { | 27 void SetMediaTimeInSeconds(int seconds) { |
| 29 return time_source_.SetMediaTime(base::TimeDelta::FromSeconds(seconds)); | 28 return time_source_.SetMediaTime(base::TimeDelta::FromSeconds(seconds)); |
| 30 } | 29 } |
| 31 | 30 |
| 32 bool IsWallClockNowForMediaTimeInSeconds(int seconds) { | 31 WallClockTimeSource time_source_; |
| 33 return tick_clock_->NowTicks() == | |
| 34 time_source_.GetWallClockTime(base::TimeDelta::FromSeconds(seconds)); | |
| 35 } | |
| 36 | 32 |
| 37 bool IsWallClockNullForMediaTimeInSeconds(int seconds) { | 33 private: |
| 38 return time_source_.GetWallClockTime(base::TimeDelta::FromSeconds(seconds)) | |
| 39 .is_null(); | |
| 40 } | |
| 41 | |
| 42 protected: | |
| 43 WallClockTimeSource time_source_; | |
| 44 base::SimpleTestTickClock* tick_clock_; // Owned by |time_source_|. | 34 base::SimpleTestTickClock* tick_clock_; // Owned by |time_source_|. |
| 45 | 35 |
| 46 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest); | 36 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest); |
| 47 }; | 37 }; |
| 48 | 38 |
| 49 TEST_F(WallClockTimeSourceTest, InitialTimeIsZero) { | 39 TEST_F(WallClockTimeSourceTest, InitialTimeIsZero) { |
| 50 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 40 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 51 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); | |
| 52 } | 41 } |
| 53 | 42 |
| 54 TEST_F(WallClockTimeSourceTest, InitialTimeIsNotTicking) { | 43 TEST_F(WallClockTimeSourceTest, InitialTimeIsNotTicking) { |
| 55 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 44 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 56 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); | |
| 57 AdvanceTimeInSeconds(100); | 45 AdvanceTimeInSeconds(100); |
| 58 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 46 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 59 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); | |
| 60 } | 47 } |
| 61 | 48 |
| 62 TEST_F(WallClockTimeSourceTest, InitialPlaybackRateIsOne) { | 49 TEST_F(WallClockTimeSourceTest, InitialPlaybackRateIsOne) { |
| 63 time_source_.StartTicking(); | 50 time_source_.StartTicking(); |
| 64 | 51 |
| 65 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 52 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 66 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0)); | |
| 67 AdvanceTimeInSeconds(100); | 53 AdvanceTimeInSeconds(100); |
| 68 EXPECT_EQ(100, CurrentMediaTimeInSeconds()); | 54 EXPECT_EQ(100, CurrentMediaTimeInSeconds()); |
| 69 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(100)); | |
| 70 } | 55 } |
| 71 | 56 |
| 72 TEST_F(WallClockTimeSourceTest, SetMediaTime) { | 57 TEST_F(WallClockTimeSourceTest, SetMediaTime) { |
| 73 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 58 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 74 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(0)); | |
| 75 SetMediaTimeInSeconds(10); | 59 SetMediaTimeInSeconds(10); |
| 76 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); | 60 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); |
| 77 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(10)); | |
| 78 } | 61 } |
| 79 | 62 |
| 80 TEST_F(WallClockTimeSourceTest, SetPlaybackRate) { | 63 TEST_F(WallClockTimeSourceTest, SetPlaybackRate) { |
| 81 time_source_.StartTicking(); | 64 time_source_.StartTicking(); |
| 82 | 65 |
| 83 time_source_.SetPlaybackRate(0.5); | 66 time_source_.SetPlaybackRate(0.5); |
| 84 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 67 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 85 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0)); | |
| 86 AdvanceTimeInSeconds(10); | 68 AdvanceTimeInSeconds(10); |
| 87 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); | 69 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); |
| 88 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5)); | |
| 89 | 70 |
| 90 time_source_.SetPlaybackRate(2); | 71 time_source_.SetPlaybackRate(2); |
| 91 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); | 72 EXPECT_EQ(5, CurrentMediaTimeInSeconds()); |
| 92 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5)); | |
| 93 AdvanceTimeInSeconds(10); | 73 AdvanceTimeInSeconds(10); |
| 94 EXPECT_EQ(25, CurrentMediaTimeInSeconds()); | 74 EXPECT_EQ(25, CurrentMediaTimeInSeconds()); |
| 95 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(25)); | |
| 96 } | 75 } |
| 97 | 76 |
| 98 TEST_F(WallClockTimeSourceTest, StopTicking) { | 77 TEST_F(WallClockTimeSourceTest, StopTicking) { |
| 99 time_source_.StartTicking(); | 78 time_source_.StartTicking(); |
| 100 | 79 |
| 101 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); | 80 EXPECT_EQ(0, CurrentMediaTimeInSeconds()); |
| 102 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0)); | |
| 103 AdvanceTimeInSeconds(10); | 81 AdvanceTimeInSeconds(10); |
| 104 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); | 82 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); |
| 105 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(10)); | |
| 106 | 83 |
| 107 time_source_.StopTicking(); | 84 time_source_.StopTicking(); |
| 108 | 85 |
| 109 AdvanceTimeInSeconds(10); | 86 AdvanceTimeInSeconds(10); |
| 110 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); | 87 EXPECT_EQ(10, CurrentMediaTimeInSeconds()); |
| 111 EXPECT_TRUE(IsWallClockNullForMediaTimeInSeconds(10)); | |
| 112 } | 88 } |
| 113 | 89 |
| 114 } // namespace media | 90 } // namespace media |
| OLD | NEW |