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 "modules/webaudio/BaseAudioContext.h" | 5 #include "modules/webaudio/BaseAudioContext.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/DocumentUserGestureToken.h" | 8 #include "core/dom/DocumentUserGestureToken.h" |
9 #include "core/frame/FrameOwner.h" | 9 #include "core/frame/FrameOwner.h" |
10 #include "core/frame/FrameTypes.h" | 10 #include "core/frame/FrameTypes.h" |
11 #include "core/frame/FrameView.h" | 11 #include "core/frame/FrameView.h" |
12 #include "core/frame/Settings.h" | 12 #include "core/frame/Settings.h" |
13 #include "core/loader/DocumentLoader.h" | 13 #include "core/loader/DocumentLoader.h" |
14 #include "core/loader/EmptyClients.h" | 14 #include "core/loader/EmptyClients.h" |
15 #include "core/testing/DummyPageHolder.h" | 15 #include "core/testing/DummyPageHolder.h" |
| 16 #include "modules/webaudio/AudioContextOptions.h" |
16 #include "platform/UserGestureIndicator.h" | 17 #include "platform/UserGestureIndicator.h" |
17 #include "platform/testing/HistogramTester.h" | 18 #include "platform/testing/HistogramTester.h" |
18 #include "platform/testing/TestingPlatformSupport.h" | 19 #include "platform/testing/TestingPlatformSupport.h" |
19 #include "public/platform/WebAudioDevice.h" | 20 #include "public/platform/WebAudioDevice.h" |
| 21 #include "public/platform/WebAudioLatencyHint.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 | 23 |
22 namespace blink { | 24 namespace blink { |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 const char* const kCrossOriginMetric = "WebAudio.Autoplay.CrossOrigin"; | 28 const char* const kCrossOriginMetric = "WebAudio.Autoplay.CrossOrigin"; |
27 | 29 |
28 class MockCrossOriginFrameLoaderClient final : public EmptyFrameLoaderClient { | 30 class MockCrossOriginFrameLoaderClient final : public EmptyFrameLoaderClient { |
29 public: | 31 public: |
(...skipping 10 matching lines...) Expand all Loading... |
40 Frame* top() const override { return m_parent.get(); } | 42 Frame* top() const override { return m_parent.get(); } |
41 | 43 |
42 private: | 44 private: |
43 explicit MockCrossOriginFrameLoaderClient(Frame* parent) : m_parent(parent) {} | 45 explicit MockCrossOriginFrameLoaderClient(Frame* parent) : m_parent(parent) {} |
44 | 46 |
45 Member<Frame> m_parent; | 47 Member<Frame> m_parent; |
46 }; | 48 }; |
47 | 49 |
48 class MockWebAudioDevice : public WebAudioDevice { | 50 class MockWebAudioDevice : public WebAudioDevice { |
49 public: | 51 public: |
50 explicit MockWebAudioDevice(double sampleRate) : m_sampleRate(sampleRate) {} | 52 explicit MockWebAudioDevice(double sampleRate, int framesPerBuffer) |
| 53 : m_sampleRate(sampleRate), m_framesPerBuffer(framesPerBuffer) {} |
51 ~MockWebAudioDevice() override = default; | 54 ~MockWebAudioDevice() override = default; |
52 | 55 |
53 void start() override {} | 56 void start() override {} |
54 void stop() override {} | 57 void stop() override {} |
55 double sampleRate() override { return m_sampleRate; } | 58 double sampleRate() override { return m_sampleRate; } |
| 59 int framesPerBuffer() override { return m_framesPerBuffer; } |
56 | 60 |
57 private: | 61 private: |
58 double m_sampleRate; | 62 double m_sampleRate; |
| 63 int m_framesPerBuffer; |
59 }; | 64 }; |
60 | 65 |
61 class BaseAudioContextTestPlatform : public TestingPlatformSupport { | 66 class BaseAudioContextTestPlatform : public TestingPlatformSupport { |
62 public: | 67 public: |
63 WebAudioDevice* createAudioDevice(size_t bufferSize, | 68 WebAudioDevice* createAudioDevice(unsigned numberOfInputChannels, |
64 unsigned numberOfInputChannels, | |
65 unsigned numberOfChannels, | 69 unsigned numberOfChannels, |
66 double sampleRate, | 70 const WebAudioLatencyHint& latencyHint, |
67 WebAudioDevice::RenderCallback*, | 71 WebAudioDevice::RenderCallback*, |
68 const WebString& deviceId, | 72 const WebString& deviceId, |
69 const WebSecurityOrigin&) override { | 73 const WebSecurityOrigin&) override { |
70 return new MockWebAudioDevice(sampleRate); | 74 return new MockWebAudioDevice(audioHardwareSampleRate(), |
| 75 audioHardwareBufferSize()); |
71 } | 76 } |
72 | 77 |
73 double audioHardwareSampleRate() override { return 44100; } | 78 double audioHardwareSampleRate() override { return 44100; } |
| 79 size_t audioHardwareBufferSize() override { return 128; } |
74 }; | 80 }; |
75 | 81 |
76 } // anonymous namespace | 82 } // anonymous namespace |
77 | 83 |
78 class BaseAudioContextTest : public ::testing::Test { | 84 class BaseAudioContextTest : public ::testing::Test { |
79 protected: | 85 protected: |
80 using AutoplayStatus = BaseAudioContext::AutoplayStatus; | 86 using AutoplayStatus = BaseAudioContext::AutoplayStatus; |
81 | 87 |
82 void SetUp() override { | 88 void SetUp() override { |
83 m_dummyPageHolder = DummyPageHolder::create(); | 89 m_dummyPageHolder = DummyPageHolder::create(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 private: | 127 private: |
122 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | 128 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
123 Persistent<DummyFrameOwner> m_dummyFrameOwner; | 129 Persistent<DummyFrameOwner> m_dummyFrameOwner; |
124 Persistent<LocalFrame> m_childFrame; | 130 Persistent<LocalFrame> m_childFrame; |
125 ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> m_platform; | 131 ScopedTestingPlatformSupport<BaseAudioContextTestPlatform> m_platform; |
126 }; | 132 }; |
127 | 133 |
128 TEST_F(BaseAudioContextTest, AutoplayMetrics_NoRestriction) { | 134 TEST_F(BaseAudioContextTest, AutoplayMetrics_NoRestriction) { |
129 HistogramTester histogramTester; | 135 HistogramTester histogramTester; |
130 | 136 |
131 BaseAudioContext* audioContext = | 137 BaseAudioContext* audioContext = BaseAudioContext::create( |
132 BaseAudioContext::create(document(), ASSERT_NO_EXCEPTION); | 138 document(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
133 recordAutoplayStatus(audioContext); | 139 recordAutoplayStatus(audioContext); |
134 | 140 |
135 histogramTester.expectTotalCount(kCrossOriginMetric, 0); | 141 histogramTester.expectTotalCount(kCrossOriginMetric, 0); |
136 } | 142 } |
137 | 143 |
138 TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateNoGesture) { | 144 TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateNoGesture) { |
139 HistogramTester histogramTester; | 145 HistogramTester histogramTester; |
140 createChildFrame(); | 146 createChildFrame(); |
141 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 147 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
142 | 148 |
143 BaseAudioContext* audioContext = | 149 BaseAudioContext* audioContext = BaseAudioContext::create( |
144 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 150 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
145 recordAutoplayStatus(audioContext); | 151 recordAutoplayStatus(audioContext); |
146 | 152 |
147 histogramTester.expectBucketCount(kCrossOriginMetric, | 153 histogramTester.expectBucketCount(kCrossOriginMetric, |
148 AutoplayStatus::AutoplayStatusFailed, 1); | 154 AutoplayStatus::AutoplayStatusFailed, 1); |
149 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 155 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
150 } | 156 } |
151 | 157 |
152 TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeNoGesture) { | 158 TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeNoGesture) { |
153 HistogramTester histogramTester; | 159 HistogramTester histogramTester; |
154 createChildFrame(); | 160 createChildFrame(); |
155 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 161 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
156 | 162 |
157 ScriptState::Scope scope(getScriptStateFrom(childDocument())); | 163 ScriptState::Scope scope(getScriptStateFrom(childDocument())); |
158 | 164 |
159 BaseAudioContext* audioContext = | 165 BaseAudioContext* audioContext = BaseAudioContext::create( |
160 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 166 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
161 audioContext->resumeContext(getScriptStateFrom(childDocument())); | 167 audioContext->resumeContext(getScriptStateFrom(childDocument())); |
162 rejectPendingResolvers(audioContext); | 168 rejectPendingResolvers(audioContext); |
163 recordAutoplayStatus(audioContext); | 169 recordAutoplayStatus(audioContext); |
164 | 170 |
165 histogramTester.expectBucketCount(kCrossOriginMetric, | 171 histogramTester.expectBucketCount(kCrossOriginMetric, |
166 AutoplayStatus::AutoplayStatusFailed, 1); | 172 AutoplayStatus::AutoplayStatusFailed, 1); |
167 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 173 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
168 } | 174 } |
169 | 175 |
170 TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateGesture) { | 176 TEST_F(BaseAudioContextTest, AutoplayMetrics_CreateGesture) { |
171 HistogramTester histogramTester; | 177 HistogramTester histogramTester; |
172 createChildFrame(); | 178 createChildFrame(); |
173 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 179 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
174 | 180 |
175 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( | 181 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( |
176 &childDocument(), UserGestureToken::NewGesture)); | 182 &childDocument(), UserGestureToken::NewGesture)); |
177 | 183 |
178 BaseAudioContext* audioContext = | 184 BaseAudioContext* audioContext = BaseAudioContext::create( |
179 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 185 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
180 recordAutoplayStatus(audioContext); | 186 recordAutoplayStatus(audioContext); |
181 | 187 |
182 histogramTester.expectBucketCount(kCrossOriginMetric, | 188 histogramTester.expectBucketCount(kCrossOriginMetric, |
183 AutoplayStatus::AutoplayStatusSucceeded, 1); | 189 AutoplayStatus::AutoplayStatusSucceeded, 1); |
184 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 190 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
185 } | 191 } |
186 | 192 |
187 TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeGesture) { | 193 TEST_F(BaseAudioContextTest, AutoplayMetrics_CallResumeGesture) { |
188 HistogramTester histogramTester; | 194 HistogramTester histogramTester; |
189 createChildFrame(); | 195 createChildFrame(); |
190 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 196 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
191 | 197 |
192 ScriptState::Scope scope(getScriptStateFrom(childDocument())); | 198 ScriptState::Scope scope(getScriptStateFrom(childDocument())); |
193 | 199 |
194 BaseAudioContext* audioContext = | 200 BaseAudioContext* audioContext = BaseAudioContext::create( |
195 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 201 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
196 | 202 |
197 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( | 203 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( |
198 &childDocument(), UserGestureToken::NewGesture)); | 204 &childDocument(), UserGestureToken::NewGesture)); |
199 | 205 |
200 audioContext->resumeContext(getScriptStateFrom(childDocument())); | 206 audioContext->resumeContext(getScriptStateFrom(childDocument())); |
201 rejectPendingResolvers(audioContext); | 207 rejectPendingResolvers(audioContext); |
202 recordAutoplayStatus(audioContext); | 208 recordAutoplayStatus(audioContext); |
203 | 209 |
204 histogramTester.expectBucketCount(kCrossOriginMetric, | 210 histogramTester.expectBucketCount(kCrossOriginMetric, |
205 AutoplayStatus::AutoplayStatusSucceeded, 1); | 211 AutoplayStatus::AutoplayStatusSucceeded, 1); |
206 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 212 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
207 } | 213 } |
208 | 214 |
209 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGesture) { | 215 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGesture) { |
210 HistogramTester histogramTester; | 216 HistogramTester histogramTester; |
211 createChildFrame(); | 217 createChildFrame(); |
212 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 218 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
213 | 219 |
214 BaseAudioContext* audioContext = | 220 BaseAudioContext* audioContext = BaseAudioContext::create( |
215 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 221 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
216 audioContext->maybeRecordStartAttempt(); | 222 audioContext->maybeRecordStartAttempt(); |
217 recordAutoplayStatus(audioContext); | 223 recordAutoplayStatus(audioContext); |
218 | 224 |
219 histogramTester.expectBucketCount(kCrossOriginMetric, | 225 histogramTester.expectBucketCount(kCrossOriginMetric, |
220 AutoplayStatus::AutoplayStatusFailed, 1); | 226 AutoplayStatus::AutoplayStatusFailed, 1); |
221 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 227 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
222 } | 228 } |
223 | 229 |
224 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGesture) { | 230 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGesture) { |
225 HistogramTester histogramTester; | 231 HistogramTester histogramTester; |
226 createChildFrame(); | 232 createChildFrame(); |
227 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 233 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
228 | 234 |
229 BaseAudioContext* audioContext = | 235 BaseAudioContext* audioContext = BaseAudioContext::create( |
230 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 236 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
231 | 237 |
232 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( | 238 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( |
233 &childDocument(), UserGestureToken::NewGesture)); | 239 &childDocument(), UserGestureToken::NewGesture)); |
234 audioContext->maybeRecordStartAttempt(); | 240 audioContext->maybeRecordStartAttempt(); |
235 recordAutoplayStatus(audioContext); | 241 recordAutoplayStatus(audioContext); |
236 | 242 |
237 histogramTester.expectBucketCount( | 243 histogramTester.expectBucketCount( |
238 kCrossOriginMetric, AutoplayStatus::AutoplayStatusFailedWithStart, 1); | 244 kCrossOriginMetric, AutoplayStatus::AutoplayStatusFailedWithStart, 1); |
239 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 245 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
240 } | 246 } |
241 | 247 |
242 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGestureThenSuccess) { | 248 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartNoGestureThenSuccess) { |
243 HistogramTester histogramTester; | 249 HistogramTester histogramTester; |
244 createChildFrame(); | 250 createChildFrame(); |
245 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 251 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
246 | 252 |
247 ScriptState::Scope scope(getScriptStateFrom(childDocument())); | 253 ScriptState::Scope scope(getScriptStateFrom(childDocument())); |
248 | 254 |
249 BaseAudioContext* audioContext = | 255 BaseAudioContext* audioContext = BaseAudioContext::create( |
250 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 256 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
251 audioContext->maybeRecordStartAttempt(); | 257 audioContext->maybeRecordStartAttempt(); |
252 | 258 |
253 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( | 259 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( |
254 &childDocument(), UserGestureToken::NewGesture)); | 260 &childDocument(), UserGestureToken::NewGesture)); |
255 audioContext->resumeContext(getScriptStateFrom(childDocument())); | 261 audioContext->resumeContext(getScriptStateFrom(childDocument())); |
256 rejectPendingResolvers(audioContext); | 262 rejectPendingResolvers(audioContext); |
257 recordAutoplayStatus(audioContext); | 263 recordAutoplayStatus(audioContext); |
258 | 264 |
259 histogramTester.expectBucketCount(kCrossOriginMetric, | 265 histogramTester.expectBucketCount(kCrossOriginMetric, |
260 AutoplayStatus::AutoplayStatusSucceeded, 1); | 266 AutoplayStatus::AutoplayStatusSucceeded, 1); |
261 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 267 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
262 } | 268 } |
263 | 269 |
264 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGestureThenSucces) { | 270 TEST_F(BaseAudioContextTest, AutoplayMetrics_NodeStartGestureThenSucces) { |
265 HistogramTester histogramTester; | 271 HistogramTester histogramTester; |
266 createChildFrame(); | 272 createChildFrame(); |
267 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); | 273 childDocument().settings()->setMediaPlaybackRequiresUserGesture(true); |
268 | 274 |
269 ScriptState::Scope scope(getScriptStateFrom(childDocument())); | 275 ScriptState::Scope scope(getScriptStateFrom(childDocument())); |
270 | 276 |
271 BaseAudioContext* audioContext = | 277 BaseAudioContext* audioContext = BaseAudioContext::create( |
272 BaseAudioContext::create(childDocument(), ASSERT_NO_EXCEPTION); | 278 childDocument(), AudioContextOptions(), ASSERT_NO_EXCEPTION); |
273 | 279 |
274 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( | 280 UserGestureIndicator userGestureScope(DocumentUserGestureToken::create( |
275 &childDocument(), UserGestureToken::NewGesture)); | 281 &childDocument(), UserGestureToken::NewGesture)); |
276 audioContext->maybeRecordStartAttempt(); | 282 audioContext->maybeRecordStartAttempt(); |
277 audioContext->resumeContext(getScriptStateFrom(childDocument())); | 283 audioContext->resumeContext(getScriptStateFrom(childDocument())); |
278 rejectPendingResolvers(audioContext); | 284 rejectPendingResolvers(audioContext); |
279 recordAutoplayStatus(audioContext); | 285 recordAutoplayStatus(audioContext); |
280 | 286 |
281 histogramTester.expectBucketCount(kCrossOriginMetric, | 287 histogramTester.expectBucketCount(kCrossOriginMetric, |
282 AutoplayStatus::AutoplayStatusSucceeded, 1); | 288 AutoplayStatus::AutoplayStatusSucceeded, 1); |
283 histogramTester.expectTotalCount(kCrossOriginMetric, 1); | 289 histogramTester.expectTotalCount(kCrossOriginMetric, 1); |
284 } | 290 } |
285 | 291 |
286 } // namespace blink | 292 } // namespace blink |
OLD | NEW |