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/histogram_tester.h" |
| 11 #include "base/test/power_monitor_test_base.h" |
| 12 #include "base/test/test_message_loop.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 class WatchTimeReporterTest : public testing::Test { |
| 22 public: |
| 23 WatchTimeReporterTest() |
| 24 : test_power_source_(new base::PowerMonitorTestSource()), |
| 25 test_power_monitor_(new base::PowerMonitor( |
| 26 std::unique_ptr<base::PowerMonitorSource>(test_power_source_))) {} |
| 27 |
| 28 ~WatchTimeReporterTest() override {} |
| 29 |
| 30 protected: |
| 31 void Initialize(bool has_audio, |
| 32 bool has_video, |
| 33 bool is_mse, |
| 34 bool is_encrypted, |
| 35 const gfx::Size& initial_video_size) { |
| 36 wtr_.reset(new WatchTimeReporter( |
| 37 has_audio, has_video, is_mse, is_encrypted, initial_video_size, |
| 38 base::Bind(&WatchTimeReporterTest::GetCurrentMediaTime, |
| 39 base::Unretained(this)))); |
| 40 // Setup the reporting interval to be immediate to avoid spinning real time |
| 41 // within the unit test. |
| 42 wtr_->reporting_interval_ = base::TimeDelta(); |
| 43 } |
| 44 |
| 45 void CycleReportingTimer() { |
| 46 base::RunLoop run_loop; |
| 47 message_loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 48 run_loop.Run(); |
| 49 } |
| 50 |
| 51 base::TimeDelta GetReportedWatchTime(const char* histogram_name) { |
| 52 std::unique_ptr<base::HistogramSamples> samples = |
| 53 histograms_.GetHistogramSamplesSinceCreation(histogram_name); |
| 54 return samples->TotalCount() |
| 55 ? base::TimeDelta::FromMilliseconds(samples->sum()) |
| 56 : kNoTimestamp; |
| 57 } |
| 58 |
| 59 base::TimeDelta GetAllWatchTime() { |
| 60 return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoAll); |
| 61 } |
| 62 |
| 63 base::TimeDelta GetMseWatchTime() { |
| 64 return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoMse); |
| 65 } |
| 66 |
| 67 base::TimeDelta GetEmeWatchTime() { |
| 68 return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoEme); |
| 69 } |
| 70 |
| 71 base::TimeDelta GetAcWatchTime() { |
| 72 return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoAc); |
| 73 } |
| 74 |
| 75 base::TimeDelta GetBatteryWatchTime() { |
| 76 return GetReportedWatchTime(WatchTimeReporter::kHistogramAudioVideoBattery); |
| 77 } |
| 78 |
| 79 bool IsMonitoring() { return wtr_->reporting_timer_.IsRunning(); } |
| 80 |
| 81 MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta()); |
| 82 |
| 83 base::HistogramTester histograms_; |
| 84 base::TestMessageLoop message_loop_; |
| 85 base::PowerMonitorTestSource* test_power_source_; |
| 86 std::unique_ptr<base::PowerMonitor> test_power_monitor_; |
| 87 std::unique_ptr<WatchTimeReporter> wtr_; |
| 88 |
| 89 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(WatchTimeReporterTest); |
| 91 }; |
| 92 |
| 93 // Tests that watch time reporting is appropriately enabled or disabled. |
| 94 TEST_F(WatchTimeReporterTest, WatchTimeReporter) { |
| 95 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 96 .WillRepeatedly(testing::Return(base::TimeDelta())); |
| 97 |
| 98 Initialize(false, true, true, true, kSizeJustRight); |
| 99 wtr_->OnPlaying(); |
| 100 EXPECT_FALSE(IsMonitoring()); |
| 101 |
| 102 Initialize(true, false, true, true, kSizeJustRight); |
| 103 wtr_->OnPlaying(); |
| 104 EXPECT_FALSE(IsMonitoring()); |
| 105 |
| 106 constexpr gfx::Size kSizeTooSmall = gfx::Size(100, 100); |
| 107 Initialize(true, true, true, true, kSizeTooSmall); |
| 108 wtr_->OnPlaying(); |
| 109 EXPECT_FALSE(IsMonitoring()); |
| 110 |
| 111 Initialize(true, true, true, true, kSizeJustRight); |
| 112 wtr_->OnPlaying(); |
| 113 EXPECT_TRUE(IsMonitoring()); |
| 114 |
| 115 Initialize(true, true, false, false, kSizeJustRight); |
| 116 wtr_->OnPlaying(); |
| 117 EXPECT_TRUE(IsMonitoring()); |
| 118 |
| 119 Initialize(true, true, true, false, kSizeJustRight); |
| 120 wtr_->OnPlaying(); |
| 121 EXPECT_TRUE(IsMonitoring()); |
| 122 } |
| 123 |
| 124 // Tests that basic reporting for the all category works. |
| 125 TEST_F(WatchTimeReporterTest, WatchTimeReporterBasic) { |
| 126 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5); |
| 127 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10); |
| 128 EXPECT_CALL(*this, GetCurrentMediaTime()) |
| 129 .WillOnce(testing::Return(base::TimeDelta())) |
| 130 .WillOnce(testing::Return(kWatchTimeEarly)) |
| 131 .WillRepeatedly(testing::Return(kWatchTimeLate)); |
| 132 Initialize(true, true, true, true, kSizeJustRight); |
| 133 wtr_->OnPlaying(); |
| 134 EXPECT_TRUE(IsMonitoring()); |
| 135 |
| 136 // No histogram should have been recorded yet since the message loop has not |
| 137 // had any chance to pump. |
| 138 EXPECT_EQ(kNoTimestamp, GetAllWatchTime()); |
| 139 CycleReportingTimer(); |
| 140 |
| 141 // No watch time should have been reported since not enough time has elapsed. |
| 142 EXPECT_EQ(kNoTimestamp, GetAllWatchTime()); |
| 143 CycleReportingTimer(); |
| 144 |
| 145 // Watch time should have been reported. |
| 146 EXPECT_EQ(kWatchTimeLate, GetAllWatchTime()); |
| 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 // No histogram should have been recorded yet since the message loop has not |
| 160 // had any chance to pump. |
| 161 EXPECT_EQ(kNoTimestamp, GetAllWatchTime()); |
| 162 |
| 163 // Finalize the histogram. |
| 164 wtr_.reset(); |
| 165 EXPECT_EQ(kWatchTime, GetAllWatchTime()); |
| 166 EXPECT_EQ(kWatchTime, GetMseWatchTime()); |
| 167 EXPECT_EQ(kWatchTime, GetEmeWatchTime()); |
| 168 EXPECT_EQ(kWatchTime, GetAcWatchTime()); |
| 169 EXPECT_EQ(kNoTimestamp, GetBatteryWatchTime()); |
| 170 } |
| 171 |
| 172 } // namespace media |
OLD | NEW |