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