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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 <memory>
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/run_loop.h"
10 #include "base/test/power_monitor_test_base.h"
11 #include "base/test/test_message_loop.h"
12 #include "media/base/mock_media_log.h"
13 #include "media/blink/watch_time_reporter.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace media {
18
19 constexpr gfx::Size kSizeJustRight = gfx::Size(201, 201);
20
21 #define EXPECT_WATCH_TIME(key, value) \
22 EXPECT_CALL(*media_log_, OnWatchTimeUpdate(key, value));
23
24 class WatchTimeReporterTest : public testing::Test {
25 public:
26 WatchTimeReporterTest()
27 : media_log_(new testing::StrictMock<WatchTimeLogMonitor>()),
28 test_power_source_(new base::PowerMonitorTestSource()),
29 test_power_monitor_(new base::PowerMonitor(
30 std::unique_ptr<base::PowerMonitorSource>(test_power_source_))) {}
31 ~WatchTimeReporterTest() override {}
32
33 protected:
34 class WatchTimeLogMonitor : public MediaLog {
35 public:
36 WatchTimeLogMonitor() {}
37
38 void AddEvent(std::unique_ptr<MediaLogEvent> event) override {
39 ASSERT_EQ(event->type, MediaLogEvent::Type::WATCH_TIME_UPDATE);
40
41 for (base::DictionaryValue::Iterator it(event->params); !it.IsAtEnd();
42 it.Advance()) {
43 double in_seconds;
44 ASSERT_TRUE(it.value().GetAsDouble(&in_seconds));
45 OnWatchTimeUpdate(it.key(), base::TimeDelta::FromSecondsD(in_seconds));
46 }
47 }
48
49 MOCK_METHOD2(OnWatchTimeUpdate, void(const std::string&, base::TimeDelta));
50
51 protected:
52 ~WatchTimeLogMonitor() override {}
53
54 private:
55 DISALLOW_COPY_AND_ASSIGN(WatchTimeLogMonitor);
56 };
57
58 void Initialize(bool has_audio,
59 bool has_video,
60 bool is_mse,
61 bool is_encrypted,
62 const gfx::Size& initial_video_size) {
63 wtr_.reset(new WatchTimeReporter(
64 has_audio, has_video, is_mse, is_encrypted, media_log_,
65 initial_video_size,
66 base::Bind(&WatchTimeReporterTest::GetCurrentMediaTime,
67 base::Unretained(this))));
68 // Setup the reporting interval to be immediate to avoid spinning real time
69 // within the unit test.
70 wtr_->reporting_interval_ = base::TimeDelta();
71 }
72
73 void CycleReportingTimer() {
74 base::RunLoop run_loop;
75 message_loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
76 run_loop.Run();
77 }
78
79 bool IsMonitoring() { return wtr_->reporting_timer_.IsRunning(); }
80
81 MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta());
82
83 base::TestMessageLoop message_loop_;
84 scoped_refptr<testing::StrictMock<WatchTimeLogMonitor>> media_log_;
85
86 base::PowerMonitorTestSource* test_power_source_;
87 std::unique_ptr<base::PowerMonitor> test_power_monitor_;
88
89 std::unique_ptr<WatchTimeReporter> wtr_;
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(WatchTimeReporterTest);
93 };
94
95 // Tests that watch time reporting is appropriately enabled or disabled.
96 TEST_F(WatchTimeReporterTest, WatchTimeReporter) {
97 EXPECT_CALL(*this, GetCurrentMediaTime())
98 .WillRepeatedly(testing::Return(base::TimeDelta()));
99
100 Initialize(false, true, true, true, kSizeJustRight);
101 wtr_->OnPlaying();
102 EXPECT_FALSE(IsMonitoring());
103
104 Initialize(true, false, true, true, kSizeJustRight);
105 wtr_->OnPlaying();
106 EXPECT_FALSE(IsMonitoring());
107
108 constexpr gfx::Size kSizeTooSmall = gfx::Size(100, 100);
109 Initialize(true, true, true, true, kSizeTooSmall);
110 wtr_->OnPlaying();
111 EXPECT_FALSE(IsMonitoring());
112
113 Initialize(true, true, true, true, kSizeJustRight);
114 wtr_->OnPlaying();
115 EXPECT_TRUE(IsMonitoring());
116
117 Initialize(true, true, false, false, kSizeJustRight);
118 wtr_->OnPlaying();
119 EXPECT_TRUE(IsMonitoring());
120
121 Initialize(true, true, true, false, kSizeJustRight);
122 wtr_->OnPlaying();
123 EXPECT_TRUE(IsMonitoring());
124 }
125
126 // Tests that basic reporting for the all category works.
127 TEST_F(WatchTimeReporterTest, WatchTimeReporterBasic) {
128 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5);
129 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10);
130 EXPECT_CALL(*this, GetCurrentMediaTime())
131 .WillOnce(testing::Return(base::TimeDelta()))
132 .WillOnce(testing::Return(kWatchTimeEarly))
133 .WillRepeatedly(testing::Return(kWatchTimeLate));
134 Initialize(true, true, true, true, kSizeJustRight);
135 wtr_->OnPlaying();
136 EXPECT_TRUE(IsMonitoring());
137
138 // No log should have been generated yet since the message loop has not had
139 // any chance to pump.
140 CycleReportingTimer();
141
142 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAc, kWatchTimeLate);
143 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAll, kWatchTimeLate);
144 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoEme, kWatchTimeLate);
145 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoMse, kWatchTimeLate);
146 CycleReportingTimer();
147 }
148
149 // Tests that watch time is finalized upon destruction.
150 TEST_F(WatchTimeReporterTest, WatchTimeReporterFinalizeOnDestruction) {
151 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10);
152 EXPECT_CALL(*this, GetCurrentMediaTime())
153 .WillOnce(testing::Return(base::TimeDelta()))
154 .WillOnce(testing::Return(kWatchTime));
155 Initialize(true, true, true, true, kSizeJustRight);
156 wtr_->OnPlaying();
157 EXPECT_TRUE(IsMonitoring());
158
159 // Finalize the histogram before any cycles of the timer have run.
160 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAc, kWatchTime);
161 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoAll, kWatchTime);
162 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoEme, kWatchTime);
163 EXPECT_WATCH_TIME(WatchTimeReporter::kHistogramAudioVideoMse, kWatchTime);
164 wtr_.reset();
165 }
166
167 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698