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

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: 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 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..ddebd2da503b1374a30cdf8c6e9e8f6e15f7dd4e
--- /dev/null
+++ b/media/base/download_rate_monitor_unittest.cc
@@ -0,0 +1,199 @@
+// 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|.
+ // Returns the number of bytes buffered in the media file after the
+ // network activity.
+ int SimulateNetwork(const base::Time& starting_time,
scherkus (not reviewing) 2011/11/16 02:15:17 nit: you call base::Time::FromDoubleT() every 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);
+ return bytes_buffered;
+ }
+
+ 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,
+ 10);
+}
+
+// If the user pauses and the pipeline stops downloading data, make sure the
+// DownloadRateMonitor understands that the download is not stalling.
+TEST_F(DownloadRateMonitorTest, DownloadRateGreaterThanBitrate_Pause) {
+ 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());
+ int buffered = 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),
+ buffered,
+ 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 very quickly 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