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_.set_total_bytes(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|, which is |
| 29 // number of seconds since unix epoch (Jan 1, 1970). |
| 30 // Returns the number of bytes buffered in the media file after the |
| 31 // network activity. |
| 32 int SimulateNetwork(double starting_time, |
| 33 int starting_bytes, |
| 34 int bytes_per_packet, |
| 35 int ms_between_packets, |
| 36 int number_of_packets) { |
| 37 int bytes_buffered = starting_bytes; |
| 38 base::Time packet_time = base::Time::FromDoubleT(starting_time); |
| 39 |
| 40 monitor_.SetNetworkActivity(true); |
| 41 // Loop executes (number_of_packets + 1) times because a packet needs a |
| 42 // starting and end point. |
| 43 for (int i = 0; i < number_of_packets + 1; ++i) { |
| 44 monitor_.SetBufferedBytes(bytes_buffered, packet_time); |
| 45 packet_time += base::TimeDelta::FromMilliseconds(ms_between_packets); |
| 46 bytes_buffered += bytes_per_packet; |
| 47 } |
| 48 monitor_.SetNetworkActivity(false); |
| 49 return bytes_buffered; |
| 50 } |
| 51 |
| 52 DownloadRateMonitor monitor_; |
| 53 |
| 54 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitorTest); |
| 56 }; |
| 57 |
| 58 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate) { |
| 59 static const int media_bitrate = 1024 * 1024 * 8; |
| 60 |
| 61 // Simulate downloading at double the media's bitrate. |
| 62 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 63 base::Unretained(this)), media_bitrate); |
| 64 EXPECT_CALL(*this, CanPlayThrough()); |
| 65 SimulateNetwork(1, 0, 2 * media_bitrate / 8, 1000, 10); |
| 66 } |
| 67 |
| 68 // If the user pauses and the pipeline stops downloading data, make sure the |
| 69 // DownloadRateMonitor understands that the download is not stalling. |
| 70 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_Pause) { |
| 71 static const int media_bitrate = 1024 * 1024 * 8; |
| 72 static const int download_byte_rate = 1.1 * media_bitrate / 8; |
| 73 |
| 74 // Start downloading faster than the media's bitrate. |
| 75 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 76 base::Unretained(this)), media_bitrate); |
| 77 EXPECT_CALL(*this, CanPlayThrough()); |
| 78 int buffered = SimulateNetwork(1, 0, download_byte_rate, 1000, 2); |
| 79 |
| 80 // Then "pause" for 3 minutes and continue downloading at same rate. |
| 81 SimulateNetwork(60 * 3, buffered, download_byte_rate, 1000, 4); |
| 82 } |
| 83 |
| 84 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekForward) { |
| 85 static const int media_bitrate = 1024 * 1024 * 8; |
| 86 static const int download_byte_rate = 1.1 * media_bitrate / 8; |
| 87 |
| 88 // Start downloading faster than the media's bitrate. |
| 89 EXPECT_CALL(*this, CanPlayThrough()); |
| 90 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 91 base::Unretained(this)), media_bitrate); |
| 92 SimulateNetwork(1, 0, download_byte_rate, 1000, 2); |
| 93 |
| 94 // Then seek forward mid-file and continue downloading at same rate. |
| 95 SimulateNetwork(4, kMediaSizeInBytes / 2, download_byte_rate, 1000, 4); |
| 96 } |
| 97 |
| 98 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekBackward) { |
| 99 static const int media_bitrate = 1024 * 1024 * 8; |
| 100 static const int download_byte_rate = 1.1 * media_bitrate / 8; |
| 101 |
| 102 // Start downloading faster than the media's bitrate, in middle of file. |
| 103 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 104 base::Unretained(this)), media_bitrate); |
| 105 SimulateNetwork(1, kMediaSizeInBytes / 2, download_byte_rate, 1000, 2); |
| 106 |
| 107 // Then seek back to beginning and continue downloading at same rate. |
| 108 EXPECT_CALL(*this, CanPlayThrough()); |
| 109 SimulateNetwork(4, 0, download_byte_rate, 1000, 4); |
| 110 } |
| 111 |
| 112 TEST_F(DownloadRateMonitorTest, DownloadRateLessThanBitrate) { |
| 113 static const int media_bitrate = 1024 * 1024 * 8; |
| 114 |
| 115 // Simulate downloading at half the media's bitrate. |
| 116 EXPECT_CALL(*this, CanPlayThrough()) |
| 117 .Times(0); |
| 118 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 119 base::Unretained(this)), media_bitrate); |
| 120 SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, 10); |
| 121 } |
| 122 |
| 123 TEST_F(DownloadRateMonitorTest, MediaIsLoaded) { |
| 124 static const int media_bitrate = 1024 * 1024 * 8; |
| 125 |
| 126 monitor_.set_loaded(true); |
| 127 |
| 128 // Simulate no data downloaded (source is already loaded). |
| 129 EXPECT_CALL(*this, CanPlayThrough()); |
| 130 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 131 base::Unretained(this)), media_bitrate); |
| 132 SimulateNetwork(1, 0, 0, 1000, 10); |
| 133 } |
| 134 |
| 135 TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) { |
| 136 static const int media_bitrate = 1024 * 1024 * 8; |
| 137 |
| 138 // Simulate downloading half the video very quickly in one chunk. |
| 139 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 140 base::Unretained(this)), media_bitrate); |
| 141 EXPECT_CALL(*this, CanPlayThrough()); |
| 142 SimulateNetwork(1, 0, kMediaSizeInBytes / 2, 10, 1); |
| 143 } |
| 144 |
| 145 TEST_F(DownloadRateMonitorTest, DownloadEntireVideo) { |
| 146 static const int seconds_of_data = 20; |
| 147 static const int media_bitrate = kMediaSizeInBytes * 8 / seconds_of_data; |
| 148 |
| 149 // Simulate downloading entire video at half the bitrate of the video. |
| 150 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough, |
| 151 base::Unretained(this)), media_bitrate); |
| 152 EXPECT_CALL(*this, CanPlayThrough()); |
| 153 SimulateNetwork(1, 0, media_bitrate / 8 / 2, 1000, seconds_of_data * 2); |
| 154 } |
| 155 |
| 156 } // namespace media |
OLD | NEW |