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

Side by Side Diff: media/blink/watch_time_reporter_unittest.cc

Issue 2498113004: Add audio only watch time metrics. (Closed)
Patch Set: Comments and tests. Created 4 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 unified diff | Download patch
« no previous file with comments | « media/blink/watch_time_reporter.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/test/test_message_loop.h" 10 #include "base/test/test_message_loop.h"
11 #include "media/base/mock_media_log.h" 11 #include "media/base/mock_media_log.h"
12 #include "media/blink/watch_time_reporter.h" 12 #include "media/blink/watch_time_reporter.h"
13 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace media { 16 namespace media {
17 17
18 constexpr gfx::Size kSizeJustRight = gfx::Size(201, 201); 18 constexpr gfx::Size kSizeJustRight = gfx::Size(201, 201);
19 19
20 #define EXPECT_WATCH_TIME(key, value) \ 20 #define EXPECT_WATCH_TIME(key, value) \
21 EXPECT_CALL(*media_log_, OnWatchTimeUpdate(key, value)).RetiresOnSaturation(); 21 do { \
22 EXPECT_CALL( \
23 *media_log_, \
24 OnWatchTimeUpdate(has_video_ ? MediaLog::kWatchTimeAudioVideo##key \
25 : MediaLog::kWatchTimeAudio##key, \
26 value)) \
27 .RetiresOnSaturation(); \
28 } while (0)
22 29
23 #define EXPECT_WATCH_TIME_FINALIZED() \ 30 #define EXPECT_WATCH_TIME_FINALIZED() \
24 EXPECT_CALL(*media_log_, OnWatchTimeFinalized()).RetiresOnSaturation(); 31 EXPECT_CALL(*media_log_, OnWatchTimeFinalized()).RetiresOnSaturation();
25 32
26 #define EXPECT_POWER_WATCH_TIME_FINALIZED() \ 33 #define EXPECT_POWER_WATCH_TIME_FINALIZED() \
27 EXPECT_CALL(*media_log_, OnPowerWatchTimeFinalized()).RetiresOnSaturation(); 34 EXPECT_CALL(*media_log_, OnPowerWatchTimeFinalized()).RetiresOnSaturation();
28 35
29 class WatchTimeReporterTest : public testing::Test { 36 class WatchTimeReporterTest : public testing::TestWithParam<bool> {
30 public: 37 public:
31 WatchTimeReporterTest() 38 WatchTimeReporterTest()
32 : media_log_(new testing::StrictMock<WatchTimeLogMonitor>()) {} 39 : has_video_(GetParam()),
40 media_log_(new testing::StrictMock<WatchTimeLogMonitor>()) {}
33 ~WatchTimeReporterTest() override {} 41 ~WatchTimeReporterTest() override {}
34 42
35 protected: 43 protected:
36 class WatchTimeLogMonitor : public MediaLog { 44 class WatchTimeLogMonitor : public MediaLog {
37 public: 45 public:
38 WatchTimeLogMonitor() {} 46 WatchTimeLogMonitor() {}
39 47
40 void AddEvent(std::unique_ptr<MediaLogEvent> event) override { 48 void AddEvent(std::unique_ptr<MediaLogEvent> event) override {
41 ASSERT_EQ(event->type, MediaLogEvent::Type::WATCH_TIME_UPDATE); 49 ASSERT_EQ(event->type, MediaLogEvent::Type::WATCH_TIME_UPDATE);
42 50
(...skipping 19 matching lines...) Expand all
62 MOCK_METHOD2(OnWatchTimeUpdate, void(const std::string&, base::TimeDelta)); 70 MOCK_METHOD2(OnWatchTimeUpdate, void(const std::string&, base::TimeDelta));
63 71
64 protected: 72 protected:
65 ~WatchTimeLogMonitor() override {} 73 ~WatchTimeLogMonitor() override {}
66 74
67 private: 75 private:
68 DISALLOW_COPY_AND_ASSIGN(WatchTimeLogMonitor); 76 DISALLOW_COPY_AND_ASSIGN(WatchTimeLogMonitor);
69 }; 77 };
70 78
71 void Initialize(bool has_audio, 79 void Initialize(bool has_audio,
72 bool has_video,
73 bool is_mse, 80 bool is_mse,
74 bool is_encrypted, 81 bool is_encrypted,
75 const gfx::Size& initial_video_size) { 82 const gfx::Size& initial_video_size) {
76 wtr_.reset(new WatchTimeReporter( 83 wtr_.reset(new WatchTimeReporter(
77 has_audio, has_video, is_mse, is_encrypted, media_log_, 84 has_audio, has_video_, is_mse, is_encrypted, media_log_,
78 initial_video_size, 85 initial_video_size,
79 base::Bind(&WatchTimeReporterTest::GetCurrentMediaTime, 86 base::Bind(&WatchTimeReporterTest::GetCurrentMediaTime,
80 base::Unretained(this)))); 87 base::Unretained(this))));
81 88
82 // Setup the reporting interval to be immediate to avoid spinning real time 89 // Setup the reporting interval to be immediate to avoid spinning real time
83 // within the unit test. 90 // within the unit test.
84 wtr_->reporting_interval_ = base::TimeDelta(); 91 wtr_->reporting_interval_ = base::TimeDelta();
85 } 92 }
86 93
87 void CycleReportingTimer() { 94 void CycleReportingTimer() {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 131
125 // Indicates that power watch time should be reported to the battery metric. 132 // Indicates that power watch time should be reported to the battery metric.
126 kStartOnBattery = 16, 133 kStartOnBattery = 16,
127 134
128 // Indicates an extra start event may be generated during test execution. 135 // Indicates an extra start event may be generated during test execution.
129 kFinalizeInterleavedStartEvent = 32, 136 kFinalizeInterleavedStartEvent = 32,
130 }; 137 };
131 138
132 template <int TestFlags = 0, typename HysteresisTestCallback> 139 template <int TestFlags = 0, typename HysteresisTestCallback>
133 void RunHysteresisTest(HysteresisTestCallback test_callback_func) { 140 void RunHysteresisTest(HysteresisTestCallback test_callback_func) {
134 Initialize(true, true, false, false, kSizeJustRight); 141 Initialize(true, false, false, kSizeJustRight);
135 142
136 // Setup all current time expectations first since they need to use the 143 // Setup all current time expectations first since they need to use the
137 // InSequence macro for ease of use, but we don't want the watch time 144 // InSequence macro for ease of use, but we don't want the watch time
138 // expectations to be in sequence (or expectations would depend on sorted 145 // expectations to be in sequence (or expectations would depend on sorted
139 // order of histogram names). 146 // order of histogram names).
140 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(10); 147 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(10);
141 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(12); 148 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(12);
142 constexpr base::TimeDelta kWatchTime3 = base::TimeDelta::FromSeconds(15); 149 constexpr base::TimeDelta kWatchTime3 = base::TimeDelta::FromSeconds(15);
143 constexpr base::TimeDelta kWatchTime4 = base::TimeDelta::FromSeconds(30); 150 constexpr base::TimeDelta kWatchTime4 = base::TimeDelta::FromSeconds(30);
144 { 151 {
(...skipping 27 matching lines...) Expand all
172 } 179 }
173 } 180 }
174 181
175 wtr_->OnPlaying(); 182 wtr_->OnPlaying();
176 EXPECT_TRUE(IsMonitoring()); 183 EXPECT_TRUE(IsMonitoring());
177 if (TestFlags & kStartOnBattery) 184 if (TestFlags & kStartOnBattery)
178 SetOnBatteryPower(true); 185 SetOnBatteryPower(true);
179 else 186 else
180 ASSERT_FALSE(wtr_->is_on_battery_power_); 187 ASSERT_FALSE(wtr_->is_on_battery_power_);
181 188
182 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime1); 189 EXPECT_WATCH_TIME(All, kWatchTime1);
183 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime1); 190 EXPECT_WATCH_TIME(Src, kWatchTime1);
184 EXPECT_WATCH_TIME(TestFlags & kStartOnBattery 191 if (TestFlags & kStartOnBattery)
185 ? MediaLog::kWatchTimeAudioVideoBattery 192 EXPECT_WATCH_TIME(Battery, kWatchTime1);
186 : MediaLog::kWatchTimeAudioVideoAc, 193 else
187 kWatchTime1); 194 EXPECT_WATCH_TIME(Ac, kWatchTime1);
195
188 CycleReportingTimer(); 196 CycleReportingTimer();
189 197
190 // Invoke the test. 198 // Invoke the test.
191 test_callback_func(); 199 test_callback_func();
192 200
193 const base::TimeDelta kExpectedWatchTime = 201 const base::TimeDelta kExpectedWatchTime =
194 TestFlags & kAccumulationContinuesAfterTest ? kWatchTime3 : kWatchTime2; 202 TestFlags & kAccumulationContinuesAfterTest ? kWatchTime3 : kWatchTime2;
195 203
196 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kExpectedWatchTime); 204 EXPECT_WATCH_TIME(All, kExpectedWatchTime);
197 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kExpectedWatchTime); 205 EXPECT_WATCH_TIME(Src, kExpectedWatchTime);
198 EXPECT_WATCH_TIME( 206 const base::TimeDelta kExpectedPowerWatchTime =
199 TestFlags & kStartOnBattery ? MediaLog::kWatchTimeAudioVideoBattery 207 TestFlags & kFinalizePowerWatchTime ? kWatchTime2 : kExpectedWatchTime;
200 : MediaLog::kWatchTimeAudioVideoAc, 208 if (TestFlags & kStartOnBattery)
201 TestFlags & kFinalizePowerWatchTime ? kWatchTime2 : kExpectedWatchTime); 209 EXPECT_WATCH_TIME(Battery, kExpectedPowerWatchTime);
210 else
211 EXPECT_WATCH_TIME(Ac, kExpectedPowerWatchTime);
202 212
203 // If we're not testing battery watch time, this is the end of the test. 213 // If we're not testing battery watch time, this is the end of the test.
204 if (!(TestFlags & kTransitionPowerWatchTime)) { 214 if (!(TestFlags & kTransitionPowerWatchTime)) {
205 EXPECT_WATCH_TIME_FINALIZED(); 215 EXPECT_WATCH_TIME_FINALIZED();
206 wtr_.reset(); 216 wtr_.reset();
207 return; 217 return;
208 } 218 }
209 219
210 ASSERT_TRUE(TestFlags & kAccumulationContinuesAfterTest) 220 ASSERT_TRUE(TestFlags & kAccumulationContinuesAfterTest)
211 << "kTransitionPowerWatchTime tests must be done with " 221 << "kTransitionPowerWatchTime tests must be done with "
212 "kAccumulationContinuesAfterTest"; 222 "kAccumulationContinuesAfterTest";
213 223
214 EXPECT_POWER_WATCH_TIME_FINALIZED(); 224 EXPECT_POWER_WATCH_TIME_FINALIZED();
215 CycleReportingTimer(); 225 CycleReportingTimer();
216 226
217 // Run one last cycle that is long enough to trigger a new watch time entry 227 // Run one last cycle that is long enough to trigger a new watch time entry
218 // on the opposite of the current power watch time graph; i.e. if we started 228 // on the opposite of the current power watch time graph; i.e. if we started
219 // on battery we'll now record one for ac and vice versa. 229 // on battery we'll now record one for ac and vice versa.
220 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime4); 230 EXPECT_WATCH_TIME(All, kWatchTime4);
221 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime4); 231 EXPECT_WATCH_TIME(Src, kWatchTime4);
222 EXPECT_WATCH_TIME(TestFlags & kStartOnBattery 232 if (TestFlags & kStartOnBattery)
223 ? MediaLog::kWatchTimeAudioVideoAc 233 EXPECT_WATCH_TIME(Ac, kWatchTime4 - kWatchTime2);
224 : MediaLog::kWatchTimeAudioVideoBattery, 234 else
225 kWatchTime4 - kWatchTime2); 235 EXPECT_WATCH_TIME(Battery, kWatchTime4 - kWatchTime2);
226 EXPECT_WATCH_TIME_FINALIZED(); 236 EXPECT_WATCH_TIME_FINALIZED();
227 wtr_.reset(); 237 wtr_.reset();
228 } 238 }
229 239
230 MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta()); 240 MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta());
231 241
242 const bool has_video_;
232 base::TestMessageLoop message_loop_; 243 base::TestMessageLoop message_loop_;
233 scoped_refptr<testing::StrictMock<WatchTimeLogMonitor>> media_log_; 244 scoped_refptr<testing::StrictMock<WatchTimeLogMonitor>> media_log_;
234 std::unique_ptr<WatchTimeReporter> wtr_; 245 std::unique_ptr<WatchTimeReporter> wtr_;
235 246
236 private: 247 private:
237 DISALLOW_COPY_AND_ASSIGN(WatchTimeReporterTest); 248 DISALLOW_COPY_AND_ASSIGN(WatchTimeReporterTest);
238 }; 249 };
239 250
240 // Tests that watch time reporting is appropriately enabled or disabled. 251 // Tests that watch time reporting is appropriately enabled or disabled.
241 TEST_F(WatchTimeReporterTest, WatchTimeReporter) { 252 TEST_P(WatchTimeReporterTest, WatchTimeReporter) {
242 EXPECT_CALL(*this, GetCurrentMediaTime()) 253 EXPECT_CALL(*this, GetCurrentMediaTime())
243 .WillRepeatedly(testing::Return(base::TimeDelta())); 254 .WillRepeatedly(testing::Return(base::TimeDelta()));
244 255
245 Initialize(false, true, true, true, kSizeJustRight); 256 Initialize(!has_video_, true, true, gfx::Size());
246 wtr_->OnPlaying(); 257 wtr_->OnPlaying();
247 EXPECT_FALSE(IsMonitoring()); 258 EXPECT_EQ(!has_video_, IsMonitoring());
248 259
249 Initialize(true, false, true, true, kSizeJustRight); 260 Initialize(true, true, true, gfx::Size());
250 wtr_->OnPlaying(); 261 wtr_->OnPlaying();
251 EXPECT_FALSE(IsMonitoring()); 262 EXPECT_EQ(!has_video_, IsMonitoring());
252 263
253 constexpr gfx::Size kSizeTooSmall = gfx::Size(100, 100); 264 constexpr gfx::Size kSizeTooSmall = gfx::Size(100, 100);
254 Initialize(true, true, true, true, kSizeTooSmall); 265 Initialize(!has_video_, true, true, kSizeTooSmall);
255 wtr_->OnPlaying(); 266 wtr_->OnPlaying();
256 EXPECT_FALSE(IsMonitoring()); 267 EXPECT_EQ(!has_video_, IsMonitoring());
257 268
258 Initialize(true, true, true, true, kSizeJustRight); 269 Initialize(true, true, true, kSizeJustRight);
259 wtr_->OnPlaying(); 270 wtr_->OnPlaying();
260 EXPECT_TRUE(IsMonitoring()); 271 EXPECT_TRUE(IsMonitoring());
261 272
262 Initialize(true, true, false, false, kSizeJustRight); 273 Initialize(true, false, false, kSizeJustRight);
263 wtr_->OnPlaying(); 274 wtr_->OnPlaying();
264 EXPECT_TRUE(IsMonitoring()); 275 EXPECT_TRUE(IsMonitoring());
265 276
266 Initialize(true, true, true, false, kSizeJustRight); 277 Initialize(true, true, false, kSizeJustRight);
267 wtr_->OnPlaying(); 278 wtr_->OnPlaying();
268 EXPECT_TRUE(IsMonitoring()); 279 EXPECT_TRUE(IsMonitoring());
280
281 Initialize(true, true, true, gfx::Size());
282 wtr_->OnPlaying();
283 EXPECT_EQ(!has_video_, IsMonitoring());
284
285 Initialize(true, false, false, gfx::Size());
286 wtr_->OnPlaying();
287 EXPECT_EQ(!has_video_, IsMonitoring());
288
289 Initialize(true, true, false, gfx::Size());
290 wtr_->OnPlaying();
291 EXPECT_EQ(!has_video_, IsMonitoring());
269 } 292 }
270 293
271 // Tests that basic reporting for the all category works. 294 // Tests that basic reporting for the all category works.
272 TEST_F(WatchTimeReporterTest, WatchTimeReporterBasic) { 295 TEST_P(WatchTimeReporterTest, WatchTimeReporterBasic) {
273 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5); 296 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5);
274 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10); 297 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10);
275 EXPECT_CALL(*this, GetCurrentMediaTime()) 298 EXPECT_CALL(*this, GetCurrentMediaTime())
276 .WillOnce(testing::Return(base::TimeDelta())) 299 .WillOnce(testing::Return(base::TimeDelta()))
277 .WillOnce(testing::Return(kWatchTimeEarly)) 300 .WillOnce(testing::Return(kWatchTimeEarly))
278 .WillRepeatedly(testing::Return(kWatchTimeLate)); 301 .WillRepeatedly(testing::Return(kWatchTimeLate));
279 Initialize(true, true, true, true, kSizeJustRight); 302 Initialize(true, true, true, kSizeJustRight);
280 wtr_->OnPlaying(); 303 wtr_->OnPlaying();
281 EXPECT_TRUE(IsMonitoring()); 304 EXPECT_TRUE(IsMonitoring());
282 305
283 // No log should have been generated yet since the message loop has not had 306 // No log should have been generated yet since the message loop has not had
284 // any chance to pump. 307 // any chance to pump.
285 CycleReportingTimer(); 308 CycleReportingTimer();
286 309
287 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTimeLate); 310 EXPECT_WATCH_TIME(Ac, kWatchTimeLate);
288 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTimeLate); 311 EXPECT_WATCH_TIME(All, kWatchTimeLate);
289 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTimeLate); 312 EXPECT_WATCH_TIME(Eme, kWatchTimeLate);
290 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTimeLate); 313 EXPECT_WATCH_TIME(Mse, kWatchTimeLate);
291 CycleReportingTimer(); 314 CycleReportingTimer();
292 } 315 }
316
317 // Tests that basic reporting for the all category works.
318 TEST_P(WatchTimeReporterTest, WatchTimeReporterShownHidden) {
319 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(8);
320 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10);
321 EXPECT_CALL(*this, GetCurrentMediaTime())
322 .WillOnce(testing::Return(base::TimeDelta()))
323 .WillOnce(testing::Return(kWatchTimeEarly))
324 .WillRepeatedly(testing::Return(kWatchTimeLate));
325 Initialize(true, true, true, kSizeJustRight);
326 wtr_->OnPlaying();
327 EXPECT_TRUE(IsMonitoring());
328
329 // If we have video, this will halt watch time collection, if only audio it
330 // will do nothing. Consume the expectation if audio only.
331 wtr_->OnHidden();
332 if (!has_video_)
333 GetCurrentMediaTime();
334
335 const base::TimeDelta kExpectedWatchTime =
336 has_video_ ? kWatchTimeEarly : kWatchTimeLate;
337 EXPECT_WATCH_TIME(Ac, kExpectedWatchTime);
338 EXPECT_WATCH_TIME(All, kExpectedWatchTime);
339 EXPECT_WATCH_TIME(Eme, kExpectedWatchTime);
340 EXPECT_WATCH_TIME(Mse, kExpectedWatchTime);
341 EXPECT_WATCH_TIME_FINALIZED();
342 wtr_.reset();
343 }
344
293 // Tests that starting from a non-zero base works. 345 // Tests that starting from a non-zero base works.
294 TEST_F(WatchTimeReporterTest, WatchTimeReporterNonZeroStart) { 346 TEST_P(WatchTimeReporterTest, WatchTimeReporterNonZeroStart) {
295 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(5); 347 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(5);
296 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(15); 348 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(15);
297 EXPECT_CALL(*this, GetCurrentMediaTime()) 349 EXPECT_CALL(*this, GetCurrentMediaTime())
298 .WillOnce(testing::Return(kWatchTime1)) 350 .WillOnce(testing::Return(kWatchTime1))
299 .WillRepeatedly(testing::Return(kWatchTime2)); 351 .WillRepeatedly(testing::Return(kWatchTime2));
300 Initialize(true, true, true, true, kSizeJustRight); 352 Initialize(true, true, true, kSizeJustRight);
301 wtr_->OnPlaying(); 353 wtr_->OnPlaying();
302 EXPECT_TRUE(IsMonitoring()); 354 EXPECT_TRUE(IsMonitoring());
303 355
304 const base::TimeDelta kWatchTime = kWatchTime2 - kWatchTime1; 356 const base::TimeDelta kWatchTime = kWatchTime2 - kWatchTime1;
305 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 357 EXPECT_WATCH_TIME(Ac, kWatchTime);
306 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 358 EXPECT_WATCH_TIME(All, kWatchTime);
307 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); 359 EXPECT_WATCH_TIME(Eme, kWatchTime);
308 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); 360 EXPECT_WATCH_TIME(Mse, kWatchTime);
309 CycleReportingTimer(); 361 CycleReportingTimer();
310 } 362 }
311 363
312 // Tests that seeking causes an immediate finalization. 364 // Tests that seeking causes an immediate finalization.
313 TEST_F(WatchTimeReporterTest, SeekFinalizes) { 365 TEST_P(WatchTimeReporterTest, SeekFinalizes) {
314 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); 366 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10);
315 EXPECT_CALL(*this, GetCurrentMediaTime()) 367 EXPECT_CALL(*this, GetCurrentMediaTime())
316 .WillOnce(testing::Return(base::TimeDelta())) 368 .WillOnce(testing::Return(base::TimeDelta()))
317 .WillOnce(testing::Return(kWatchTime)); 369 .WillOnce(testing::Return(kWatchTime));
318 Initialize(true, true, true, true, kSizeJustRight); 370 Initialize(true, true, true, kSizeJustRight);
319 wtr_->OnPlaying(); 371 wtr_->OnPlaying();
320 EXPECT_TRUE(IsMonitoring()); 372 EXPECT_TRUE(IsMonitoring());
321 373
322 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 374 EXPECT_WATCH_TIME(Ac, kWatchTime);
323 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 375 EXPECT_WATCH_TIME(All, kWatchTime);
324 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); 376 EXPECT_WATCH_TIME(Eme, kWatchTime);
325 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); 377 EXPECT_WATCH_TIME(Mse, kWatchTime);
326 EXPECT_WATCH_TIME_FINALIZED(); 378 EXPECT_WATCH_TIME_FINALIZED();
327 wtr_->OnSeeking(); 379 wtr_->OnSeeking();
328 } 380 }
329 381
330 // Tests that seeking causes an immediate finalization, but does not trample a 382 // Tests that seeking causes an immediate finalization, but does not trample a
331 // previously set finalize time. 383 // previously set finalize time.
332 TEST_F(WatchTimeReporterTest, SeekFinalizeDoesNotTramplePreviousFinalize) { 384 TEST_P(WatchTimeReporterTest, SeekFinalizeDoesNotTramplePreviousFinalize) {
333 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); 385 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10);
334 EXPECT_CALL(*this, GetCurrentMediaTime()) 386 EXPECT_CALL(*this, GetCurrentMediaTime())
335 .WillOnce(testing::Return(base::TimeDelta())) 387 .WillOnce(testing::Return(base::TimeDelta()))
336 .WillOnce(testing::Return(kWatchTime)); 388 .WillOnce(testing::Return(kWatchTime));
337 Initialize(true, true, true, true, kSizeJustRight); 389 Initialize(true, true, true, kSizeJustRight);
338 wtr_->OnPlaying(); 390 wtr_->OnPlaying();
339 EXPECT_TRUE(IsMonitoring()); 391 EXPECT_TRUE(IsMonitoring());
340 392
341 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 393 EXPECT_WATCH_TIME(Ac, kWatchTime);
342 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 394 EXPECT_WATCH_TIME(All, kWatchTime);
343 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); 395 EXPECT_WATCH_TIME(Eme, kWatchTime);
344 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); 396 EXPECT_WATCH_TIME(Mse, kWatchTime);
345 EXPECT_WATCH_TIME_FINALIZED(); 397 EXPECT_WATCH_TIME_FINALIZED();
346 wtr_->OnPaused(); 398 wtr_->OnPaused();
347 wtr_->OnSeeking(); 399 wtr_->OnSeeking();
348 } 400 }
349 401
350 // Tests that watch time is finalized upon destruction. 402 // Tests that watch time is finalized upon destruction.
351 TEST_F(WatchTimeReporterTest, WatchTimeReporterFinalizeOnDestruction) { 403 TEST_P(WatchTimeReporterTest, WatchTimeReporterFinalizeOnDestruction) {
352 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); 404 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10);
353 EXPECT_CALL(*this, GetCurrentMediaTime()) 405 EXPECT_CALL(*this, GetCurrentMediaTime())
354 .WillOnce(testing::Return(base::TimeDelta())) 406 .WillOnce(testing::Return(base::TimeDelta()))
355 .WillOnce(testing::Return(kWatchTime)); 407 .WillOnce(testing::Return(kWatchTime));
356 Initialize(true, true, true, true, kSizeJustRight); 408 Initialize(true, true, true, kSizeJustRight);
357 wtr_->OnPlaying(); 409 wtr_->OnPlaying();
358 EXPECT_TRUE(IsMonitoring()); 410 EXPECT_TRUE(IsMonitoring());
359 411
360 // Finalize the histogram before any cycles of the timer have run. 412 // Finalize the histogram before any cycles of the timer have run.
361 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 413 EXPECT_WATCH_TIME(Ac, kWatchTime);
362 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 414 EXPECT_WATCH_TIME(All, kWatchTime);
363 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); 415 EXPECT_WATCH_TIME(Eme, kWatchTime);
364 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); 416 EXPECT_WATCH_TIME(Mse, kWatchTime);
365 EXPECT_WATCH_TIME_FINALIZED(); 417 EXPECT_WATCH_TIME_FINALIZED();
366 wtr_.reset(); 418 wtr_.reset();
367 } 419 }
368 420
369 // Tests that watch time categories are mapped correctly. 421 // Tests that watch time categories are mapped correctly.
370 TEST_F(WatchTimeReporterTest, WatchTimeCategoryMapping) { 422 TEST_P(WatchTimeReporterTest, WatchTimeCategoryMapping) {
371 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); 423 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10);
372 424
373 // Verify ac, all, src 425 // Verify ac, all, src
374 EXPECT_CALL(*this, GetCurrentMediaTime()) 426 EXPECT_CALL(*this, GetCurrentMediaTime())
375 .WillOnce(testing::Return(base::TimeDelta())) 427 .WillOnce(testing::Return(base::TimeDelta()))
376 .WillOnce(testing::Return(kWatchTime)); 428 .WillOnce(testing::Return(kWatchTime));
377 Initialize(true, true, false, false, kSizeJustRight); 429 Initialize(true, false, false, kSizeJustRight);
378 wtr_->OnPlaying(); 430 wtr_->OnPlaying();
379 EXPECT_TRUE(IsMonitoring()); 431 EXPECT_TRUE(IsMonitoring());
380 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 432 EXPECT_WATCH_TIME(Ac, kWatchTime);
381 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 433 EXPECT_WATCH_TIME(All, kWatchTime);
382 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime); 434 EXPECT_WATCH_TIME(Src, kWatchTime);
383 EXPECT_WATCH_TIME_FINALIZED(); 435 EXPECT_WATCH_TIME_FINALIZED();
384 wtr_.reset(); 436 wtr_.reset();
385 437
386 // Verify ac, all, mse 438 // Verify ac, all, mse
387 EXPECT_CALL(*this, GetCurrentMediaTime()) 439 EXPECT_CALL(*this, GetCurrentMediaTime())
388 .WillOnce(testing::Return(base::TimeDelta())) 440 .WillOnce(testing::Return(base::TimeDelta()))
389 .WillOnce(testing::Return(kWatchTime)); 441 .WillOnce(testing::Return(kWatchTime));
390 Initialize(true, true, true, false, kSizeJustRight); 442 Initialize(true, true, false, kSizeJustRight);
391 wtr_->OnPlaying(); 443 wtr_->OnPlaying();
392 EXPECT_TRUE(IsMonitoring()); 444 EXPECT_TRUE(IsMonitoring());
393 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 445 EXPECT_WATCH_TIME(Ac, kWatchTime);
394 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 446 EXPECT_WATCH_TIME(All, kWatchTime);
395 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); 447 EXPECT_WATCH_TIME(Mse, kWatchTime);
396 EXPECT_WATCH_TIME_FINALIZED(); 448 EXPECT_WATCH_TIME_FINALIZED();
397 wtr_.reset(); 449 wtr_.reset();
398 450
399 // Verify ac, all, eme, src 451 // Verify ac, all, eme, src
400 EXPECT_CALL(*this, GetCurrentMediaTime()) 452 EXPECT_CALL(*this, GetCurrentMediaTime())
401 .WillOnce(testing::Return(base::TimeDelta())) 453 .WillOnce(testing::Return(base::TimeDelta()))
402 .WillOnce(testing::Return(kWatchTime)); 454 .WillOnce(testing::Return(kWatchTime));
403 Initialize(true, true, false, true, kSizeJustRight); 455 Initialize(true, false, true, kSizeJustRight);
404 wtr_->OnPlaying(); 456 wtr_->OnPlaying();
405 EXPECT_TRUE(IsMonitoring()); 457 EXPECT_TRUE(IsMonitoring());
406 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); 458 EXPECT_WATCH_TIME(Ac, kWatchTime);
407 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 459 EXPECT_WATCH_TIME(All, kWatchTime);
408 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); 460 EXPECT_WATCH_TIME(Eme, kWatchTime);
409 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime); 461 EXPECT_WATCH_TIME(Src, kWatchTime);
410 EXPECT_WATCH_TIME_FINALIZED(); 462 EXPECT_WATCH_TIME_FINALIZED();
411 wtr_.reset(); 463 wtr_.reset();
412 464
413 // Verify all, battery, src 465 // Verify all, battery, src
414 EXPECT_CALL(*this, GetCurrentMediaTime()) 466 EXPECT_CALL(*this, GetCurrentMediaTime())
415 .WillOnce(testing::Return(base::TimeDelta())) 467 .WillOnce(testing::Return(base::TimeDelta()))
416 .WillOnce(testing::Return(kWatchTime)); 468 .WillOnce(testing::Return(kWatchTime));
417 Initialize(true, true, false, false, kSizeJustRight); 469 Initialize(true, false, false, kSizeJustRight);
418 wtr_->OnPlaying(); 470 wtr_->OnPlaying();
419 SetOnBatteryPower(true); 471 SetOnBatteryPower(true);
420 EXPECT_TRUE(IsMonitoring()); 472 EXPECT_TRUE(IsMonitoring());
421 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); 473 EXPECT_WATCH_TIME(All, kWatchTime);
422 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoBattery, kWatchTime); 474 EXPECT_WATCH_TIME(Battery, kWatchTime);
423 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime); 475 EXPECT_WATCH_TIME(Src, kWatchTime);
424 EXPECT_WATCH_TIME_FINALIZED(); 476 EXPECT_WATCH_TIME_FINALIZED();
425 wtr_.reset(); 477 wtr_.reset();
426 } 478 }
427 479
428 TEST_F(WatchTimeReporterTest, PlayPauseHysteresisContinuation) { 480 TEST_P(WatchTimeReporterTest, PlayPauseHysteresisContinuation) {
429 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() { 481 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() {
430 wtr_->OnPaused(); 482 wtr_->OnPaused();
431 wtr_->OnPlaying(); 483 wtr_->OnPlaying();
432 }); 484 });
433 } 485 }
434 486
435 TEST_F(WatchTimeReporterTest, PlayPauseHysteresisFinalized) { 487 TEST_P(WatchTimeReporterTest, PlayPauseHysteresisFinalized) {
436 RunHysteresisTest([this]() { wtr_->OnPaused(); }); 488 RunHysteresisTest([this]() { wtr_->OnPaused(); });
437 } 489 }
438 490
439 TEST_F(WatchTimeReporterTest, OnVolumeChangeHysteresisContinuation) { 491 TEST_P(WatchTimeReporterTest, OnVolumeChangeHysteresisContinuation) {
440 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() { 492 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() {
441 wtr_->OnVolumeChange(0); 493 wtr_->OnVolumeChange(0);
442 wtr_->OnVolumeChange(1); 494 wtr_->OnVolumeChange(1);
443 }); 495 });
444 } 496 }
445 497
446 TEST_F(WatchTimeReporterTest, OnVolumeChangeHysteresisFinalized) { 498 TEST_P(WatchTimeReporterTest, OnVolumeChangeHysteresisFinalized) {
447 RunHysteresisTest([this]() { wtr_->OnVolumeChange(0); }); 499 RunHysteresisTest([this]() { wtr_->OnVolumeChange(0); });
448 } 500 }
449 501
450 TEST_F(WatchTimeReporterTest, OnShownHiddenHysteresisContinuation) { 502 TEST_P(WatchTimeReporterTest, OnShownHiddenHysteresisContinuation) {
503 if (!has_video_)
504 return;
451 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() { 505 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() {
452 wtr_->OnHidden(); 506 wtr_->OnHidden();
453 wtr_->OnShown(); 507 wtr_->OnShown();
454 }); 508 });
455 } 509 }
456 510
457 TEST_F(WatchTimeReporterTest, OnShownHiddenHysteresisFinalized) { 511 TEST_P(WatchTimeReporterTest, OnShownHiddenHysteresisFinalized) {
512 if (!has_video_)
513 return;
458 RunHysteresisTest([this]() { wtr_->OnHidden(); }); 514 RunHysteresisTest([this]() { wtr_->OnHidden(); });
459 } 515 }
460 516
461 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisBatteryContinuation) { 517 TEST_P(WatchTimeReporterTest, OnPowerStateChangeHysteresisBatteryContinuation) {
462 RunHysteresisTest<kAccumulationContinuesAfterTest | 518 RunHysteresisTest<kAccumulationContinuesAfterTest |
463 kFinalizeExitDoesNotRequireCurrentTime | kStartOnBattery>( 519 kFinalizeExitDoesNotRequireCurrentTime | kStartOnBattery>(
464 [this]() { 520 [this]() {
465 OnPowerStateChange(false); 521 OnPowerStateChange(false);
466 OnPowerStateChange(true); 522 OnPowerStateChange(true);
467 }); 523 });
468 } 524 }
469 525
470 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisBatteryFinalized) { 526 TEST_P(WatchTimeReporterTest, OnPowerStateChangeHysteresisBatteryFinalized) {
471 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime | 527 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime |
472 kStartOnBattery>([this]() { OnPowerStateChange(false); }); 528 kStartOnBattery>([this]() { OnPowerStateChange(false); });
473 } 529 }
474 530
475 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisAcContinuation) { 531 TEST_P(WatchTimeReporterTest, OnPowerStateChangeHysteresisAcContinuation) {
476 RunHysteresisTest<kAccumulationContinuesAfterTest | 532 RunHysteresisTest<kAccumulationContinuesAfterTest |
477 kFinalizeExitDoesNotRequireCurrentTime>([this]() { 533 kFinalizeExitDoesNotRequireCurrentTime>([this]() {
478 OnPowerStateChange(true); 534 OnPowerStateChange(true);
479 OnPowerStateChange(false); 535 OnPowerStateChange(false);
480 }); 536 });
481 } 537 }
482 538
483 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisAcFinalized) { 539 TEST_P(WatchTimeReporterTest, OnPowerStateChangeHysteresisAcFinalized) {
484 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime>( 540 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime>(
485 [this]() { OnPowerStateChange(true); }); 541 [this]() { OnPowerStateChange(true); });
486 } 542 }
487 543
488 TEST_F(WatchTimeReporterTest, OnPowerStateChangeBatteryTransitions) { 544 TEST_P(WatchTimeReporterTest, OnPowerStateChangeBatteryTransitions) {
489 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime | 545 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime |
490 kStartOnBattery | kTransitionPowerWatchTime>( 546 kStartOnBattery | kTransitionPowerWatchTime>(
491 [this]() { OnPowerStateChange(false); }); 547 [this]() { OnPowerStateChange(false); });
492 } 548 }
493 549
494 TEST_F(WatchTimeReporterTest, OnPowerStateChangeAcTransitions) { 550 TEST_P(WatchTimeReporterTest, OnPowerStateChangeAcTransitions) {
495 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime | 551 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime |
496 kTransitionPowerWatchTime>( 552 kTransitionPowerWatchTime>(
497 [this]() { OnPowerStateChange(true); }); 553 [this]() { OnPowerStateChange(true); });
498 } 554 }
499 555
500 // Tests that the first finalize is the only one that matters. 556 // Tests that the first finalize is the only one that matters.
501 TEST_F(WatchTimeReporterTest, HysteresisFinalizedWithEarliest) { 557 TEST_P(WatchTimeReporterTest, HysteresisFinalizedWithEarliest) {
502 RunHysteresisTest([this]() { 558 RunHysteresisTest([this]() {
503 wtr_->OnPaused(); 559 wtr_->OnPaused();
504 560
505 // These subsequent "stop events" should do nothing since a finalize time 561 // These subsequent "stop events" should do nothing since a finalize time
506 // has already been selected. 562 // has already been selected.
507 wtr_->OnHidden(); 563 wtr_->OnHidden();
508 wtr_->OnVolumeChange(0); 564 wtr_->OnVolumeChange(0);
509 }); 565 });
510 } 566 }
511 567
512 // Tests that if a stop, stop, start sequence occurs, the middle stop is not 568 // Tests that if a stop, stop, start sequence occurs, the middle stop is not
513 // undone and thus finalize still occurs. 569 // undone and thus finalize still occurs.
514 TEST_F(WatchTimeReporterTest, HysteresisPartialExitStillFinalizes) { 570 TEST_P(WatchTimeReporterTest, HysteresisPartialExitStillFinalizes) {
515 auto stop_event = [this](size_t i) { 571 auto stop_event = [this](size_t i) {
516 if (i == 0) 572 if (i == 0) {
517 wtr_->OnPaused(); 573 wtr_->OnPaused();
518 else if (i == 1) 574 } else if (i == 1) {
575 wtr_->OnVolumeChange(0);
576 } else {
577 ASSERT_TRUE(has_video_);
519 wtr_->OnHidden(); 578 wtr_->OnHidden();
520 else 579 }
521 wtr_->OnVolumeChange(0);
522 }; 580 };
523 581
524 auto start_event = [this](size_t i) { 582 auto start_event = [this](size_t i) {
525 if (i == 0) 583 if (i == 0) {
526 wtr_->OnPlaying(); 584 wtr_->OnPlaying();
527 else if (i == 1) 585 } else if (i == 1) {
586 wtr_->OnVolumeChange(1);
587 } else {
588 ASSERT_TRUE(has_video_);
528 wtr_->OnShown(); 589 wtr_->OnShown();
529 else 590 }
530 wtr_->OnVolumeChange(1);
531 }; 591 };
532 592
533 for (size_t i = 0; i < 3; ++i) { 593 const size_t kTestSize = has_video_ ? 3 : 2;
534 for (size_t j = 0; j < 3; ++j) { 594 for (size_t i = 0; i < kTestSize; ++i) {
595 for (size_t j = 0; j < kTestSize; ++j) {
535 if (i == j) 596 if (i == j)
536 continue; 597 continue;
537 598
538 RunHysteresisTest<kFinalizeInterleavedStartEvent>( 599 RunHysteresisTest<kFinalizeInterleavedStartEvent>(
539 [i, j, start_event, stop_event]() { 600 [i, j, start_event, stop_event]() {
540 stop_event(i); 601 stop_event(i);
541 stop_event(j); 602 stop_event(j);
542 start_event(i); 603 start_event(i);
543 }); 604 });
544 } 605 }
545 } 606 }
546 } 607 }
547 608
609 INSTANTIATE_TEST_CASE_P(WatchTimeReporterTest,
610 WatchTimeReporterTest,
611 testing::Values(true, false));
612
548 } // namespace media 613 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/watch_time_reporter.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698