Update V8 to version 4.7.56.
[chromium-blink-merge.git] / media / base / wall_clock_time_source_unittest.cc
blobf5f0bb3088adcf9cec9f5544236c74dd153b7bd0
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
3 // found in the LICENSE file.
5 #include "base/test/simple_test_tick_clock.h"
6 #include "media/base/wall_clock_time_source.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace media {
11 class WallClockTimeSourceTest : public testing::Test {
12 public:
13 WallClockTimeSourceTest() : tick_clock_(new base::SimpleTestTickClock()) {
14 time_source_.set_tick_clock_for_testing(tick_clock_.get());
15 AdvanceTimeInSeconds(1);
17 ~WallClockTimeSourceTest() override {}
19 void AdvanceTimeInSeconds(int seconds) {
20 tick_clock_->Advance(base::TimeDelta::FromSeconds(seconds));
23 int CurrentMediaTimeInSeconds() {
24 return time_source_.CurrentMediaTime().InSeconds();
27 void SetMediaTimeInSeconds(int seconds) {
28 return time_source_.SetMediaTime(base::TimeDelta::FromSeconds(seconds));
31 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp,
32 bool* is_time_moving) {
33 std::vector<base::TimeTicks> wall_clock_times;
34 *is_time_moving = time_source_.GetWallClockTimes(
35 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times);
36 return wall_clock_times[0];
39 bool IsWallClockNowForMediaTimeInSeconds(int seconds) {
40 bool is_time_moving = false;
41 return tick_clock_->NowTicks() ==
42 ConvertMediaTime(base::TimeDelta::FromSeconds(seconds),
43 &is_time_moving);
46 bool IsTimeStopped() {
47 bool is_time_moving = false;
48 // Convert any random value, it shouldn't matter for this call.
49 ConvertMediaTime(base::TimeDelta::FromSeconds(1), &is_time_moving);
50 return !is_time_moving;
53 protected:
54 WallClockTimeSource time_source_;
55 scoped_ptr<base::SimpleTestTickClock> tick_clock_;
57 DISALLOW_COPY_AND_ASSIGN(WallClockTimeSourceTest);
60 TEST_F(WallClockTimeSourceTest, InitialTimeIsZero) {
61 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
62 EXPECT_TRUE(IsTimeStopped());
65 TEST_F(WallClockTimeSourceTest, InitialTimeIsNotTicking) {
66 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
67 EXPECT_TRUE(IsTimeStopped());
68 AdvanceTimeInSeconds(100);
69 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
70 EXPECT_TRUE(IsTimeStopped());
73 TEST_F(WallClockTimeSourceTest, InitialPlaybackRateIsOne) {
74 time_source_.StartTicking();
76 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
77 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
78 AdvanceTimeInSeconds(100);
79 EXPECT_EQ(100, CurrentMediaTimeInSeconds());
80 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(100));
83 TEST_F(WallClockTimeSourceTest, SetMediaTime) {
84 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
85 EXPECT_TRUE(IsTimeStopped());
86 SetMediaTimeInSeconds(10);
87 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
88 EXPECT_TRUE(IsTimeStopped());
91 TEST_F(WallClockTimeSourceTest, SetPlaybackRate) {
92 time_source_.StartTicking();
94 time_source_.SetPlaybackRate(0.5);
95 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
96 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
97 AdvanceTimeInSeconds(10);
98 EXPECT_EQ(5, CurrentMediaTimeInSeconds());
99 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5));
101 time_source_.SetPlaybackRate(2);
102 EXPECT_EQ(5, CurrentMediaTimeInSeconds());
103 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(5));
104 AdvanceTimeInSeconds(10);
105 EXPECT_EQ(25, CurrentMediaTimeInSeconds());
106 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(25));
109 TEST_F(WallClockTimeSourceTest, StopTicking) {
110 time_source_.StartTicking();
112 EXPECT_EQ(0, CurrentMediaTimeInSeconds());
113 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(0));
114 AdvanceTimeInSeconds(10);
115 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
116 EXPECT_TRUE(IsWallClockNowForMediaTimeInSeconds(10));
118 time_source_.StopTicking();
120 AdvanceTimeInSeconds(10);
121 EXPECT_EQ(10, CurrentMediaTimeInSeconds());
122 EXPECT_TRUE(IsTimeStopped());
125 TEST_F(WallClockTimeSourceTest, ConvertsTimestampsWhenStopped) {
126 const base::TimeDelta kOneSecond = base::TimeDelta::FromSeconds(1);
127 bool is_time_moving = false;
128 EXPECT_EQ(base::TimeTicks(),
129 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
130 EXPECT_FALSE(is_time_moving);
131 EXPECT_NE(base::TimeTicks(), ConvertMediaTime(kOneSecond, &is_time_moving));
132 EXPECT_FALSE(is_time_moving);
133 time_source_.StartTicking();
134 time_source_.StopTicking();
135 EXPECT_EQ(tick_clock_->NowTicks(),
136 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
137 EXPECT_FALSE(is_time_moving);
138 EXPECT_EQ(tick_clock_->NowTicks() + kOneSecond,
139 ConvertMediaTime(kOneSecond, &is_time_moving));
140 EXPECT_FALSE(is_time_moving);
143 TEST_F(WallClockTimeSourceTest, EmptyMediaTimestampsReturnMediaWallClockTime) {
144 std::vector<base::TimeTicks> wall_clock_times;
145 bool is_time_moving = time_source_.GetWallClockTimes(
146 std::vector<base::TimeDelta>(), &wall_clock_times);
147 EXPECT_FALSE(is_time_moving);
148 EXPECT_EQ(base::TimeTicks(), wall_clock_times[0]);
150 wall_clock_times.clear();
151 time_source_.StartTicking();
152 is_time_moving = time_source_.GetWallClockTimes(
153 std::vector<base::TimeDelta>(), &wall_clock_times);
154 EXPECT_TRUE(is_time_moving);
155 EXPECT_EQ(tick_clock_->NowTicks(), wall_clock_times[0]);
157 wall_clock_times.clear();
158 time_source_.StopTicking();
159 is_time_moving = time_source_.GetWallClockTimes(
160 std::vector<base::TimeDelta>(), &wall_clock_times);
161 EXPECT_FALSE(is_time_moving);
162 EXPECT_EQ(tick_clock_->NowTicks(), wall_clock_times[0]);
165 } // namespace media