michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "AudioEventTimeline.h" michael@0: #include "TestHarness.h" michael@0: #include michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: using std::numeric_limits; michael@0: michael@0: // Some simple testing primitives michael@0: void ok(bool val, const char* msg) michael@0: { michael@0: if (val) { michael@0: passed(msg); michael@0: } else { michael@0: fail(msg); michael@0: } michael@0: } michael@0: michael@0: namespace std { michael@0: michael@0: template michael@0: basic_ostream >& michael@0: operator<<(basic_ostream >& os, nsresult rv) michael@0: { michael@0: os << static_cast(rv); michael@0: return os; michael@0: } michael@0: michael@0: } michael@0: michael@0: template michael@0: void is(const T& a, const U& b, const char* msg) michael@0: { michael@0: std::stringstream ss; michael@0: ss << msg << ", Got: " << a << ", expected: " << b; michael@0: ok(a == b, ss.str().c_str()); michael@0: } michael@0: michael@0: template <> michael@0: void is(const float& a, const float& b, const char* msg) michael@0: { michael@0: // stupidly high, since we mostly care about the correctness of the algorithm michael@0: const float kEpsilon = 0.00001f; michael@0: michael@0: std::stringstream ss; michael@0: ss << msg << ", Got: " << a << ", expected: " << b; michael@0: ok(fabsf(a - b) < kEpsilon, ss.str().c_str()); michael@0: } michael@0: michael@0: class ErrorResultMock michael@0: { michael@0: public: michael@0: ErrorResultMock() michael@0: : mRv(NS_OK) michael@0: { michael@0: } michael@0: void Throw(nsresult aRv) michael@0: { michael@0: mRv = aRv; michael@0: } michael@0: michael@0: operator nsresult() const michael@0: { michael@0: return mRv; michael@0: } michael@0: michael@0: ErrorResultMock& operator=(nsresult aRv) michael@0: { michael@0: mRv = aRv; michael@0: return *this; michael@0: } michael@0: michael@0: private: michael@0: nsresult mRv; michael@0: }; michael@0: michael@0: typedef AudioEventTimeline Timeline; michael@0: michael@0: void TestSpecExample() michael@0: { michael@0: // First, run the basic tests michael@0: Timeline timeline(10.0f); michael@0: is(timeline.Value(), 10.0f, "Correct default value returned"); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: float curve[] = { -1.0f, 0.0f, 1.0f }; michael@0: michael@0: // This test is copied from the example in the Web Audio spec michael@0: const double t0 = 0.0, michael@0: t1 = 0.1, michael@0: t2 = 0.2, michael@0: t3 = 0.3, michael@0: t4 = 0.4, michael@0: t5 = 0.6, michael@0: t6 = 0.7, michael@0: t7 = 1.0; michael@0: timeline.SetValueAtTime(0.2f, t0, rv); michael@0: is(rv, NS_OK, "SetValueAtTime succeeded"); michael@0: timeline.SetValueAtTime(0.3f, t1, rv); michael@0: is(rv, NS_OK, "SetValueAtTime succeeded"); michael@0: timeline.SetValueAtTime(0.4f, t2, rv); michael@0: is(rv, NS_OK, "SetValueAtTime succeeded"); michael@0: timeline.LinearRampToValueAtTime(1.0f, t3, rv); michael@0: is(rv, NS_OK, "LinearRampToValueAtTime succeeded"); michael@0: timeline.LinearRampToValueAtTime(0.15f, t4, rv); michael@0: is(rv, NS_OK, "LinearRampToValueAtTime succeeded"); michael@0: timeline.ExponentialRampToValueAtTime(0.75f, t5, rv); michael@0: is(rv, NS_OK, "ExponentialRampToValueAtTime succeeded"); michael@0: timeline.ExponentialRampToValueAtTime(0.05f, t6, rv); michael@0: is(rv, NS_OK, "ExponentialRampToValueAtTime succeeded"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), t6, t7 - t6, rv); michael@0: is(rv, NS_OK, "SetValueCurveAtTime succeeded"); michael@0: michael@0: is(timeline.GetValueAtTime(0.0), 0.2f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.05), 0.2f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.1), 0.3f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.15), 0.3f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.2), 0.4f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.25), (0.4f + 1.0f) / 2, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.3), 1.0f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.35), (1.0f + 0.15f) / 2, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.4), 0.15f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.45), (0.15f * powf(0.75f / 0.15f, 0.05f / 0.2f)), "Correct value"); michael@0: is(timeline.GetValueAtTime(0.5), (0.15f * powf(0.75f / 0.15f, 0.5f)), "Correct value"); michael@0: is(timeline.GetValueAtTime(0.55), (0.15f * powf(0.75f / 0.15f, 0.15f / 0.2f)), "Correct value"); michael@0: is(timeline.GetValueAtTime(0.6), 0.75f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.65), (0.75f * powf(0.05f / 0.75f, 0.5f)), "Correct value"); michael@0: is(timeline.GetValueAtTime(0.7), -1.0f, "Correct value"); michael@0: is(timeline.GetValueAtTime(0.9), 0.0f, "Correct value"); michael@0: is(timeline.GetValueAtTime(1.0), 1.0f, "Correct value"); michael@0: } michael@0: michael@0: void TestInvalidEvents() michael@0: { michael@0: static_assert(numeric_limits::has_quiet_NaN, "Platform must have a quiet NaN"); michael@0: const float NaN = numeric_limits::quiet_NaN(); michael@0: const float Infinity = numeric_limits::infinity(); michael@0: Timeline timeline(10.0f); michael@0: michael@0: float curve[] = { -1.0f, 0.0f, 1.0f }; michael@0: float badCurve1[] = { -1.0f, NaN, 1.0f }; michael@0: float badCurve2[] = { -1.0f, Infinity, 1.0f }; michael@0: float badCurve3[] = { -1.0f, -Infinity, 1.0f }; michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValueAtTime(NaN, 0.1, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueAtTime(Infinity, 0.1, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueAtTime(-Infinity, 0.1, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.LinearRampToValueAtTime(NaN, 0.2, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.LinearRampToValueAtTime(Infinity, 0.2, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.LinearRampToValueAtTime(-Infinity, 0.2, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.ExponentialRampToValueAtTime(NaN, 0.3, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.ExponentialRampToValueAtTime(Infinity, 0.3, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.ExponentialRampToValueAtTime(-Infinity, 0.4, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.ExponentialRampToValueAtTime(0, 0.5, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetTargetAtTime(NaN, 0.4, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetTargetAtTime(Infinity, 0.4, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetTargetAtTime(-Infinity, 0.4, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetTargetAtTime(0.4f, NaN, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetTargetAtTime(0.4f, Infinity, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetTargetAtTime(0.4f, -Infinity, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(nullptr, 0, 1.0, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(badCurve1, ArrayLength(badCurve1), 1.0, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(badCurve2, ArrayLength(badCurve2), 1.0, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(badCurve3, ArrayLength(badCurve3), 1.0, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), NaN, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), Infinity, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), -Infinity, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), 1.0, NaN, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), 1.0, Infinity, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValueCurveAtTime(curve, ArrayLength(curve), 1.0, -Infinity, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: } michael@0: michael@0: void TestEventReplacement() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: is(timeline.GetEventCount(), 0u, "No events yet"); michael@0: timeline.SetValueAtTime(10.0f, 0.1, rv); michael@0: is(timeline.GetEventCount(), 1u, "One event scheduled now"); michael@0: timeline.SetValueAtTime(20.0f, 0.1, rv); michael@0: is(rv, NS_OK, "Event scheduling should be successful"); michael@0: is(timeline.GetEventCount(), 1u, "Event should be replaced"); michael@0: is(timeline.GetValueAtTime(0.1), 20.0f, "The first event should be overwritten"); michael@0: timeline.LinearRampToValueAtTime(30.0f, 0.1, rv); michael@0: is(rv, NS_OK, "Event scheduling should be successful"); michael@0: is(timeline.GetEventCount(), 2u, "Different event type should be appended"); michael@0: is(timeline.GetValueAtTime(0.1), 30.0f, "The first event should be overwritten"); michael@0: } michael@0: michael@0: void TestEventRemoval() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValueAtTime(10.0f, 0.1, rv); michael@0: timeline.SetValueAtTime(15.0f, 0.15, rv); michael@0: timeline.SetValueAtTime(20.0f, 0.2, rv); michael@0: timeline.LinearRampToValueAtTime(30.0f, 0.3, rv); michael@0: is(timeline.GetEventCount(), 4u, "Should have three events initially"); michael@0: timeline.CancelScheduledValues(0.4); michael@0: is(timeline.GetEventCount(), 4u, "Trying to delete past the end of the array should have no effect"); michael@0: timeline.CancelScheduledValues(0.3); michael@0: is(timeline.GetEventCount(), 3u, "Should successfully delete one event"); michael@0: timeline.CancelScheduledValues(0.12); michael@0: is(timeline.GetEventCount(), 1u, "Should successfully delete two events"); michael@0: timeline.CancelAllEvents(); michael@0: ok(timeline.HasSimpleValue(), "No event should remain scheduled"); michael@0: } michael@0: michael@0: void TestBeforeFirstEventSetValue() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValueAtTime(20.0f, 1.0, rv); michael@0: is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); michael@0: } michael@0: michael@0: void TestBeforeFirstEventSetTarget() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetTargetAtTime(20.0f, 1.0, 5.0, rv); michael@0: is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); michael@0: } michael@0: michael@0: void TestBeforeFirstEventLinearRamp() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.LinearRampToValueAtTime(20.0f, 1.0, rv); michael@0: is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); michael@0: } michael@0: michael@0: void TestBeforeFirstEventExponentialRamp() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.ExponentialRampToValueAtTime(20.0f, 1.0, rv); michael@0: is(timeline.GetValueAtTime(0.5), 10.0f, "Retrun the default value before the first event"); michael@0: } michael@0: michael@0: void TestAfterLastValueEvent() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValueAtTime(20.0f, 1.0, rv); michael@0: is(timeline.GetValueAtTime(1.5), 20.0f, "Return the last value after the last SetValue event"); michael@0: } michael@0: michael@0: void TestAfterLastTargetValueEvent() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetTargetAtTime(20.0f, 1.0, 5.0, rv); michael@0: 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"); michael@0: } michael@0: michael@0: void TestAfterLastTargetValueEventWithValueSet() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValue(50.f); michael@0: timeline.SetTargetAtTime(20.0f, 1.0, 5.0, rv); michael@0: michael@0: // When using SetTargetValueAtTime, Timeline become stateful: the value for michael@0: // time t may depend on the time t-1, so we can't just query the value at a michael@0: // time and get the right value. We have to call GetValueAtTime for the michael@0: // previous times. michael@0: for (double i = 0.0; i < 9.99; i+=0.01) { michael@0: timeline.GetValueAtTime(i); michael@0: } michael@0: michael@0: 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"); michael@0: } michael@0: michael@0: void TestValue() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: is(timeline.Value(), 10.0f, "value should initially match the default value"); michael@0: timeline.SetValue(20.0f); michael@0: is(timeline.Value(), 20.0f, "Should be able to set the value"); michael@0: timeline.SetValueAtTime(20.0f, 1.0, rv); michael@0: // TODO: The following check needs to change when we compute the value based on the current time of the context michael@0: is(timeline.Value(), 20.0f, "TODO..."); michael@0: timeline.SetValue(30.0f); michael@0: is(timeline.Value(), 20.0f, "Should not be able to set the value"); michael@0: } michael@0: michael@0: void TestLinearRampAtZero() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.LinearRampToValueAtTime(20.0f, 0.0, rv); michael@0: is(timeline.GetValueAtTime(0.0), 20.0f, "Should get the correct value when t0 == t1 == 0"); michael@0: } michael@0: michael@0: void TestExponentialRampAtZero() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.ExponentialRampToValueAtTime(20.0f, 0.0, rv); michael@0: is(timeline.GetValueAtTime(0.0), 20.0f, "Should get the correct value when t0 == t1 == 0"); michael@0: } michael@0: michael@0: void TestLinearRampAtSameTime() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValueAtTime(5.0f, 1.0, rv); michael@0: timeline.LinearRampToValueAtTime(20.0f, 1.0, rv); michael@0: is(timeline.GetValueAtTime(1.0), 20.0f, "Should get the correct value when t0 == t1"); michael@0: } michael@0: michael@0: void TestExponentialRampAtSameTime() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetValueAtTime(5.0f, 1.0, rv); michael@0: timeline.ExponentialRampToValueAtTime(20.0f, 1.0, rv); michael@0: is(timeline.GetValueAtTime(1.0), 20.0f, "Should get the correct value when t0 == t1"); michael@0: } michael@0: michael@0: void TestSetTargetZeroTimeConstant() michael@0: { michael@0: Timeline timeline(10.0f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.SetTargetAtTime(20.0f, 1.0, 0.0, rv); michael@0: is(timeline.GetValueAtTime(10.), 20.f, "Should get the correct value with timeConstant == 0"); michael@0: } michael@0: michael@0: void TestExponentialInvalidPreviousZeroValue() michael@0: { michael@0: Timeline timeline(0.f); michael@0: michael@0: ErrorResultMock rv; michael@0: michael@0: timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.SetValue(1.f); michael@0: rv = NS_OK; michael@0: timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); michael@0: is(rv, NS_OK, "Should succeed this time"); michael@0: timeline.CancelScheduledValues(0.0); michael@0: is(timeline.GetEventCount(), 0u, "Should have no events scheduled"); michael@0: rv = NS_OK; michael@0: timeline.SetValueAtTime(0.f, 0.5, rv); michael@0: is(rv, NS_OK, "Should succeed"); michael@0: timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); michael@0: is(rv, NS_ERROR_DOM_SYNTAX_ERR, "Correct error code returned"); michael@0: timeline.CancelScheduledValues(0.0); michael@0: is(timeline.GetEventCount(), 0u, "Should have no events scheduled"); michael@0: rv = NS_OK; michael@0: timeline.ExponentialRampToValueAtTime(1.f, 1.0, rv); michael@0: is(rv, NS_OK, "Should succeed this time"); michael@0: } michael@0: michael@0: int main() michael@0: { michael@0: ScopedXPCOM xpcom("TestAudioEventTimeline"); michael@0: if (xpcom.failed()) { michael@0: return 1; michael@0: } michael@0: michael@0: TestSpecExample(); michael@0: TestInvalidEvents(); michael@0: TestEventReplacement(); michael@0: TestEventRemoval(); michael@0: TestBeforeFirstEventSetValue(); michael@0: TestBeforeFirstEventSetTarget(); michael@0: TestBeforeFirstEventLinearRamp(); michael@0: TestBeforeFirstEventExponentialRamp(); michael@0: TestAfterLastValueEvent(); michael@0: TestAfterLastTargetValueEvent(); michael@0: TestAfterLastTargetValueEventWithValueSet(); michael@0: TestValue(); michael@0: TestLinearRampAtZero(); michael@0: TestExponentialRampAtZero(); michael@0: TestLinearRampAtSameTime(); michael@0: TestExponentialRampAtSameTime(); michael@0: TestSetTargetZeroTimeConstant(); michael@0: TestExponentialInvalidPreviousZeroValue(); michael@0: michael@0: return gFailCount > 0; michael@0: } michael@0: