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

Unified 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: . 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 side-by-side diff with in-line comments
Download patch
Index: media/base/download_rate_monitor_unittest.cc
diff --git a/media/base/download_rate_monitor_unittest.cc b/media/base/download_rate_monitor_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d4d9cc97c88f3f6a415900bdf2f36eb034b2d042
--- /dev/null
+++ b/media/base/download_rate_monitor_unittest.cc
@@ -0,0 +1,194 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/base/download_rate_monitor.h"
+
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using ::testing::Mock;
+
+namespace media {
+
+class DownloadRateMonitorTest : public ::testing::Test {
+ public:
+ DownloadRateMonitorTest() {
+ monitor_.SetTotalBytes(kMediaSizeInBytes);
+ }
+
+ virtual ~DownloadRateMonitorTest() { }
+
+ MOCK_METHOD0(CanPlayThrough, void());
+
+ protected:
+ static const int kMediaSizeInBytes = 20 * 1024 * 1024;
+
+ // Simulates downloading of the media file. Packets are timed evenly in
+ // |ms_between_packets| intervals, starting at |starting_time|.
+ void SimulateNetwork(const base::Time& starting_time,
+ int starting_bytes,
+ int bytes_per_packet,
+ int ms_between_packets,
+ int number_of_packets) {
+ int bytes_buffered = starting_bytes;
+ base::Time packet_time = starting_time;
+
+ monitor_.SetNetworkActivity(true);
+ // Loop executes (number_of_packets + 1) times because a packet needs a
+ // starting and end point.
+ for (int i = 0; i < number_of_packets + 1; ++i) {
+ monitor_.SetBufferedBytes(bytes_buffered, packet_time);
+ packet_time += base::TimeDelta::FromMilliseconds(ms_between_packets);
+ bytes_buffered += bytes_per_packet;
+ }
+ monitor_.SetNetworkActivity(false);
+ }
+
+ DownloadRateMonitor monitor_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitorTest);
+};
+
+TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate) {
+ static const int media_bitrate = 1024 * 1024 * 8;
+
+ // Simulate downloading at double the media's bitrate.
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ EXPECT_CALL(*this, CanPlayThrough());
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ 2 * media_bitrate / 8,
+ 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
+ 10);
+}
+
+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
+ static const int media_bitrate = 1024 * 1024 * 8;
+ static const int download_byte_rate = 1.1 * media_bitrate / 8;
+
+ // Start downloading faster than the media's bitrate.
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ EXPECT_CALL(*this, CanPlayThrough());
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ download_byte_rate,
+ 1000,
+ 2);
+
+ // Then "pause" for 3 minutes and continue downloading at same rate.
+ SimulateNetwork(base::Time::FromDoubleT(60 * 3),
+ 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.
+ download_byte_rate,
+ 1000,
+ 4);
+}
+
+TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekForward) {
+ static const int media_bitrate = 1024 * 1024 * 8;
+ static const int download_byte_rate = 1.1 * media_bitrate / 8;
+
+ // Start downloading faster than the media's bitrate.
+ EXPECT_CALL(*this, CanPlayThrough());
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ download_byte_rate,
+ 1000,
+ 2);
+
+ // Then seek forward mid-file and continue downloading at same rate.
+ SimulateNetwork(base::Time::FromDoubleT(4),
+ kMediaSizeInBytes / 2,
+ download_byte_rate,
+ 1000,
+ 4);
+}
+
+TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_SeekBackward) {
+ static const int media_bitrate = 1024 * 1024 * 8;
+ static const int download_byte_rate = 1.1 * media_bitrate / 8;
+
+ // Start downloading faster than the media's bitrate, in middle of file.
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ kMediaSizeInBytes / 2,
+ download_byte_rate,
+ 1000,
+ 2);
+
+ // Then seek back to beginning and continue downloading at same rate.
+ EXPECT_CALL(*this, CanPlayThrough());
+ SimulateNetwork(base::Time::FromDoubleT(4),
+ 0,
+ download_byte_rate,
+ 1000,
+ 4);
+}
+
+TEST_F(DownloadRateMonitorTest, DownloadRateLessThanBitrate) {
+ static const int media_bitrate = 1024 * 1024 * 8;
+
+ // Simulate downloading at half the media's bitrate.
+ EXPECT_CALL(*this, CanPlayThrough())
+ .Times(0);
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ media_bitrate / 8 / 2,
+ 1000,
+ 10);
+}
+
+TEST_F(DownloadRateMonitorTest, MediaIsLoaded) {
+ static const int media_bitrate = 1024 * 1024 * 8;
+
+ monitor_.SetLoaded(true);
+
+ // Simulate no data downloaded (source is already loaded).
+ EXPECT_CALL(*this, CanPlayThrough());
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ 0,
+ 1000,
+ 10);
+}
+
+TEST_F(DownloadRateMonitorTest, VeryFastDownloadRate) {
+ static const int media_bitrate = 1024 * 1024 * 8;
+
+ // Simulate downloading half the video in one chunk.
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ EXPECT_CALL(*this, CanPlayThrough());
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ kMediaSizeInBytes / 2,
+ 10,
+ 1);
+}
+
+TEST_F(DownloadRateMonitorTest, DownloadEntireVideo) {
+ static const int seconds_of_data = 20;
+ static const int media_bitrate = kMediaSizeInBytes * 8 / seconds_of_data;
+
+ // Simulate downloading entire video at half the bitrate of the video.
+ monitor_.Start(base::Bind(&DownloadRateMonitorTest::CanPlayThrough,
+ base::Unretained(this)), media_bitrate);
+ EXPECT_CALL(*this, CanPlayThrough());
+ SimulateNetwork(base::Time::FromDoubleT(1),
+ 0,
+ media_bitrate / 8 / 2,
+ 1000,
+ seconds_of_data * 2);
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698