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

Unified Diff: media/blink/watch_time_reporter_unittest.cc

Issue 2160963002: Add watch time metrics for HTML5 media playback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more tests. Created 4 years, 4 months 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/blink/watch_time_reporter_unittest.cc
diff --git a/media/blink/watch_time_reporter_unittest.cc b/media/blink/watch_time_reporter_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f4271836c21ec8d2fc61e5031a24cb2d7afce8ad
--- /dev/null
+++ b/media/blink/watch_time_reporter_unittest.cc
@@ -0,0 +1,183 @@
+// Copyright 2016 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 <memory>
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/metrics/histogram_base.h"
+#include "base/metrics/histogram_samples.h"
+#include "base/metrics/statistics_recorder.h"
+#include "base/run_loop.h"
+#include "base/test/power_monitor_test_base.h"
+#include "base/test/test_message_loop.h"
+#include "media/blink/watch_time_reporter.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace media {
+
+constexpr gfx::Size kSizeJustRight = gfx::Size(201, 201);
+
+class WatchTimeReporterTest : public testing::Test {
+ public:
+ WatchTimeReporterTest()
+ : test_power_source_(new base::PowerMonitorTestSource()),
+ test_power_monitor_(new base::PowerMonitor(
+ std::unique_ptr<base::PowerMonitorSource>(test_power_source_))) {
+ statistics_recorder_ =
+ base::StatisticsRecorder::CreateTemporaryForTesting();
+ }
+
+ ~WatchTimeReporterTest() override {}
+
+ protected:
+ void Initialize(bool has_audio,
+ bool has_video,
+ bool is_mse,
+ bool is_encrypted,
+ const gfx::Size& initial_video_size) {
+ wtr_.reset(new WatchTimeReporter(
+ has_audio, has_video, is_mse, is_encrypted, initial_video_size,
+ base::Bind(&WatchTimeReporterTest::GetCurrentMediaTime,
+ base::Unretained(this))));
+ // Setup the reporting interval to be immediate to avoid spinning real time
+ // within the unit test.
+ wtr_->reporting_interval_ = base::TimeDelta();
+ }
+
+ void CycleReportingTimer() {
+ base::RunLoop run_loop;
+ message_loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
+ run_loop.Run();
+ }
+
+ base::TimeDelta GetReportedWatchTime(const char* histogram_name) {
+ const base::HistogramBase* histogram =
+ statistics_recorder_->FindHistogram(histogram_name);
+ if (!histogram)
+ return kNoTimestamp;
+
+ std::unique_ptr<base::HistogramSamples> samples =
+ histogram->SnapshotSamples();
+ if (!samples)
+ return kNoTimestamp;
+
+ return base::TimeDelta::FromMilliseconds(samples->sum());
+ }
+
+ base::TimeDelta GetAllWatchTime() {
+ return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoAll);
+ }
+
+ base::TimeDelta GetMseWatchTime() {
+ return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoMse);
+ }
+
+ base::TimeDelta GetEmeWatchTime() {
+ return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoEme);
+ }
+
+ base::TimeDelta GetAcWatchTime() {
+ return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoAc);
+ }
+
+ base::TimeDelta GetBatteryWatchTime() {
+ return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoBattery);
+ }
+
+ bool IsMonitoring() { return wtr_->reporting_timer_.IsRunning(); }
+
+ MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta());
+
+ base::TestMessageLoop message_loop_;
+ base::PowerMonitorTestSource* test_power_source_;
+ std::unique_ptr<base::StatisticsRecorder> statistics_recorder_;
+ std::unique_ptr<base::PowerMonitor> test_power_monitor_;
+ std::unique_ptr<WatchTimeReporter> wtr_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(WatchTimeReporterTest);
+};
+
+// Tests that watch time reporting is appropriately enabled or disabled.
+TEST_F(WatchTimeReporterTest, WatchTimeReporter) {
+ EXPECT_CALL(*this, GetCurrentMediaTime())
+ .WillRepeatedly(testing::Return(base::TimeDelta()));
+
+ Initialize(false, true, true, true, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_FALSE(IsMonitoring());
+
+ Initialize(true, false, true, true, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_FALSE(IsMonitoring());
+
+ constexpr gfx::Size kSizeTooSmall = gfx::Size(100, 100);
+ Initialize(true, true, true, true, kSizeTooSmall);
+ wtr_->OnPlaying();
+ EXPECT_FALSE(IsMonitoring());
+
+ Initialize(true, true, true, true, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_TRUE(IsMonitoring());
+
+ Initialize(true, true, false, false, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_TRUE(IsMonitoring());
+
+ Initialize(true, true, true, false, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_TRUE(IsMonitoring());
+}
+
+// Tests that basic reporting for the all category works.
+TEST_F(WatchTimeReporterTest, WatchTimeReporterBasic) {
+ constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5);
+ constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10);
+ EXPECT_CALL(*this, GetCurrentMediaTime())
+ .WillOnce(testing::Return(base::TimeDelta()))
+ .WillOnce(testing::Return(kWatchTimeEarly))
+ .WillRepeatedly(testing::Return(kWatchTimeLate));
+ Initialize(true, true, true, true, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_TRUE(IsMonitoring());
+
+ // No histogram should have been recorded yet since the message loop has not
+ // had any chance to pump.
+ EXPECT_EQ(kNoTimestamp, GetAllWatchTime());
+ CycleReportingTimer();
+
+ // No watch time should have been reported since not enough time has elapsed.
+ EXPECT_EQ(kNoTimestamp, GetAllWatchTime());
+ CycleReportingTimer();
+
+ // Watch time should have been reported.
+ EXPECT_EQ(kWatchTimeLate, GetAllWatchTime());
+}
+
+// Tests that watch time is finalized upon destruction.
+TEST_F(WatchTimeReporterTest, WatchTimeReporterFinalizeOnDestruction) {
+ constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10);
+ EXPECT_CALL(*this, GetCurrentMediaTime())
+ .WillOnce(testing::Return(base::TimeDelta()))
+ .WillOnce(testing::Return(kWatchTime));
+ Initialize(true, true, true, true, kSizeJustRight);
+ wtr_->OnPlaying();
+ EXPECT_TRUE(IsMonitoring());
+
+ // No histogram should have been recorded yet since the message loop has not
+ // had any chance to pump.
+ EXPECT_EQ(kNoTimestamp, GetAllWatchTime());
+
+ // Finalize the histogram.
+ wtr_.reset();
+ EXPECT_EQ(kWatchTime, GetAllWatchTime());
+ EXPECT_EQ(kWatchTime, GetMseWatchTime());
+ EXPECT_EQ(kWatchTime, GetEmeWatchTime());
+ EXPECT_EQ(kWatchTime, GetAcWatchTime());
+ EXPECT_EQ(kNoTimestamp, GetBatteryWatchTime());
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698