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