OLD | NEW |
(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/test_message_loop.h" |
| 11 #include "media/base/mock_media_log.h" |
| 12 #include "media/blink/watch_time_reporter.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 constexpr gfx::Size kSizeJustRight = gfx::Size(201, 201); |
| 19 |
| 20 #define EXPECT_WATCH_TIME(key, value) \ |
| 21 EXPECT_CALL(*media_log_, OnWatchTimeUpdate(key, value)).RetiresOnSaturation(); |
| 22 |
| 23 #define EXPECT_WATCH_TIME_FINALIZED() \ |
| 24 EXPECT_CALL(*media_log_, OnWatchTimeFinalized()).RetiresOnSaturation(); |
| 25 |
| 26 #define EXPECT_POWER_WATCH_TIME_FINALIZED() \ |
| 27 EXPECT_CALL(*media_log_, OnPowerWatchTimeFinalized()).RetiresOnSaturation(); |
| 28 |
| 29 class WatchTimeReporterTest : public testing::Test { |
| 30 public: |
| 31 WatchTimeReporterTest() |
| 32 : media_log_(new testing::StrictMock<WatchTimeLogMonitor>()) {} |
| 33 ~WatchTimeReporterTest() override {} |
| 34 |
| 35 protected: |
| 36 class WatchTimeLogMonitor : public MediaLog { |
| 37 public: |
| 38 WatchTimeLogMonitor() {} |
| 39 |
| 40 void AddEvent(std::unique_ptr<MediaLogEvent> event) override { |
| 41 ASSERT_EQ(event->type, MediaLogEvent::Type::WATCH_TIME_UPDATE); |
| 42 |
| 43 for (base::DictionaryValue::Iterator it(event->params); !it.IsAtEnd(); |
| 44 it.Advance()) { |
| 45 bool finalize; |
| 46 if (it.value().GetAsBoolean(&finalize)) { |
| 47 if (it.key() == MediaLog::kWatchTimeFinalize) |
| 48 OnWatchTimeFinalized(); |
| 49 else |
| 50 OnPowerWatchTimeFinalized(); |
| 51 continue; |
| 52 } |
| 53 |
| 54 double in_seconds; |
| 55 ASSERT_TRUE(it.value().GetAsDouble(&in_seconds)); |
| 56 OnWatchTimeUpdate(it.key(), base::TimeDelta::FromSecondsD(in_seconds)); |
| 57 } |
| 58 } |
| 59 |
| 60 MOCK_METHOD0(OnWatchTimeFinalized, void(void)); |
| 61 MOCK_METHOD0(OnPowerWatchTimeFinalized, void(void)); |
| 62 MOCK_METHOD2(OnWatchTimeUpdate, void(const std::string&, base::TimeDelta)); |
| 63 |
| 64 protected: |
| 65 ~WatchTimeLogMonitor() override {} |
| 66 |
| 67 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(WatchTimeLogMonitor); |
| 69 }; |
| 70 |
| 71 void Initialize(bool has_audio, |
| 72 bool has_video, |
| 73 bool is_mse, |
| 74 bool is_encrypted, |
| 75 const gfx::Size& initial_video_size) { |
| 76 wtr_.reset(new WatchTimeReporter( |
| 77 has_audio, has_video, is_mse, is_encrypted, media_log_, |
| 78 initial_video_size, |
| 79 base::Bind(&WatchTimeReporterTest::GetCurrentMediaTime, |
| 80 base::Unretained(this)))); |
| 81 |
| 82 // Setup the reporting interval to be immediate to avoid spinning real time |
| 83 // within the unit test. |
| 84 wtr_->reporting_interval_ = base::TimeDelta(); |
| 85 } |
| 86 |
| 87 void CycleReportingTimer() { |
| 88 base::RunLoop run_loop; |
| 89 message_loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 90 run_loop.Run(); |
| 91 } |
| 92 |
| 93 bool IsMonitoring() { return wtr_->reporting_timer_.IsRunning(); } |
| 94 |
| 95 // We call directly into the reporter for this instead of using an actual |
| 96 // PowerMonitorTestSource since that results in a posted tasks which interfere |
| 97 // with our ability to test the timer. |
| 98 void SetOnBatteryPower(bool on_battery_power) { |
| 99 wtr_->is_on_battery_power_ = on_battery_power; |
| 100 } |
| 101 |
| 102 void OnPowerStateChange(bool on_battery_power) { |
| 103 wtr_->OnPowerStateChange(on_battery_power); |
| 104 } |
| 105 |
| 106 enum { |
| 107 // After |test_callback_func| is executed, should watch time continue to |
| 108 // accumulate? |
| 109 kAccumulationContinuesAfterTest = 1, |
| 110 |
| 111 // |test_callback_func| for hysteresis tests enters and exits finalize mode |
| 112 // for watch time, not all exits require a new current time update. |
| 113 kFinalizeExitDoesNotRequireCurrentTime = 2, |
| 114 |
| 115 // During finalize the watch time should not continue on the starting power |
| 116 // metric. By default this means the AC metric will be finalized, but if |
| 117 // used with |kStartOnBattery| it will be the battery metric. |
| 118 kFinalizePowerWatchTime = 4, |
| 119 |
| 120 // During finalize the watch time should continue on the metric opposite the |
| 121 // starting metric (by default it's AC, it's battery if |kStartOnBattery| is |
| 122 // specified. |
| 123 kTransitionPowerWatchTime = 8, |
| 124 |
| 125 // Indicates that power watch time should be reported to the battery metric. |
| 126 kStartOnBattery = 16, |
| 127 |
| 128 // Indicates an extra start event may be generated during test execution. |
| 129 kFinalizeInterleavedStartEvent = 32, |
| 130 }; |
| 131 |
| 132 template <int TestFlags = 0, typename HysteresisTestCallback> |
| 133 void RunHysteresisTest(HysteresisTestCallback test_callback_func) { |
| 134 Initialize(true, true, false, false, kSizeJustRight); |
| 135 |
| 136 // 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 |
| 138 // expectations to be in sequence (or expectations would depend on sorted |
| 139 // order of histogram names). |
| 140 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(10); |
| 141 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(12); |
| 142 constexpr base::TimeDelta kWatchTime3 = base::TimeDelta::FromSeconds(15); |
| 143 constexpr base::TimeDelta kWatchTime4 = base::TimeDelta::FromSeconds(30); |
| 144 { |
| 145 testing::InSequence s; |
| 146 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 147 .WillOnce(testing::Return(base::TimeDelta())) |
| 148 .WillOnce(testing::Return(kWatchTime1)); |
| 149 |
| 150 // Setup conditions depending on if the test will not resume watch time |
| 151 // accumulation or not; i.e. the finalize criteria will not be undone |
| 152 // within the hysteresis time. |
| 153 if (TestFlags & kAccumulationContinuesAfterTest) { |
| 154 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 155 .Times(TestFlags & (kFinalizeExitDoesNotRequireCurrentTime | |
| 156 kFinalizePowerWatchTime) |
| 157 ? 1 |
| 158 : 2) |
| 159 .WillRepeatedly(testing::Return(kWatchTime2)); |
| 160 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 161 .WillOnce(testing::Return(kWatchTime3)); |
| 162 } else { |
| 163 // Current time should be requested when entering the finalize state. |
| 164 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 165 .Times(TestFlags & kFinalizeInterleavedStartEvent ? 2 : 1) |
| 166 .WillRepeatedly(testing::Return(kWatchTime2)); |
| 167 } |
| 168 |
| 169 if (TestFlags & kTransitionPowerWatchTime) { |
| 170 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 171 .WillOnce(testing::Return(kWatchTime4)); |
| 172 } |
| 173 } |
| 174 |
| 175 wtr_->OnPlaying(); |
| 176 EXPECT_TRUE(IsMonitoring()); |
| 177 if (TestFlags & kStartOnBattery) |
| 178 SetOnBatteryPower(true); |
| 179 else |
| 180 ASSERT_FALSE(wtr_->is_on_battery_power_); |
| 181 |
| 182 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime1); |
| 183 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime1); |
| 184 EXPECT_WATCH_TIME(TestFlags & kStartOnBattery |
| 185 ? MediaLog::kWatchTimeAudioVideoBattery |
| 186 : MediaLog::kWatchTimeAudioVideoAc, |
| 187 kWatchTime1); |
| 188 CycleReportingTimer(); |
| 189 |
| 190 // Invoke the test. |
| 191 test_callback_func(); |
| 192 |
| 193 const base::TimeDelta kExpectedWatchTime = |
| 194 TestFlags & kAccumulationContinuesAfterTest ? kWatchTime3 : kWatchTime2; |
| 195 |
| 196 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kExpectedWatchTime); |
| 197 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kExpectedWatchTime); |
| 198 EXPECT_WATCH_TIME( |
| 199 TestFlags & kStartOnBattery ? MediaLog::kWatchTimeAudioVideoBattery |
| 200 : MediaLog::kWatchTimeAudioVideoAc, |
| 201 TestFlags & kFinalizePowerWatchTime ? kWatchTime2 : kExpectedWatchTime); |
| 202 |
| 203 // If we're not testing battery watch time, this is the end of the test. |
| 204 if (!(TestFlags & kTransitionPowerWatchTime)) { |
| 205 EXPECT_WATCH_TIME_FINALIZED(); |
| 206 wtr_.reset(); |
| 207 return; |
| 208 } |
| 209 |
| 210 ASSERT_TRUE(TestFlags & kAccumulationContinuesAfterTest) |
| 211 << "kTransitionPowerWatchTime tests must be done with " |
| 212 "kAccumulationContinuesAfterTest"; |
| 213 |
| 214 EXPECT_POWER_WATCH_TIME_FINALIZED(); |
| 215 CycleReportingTimer(); |
| 216 |
| 217 // 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 |
| 219 // on battery we'll now record one for ac and vice versa. |
| 220 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime4); |
| 221 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime4); |
| 222 EXPECT_WATCH_TIME(TestFlags & kStartOnBattery |
| 223 ? MediaLog::kWatchTimeAudioVideoAc |
| 224 : MediaLog::kWatchTimeAudioVideoBattery, |
| 225 kWatchTime4 - kWatchTime2); |
| 226 EXPECT_WATCH_TIME_FINALIZED(); |
| 227 wtr_.reset(); |
| 228 } |
| 229 |
| 230 MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta()); |
| 231 |
| 232 base::TestMessageLoop message_loop_; |
| 233 scoped_refptr<testing::StrictMock<WatchTimeLogMonitor>> media_log_; |
| 234 std::unique_ptr<WatchTimeReporter> wtr_; |
| 235 |
| 236 private: |
| 237 DISALLOW_COPY_AND_ASSIGN(WatchTimeReporterTest); |
| 238 }; |
| 239 |
| 240 // Tests that watch time reporting is appropriately enabled or disabled. |
| 241 TEST_F(WatchTimeReporterTest, WatchTimeReporter) { |
| 242 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 243 .WillRepeatedly(testing::Return(base::TimeDelta())); |
| 244 |
| 245 Initialize(false, true, true, true, kSizeJustRight); |
| 246 wtr_->OnPlaying(); |
| 247 EXPECT_FALSE(IsMonitoring()); |
| 248 |
| 249 Initialize(true, false, true, true, kSizeJustRight); |
| 250 wtr_->OnPlaying(); |
| 251 EXPECT_FALSE(IsMonitoring()); |
| 252 |
| 253 constexpr gfx::Size kSizeTooSmall = gfx::Size(100, 100); |
| 254 Initialize(true, true, true, true, kSizeTooSmall); |
| 255 wtr_->OnPlaying(); |
| 256 EXPECT_FALSE(IsMonitoring()); |
| 257 |
| 258 Initialize(true, true, true, true, kSizeJustRight); |
| 259 wtr_->OnPlaying(); |
| 260 EXPECT_TRUE(IsMonitoring()); |
| 261 |
| 262 Initialize(true, true, false, false, kSizeJustRight); |
| 263 wtr_->OnPlaying(); |
| 264 EXPECT_TRUE(IsMonitoring()); |
| 265 |
| 266 Initialize(true, true, true, false, kSizeJustRight); |
| 267 wtr_->OnPlaying(); |
| 268 EXPECT_TRUE(IsMonitoring()); |
| 269 } |
| 270 |
| 271 // Tests that basic reporting for the all category works. |
| 272 TEST_F(WatchTimeReporterTest, WatchTimeReporterBasic) { |
| 273 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5); |
| 274 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10); |
| 275 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 276 .WillOnce(testing::Return(base::TimeDelta())) |
| 277 .WillOnce(testing::Return(kWatchTimeEarly)) |
| 278 .WillRepeatedly(testing::Return(kWatchTimeLate)); |
| 279 Initialize(true, true, true, true, kSizeJustRight); |
| 280 wtr_->OnPlaying(); |
| 281 EXPECT_TRUE(IsMonitoring()); |
| 282 |
| 283 // No log should have been generated yet since the message loop has not had |
| 284 // any chance to pump. |
| 285 CycleReportingTimer(); |
| 286 |
| 287 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTimeLate); |
| 288 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTimeLate); |
| 289 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTimeLate); |
| 290 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTimeLate); |
| 291 CycleReportingTimer(); |
| 292 } |
| 293 // Tests that starting from a non-zero base works. |
| 294 TEST_F(WatchTimeReporterTest, WatchTimeReporterNonZeroStart) { |
| 295 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(5); |
| 296 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(15); |
| 297 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 298 .WillOnce(testing::Return(kWatchTime1)) |
| 299 .WillRepeatedly(testing::Return(kWatchTime2)); |
| 300 Initialize(true, true, true, true, kSizeJustRight); |
| 301 wtr_->OnPlaying(); |
| 302 EXPECT_TRUE(IsMonitoring()); |
| 303 |
| 304 const base::TimeDelta kWatchTime = kWatchTime2 - kWatchTime1; |
| 305 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 306 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 307 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); |
| 308 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); |
| 309 CycleReportingTimer(); |
| 310 } |
| 311 |
| 312 // Tests that seeking causes an immediate finalization. |
| 313 TEST_F(WatchTimeReporterTest, SeekFinalizes) { |
| 314 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); |
| 315 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 316 .WillOnce(testing::Return(base::TimeDelta())) |
| 317 .WillOnce(testing::Return(kWatchTime)); |
| 318 Initialize(true, true, true, true, kSizeJustRight); |
| 319 wtr_->OnPlaying(); |
| 320 EXPECT_TRUE(IsMonitoring()); |
| 321 |
| 322 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 323 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 324 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); |
| 325 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); |
| 326 EXPECT_WATCH_TIME_FINALIZED(); |
| 327 wtr_->OnSeeking(); |
| 328 } |
| 329 |
| 330 // Tests that seeking causes an immediate finalization, but does not trample a |
| 331 // previously set finalize time. |
| 332 TEST_F(WatchTimeReporterTest, SeekFinalizeDoesNotTramplePreviousFinalize) { |
| 333 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); |
| 334 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 335 .WillOnce(testing::Return(base::TimeDelta())) |
| 336 .WillOnce(testing::Return(kWatchTime)); |
| 337 Initialize(true, true, true, true, kSizeJustRight); |
| 338 wtr_->OnPlaying(); |
| 339 EXPECT_TRUE(IsMonitoring()); |
| 340 |
| 341 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 342 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 343 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); |
| 344 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); |
| 345 EXPECT_WATCH_TIME_FINALIZED(); |
| 346 wtr_->OnPaused(); |
| 347 wtr_->OnSeeking(); |
| 348 } |
| 349 |
| 350 // Tests that watch time is finalized upon destruction. |
| 351 TEST_F(WatchTimeReporterTest, WatchTimeReporterFinalizeOnDestruction) { |
| 352 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); |
| 353 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 354 .WillOnce(testing::Return(base::TimeDelta())) |
| 355 .WillOnce(testing::Return(kWatchTime)); |
| 356 Initialize(true, true, true, true, kSizeJustRight); |
| 357 wtr_->OnPlaying(); |
| 358 EXPECT_TRUE(IsMonitoring()); |
| 359 |
| 360 // Finalize the histogram before any cycles of the timer have run. |
| 361 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 362 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 363 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); |
| 364 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); |
| 365 EXPECT_WATCH_TIME_FINALIZED(); |
| 366 wtr_.reset(); |
| 367 } |
| 368 |
| 369 // Tests that watch time categories are mapped correctly. |
| 370 TEST_F(WatchTimeReporterTest, WatchTimeCategoryMapping) { |
| 371 constexpr base::TimeDelta kWatchTime = base::TimeDelta::FromSeconds(10); |
| 372 |
| 373 // Verify ac, all, src |
| 374 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 375 .WillOnce(testing::Return(base::TimeDelta())) |
| 376 .WillOnce(testing::Return(kWatchTime)); |
| 377 Initialize(true, true, false, false, kSizeJustRight); |
| 378 wtr_->OnPlaying(); |
| 379 EXPECT_TRUE(IsMonitoring()); |
| 380 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 381 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 382 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime); |
| 383 EXPECT_WATCH_TIME_FINALIZED(); |
| 384 wtr_.reset(); |
| 385 |
| 386 // Verify ac, all, mse |
| 387 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 388 .WillOnce(testing::Return(base::TimeDelta())) |
| 389 .WillOnce(testing::Return(kWatchTime)); |
| 390 Initialize(true, true, true, false, kSizeJustRight); |
| 391 wtr_->OnPlaying(); |
| 392 EXPECT_TRUE(IsMonitoring()); |
| 393 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 394 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 395 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoMse, kWatchTime); |
| 396 EXPECT_WATCH_TIME_FINALIZED(); |
| 397 wtr_.reset(); |
| 398 |
| 399 // Verify ac, all, eme, src |
| 400 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 401 .WillOnce(testing::Return(base::TimeDelta())) |
| 402 .WillOnce(testing::Return(kWatchTime)); |
| 403 Initialize(true, true, false, true, kSizeJustRight); |
| 404 wtr_->OnPlaying(); |
| 405 EXPECT_TRUE(IsMonitoring()); |
| 406 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAc, kWatchTime); |
| 407 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 408 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoEme, kWatchTime); |
| 409 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime); |
| 410 EXPECT_WATCH_TIME_FINALIZED(); |
| 411 wtr_.reset(); |
| 412 |
| 413 // Verify all, battery, src |
| 414 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 415 .WillOnce(testing::Return(base::TimeDelta())) |
| 416 .WillOnce(testing::Return(kWatchTime)); |
| 417 Initialize(true, true, false, false, kSizeJustRight); |
| 418 wtr_->OnPlaying(); |
| 419 SetOnBatteryPower(true); |
| 420 EXPECT_TRUE(IsMonitoring()); |
| 421 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoAll, kWatchTime); |
| 422 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoBattery, kWatchTime); |
| 423 EXPECT_WATCH_TIME(MediaLog::kWatchTimeAudioVideoSrc, kWatchTime); |
| 424 EXPECT_WATCH_TIME_FINALIZED(); |
| 425 wtr_.reset(); |
| 426 } |
| 427 |
| 428 TEST_F(WatchTimeReporterTest, PlayPauseHysteresisContinuation) { |
| 429 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() { |
| 430 wtr_->OnPaused(); |
| 431 wtr_->OnPlaying(); |
| 432 }); |
| 433 } |
| 434 |
| 435 TEST_F(WatchTimeReporterTest, PlayPauseHysteresisFinalized) { |
| 436 RunHysteresisTest([this]() { wtr_->OnPaused(); }); |
| 437 } |
| 438 |
| 439 TEST_F(WatchTimeReporterTest, OnVolumeChangeHysteresisContinuation) { |
| 440 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() { |
| 441 wtr_->OnVolumeChange(0); |
| 442 wtr_->OnVolumeChange(1); |
| 443 }); |
| 444 } |
| 445 |
| 446 TEST_F(WatchTimeReporterTest, OnVolumeChangeHysteresisFinalized) { |
| 447 RunHysteresisTest([this]() { wtr_->OnVolumeChange(0); }); |
| 448 } |
| 449 |
| 450 TEST_F(WatchTimeReporterTest, OnShownHiddenHysteresisContinuation) { |
| 451 RunHysteresisTest<kAccumulationContinuesAfterTest>([this]() { |
| 452 wtr_->OnHidden(); |
| 453 wtr_->OnShown(); |
| 454 }); |
| 455 } |
| 456 |
| 457 TEST_F(WatchTimeReporterTest, OnShownHiddenHysteresisFinalized) { |
| 458 RunHysteresisTest([this]() { wtr_->OnHidden(); }); |
| 459 } |
| 460 |
| 461 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisBatteryContinuation) { |
| 462 RunHysteresisTest<kAccumulationContinuesAfterTest | |
| 463 kFinalizeExitDoesNotRequireCurrentTime | kStartOnBattery>( |
| 464 [this]() { |
| 465 OnPowerStateChange(false); |
| 466 OnPowerStateChange(true); |
| 467 }); |
| 468 } |
| 469 |
| 470 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisBatteryFinalized) { |
| 471 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime | |
| 472 kStartOnBattery>([this]() { OnPowerStateChange(false); }); |
| 473 } |
| 474 |
| 475 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisAcContinuation) { |
| 476 RunHysteresisTest<kAccumulationContinuesAfterTest | |
| 477 kFinalizeExitDoesNotRequireCurrentTime>([this]() { |
| 478 OnPowerStateChange(true); |
| 479 OnPowerStateChange(false); |
| 480 }); |
| 481 } |
| 482 |
| 483 TEST_F(WatchTimeReporterTest, OnPowerStateChangeHysteresisAcFinalized) { |
| 484 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime>( |
| 485 [this]() { OnPowerStateChange(true); }); |
| 486 } |
| 487 |
| 488 TEST_F(WatchTimeReporterTest, OnPowerStateChangeBatteryTransitions) { |
| 489 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime | |
| 490 kStartOnBattery | kTransitionPowerWatchTime>( |
| 491 [this]() { OnPowerStateChange(false); }); |
| 492 } |
| 493 |
| 494 TEST_F(WatchTimeReporterTest, OnPowerStateChangeAcTransitions) { |
| 495 RunHysteresisTest<kAccumulationContinuesAfterTest | kFinalizePowerWatchTime | |
| 496 kTransitionPowerWatchTime>( |
| 497 [this]() { OnPowerStateChange(true); }); |
| 498 } |
| 499 |
| 500 // Tests that the first finalize is the only one that matters. |
| 501 TEST_F(WatchTimeReporterTest, HysteresisFinalizedWithEarliest) { |
| 502 RunHysteresisTest([this]() { |
| 503 wtr_->OnPaused(); |
| 504 |
| 505 // These subsequent "stop events" should do nothing since a finalize time |
| 506 // has already been selected. |
| 507 wtr_->OnHidden(); |
| 508 wtr_->OnVolumeChange(0); |
| 509 }); |
| 510 } |
| 511 |
| 512 // Tests that if a stop, stop, start sequence occurs, the middle stop is not |
| 513 // undone and thus finalize still occurs. |
| 514 TEST_F(WatchTimeReporterTest, HysteresisPartialExitStillFinalizes) { |
| 515 auto stop_event = [this](size_t i) { |
| 516 if (i == 0) |
| 517 wtr_->OnPaused(); |
| 518 else if (i == 1) |
| 519 wtr_->OnHidden(); |
| 520 else |
| 521 wtr_->OnVolumeChange(0); |
| 522 }; |
| 523 |
| 524 auto start_event = [this](size_t i) { |
| 525 if (i == 0) |
| 526 wtr_->OnPlaying(); |
| 527 else if (i == 1) |
| 528 wtr_->OnShown(); |
| 529 else |
| 530 wtr_->OnVolumeChange(1); |
| 531 }; |
| 532 |
| 533 for (size_t i = 0; i < 3; ++i) { |
| 534 for (size_t j = 0; j < 3; ++j) { |
| 535 if (i == j) |
| 536 continue; |
| 537 |
| 538 RunHysteresisTest<kFinalizeInterleavedStartEvent>( |
| 539 [i, j, start_event, stop_event]() { |
| 540 stop_event(i); |
| 541 stop_event(j); |
| 542 start_event(i); |
| 543 }); |
| 544 } |
| 545 } |
| 546 } |
| 547 |
| 548 } // namespace media |
OLD | NEW |