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

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: Only log watch time once. 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..4dfac9a4a3fbe6c5f355094339f84a87f3c7e67b
--- /dev/null
+++ b/media/blink/watch_time_reporter_unittest.cc
@@ -0,0 +1,167 @@
+// 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/run_loop.h"
+#include "base/test/power_monitor_test_base.h"
+#include "base/test/test_message_loop.h"
+#include "media/base/mock_media_log.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);
+
+#define EXPECT_WATCH_TIME(key, value) \
+ EXPECT_CALL(*media_log_, OnWatchTimeUpdate(key, value));
+
+class WatchTimeReporterTest : public testing::Test {
+ public:
+ WatchTimeReporterTest()
+ : media_log_(new testing::StrictMock<WatchTimeLogMonitor>()),
+ test_power_source_(new base::PowerMonitorTestSource()),
+ test_power_monitor_(new base::PowerMonitor(
+ std::unique_ptr<base::PowerMonitorSource>(test_power_source_))) {}
+ ~WatchTimeReporterTest() override {}
+
+ protected:
+ class WatchTimeLogMonitor : public MediaLog {
+ public:
+ WatchTimeLogMonitor() {}
+
+ void AddEvent(std::unique_ptr<MediaLogEvent> event) override {
+ ASSERT_EQ(event->type, MediaLogEvent::Type::WATCH_TIME_UPDATE);
+
+ for (base::DictionaryValue::Iterator it(event->params); !it.IsAtEnd();
+ it.Advance()) {
+ double in_seconds;
+ ASSERT_TRUE(it.value().GetAsDouble(&in_seconds));
+ OnWatchTimeUpdate(it.key(), base::TimeDelta::FromSecondsD(in_seconds));
+ }
+ }
+
+ MOCK_METHOD2(OnWatchTimeUpdate, void(const std::string&, base::TimeDelta));
+
+ protected:
+ ~WatchTimeLogMonitor() override {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(WatchTimeLogMonitor);
+ };
+
+ 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, media_log_,
+ 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();
+ }
+
+ bool IsMonitoring() { return wtr_->reporting_timer_.IsRunning(); }
+
+ MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta());
+
+ base::TestMessageLoop message_loop_;
+ scoped_refptr<testing::StrictMock<WatchTimeLogMonitor>> media_log_;
+
+ base::PowerMonitorTestSource* test_power_source_;
+ 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 log should have been generated yet since the message loop has not had
+ // any chance to pump.
+ CycleReportingTimer();
+
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAc, kWatchTimeLate);
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAll, kWatchTimeLate);
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoEme, kWatchTimeLate);
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoMse, kWatchTimeLate);
+ CycleReportingTimer();
+}
+
+// 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());
+
+ // Finalize the histogram before any cycles of the timer have run.
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAc, kWatchTime);
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAll, kWatchTime);
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoEme, kWatchTime);
+ EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoMse, kWatchTime);
+ wtr_.reset();
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698