Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "media/base/download_rate_monitor.h" | |
| 6 | |
| 7 #include "testing/gmock/include/gmock/gmock.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 using ::testing::Mock; | |
| 11 | |
| 12 namespace media { | |
| 13 | |
| 14 class DownloadRateMonitorTest : public ::testing::Test { | |
| 15 public: | |
| 16 DownloadRateMonitorTest() { | |
| 17 monitor_.SetTotalBytes(kMediaSizeInBytes); | |
| 18 } | |
| 19 | |
| 20 virtual ~DownloadRateMonitorTest() { } | |
| 21 | |
| 22 MOCK_METHOD0(CanPlayThrough, void()); | |
| 23 | |
| 24 protected: | |
| 25 static const int kMediaSizeInBytes = 20 * 1024 * 1024; | |
| 26 | |
| 27 // Simulates downloading of the media file. Packets are timed evenly in | |
| 28 // |ms_between_packets| intervals, starting at |starting_time|. | |
| 29 void SimulateNetwork(const base::Time& starting_time, | |
| 30 int starting_bytes, | |
| 31 int bytes_per_packet, | |
| 32 int ms_between_packets, | |
| 33 int number_of_packets) { | |
| 34 int bytes_buffered = starting_bytes; | |
| 35 base::Time packet_time = starting_time; | |
| 36 | |
| 37 monitor_.SetNetworkActivity(true); | |
| 38 // Loop executes (number_of_packets + 1) times because a packet needs a | |
| 39 // starting and end point. | |
| 40 for (int i = 0; i < number_of_packets + 1; ++i) { | |
| 41 monitor_.SetBufferedBytes(bytes_buffered, packet_time); | |
| 42 packet_time += base::TimeDelta::FromMilliseconds(ms_between_packets); | |
| 43 bytes_buffered += bytes_per_packet; | |
| 44 } | |
| 45 monitor_.SetNetworkActivity(false); | |
| 46 } | |
| 47 | |
| 48 DownloadRateMonitor monitor_; | |
| 49 | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitorTest); | |
| 52 }; | |
| 53 | |
| 54 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate) { | |
| 55 static const int media_bitrate = 1024 * 1024 * 8; | |
| 56 | |
| 57 // Simulate downloading at double the media's bitrate. | |
| 58 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 59 base::Unretained(this)), media_bitrate); | |
| 60 EXPECT_CALL(*this, CanPlayThrough()); | |
| 61 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 62 0, | |
| 63 2 * media_bitrate / 8, | |
| 64 1000, | |
|
acolwell GONE FROM CHROMIUM
2011/11/15 21:43:10
nit: Consider removing this parameter since it is
vrk (LEFT CHROMIUM)
2011/11/15 22:41:35
I kept the param because I wanted to test in parti
| |
| 65 10); | |
| 66 } | |
| 67 | |
| 68 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_Pause) { | |
|
acolwell GONE FROM CHROMIUM
2011/11/15 21:43:10
I don't understand what behavior this is testing.
vrk (LEFT CHROMIUM)
2011/11/15 22:41:35
Yup! If the user pauses and the pipeline stops dow
| |
| 69 static const int media_bitrate = 1024 * 1024 * 8; | |
| 70 static const int download_byte_rate = 1.1 * media_bitrate / 8; | |
| 71 | |
| 72 // Start downloading faster than the media's bitrate. | |
| 73 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 74 base::Unretained(this)), media_bitrate); | |
| 75 EXPECT_CALL(*this, CanPlayThrough()); | |
| 76 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 77 0, | |
| 78 download_byte_rate, | |
| 79 1000, | |
| 80 2); | |
| 81 | |
| 82 // Then "pause" for 3 minutes and continue downloading at same rate. | |
| 83 SimulateNetwork(base::Time::FromDoubleT(60 * 3), | |
| 84 2 * download_byte_rate, | |
|
acolwell GONE FROM CHROMIUM
2011/11/15 21:43:10
nit: Consider having SimulateNetwork() return the
vrk (LEFT CHROMIUM)
2011/11/15 22:41:35
Good idea, done.
| |
| 85 download_byte_rate, | |
| 86 1000, | |
| 87 4); | |
| 88 } | |
| 89 | |
| 90 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekForward) { | |
| 91 static const int media_bitrate = 1024 * 1024 * 8; | |
| 92 static const int download_byte_rate = 1.1 * media_bitrate / 8; | |
| 93 | |
| 94 // Start downloading faster than the media's bitrate. | |
| 95 EXPECT_CALL(*this, CanPlayThrough()); | |
| 96 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 97 base::Unretained(this)), media_bitrate); | |
| 98 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 99 0, | |
| 100 download_byte_rate, | |
| 101 1000, | |
| 102 2); | |
| 103 | |
| 104 // Then seek forward mid-file and continue downloading at same rate. | |
| 105 SimulateNetwork(base::Time::FromDoubleT(4), | |
| 106 kMediaSizeInBytes / 2, | |
| 107 download_byte_rate, | |
| 108 1000, | |
| 109 4); | |
| 110 } | |
| 111 | |
| 112 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekBackward) { | |
| 113 static const int media_bitrate = 1024 * 1024 * 8; | |
| 114 static const int download_byte_rate = 1.1 * media_bitrate / 8; | |
| 115 | |
| 116 // Start downloading faster than the media's bitrate, in middle of file. | |
| 117 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 118 base::Unretained(this)), media_bitrate); | |
| 119 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 120 kMediaSizeInBytes / 2, | |
| 121 download_byte_rate, | |
| 122 1000, | |
| 123 2); | |
| 124 | |
| 125 // Then seek back to beginning and continue downloading at same rate. | |
| 126 EXPECT_CALL(*this, CanPlayThrough()); | |
| 127 SimulateNetwork(base::Time::FromDoubleT(4), | |
| 128 0, | |
| 129 download_byte_rate, | |
| 130 1000, | |
| 131 4); | |
| 132 } | |
| 133 | |
| 134 TEST_F(DownloadRateMonitorTest, DownloadRateLessThanBitrate) { | |
| 135 static const int media_bitrate = 1024 * 1024 * 8; | |
| 136 | |
| 137 // Simulate downloading at half the media's bitrate. | |
| 138 EXPECT_CALL(*this, CanPlayThrough()) | |
| 139 .Times(0); | |
| 140 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 141 base::Unretained(this)), media_bitrate); | |
| 142 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 143 0, | |
| 144 media_bitrate / 8 / 2, | |
| 145 1000, | |
| 146 10); | |
| 147 } | |
| 148 | |
| 149 TEST_F(DownloadRateMonitorTest, MediaIsLoaded) { | |
| 150 static const int media_bitrate = 1024 * 1024 * 8; | |
| 151 | |
| 152 monitor_.SetLoaded(true); | |
| 153 | |
| 154 // Simulate no data downloaded (source is already loaded). | |
| 155 EXPECT_CALL(*this, CanPlayThrough()); | |
| 156 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 157 base::Unretained(this)), media_bitrate); | |
| 158 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 159 0, | |
| 160 0, | |
| 161 1000, | |
| 162 10); | |
| 163 } | |
| 164 | |
| 165 TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) { | |
| 166 static const int media_bitrate = 1024 * 1024 * 8; | |
| 167 | |
| 168 // Simulate downloading half the video in one chunk. | |
| 169 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 170 base::Unretained(this)), media_bitrate); | |
| 171 EXPECT_CALL(*this, CanPlayThrough()); | |
| 172 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 173 0, | |
| 174 kMediaSizeInBytes / 2, | |
| 175 10, | |
| 176 1); | |
| 177 } | |
| 178 | |
| 179 TEST_F(DownloadRateMonitorTest, DownloadEntireVideo) { | |
| 180 static const int seconds_of_data = 20; | |
| 181 static const int media_bitrate = kMediaSizeInBytes * 8 / seconds_of_data; | |
| 182 | |
| 183 // Simulate downloading entire video at half the bitrate of the video. | |
| 184 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, | |
| 185 base::Unretained(this)), media_bitrate); | |
| 186 EXPECT_CALL(*this, CanPlayThrough()); | |
| 187 SimulateNetwork(base::Time::FromDoubleT(1), | |
| 188 0, | |
| 189 media_bitrate / 8 / 2, | |
| 190 1000, | |
| 191 seconds_of_data * 2); | |
| 192 } | |
| 193 | |
| 194 } // namespace media | |
| OLD | NEW |