Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: media/base/download_rate_monitor_unittest.cc

Issue 8399023: Fire canplaythrough event at the proper time for audio/video (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Responses to CR Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Returns the number of bytes buffered in the media file after the
30 // network activity.
31 int SimulateNetwork(const base::Time& starting_time,
scherkus (not reviewing) 2011/11/16 02:15:17 nit: you call base::Time::FromDoubleT() every time
32 int starting_bytes,
33 int bytes_per_packet,
34 int ms_between_packets,
35 int number_of_packets) {
36 int bytes_buffered = starting_bytes;
37 base::Time packet_time = starting_time;
38
39 monitor_.SetNetworkActivity(true);
40 // Loop executes (number_of_packets + 1) times because a packet needs a
41 // starting and end point.
42 for (int i = 0; i < number_of_packets + 1; ++i) {
43 monitor_.SetBufferedBytes(bytes_buffered, packet_time);
44 packet_time += base::TimeDelta::FromMilliseconds(ms_between_packets);
45 bytes_buffered += bytes_per_packet;
46 }
47 monitor_.SetNetworkActivity(false);
48 return bytes_buffered;
49 }
50
51 DownloadRateMonitor monitor_;
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitorTest);
55 };
56
57 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate) {
58 static const int media_bitrate = 1024 * 1024 * 8;
59
60 // Simulate downloading at double the media's bitrate.
61 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
62 base::Unretained(this)), media_bitrate);
63 EXPECT_CALL(*this, CanPlayThrough());
64 SimulateNetwork(base::Time::FromDoubleT(1),
65 0,
66 2 * media_bitrate / 8,
67 1000,
68 10);
69 }
70
71 // If the user pauses and the pipeline stops downloading data, make sure the
72 // DownloadRateMonitor understands that the download is not stalling.
73 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_Pause) {
74 static const int media_bitrate = 1024 * 1024 * 8;
75 static const int download_byte_rate = 1.1 * media_bitrate / 8;
76
77 // Start downloading faster than the media's bitrate.
78 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
79 base::Unretained(this)), media_bitrate);
80 EXPECT_CALL(*this, CanPlayThrough());
81 int buffered = SimulateNetwork(base::Time::FromDoubleT(1),
82 0,
83 download_byte_rate,
84 1000,
85 2);
86
87 // Then "pause" for 3 minutes and continue downloading at same rate.
88 SimulateNetwork(base::Time::FromDoubleT(60 * 3),
89 buffered,
90 download_byte_rate,
91 1000,
92 4);
93 }
94
95 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekForward) {
96 static const int media_bitrate = 1024 * 1024 * 8;
97 static const int download_byte_rate = 1.1 * media_bitrate / 8;
98
99 // Start downloading faster than the media's bitrate.
100 EXPECT_CALL(*this, CanPlayThrough());
101 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
102 base::Unretained(this)), media_bitrate);
103 SimulateNetwork(base::Time::FromDoubleT(1),
104 0,
105 download_byte_rate,
106 1000,
107 2);
108
109 // Then seek forward mid-file and continue downloading at same rate.
110 SimulateNetwork(base::Time::FromDoubleT(4),
111 kMediaSizeInBytes / 2,
112 download_byte_rate,
113 1000,
114 4);
115 }
116
117 TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekBackward) {
118 static const int media_bitrate = 1024 * 1024 * 8;
119 static const int download_byte_rate = 1.1 * media_bitrate / 8;
120
121 // Start downloading faster than the media's bitrate, in middle of file.
122 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
123 base::Unretained(this)), media_bitrate);
124 SimulateNetwork(base::Time::FromDoubleT(1),
125 kMediaSizeInBytes / 2,
126 download_byte_rate,
127 1000,
128 2);
129
130 // Then seek back to beginning and continue downloading at same rate.
131 EXPECT_CALL(*this, CanPlayThrough());
132 SimulateNetwork(base::Time::FromDoubleT(4),
133 0,
134 download_byte_rate,
135 1000,
136 4);
137 }
138
139 TEST_F(DownloadRateMonitorTest, DownloadRateLessThanBitrate) {
140 static const int media_bitrate = 1024 * 1024 * 8;
141
142 // Simulate downloading at half the media's bitrate.
143 EXPECT_CALL(*this, CanPlayThrough())
144 .Times(0);
145 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
146 base::Unretained(this)), media_bitrate);
147 SimulateNetwork(base::Time::FromDoubleT(1),
148 0,
149 media_bitrate / 8 / 2,
150 1000,
151 10);
152 }
153
154 TEST_F(DownloadRateMonitorTest, MediaIsLoaded) {
155 static const int media_bitrate = 1024 * 1024 * 8;
156
157 monitor_.SetLoaded(true);
158
159 // Simulate no data downloaded (source is already loaded).
160 EXPECT_CALL(*this, CanPlayThrough());
161 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
162 base::Unretained(this)), media_bitrate);
163 SimulateNetwork(base::Time::FromDoubleT(1),
164 0,
165 0,
166 1000,
167 10);
168 }
169
170 TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) {
171 static const int media_bitrate = 1024 * 1024 * 8;
172
173 // Simulate downloading half the video very quickly in one chunk.
174 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
175 base::Unretained(this)), media_bitrate);
176 EXPECT_CALL(*this, CanPlayThrough());
177 SimulateNetwork(base::Time::FromDoubleT(1),
178 0,
179 kMediaSizeInBytes / 2,
180 10,
181 1);
182 }
183
184 TEST_F(DownloadRateMonitorTest, DownloadEntireVideo) {
185 static const int seconds_of_data = 20;
186 static const int media_bitrate = kMediaSizeInBytes * 8 / seconds_of_data;
187
188 // Simulate downloading entire video at half the bitrate of the video.
189 monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
190 base::Unretained(this)), media_bitrate);
191 EXPECT_CALL(*this, CanPlayThrough());
192 SimulateNetwork(base::Time::FromDoubleT(1),
193 0,
194 media_bitrate / 8 / 2,
195 1000,
196 seconds_of_data * 2);
197 }
198
199 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698