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