|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "AudioEventTimeline.h" |
|
8 #include "TestHarness.h" |
|
9 #include <sstream> |
|
10 #include <limits> |
|
11 |
|
12 using namespace mozilla; |
|
13 using namespace mozilla::dom; |
|
14 using std::numeric_limits; |
|
15 |
|
16 // Some simple testing primitives |
|
17 void ok(bool val, const char* msg) |
|
18 { |
|
19 if (val) { |
|
20 passed(msg); |
|
21 } else { |
|
22 fail(msg); |
|
23 } |
|
24 } |
|
25 |
|
26 namespace std { |
|
27 |
|
28 template <class T> |
|
29 basic_ostream<T, char_traits<T> >& |
|
30 operator<<(basic_ostream<T, char_traits<T> >& os, nsresult rv) |
|
31 { |
|
32 os << static_cast<uint32_t>(rv); |
|
33 return os; |
|
34 } |
|
35 |
|
36 } |
|
37 |
|
38 template <class T, class U> |
|
39 void is(const T& a, const U& b, const char* msg) |
|
40 { |
|
41 std::stringstream ss; |
|
42 ss << msg << ", Got: " << a << ", expected: " << b; |
|
43 ok(a == b, ss.str().c_str()); |
|
44 } |
|
45 |
|
46 template <> |
|
47 void is(const float& a, const float& b, const char* msg) |
|
48 { |
|
49 // stupidly high, since we mostly care about the correctness of the algorithm |
|
50 const float kEpsilon = 0.00001f; |
|
51 |
|
52 std::stringstream ss; |
|
53 ss << msg << ", Got: " << a << ", expected: " << b; |
|
54 ok(fabsf(a - b) < kEpsilon, ss.str().c_str()); |
|
55 } |
|
56 |
|
57 class ErrorResultMock |
|
58 { |
|
59 public: |
|
60 ErrorResultMock() |
|
61 : mRv(NS_OK) |
|
62 { |
|
63 } |
|
64 void Throw(nsresult aRv) |
|
65 { |
|
66 mRv = aRv; |
|
67 } |
|
68 |
|
69 operator nsresult() const |
|
70 { |
|
71 return mRv; |
|
72 } |
|
73 |
|
74 ErrorResultMock& operator=(nsresult aRv) |
|
75 { |
|
76 mRv = aRv; |
|
77 return *this; |
|
78 } |
|
79 |
|
80 private: |
|
81 nsresult mRv; |
|
82 }; |
|
83 |
|
84 typedef AudioEventTimeline<ErrorResultMock> Timeline; |
|
85 |
|
86 void TestSpecExample() |
|
87 { |
|
88 // First, run the basic tests |
|
89 Timeline timeline(10.0f); |
|
90 is(timeline.Value(), 10.0f, "Correct default value returned"); |
|
91 |
|
92 ErrorResultMock rv; |
|
93 |
|
94 float curve[] = { -1.0f, 0.0f, 1.0f }; |
|
95 |
|
96 // This test is copied from the example in the Web Audio spec |
|
97 const double t0 = 0.0, |
|
98 t1 = 0.1, |
|
99 t2 = 0.2, |
|
100 t3 = 0.3, |
|
101 t4 = 0.4, |
|
102 t5 = 0.6, |
|
103 t6 = 0.7, |
|
104 t7 = 1.0; |
|
105 timeline.SetValueAtTime(0.2f, t0, rv); |
|
106 is(rv, NS_OK, "SetValueAtTime succeeded"); |
|
107 timeline.SetValueAtTime(0.3f, t1, rv); |
|
108 is(rv, NS_OK, "SetValueAtTime succeeded"); |
|
109 timeline.SetValueAtTime(0.4f, t2, rv); |
|
110 is(rv, NS_OK, "SetValueAtTime succeeded"); |
|
111 timeline.LinearRampToValueAtTime(1.0f, t3, rv); |
|
112 is(rv, NS_OK, "LinearRampToValueAtTime succeeded"); |
|
113 timeline.LinearRampToValueAtTime(0.15f, t4, rv); |
|
114 is(rv, NS_OK, "LinearRampToValueAtTime succeeded"); |
|
115 timeline.ExponentialRampToValueAtTime(0.75f, t5, rv); |
|
116 is(rv, NS_OK, "ExponentialRampToValueAtTime succeeded"); |
|
117 timeline.ExponentialRampToValueAtTime(0.05f, t6, rv); |
|
118 is(rv, NS_OK, "ExponentialRampToValueAtTime succeeded"); |
|
119 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), t6, t7 - t6, rv); |
|
120 is(rv, NS_OK, "SetValueCurveAtTime succeeded"); |
|
121 |
|
122 is(timeline.GetValueAtTime(0.0), 0.2f, "Correct value"); |
|
123 is(timeline.GetValueAtTime(0.05), 0.2f, "Correct value"); |
|
124 is(timeline.GetValueAtTime(0.1), 0.3f, "Correct value"); |
|
125 is(timeline.GetValueAtTime(0.15), 0.3f, "Correct value"); |
|
126 is(timeline.GetValueAtTime(0.2), 0.4f, "Correct value"); |
|
127 is(timeline.GetValueAtTime(0.25), (0.4f + 1.0f) / 2, "Correct value"); |
|
128 is(timeline.GetValueAtTime(0.3), 1.0f, "Correct value"); |
|
129 is(timeline.GetValueAtTime(0.35), (1.0f + 0.15f) / 2, "Correct value"); |
|
130 is(timeline.GetValueAtTime(0.4), 0.15f, "Correct value"); |
|
131 is(timeline.GetValueAtTime(0.45), (0.15f * powf(0.75f / 0.15f, 0.05f / 0.2f)), "Correct value"); |
|
132 is(timeline.GetValueAtTime(0.5), (0.15f * powf(0.75f / 0.15f, 0.5f)), "Correct value"); |
|
133 is(timeline.GetValueAtTime(0.55), (0.15f * powf(0.75f / 0.15f, 0.15f / 0.2f)), "Correct value"); |
|
134 is(timeline.GetValueAtTime(0.6), 0.75f, "Correct value"); |
|
135 is(timeline.GetValueAtTime(0.65), (0.75f * powf(0.05f / 0.75f, 0.5f)), "Correct value"); |
|
136 is(timeline.GetValueAtTime(0.7), -1.0f, "Correct value"); |
|
137 is(timeline.GetValueAtTime(0.9), 0.0f, "Correct value"); |
|
138 is(timeline.GetValueAtTime(1.0), 1.0f, "Correct value"); |
|
139 } |
|
140 |
|
141 void TestInvalidEvents() |
|
142 { |
|
143 static_assert(numeric_limits<float>::has_quiet_NaN, "Platform must have a quiet NaN"); |
|
144 const float NaN = numeric_limits<float>::quiet_NaN(); |
|
145 const float Infinity = numeric_limits<float>::infinity(); |
|
146 Timeline timeline(10.0f); |
|
147 |
|
148 float curve[] = { -1.0f, 0.0f, 1.0f }; |
|
149 float badCurve1[] = { -1.0f, NaN, 1.0f }; |
|
150 float badCurve2[] = { -1.0f, Infinity, 1.0f }; |
|
151 float badCurve3[] = { -1.0f, -Infinity, 1.0f }; |
|
152 |
|
153 ErrorResultMock rv; |
|
154 |
|
155 timeline.SetValueAtTime(NaN, 0.1, rv); |
|
156 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
157 timeline.SetValueAtTime(Infinity, 0.1, rv); |
|
158 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
159 timeline.SetValueAtTime(-Infinity, 0.1, rv); |
|
160 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
161 timeline.LinearRampToValueAtTime(NaN, 0.2, rv); |
|
162 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
163 timeline.LinearRampToValueAtTime(Infinity, 0.2, rv); |
|
164 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
165 timeline.LinearRampToValueAtTime(-Infinity, 0.2, rv); |
|
166 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
167 timeline.ExponentialRampToValueAtTime(NaN, 0.3, rv); |
|
168 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
169 timeline.ExponentialRampToValueAtTime(Infinity, 0.3, rv); |
|
170 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
171 timeline.ExponentialRampToValueAtTime(-Infinity, 0.4, rv); |
|
172 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
173 timeline.ExponentialRampToValueAtTime(0, 0.5, rv); |
|
174 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
175 timeline.SetTargetAtTime(NaN, 0.4, 1.0, rv); |
|
176 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
177 timeline.SetTargetAtTime(Infinity, 0.4, 1.0, rv); |
|
178 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
179 timeline.SetTargetAtTime(-Infinity, 0.4, 1.0, rv); |
|
180 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
181 timeline.SetTargetAtTime(0.4f, NaN, 1.0, rv); |
|
182 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
183 timeline.SetTargetAtTime(0.4f, Infinity, 1.0, rv); |
|
184 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
185 timeline.SetTargetAtTime(0.4f, -Infinity, 1.0, rv); |
|
186 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
187 timeline.SetValueCurveAtTime(nullptr, 0, 1.0, 1.0, rv); |
|
188 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
189 timeline.SetValueCurveAtTime(badCurve1, ArrayLength(badCurve1), 1.0, 1.0, rv); |
|
190 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
191 timeline.SetValueCurveAtTime(badCurve2, ArrayLength(badCurve2), 1.0, 1.0, rv); |
|
192 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
193 timeline.SetValueCurveAtTime(badCurve3, ArrayLength(badCurve3), 1.0, 1.0, rv); |
|
194 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
195 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), NaN, 1.0, rv); |
|
196 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
197 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), Infinity, 1.0, rv); |
|
198 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
199 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), -Infinity, 1.0, rv); |
|
200 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
201 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), 1.0, NaN, rv); |
|
202 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
203 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), 1.0, Infinity, rv); |
|
204 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
205 timeline.SetValueCurveAtTime(curve, ArrayLength(curve), 1.0, -Infinity, rv); |
|
206 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
207 } |
|
208 |
|
209 void TestEventReplacement() |
|
210 { |
|
211 Timeline timeline(10.0f); |
|
212 |
|
213 ErrorResultMock rv; |
|
214 |
|
215 is(timeline.GetEventCount(), 0u, "No events yet"); |
|
216 timeline.SetValueAtTime(10.0f, 0.1, rv); |
|
217 is(timeline.GetEventCount(), 1u, "One event scheduled now"); |
|
218 timeline.SetValueAtTime(20.0f, 0.1, rv); |
|
219 is(rv, NS_OK, "Event scheduling should be successful"); |
|
220 is(timeline.GetEventCount(), 1u, "Event should be replaced"); |
|
221 is(timeline.GetValueAtTime(0.1), 20.0f, "The first event should be overwritten"); |
|
222 timeline.LinearRampToValueAtTime(30.0f, 0.1, rv); |
|
223 is(rv, NS_OK, "Event scheduling should be successful"); |
|
224 is(timeline.GetEventCount(), 2u, "Different event type should be appended"); |
|
225 is(timeline.GetValueAtTime(0.1), 30.0f, "The first event should be overwritten"); |
|
226 } |
|
227 |
|
228 void TestEventRemoval() |
|
229 { |
|
230 Timeline timeline(10.0f); |
|
231 |
|
232 ErrorResultMock rv; |
|
233 |
|
234 timeline.SetValueAtTime(10.0f, 0.1, rv); |
|
235 timeline.SetValueAtTime(15.0f, 0.15, rv); |
|
236 timeline.SetValueAtTime(20.0f, 0.2, rv); |
|
237 timeline.LinearRampToValueAtTime(30.0f, 0.3, rv); |
|
238 is(timeline.GetEventCount(), 4u, "Should have three events initially"); |
|
239 timeline.CancelScheduledValues(0.4); |
|
240 is(timeline.GetEventCount(), 4u, "Trying to delete past the end of the array should have no effect"); |
|
241 timeline.CancelScheduledValues(0.3); |
|
242 is(timeline.GetEventCount(), 3u, "Should successfully delete one event"); |
|
243 timeline.CancelScheduledValues(0.12); |
|
244 is(timeline.GetEventCount(), 1u, "Should successfully delete two events"); |
|
245 timeline.CancelAllEvents(); |
|
246 ok(timeline.HasSimpleValue(), "No event should remain scheduled"); |
|
247 } |
|
248 |
|
249 void TestBeforeFirstEventSetValue() |
|
250 { |
|
251 Timeline timeline(10.0f); |
|
252 |
|
253 ErrorResultMock rv; |
|
254 |
|
255 timeline.SetValueAtTime(20.0f, 1.0, rv); |
|
256 is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); |
|
257 } |
|
258 |
|
259 void TestBeforeFirstEventSetTarget() |
|
260 { |
|
261 Timeline timeline(10.0f); |
|
262 |
|
263 ErrorResultMock rv; |
|
264 |
|
265 timeline.SetTargetAtTime(20.0f, 1.0, 5.0, rv); |
|
266 is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); |
|
267 } |
|
268 |
|
269 void TestBeforeFirstEventLinearRamp() |
|
270 { |
|
271 Timeline timeline(10.0f); |
|
272 |
|
273 ErrorResultMock rv; |
|
274 |
|
275 timeline.LinearRampToValueAtTime(20.0f, 1.0, rv); |
|
276 is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); |
|
277 } |
|
278 |
|
279 void TestBeforeFirstEventExponentialRamp() |
|
280 { |
|
281 Timeline timeline(10.0f); |
|
282 |
|
283 ErrorResultMock rv; |
|
284 |
|
285 timeline.ExponentialRampToValueAtTime(20.0f, 1.0, rv); |
|
286 is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); |
|
287 } |
|
288 |
|
289 void TestAfterLastValueEvent() |
|
290 { |
|
291 Timeline timeline(10.0f); |
|
292 |
|
293 ErrorResultMock rv; |
|
294 |
|
295 timeline.SetValueAtTime(20.0f, 1.0, rv); |
|
296 is(timeline.GetValueAtTime(1.5), 20.0f, "Return the last value after the last SetValue event"); |
|
297 } |
|
298 |
|
299 void TestAfterLastTargetValueEvent() |
|
300 { |
|
301 Timeline timeline(10.0f); |
|
302 |
|
303 ErrorResultMock rv; |
|
304 |
|
305 timeline.SetTargetAtTime(20.0f, 1.0, 5.0, rv); |
|
306 is(timeline.GetValueAtTime(10.), (20.f + (10.f - 20.f) * expf(-9.0f / 5.0f)), "Return the value after the last SetTarget event based on the curve"); |
|
307 } |
|
308 |
|
309 void TestAfterLastTargetValueEventWithValueSet() |
|
310 { |
|
311 Timeline timeline(10.0f); |
|
312 |
|
313 ErrorResultMock rv; |
|
314 |
|
315 timeline.SetValue(50.f); |
|
316 timeline.SetTargetAtTime(20.0f, 1.0, 5.0, rv); |
|
317 |
|
318 // When using SetTargetValueAtTime, Timeline become stateful: the value for |
|
319 // time t may depend on the time t-1, so we can't just query the value at a |
|
320 // time and get the right value. We have to call GetValueAtTime for the |
|
321 // previous times. |
|
322 for (double i = 0.0; i < 9.99; i+=0.01) { |
|
323 timeline.GetValueAtTime(i); |
|
324 } |
|
325 |
|
326 is(timeline.GetValueAtTime(10.), (20.f + (50.f - 20.f) * expf(-9.0f / 5.0f)), "Return the value after SetValue and the last SetTarget event based on the curve"); |
|
327 } |
|
328 |
|
329 void TestValue() |
|
330 { |
|
331 Timeline timeline(10.0f); |
|
332 |
|
333 ErrorResultMock rv; |
|
334 |
|
335 is(timeline.Value(), 10.0f, "value should initially match the default value"); |
|
336 timeline.SetValue(20.0f); |
|
337 is(timeline.Value(), 20.0f, "Should be able to set the value"); |
|
338 timeline.SetValueAtTime(20.0f, 1.0, rv); |
|
339 // TODO: The following check needs to change when we compute the value based on the current time of the context |
|
340 is(timeline.Value(), 20.0f, "TODO..."); |
|
341 timeline.SetValue(30.0f); |
|
342 is(timeline.Value(), 20.0f, "Should not be able to set the value"); |
|
343 } |
|
344 |
|
345 void TestLinearRampAtZero() |
|
346 { |
|
347 Timeline timeline(10.0f); |
|
348 |
|
349 ErrorResultMock rv; |
|
350 |
|
351 timeline.LinearRampToValueAtTime(20.0f, 0.0, rv); |
|
352 is(timeline.GetValueAtTime(0.0), 20.0f, "Should get the correct value when t0 == t1 == 0"); |
|
353 } |
|
354 |
|
355 void TestExponentialRampAtZero() |
|
356 { |
|
357 Timeline timeline(10.0f); |
|
358 |
|
359 ErrorResultMock rv; |
|
360 |
|
361 timeline.ExponentialRampToValueAtTime(20.0f, 0.0, rv); |
|
362 is(timeline.GetValueAtTime(0.0), 20.0f, "Should get the correct value when t0 == t1 == 0"); |
|
363 } |
|
364 |
|
365 void TestLinearRampAtSameTime() |
|
366 { |
|
367 Timeline timeline(10.0f); |
|
368 |
|
369 ErrorResultMock rv; |
|
370 |
|
371 timeline.SetValueAtTime(5.0f, 1.0, rv); |
|
372 timeline.LinearRampToValueAtTime(20.0f, 1.0, rv); |
|
373 is(timeline.GetValueAtTime(1.0), 20.0f, "Should get the correct value when t0 == t1"); |
|
374 } |
|
375 |
|
376 void TestExponentialRampAtSameTime() |
|
377 { |
|
378 Timeline timeline(10.0f); |
|
379 |
|
380 ErrorResultMock rv; |
|
381 |
|
382 timeline.SetValueAtTime(5.0f, 1.0, rv); |
|
383 timeline.ExponentialRampToValueAtTime(20.0f, 1.0, rv); |
|
384 is(timeline.GetValueAtTime(1.0), 20.0f, "Should get the correct value when t0 == t1"); |
|
385 } |
|
386 |
|
387 void TestSetTargetZeroTimeConstant() |
|
388 { |
|
389 Timeline timeline(10.0f); |
|
390 |
|
391 ErrorResultMock rv; |
|
392 |
|
393 timeline.SetTargetAtTime(20.0f, 1.0, 0.0, rv); |
|
394 is(timeline.GetValueAtTime(10.), 20.f, "Should get the correct value with timeConstant == 0"); |
|
395 } |
|
396 |
|
397 void TestExponentialInvalidPreviousZeroValue() |
|
398 { |
|
399 Timeline timeline(0.f); |
|
400 |
|
401 ErrorResultMock rv; |
|
402 |
|
403 timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); |
|
404 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
405 timeline.SetValue(1.f); |
|
406 rv = NS_OK; |
|
407 timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); |
|
408 is(rv, NS_OK, "Should succeed this time"); |
|
409 timeline.CancelScheduledValues(0.0); |
|
410 is(timeline.GetEventCount(), 0u, "Should have no events scheduled"); |
|
411 rv = NS_OK; |
|
412 timeline.SetValueAtTime(0.f, 0.5, rv); |
|
413 is(rv, NS_OK, "Should succeed"); |
|
414 timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); |
|
415 is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); |
|
416 timeline.CancelScheduledValues(0.0); |
|
417 is(timeline.GetEventCount(), 0u, "Should have no events scheduled"); |
|
418 rv = NS_OK; |
|
419 timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); |
|
420 is(rv, NS_OK, "Should succeed this time"); |
|
421 } |
|
422 |
|
423 int main() |
|
424 { |
|
425 ScopedXPCOM xpcom("TestAudioEventTimeline"); |
|
426 if (xpcom.failed()) { |
|
427 return 1; |
|
428 } |
|
429 |
|
430 TestSpecExample(); |
|
431 TestInvalidEvents(); |
|
432 TestEventReplacement(); |
|
433 TestEventRemoval(); |
|
434 TestBeforeFirstEventSetValue(); |
|
435 TestBeforeFirstEventSetTarget(); |
|
436 TestBeforeFirstEventLinearRamp(); |
|
437 TestBeforeFirstEventExponentialRamp(); |
|
438 TestAfterLastValueEvent(); |
|
439 TestAfterLastTargetValueEvent(); |
|
440 TestAfterLastTargetValueEventWithValueSet(); |
|
441 TestValue(); |
|
442 TestLinearRampAtZero(); |
|
443 TestExponentialRampAtZero(); |
|
444 TestLinearRampAtSameTime(); |
|
445 TestExponentialRampAtSameTime(); |
|
446 TestSetTargetZeroTimeConstant(); |
|
447 TestExponentialInvalidPreviousZeroValue(); |
|
448 |
|
449 return gFailCount > 0; |
|
450 } |
|
451 |