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 "media/blink/buffered_data_source_host_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
5 #include "base/macros.h" | 8 #include "base/macros.h" |
6 #include "media/blink/buffered_data_source_host_impl.h" | 9 #include "base/test/simple_test_tick_clock.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
8 | 11 |
9 namespace media { | 12 namespace media { |
10 | 13 |
11 class BufferedDataSourceHostImplTest : public testing::Test { | 14 class BufferedDataSourceHostImplTest : public testing::Test { |
12 public: | 15 public: |
13 BufferedDataSourceHostImplTest() {} | 16 BufferedDataSourceHostImplTest() |
| 17 : host_(base::Bind(&BufferedDataSourceHostImplTest::ProgressCallback, |
| 18 base::Unretained(this)), |
| 19 &clock_) {} |
14 | 20 |
15 void Add() { | 21 void Add() { |
16 host_.AddBufferedTimeRanges(&ranges_, base::TimeDelta::FromSeconds(10)); | 22 host_.AddBufferedTimeRanges(&ranges_, base::TimeDelta::FromSeconds(10)); |
17 } | 23 } |
18 | 24 |
| 25 void ProgressCallback() { progress_callback_calls_++; } |
| 26 |
19 protected: | 27 protected: |
| 28 int progress_callback_calls_ = 0; |
20 BufferedDataSourceHostImpl host_; | 29 BufferedDataSourceHostImpl host_; |
21 Ranges<base::TimeDelta> ranges_; | 30 Ranges<base::TimeDelta> ranges_; |
| 31 base::SimpleTestTickClock clock_; |
22 | 32 |
23 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceHostImplTest); | 33 DISALLOW_COPY_AND_ASSIGN(BufferedDataSourceHostImplTest); |
24 }; | 34 }; |
25 | 35 |
26 TEST_F(BufferedDataSourceHostImplTest, Empty) { | 36 TEST_F(BufferedDataSourceHostImplTest, Empty) { |
27 EXPECT_FALSE(host_.DidLoadingProgress()); | 37 EXPECT_FALSE(host_.DidLoadingProgress()); |
28 Add(); | 38 Add(); |
29 EXPECT_EQ(0u, ranges_.size()); | 39 EXPECT_EQ(0u, ranges_.size()); |
30 } | 40 } |
31 | 41 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 Add(); | 76 Add(); |
67 EXPECT_EQ(1u, ranges_.size()); | 77 EXPECT_EQ(1u, ranges_.size()); |
68 } | 78 } |
69 | 79 |
70 TEST_F(BufferedDataSourceHostImplTest, DidLoadingProgress) { | 80 TEST_F(BufferedDataSourceHostImplTest, DidLoadingProgress) { |
71 host_.AddBufferedByteRange(10, 20); | 81 host_.AddBufferedByteRange(10, 20); |
72 EXPECT_TRUE(host_.DidLoadingProgress()); | 82 EXPECT_TRUE(host_.DidLoadingProgress()); |
73 EXPECT_FALSE(host_.DidLoadingProgress()); | 83 EXPECT_FALSE(host_.DidLoadingProgress()); |
74 } | 84 } |
75 | 85 |
| 86 TEST_F(BufferedDataSourceHostImplTest, CanPlayThrough) { |
| 87 host_.SetTotalBytes(100000); |
| 88 EXPECT_EQ(100000, |
| 89 host_.UnloadedBytesInInterval(Interval<int64_t>(0, 100000))); |
| 90 host_.AddBufferedByteRange(0, 10000); |
| 91 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 92 host_.AddBufferedByteRange(10000, 20000); |
| 93 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 94 host_.AddBufferedByteRange(20000, 30000); |
| 95 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 96 host_.AddBufferedByteRange(30000, 40000); |
| 97 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 98 host_.AddBufferedByteRange(40000, 50000); |
| 99 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 100 EXPECT_EQ(50000, host_.UnloadedBytesInInterval(Interval<int64_t>(0, 100000))); |
| 101 host_.AddBufferedByteRange(50000, 60000); |
| 102 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 103 host_.AddBufferedByteRange(60000, 70000); |
| 104 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 105 host_.AddBufferedByteRange(70000, 80000); |
| 106 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 107 host_.AddBufferedByteRange(80000, 90000); |
| 108 // Download rate is allowed to be estimated low, but not high. |
| 109 EXPECT_LE(host_.DownloadRate(), 10000.0f); |
| 110 EXPECT_GE(host_.DownloadRate(), 9000.0f); |
| 111 EXPECT_EQ(10000, host_.UnloadedBytesInInterval(Interval<int64_t>(0, 100000))); |
| 112 EXPECT_EQ(9, progress_callback_calls_); |
| 113 // If the video is 0.1s we can't play through. |
| 114 EXPECT_FALSE(host_.CanPlayThrough(base::TimeDelta(), |
| 115 base::TimeDelta::FromSecondsD(0.01), 1.0)); |
| 116 // If the video is 1000s we can play through. |
| 117 EXPECT_TRUE(host_.CanPlayThrough(base::TimeDelta(), |
| 118 base::TimeDelta::FromSecondsD(1000.0), 1.0)); |
| 119 // No more downloads for 1000 seconds... |
| 120 clock_.Advance(base::TimeDelta::FromSeconds(1000)); |
| 121 // Can't play through.. |
| 122 EXPECT_FALSE(host_.CanPlayThrough(base::TimeDelta(), |
| 123 base::TimeDelta::FromSecondsD(100.0), 1.0)); |
| 124 host_.AddBufferedByteRange(90000, 100000); |
| 125 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 126 EXPECT_EQ(0, host_.UnloadedBytesInInterval(Interval<int64_t>(0, 100000))); |
| 127 |
| 128 // Media is fully downloaded, so we can certainly play through, even if |
| 129 // we only have 0.01 seconds to do it. |
| 130 EXPECT_TRUE(host_.CanPlayThrough(base::TimeDelta(), |
| 131 base::TimeDelta::FromSecondsD(0.01), 1.0)); |
| 132 } |
| 133 |
| 134 TEST_F(BufferedDataSourceHostImplTest, CanPlayThroughSmallAdvances) { |
| 135 host_.SetTotalBytes(20000); |
| 136 EXPECT_EQ(20000, host_.UnloadedBytesInInterval(Interval<int64_t>(0, 20000))); |
| 137 for (int j = 1; j <= 100; j++) { |
| 138 host_.AddBufferedByteRange(0, j * 100); |
| 139 clock_.Advance(base::TimeDelta::FromSecondsD(0.01)); |
| 140 } |
| 141 // Download rate is allowed to be estimated low, but not high. |
| 142 EXPECT_LE(host_.DownloadRate(), 10000.0f); |
| 143 EXPECT_GE(host_.DownloadRate(), 9000.0f); |
| 144 EXPECT_EQ(10000, host_.UnloadedBytesInInterval(Interval<int64_t>(0, 20000))); |
| 145 EXPECT_EQ(100, progress_callback_calls_); |
| 146 // If the video is 0.1s we can't play through. |
| 147 EXPECT_FALSE(host_.CanPlayThrough(base::TimeDelta(), |
| 148 base::TimeDelta::FromSecondsD(0.01), 1.0)); |
| 149 // If the video is 1000s we can play through. |
| 150 EXPECT_TRUE(host_.CanPlayThrough(base::TimeDelta(), |
| 151 base::TimeDelta::FromSecondsD(1000.0), 1.0)); |
| 152 } |
| 153 |
76 } // namespace media | 154 } // namespace media |
OLD | NEW |