| OLD | NEW |
| 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 do { \ | 21 do { \ |
| 22 EXPECT_CALL( \ | 22 EXPECT_CALL( \ |
| 23 *media_log_, \ | 23 *media_log_, \ |
| 24 OnWatchTimeUpdate(has_video_ ? MediaLog::kWatchTimeAudioVideo##key \ | 24 OnWatchTimeUpdate(has_video_ ? MediaLog::kWatchTimeAudioVideo##key \ |
| 25 : MediaLog::kWatchTimeAudio##key, \ | 25 : MediaLog::kWatchTimeAudio##key, \ |
| 26 value)) \ | 26 value)) \ |
| 27 .RetiresOnSaturation(); \ | 27 .RetiresOnSaturation(); \ |
| 28 } while (0) | 28 } while (0) |
| 29 | 29 |
| 30 #define EXPECT_BACKGROUND_WATCH_TIME(key, value) \ |
| 31 do { \ |
| 32 DCHECK(has_video_); \ |
| 33 EXPECT_CALL(*media_log_, \ |
| 34 OnWatchTimeUpdate( \ |
| 35 MediaLog::kWatchTimeAudioVideoBackground##key, value)) \ |
| 36 .RetiresOnSaturation(); \ |
| 37 } while (0) |
| 38 |
| 30 #define EXPECT_WATCH_TIME_FINALIZED() \ | 39 #define EXPECT_WATCH_TIME_FINALIZED() \ |
| 31 EXPECT_CALL(*media_log_, OnWatchTimeFinalized()).RetiresOnSaturation(); | 40 EXPECT_CALL(*media_log_, OnWatchTimeFinalized()).RetiresOnSaturation(); |
| 32 | 41 |
| 33 #define EXPECT_POWER_WATCH_TIME_FINALIZED() \ | 42 #define EXPECT_POWER_WATCH_TIME_FINALIZED() \ |
| 34 EXPECT_CALL(*media_log_, OnPowerWatchTimeFinalized()).RetiresOnSaturation(); | 43 EXPECT_CALL(*media_log_, OnPowerWatchTimeFinalized()).RetiresOnSaturation(); |
| 35 | 44 |
| 36 class WatchTimeReporterTest : public testing::TestWithParam<bool> { | 45 class WatchTimeReporterTest : public testing::TestWithParam<bool> { |
| 37 public: | 46 public: |
| 38 WatchTimeReporterTest() | 47 WatchTimeReporterTest() |
| 39 : has_video_(GetParam()), | 48 : has_video_(GetParam()), |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 kStartOnBattery = 16, | 142 kStartOnBattery = 16, |
| 134 | 143 |
| 135 // Indicates an extra start event may be generated during test execution. | 144 // Indicates an extra start event may be generated during test execution. |
| 136 kFinalizeInterleavedStartEvent = 32, | 145 kFinalizeInterleavedStartEvent = 32, |
| 137 }; | 146 }; |
| 138 | 147 |
| 139 template <int TestFlags = 0, typename HysteresisTestCallback> | 148 template <int TestFlags = 0, typename HysteresisTestCallback> |
| 140 void RunHysteresisTest(HysteresisTestCallback test_callback_func) { | 149 void RunHysteresisTest(HysteresisTestCallback test_callback_func) { |
| 141 Initialize(true, false, false, kSizeJustRight); | 150 Initialize(true, false, false, kSizeJustRight); |
| 142 | 151 |
| 152 // Disable background reporting for the hysteresis tests. |
| 153 wtr_->background_reporter_.reset(); |
| 154 |
| 143 // Setup all current time expectations first since they need to use the | 155 // Setup all current time expectations first since they need to use the |
| 144 // InSequence macro for ease of use, but we don't want the watch time | 156 // InSequence macro for ease of use, but we don't want the watch time |
| 145 // expectations to be in sequence (or expectations would depend on sorted | 157 // expectations to be in sequence (or expectations would depend on sorted |
| 146 // order of histogram names). | 158 // order of histogram names). |
| 147 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(10); | 159 constexpr base::TimeDelta kWatchTime1 = base::TimeDelta::FromSeconds(10); |
| 148 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(12); | 160 constexpr base::TimeDelta kWatchTime2 = base::TimeDelta::FromSeconds(12); |
| 149 constexpr base::TimeDelta kWatchTime3 = base::TimeDelta::FromSeconds(15); | 161 constexpr base::TimeDelta kWatchTime3 = base::TimeDelta::FromSeconds(15); |
| 150 constexpr base::TimeDelta kWatchTime4 = base::TimeDelta::FromSeconds(30); | 162 constexpr base::TimeDelta kWatchTime4 = base::TimeDelta::FromSeconds(30); |
| 151 { | 163 { |
| 152 testing::InSequence s; | 164 testing::InSequence s; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 EXPECT_WATCH_TIME(Ac, kWatchTimeLate); | 322 EXPECT_WATCH_TIME(Ac, kWatchTimeLate); |
| 311 EXPECT_WATCH_TIME(All, kWatchTimeLate); | 323 EXPECT_WATCH_TIME(All, kWatchTimeLate); |
| 312 EXPECT_WATCH_TIME(Eme, kWatchTimeLate); | 324 EXPECT_WATCH_TIME(Eme, kWatchTimeLate); |
| 313 EXPECT_WATCH_TIME(Mse, kWatchTimeLate); | 325 EXPECT_WATCH_TIME(Mse, kWatchTimeLate); |
| 314 CycleReportingTimer(); | 326 CycleReportingTimer(); |
| 315 } | 327 } |
| 316 | 328 |
| 317 // Tests that basic reporting for the all category works. | 329 // Tests that basic reporting for the all category works. |
| 318 TEST_P(WatchTimeReporterTest, WatchTimeReporterShownHidden) { | 330 TEST_P(WatchTimeReporterTest, WatchTimeReporterShownHidden) { |
| 319 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(8); | 331 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(8); |
| 320 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10); | 332 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(25); |
| 321 EXPECT_CALL(*this, GetCurrentMediaTime()) | 333 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 322 .WillOnce(testing::Return(base::TimeDelta())) | 334 .WillOnce(testing::Return(base::TimeDelta())) |
| 323 .WillOnce(testing::Return(kWatchTimeEarly)) | 335 .WillOnce(testing::Return(kWatchTimeEarly)) |
| 336 .WillOnce(testing::Return(kWatchTimeEarly)) |
| 324 .WillRepeatedly(testing::Return(kWatchTimeLate)); | 337 .WillRepeatedly(testing::Return(kWatchTimeLate)); |
| 325 Initialize(true, true, true, kSizeJustRight); | 338 Initialize(true, true, true, kSizeJustRight); |
| 326 wtr_->OnPlaying(); | 339 wtr_->OnPlaying(); |
| 327 EXPECT_TRUE(IsMonitoring()); | 340 EXPECT_TRUE(IsMonitoring()); |
| 328 | 341 |
| 329 // If we have video, this will halt watch time collection, if only audio it | 342 // If we have video, this will halt watch time collection, if only audio it |
| 330 // will do nothing. Consume the expectation if audio only. | 343 // will do nothing. Consume the expectations if audio only. |
| 331 wtr_->OnHidden(); | 344 wtr_->OnHidden(); |
| 332 if (!has_video_) | 345 if (!has_video_) { |
| 333 GetCurrentMediaTime(); | 346 GetCurrentMediaTime(); |
| 347 GetCurrentMediaTime(); |
| 348 } else { |
| 349 const base::TimeDelta kExpectedWatchTime = kWatchTimeLate - kWatchTimeEarly; |
| 350 EXPECT_BACKGROUND_WATCH_TIME(Ac, kExpectedWatchTime); |
| 351 EXPECT_BACKGROUND_WATCH_TIME(All, kExpectedWatchTime); |
| 352 EXPECT_BACKGROUND_WATCH_TIME(Eme, kExpectedWatchTime); |
| 353 EXPECT_BACKGROUND_WATCH_TIME(Mse, kExpectedWatchTime); |
| 354 EXPECT_WATCH_TIME_FINALIZED(); |
| 355 } |
| 334 | 356 |
| 335 const base::TimeDelta kExpectedWatchTime = | 357 const base::TimeDelta kExpectedWatchTime = |
| 336 has_video_ ? kWatchTimeEarly : kWatchTimeLate; | 358 has_video_ ? kWatchTimeEarly : kWatchTimeLate; |
| 337 EXPECT_WATCH_TIME(Ac, kExpectedWatchTime); | 359 EXPECT_WATCH_TIME(Ac, kExpectedWatchTime); |
| 338 EXPECT_WATCH_TIME(All, kExpectedWatchTime); | 360 EXPECT_WATCH_TIME(All, kExpectedWatchTime); |
| 339 EXPECT_WATCH_TIME(Eme, kExpectedWatchTime); | 361 EXPECT_WATCH_TIME(Eme, kExpectedWatchTime); |
| 340 EXPECT_WATCH_TIME(Mse, kExpectedWatchTime); | 362 EXPECT_WATCH_TIME(Mse, kExpectedWatchTime); |
| 341 EXPECT_WATCH_TIME_FINALIZED(); | 363 EXPECT_WATCH_TIME_FINALIZED(); |
| 342 wtr_.reset(); | 364 wtr_.reset(); |
| 343 } | 365 } |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 }); | 626 }); |
| 605 } | 627 } |
| 606 } | 628 } |
| 607 } | 629 } |
| 608 | 630 |
| 609 INSTANTIATE_TEST_CASE_P(WatchTimeReporterTest, | 631 INSTANTIATE_TEST_CASE_P(WatchTimeReporterTest, |
| 610 WatchTimeReporterTest, | 632 WatchTimeReporterTest, |
| 611 testing::Values(true, false)); | 633 testing::Values(true, false)); |
| 612 | 634 |
| 613 } // namespace media | 635 } // namespace media |
| OLD | NEW |