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

Side by Side Diff: content/browser/media/audio_stream_monitor_unittest.cc

Issue 478543003: Use AudioStreamMonitor to control power save blocking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup. Created 6 years, 3 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/media/audio_stream_monitor.h" 5 #include "content/browser/media/audio_stream_monitor.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/test/simple_test_tick_clock.h" 13 #include "base/test/simple_test_tick_clock.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/invalidate_type.h" 14 #include "content/public/browser/invalidate_type.h"
16 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_delegate.h" 16 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_renderer_host.h"
19 #include "media/audio/audio_power_monitor.h" 18 #include "media/audio/audio_power_monitor.h"
20 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
22 21
23 using ::testing::InvokeWithoutArgs; 22 using ::testing::InvokeWithoutArgs;
24 23
24 namespace content {
25
25 namespace { 26 namespace {
26 27
28 const int kRenderProcessId = 1;
29 const int kAnotherRenderProcessId = 2;
27 const int kStreamId = 3; 30 const int kStreamId = 3;
28 const int kAnotherStreamId = 6; 31 const int kAnotherStreamId = 6;
29 32
30 // Used to confirm audio indicator state changes occur at the correct times. 33 // Used to confirm audio indicator state changes occur at the correct times.
31 class MockWebContentsDelegate : public content::WebContentsDelegate { 34 class MockWebContentsDelegate : public WebContentsDelegate {
32 public: 35 public:
33 MOCK_METHOD2(NavigationStateChanged, 36 MOCK_METHOD2(NavigationStateChanged,
34 void(const content::WebContents* source, 37 void(const WebContents* source, InvalidateTypes changed_flags));
35 content::InvalidateTypes changed_flags));
36 }; 38 };
37 39
38 } // namespace 40 } // namespace
39 41
40 class AudioStreamMonitorTest : public testing::Test { 42 class AudioStreamMonitorTest : public RenderViewHostTestHarness {
41 public: 43 public:
42 AudioStreamMonitorTest() { 44 AudioStreamMonitorTest() {
43 // Start |clock_| at non-zero. 45 // Start |clock_| at non-zero.
44 clock_.Advance(base::TimeDelta::FromSeconds(1000000)); 46 clock_.Advance(base::TimeDelta::FromSeconds(1000000));
47 }
45 48
46 // Create a WebContents instance and set it to use our mock delegate. 49 virtual void SetUp() OVERRIDE {
47 web_contents_.reset(content::WebContents::Create( 50 RenderViewHostTestHarness::SetUp();
48 content::WebContents::CreateParams(&profile_, NULL))); 51
49 web_contents_->SetDelegate(&mock_web_contents_delegate_); 52 WebContents* web_contents = RenderViewHostTestHarness::web_contents();
53 web_contents->SetDelegate(&mock_web_contents_delegate_);
50 54
51 // Create an AudioStreamMonitor instance whose lifecycle is tied to that of 55 // Create an AudioStreamMonitor instance whose lifecycle is tied to that of
52 // |web_contents_|, and override its clock with the test clock. 56 // |web_contents_|, and override its clock with the test clock.
53 AudioStreamMonitor::CreateForWebContents(web_contents_.get()); 57 AudioStreamMonitor::CreateForWebContents(web_contents);
54 CHECK(audio_stream_monitor()); 58 monitor_ = AudioStreamMonitor::FromWebContents(web_contents);
55 const_cast<base::TickClock*&>(audio_stream_monitor()->clock_) = &clock_; 59 CHECK(monitor_);
56 } 60 const_cast<base::TickClock*&>(monitor_->clock_) = &clock_;
57
58 AudioStreamMonitor* audio_stream_monitor() {
59 return AudioStreamMonitor::FromWebContents(web_contents_.get());
60 } 61 }
61 62
62 base::TimeTicks GetTestClockTime() { return clock_.NowTicks(); } 63 base::TimeTicks GetTestClockTime() { return clock_.NowTicks(); }
63 64
64 void AdvanceClock(const base::TimeDelta& delta) { clock_.Advance(delta); } 65 void AdvanceClock(const base::TimeDelta& delta) { clock_.Advance(delta); }
65 66
66 AudioStreamMonitor::ReadPowerAndClipCallback CreatePollCallback( 67 AudioStreamMonitor::ReadPowerAndClipCallback CreatePollCallback(
67 int stream_id) { 68 int stream_id) {
68 return base::Bind( 69 return base::Bind(
69 &AudioStreamMonitorTest::ReadPower, base::Unretained(this), stream_id); 70 &AudioStreamMonitorTest::ReadPower, base::Unretained(this), stream_id);
70 } 71 }
71 72
72 void SetStreamPower(int stream_id, float power) { 73 void SetStreamPower(int stream_id, float power) {
73 current_power_[stream_id] = power; 74 current_power_[stream_id] = power;
74 } 75 }
75 76
76 void SimulatePollTimerFired() { audio_stream_monitor()->Poll(); } 77 void SimulatePollTimerFired() { monitor_->Poll(); }
77 78
78 void SimulateOffTimerFired() { audio_stream_monitor()->MaybeToggle(); } 79 void SimulateOffTimerFired() { monitor_->MaybeToggle(); }
79 80
80 void ExpectIsPolling(int stream_id, bool is_polling) { 81 void ExpectIsPolling(int render_process_id, int stream_id, bool is_polling) {
81 AudioStreamMonitor* const monitor = audio_stream_monitor(); 82 const AudioStreamMonitor::StreamID key(render_process_id, stream_id);
82 EXPECT_EQ(is_polling, 83 EXPECT_EQ(
83 monitor->poll_callbacks_.find(stream_id) != 84 is_polling,
84 monitor->poll_callbacks_.end()); 85 monitor_->poll_callbacks_.find(key) != monitor_->poll_callbacks_.end());
85 EXPECT_EQ(!monitor->poll_callbacks_.empty(), 86 EXPECT_EQ(!monitor_->poll_callbacks_.empty(),
86 monitor->poll_timer_.IsRunning()); 87 monitor_->poll_timer_.IsRunning());
87 } 88 }
88 89
89 void ExpectTabWasRecentlyAudible(bool was_audible, 90 void ExpectTabWasRecentlyAudible(bool was_audible,
90 const base::TimeTicks& last_blurt_time) { 91 const base::TimeTicks& last_blurt_time) {
91 AudioStreamMonitor* const monitor = audio_stream_monitor(); 92 EXPECT_EQ(was_audible, monitor_->was_recently_audible_);
92 EXPECT_EQ(was_audible, monitor->was_recently_audible_); 93 EXPECT_EQ(last_blurt_time, monitor_->last_blurt_time_);
93 EXPECT_EQ(last_blurt_time, monitor->last_blurt_time_); 94 EXPECT_EQ(monitor_->was_recently_audible_,
94 EXPECT_EQ(monitor->was_recently_audible_, monitor->off_timer_.IsRunning()); 95 monitor_->off_timer_.IsRunning());
95 } 96 }
96 97
97 void ExpectWebContentsWillBeNotifiedOnce(bool should_be_audible) { 98 void ExpectWebContentsWillBeNotifiedOnce(bool should_be_audible) {
98 EXPECT_CALL(mock_web_contents_delegate_, 99 EXPECT_CALL(
99 NavigationStateChanged(web_contents_.get(), 100 mock_web_contents_delegate_,
100 content::INVALIDATE_TYPE_TAB)) 101 NavigationStateChanged(RenderViewHostTestHarness::web_contents(),
102 INVALIDATE_TYPE_TAB))
101 .WillOnce(InvokeWithoutArgs( 103 .WillOnce(InvokeWithoutArgs(
102 this, 104 this,
103 should_be_audible 105 should_be_audible
104 ? &AudioStreamMonitorTest::ExpectIsNotifyingForToggleOn 106 ? &AudioStreamMonitorTest::ExpectIsNotifyingForToggleOn
105 : &AudioStreamMonitorTest::ExpectIsNotifyingForToggleOff)) 107 : &AudioStreamMonitorTest::ExpectIsNotifyingForToggleOff))
106 .RetiresOnSaturation(); 108 .RetiresOnSaturation();
107 } 109 }
108 110
109 static base::TimeDelta one_polling_interval() { 111 static base::TimeDelta one_polling_interval() {
110 return base::TimeDelta::FromSeconds(1) / 112 return base::TimeDelta::FromSeconds(1) /
111 AudioStreamMonitor::kPowerMeasurementsPerSecond; 113 AudioStreamMonitor::kPowerMeasurementsPerSecond;
112 } 114 }
113 115
114 static base::TimeDelta holding_period() { 116 static base::TimeDelta holding_period() {
115 return base::TimeDelta::FromMilliseconds( 117 return base::TimeDelta::FromMilliseconds(
116 AudioStreamMonitor::kHoldOnMilliseconds); 118 AudioStreamMonitor::kHoldOnMilliseconds);
117 } 119 }
118 120
121 void StartMonitoring(
122 int render_process_id,
123 int stream_id,
124 const AudioStreamMonitor::ReadPowerAndClipCallback& callback) {
125 monitor_->StartMonitoringStreamOnUIThread(
126 render_process_id, stream_id, callback);
127 }
128
129 void StopMonitoring(int render_process_id, int stream_id) {
130 monitor_->StopMonitoringStreamOnUIThread(render_process_id, stream_id);
131 }
132
133 protected:
134 AudioStreamMonitor* monitor_;
135
119 private: 136 private:
120 std::pair<float, bool> ReadPower(int stream_id) { 137 std::pair<float, bool> ReadPower(int stream_id) {
121 return std::make_pair(current_power_[stream_id], false); 138 return std::make_pair(current_power_[stream_id], false);
122 } 139 }
123 140
124 void ExpectIsNotifyingForToggleOn() { 141 void ExpectIsNotifyingForToggleOn() {
125 EXPECT_TRUE(audio_stream_monitor()->WasRecentlyAudible()); 142 EXPECT_TRUE(monitor_->WasRecentlyAudible());
126 } 143 }
127 144
128 void ExpectIsNotifyingForToggleOff() { 145 void ExpectIsNotifyingForToggleOff() {
129 EXPECT_FALSE(audio_stream_monitor()->WasRecentlyAudible()); 146 EXPECT_FALSE(monitor_->WasRecentlyAudible());
130 } 147 }
131 148
132 content::TestBrowserThreadBundle browser_thread_bundle_;
133 TestingProfile profile_;
134 MockWebContentsDelegate mock_web_contents_delegate_; 149 MockWebContentsDelegate mock_web_contents_delegate_;
135 base::SimpleTestTickClock clock_; 150 base::SimpleTestTickClock clock_;
136 std::map<int, float> current_power_; 151 std::map<int, float> current_power_;
137 scoped_ptr<content::WebContents> web_contents_;
138 152
139 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitorTest); 153 DISALLOW_COPY_AND_ASSIGN(AudioStreamMonitorTest);
140 }; 154 };
141 155
142 // Tests that AudioStreamMonitor is polling while it has a 156 // Tests that AudioStreamMonitor is polling while it has a
143 // ReadPowerAndClipCallback, and is not polling at other times. 157 // ReadPowerAndClipCallback, and is not polling at other times.
144 TEST_F(AudioStreamMonitorTest, PollsWhenProvidedACallback) { 158 TEST_F(AudioStreamMonitorTest, PollsWhenProvidedACallback) {
145 EXPECT_FALSE(audio_stream_monitor()->WasRecentlyAudible()); 159 EXPECT_FALSE(monitor_->WasRecentlyAudible());
146 ExpectIsPolling(kStreamId, false); 160 ExpectIsPolling(kRenderProcessId, kStreamId, false);
147 161
148 audio_stream_monitor()->StartMonitoringStream(kStreamId, 162 StartMonitoring(kRenderProcessId, kStreamId, CreatePollCallback(kStreamId));
149 CreatePollCallback(kStreamId)); 163 EXPECT_FALSE(monitor_->WasRecentlyAudible());
150 EXPECT_FALSE(audio_stream_monitor()->WasRecentlyAudible()); 164 ExpectIsPolling(kRenderProcessId, kStreamId, true);
151 ExpectIsPolling(kStreamId, true);
152 165
153 audio_stream_monitor()->StopMonitoringStream(kStreamId); 166 StopMonitoring(kRenderProcessId, kStreamId);
154 EXPECT_FALSE(audio_stream_monitor()->WasRecentlyAudible()); 167 EXPECT_FALSE(monitor_->WasRecentlyAudible());
155 ExpectIsPolling(kStreamId, false); 168 ExpectIsPolling(kRenderProcessId, kStreamId, false);
156 } 169 }
157 170
158 // Tests that AudioStreamMonitor debounces the power level readings it's taking, 171 // Tests that AudioStreamMonitor debounces the power level readings it's taking,
159 // which could be fluctuating rapidly between the audible versus silence 172 // which could be fluctuating rapidly between the audible versus silence
160 // threshold. See comments in audio_stream_monitor.h for expected behavior. 173 // threshold. See comments in audio_stream_monitor.h for expected behavior.
161 TEST_F(AudioStreamMonitorTest, 174 TEST_F(AudioStreamMonitorTest,
162 ImpulsesKeepIndicatorOnUntilHoldingPeriodHasPassed) { 175 ImpulsesKeepIndicatorOnUntilHoldingPeriodHasPassed) {
163 audio_stream_monitor()->StartMonitoringStream(kStreamId, 176 StartMonitoring(kRenderProcessId, kStreamId, CreatePollCallback(kStreamId));
164 CreatePollCallback(kStreamId));
165 177
166 // Expect WebContents will get one call form AudioStreamMonitor to toggle the 178 // Expect WebContents will get one call form AudioStreamMonitor to toggle the
167 // indicator on upon the very first poll. 179 // indicator on upon the very first poll.
168 ExpectWebContentsWillBeNotifiedOnce(true); 180 ExpectWebContentsWillBeNotifiedOnce(true);
169 181
170 // Loop, each time testing a slightly longer period of polled silence. The 182 // Loop, each time testing a slightly longer period of polled silence. The
171 // indicator should remain on throughout. 183 // indicator should remain on throughout.
172 int num_silence_polls = 0; 184 int num_silence_polls = 0;
173 base::TimeTicks last_blurt_time; 185 base::TimeTicks last_blurt_time;
174 do { 186 do {
(...skipping 26 matching lines...) Expand all
201 for (int i = 0; i < 10; ++i) { 213 for (int i = 0; i < 10; ++i) {
202 SimulateOffTimerFired(); 214 SimulateOffTimerFired();
203 ExpectTabWasRecentlyAudible(false, last_blurt_time); 215 ExpectTabWasRecentlyAudible(false, last_blurt_time);
204 AdvanceClock(one_polling_interval()); 216 AdvanceClock(one_polling_interval());
205 } 217 }
206 } 218 }
207 219
208 // Tests that the AudioStreamMonitor correctly processes the blurts from two 220 // Tests that the AudioStreamMonitor correctly processes the blurts from two
209 // different streams in the same tab. 221 // different streams in the same tab.
210 TEST_F(AudioStreamMonitorTest, HandlesMultipleStreamsBlurting) { 222 TEST_F(AudioStreamMonitorTest, HandlesMultipleStreamsBlurting) {
211 audio_stream_monitor()->StartMonitoringStream(kStreamId, 223 StartMonitoring(kRenderProcessId, kStreamId, CreatePollCallback(kStreamId));
212 CreatePollCallback(kStreamId)); 224 StartMonitoring(
213 audio_stream_monitor()->StartMonitoringStream( 225 kRenderProcessId, kAnotherStreamId, CreatePollCallback(kAnotherStreamId));
214 kAnotherStreamId,
215 CreatePollCallback(kAnotherStreamId));
216 226
217 base::TimeTicks last_blurt_time; 227 base::TimeTicks last_blurt_time;
218 ExpectTabWasRecentlyAudible(false, last_blurt_time); 228 ExpectTabWasRecentlyAudible(false, last_blurt_time);
219 229
220 // Measure audible sound from the first stream and silence from the second. 230 // Measure audible sound from the first stream and silence from the second.
221 // The indicator turns on (i.e., tab was recently audible). 231 // The indicator turns on (i.e., tab was recently audible).
222 ExpectWebContentsWillBeNotifiedOnce(true); 232 ExpectWebContentsWillBeNotifiedOnce(true);
223 SetStreamPower(kStreamId, media::AudioPowerMonitor::max_power()); 233 SetStreamPower(kStreamId, media::AudioPowerMonitor::max_power());
224 SetStreamPower(kAnotherStreamId, media::AudioPowerMonitor::zero_power()); 234 SetStreamPower(kAnotherStreamId, media::AudioPowerMonitor::zero_power());
225 last_blurt_time = GetTestClockTime(); 235 last_blurt_time = GetTestClockTime();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 ExpectTabWasRecentlyAudible(false, last_blurt_time); 279 ExpectTabWasRecentlyAudible(false, last_blurt_time);
270 280
271 // Polling should not turn the indicator back while both streams are remaining 281 // Polling should not turn the indicator back while both streams are remaining
272 // silent. 282 // silent.
273 for (int i = 0; i < 100; ++i) { 283 for (int i = 0; i < 100; ++i) {
274 AdvanceClock(one_polling_interval()); 284 AdvanceClock(one_polling_interval());
275 SimulatePollTimerFired(); 285 SimulatePollTimerFired();
276 ExpectTabWasRecentlyAudible(false, last_blurt_time); 286 ExpectTabWasRecentlyAudible(false, last_blurt_time);
277 } 287 }
278 } 288 }
289
290 TEST_F(AudioStreamMonitorTest, MultipleRendererProcesses) {
291 StartMonitoring(kRenderProcessId, kStreamId, CreatePollCallback(kStreamId));
292 StartMonitoring(
293 kAnotherRenderProcessId, kStreamId, CreatePollCallback(kStreamId));
294 ExpectIsPolling(kRenderProcessId, kStreamId, true);
295 ExpectIsPolling(kAnotherRenderProcessId, kStreamId, true);
296 StopMonitoring(kAnotherRenderProcessId, kStreamId);
297 ExpectIsPolling(kRenderProcessId, kStreamId, true);
298 ExpectIsPolling(kAnotherRenderProcessId, kStreamId, false);
299 }
300
301 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698