Skip to content

Commit

Permalink
Add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliastik committed May 27, 2024
1 parent 1ed23b8 commit 5983e5b
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 11 deletions.
3 changes: 0 additions & 3 deletions lib/bufferPlayer/BufferPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ export default class BufferPlayer extends AbstractAudioElement implements Buffer
this.currentTime += (nextTime / 1000) * this.speedAudio;
this.displayTime = this.currentTime;

console.log(this.currentTime);

if (this.currentTime > this.duration) {
console.log("ok", this.loop);
if (this.loop) {
if (!this.compatibilityMode) {
this.reset(direct);
Expand Down
6 changes: 6 additions & 0 deletions tests/AudioContextMock.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MockAudioBuffer } from "./AudioBufferMock";

export class MockAudioContext {
sampleRate: number;
currentTime: number;
Expand Down Expand Up @@ -53,6 +55,10 @@ export class MockAudioContext {
}
} as unknown as MediaStreamAudioSourceNode;
}

startRendering() {
return Promise.resolve(new MockAudioBuffer(2, 1000, 44100));
}
}

export function createMockAudioContext(options?: AudioContextOptions): AudioContext {
Expand Down
19 changes: 17 additions & 2 deletions tests/AudioEditorObjectsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import BufferPlayerInterface from "../lib/bufferPlayer/interfaces/BufferPlayerIn
import EventEmitterInterface from "../lib/utils/interfaces/EventEmitterInterface";
import { MockAudioBuffer } from "./AudioBufferMock";
import { createMockAudioContext } from "./AudioContextMock";
import MockAudioNode from "./MockAudioNode";

const mockFilterManager = {
addFilters: jest.fn(),
Expand All @@ -26,7 +27,11 @@ const mockFilterManager = {
getAddingTime: jest.fn(),
setupTotalSamples: jest.fn(),
resetFilterBuffers: jest.fn(),
currentNodes: jest.fn()
currentNodes: {
input: new MockAudioNode(),
output: new MockAudioNode(),
intermediateNodes: []
}
} as unknown as FilterManagerInterface;

const mockFilterManagerWithoutEntrypoint = {
Expand Down Expand Up @@ -55,6 +60,14 @@ const mockRendererManager = {
executeAudioRenderers: jest.fn()
} as unknown as RendererManagerInterface;

const mockRendererManagerWithFakeRendererBuffer = {
addRenderers: jest.fn(),
toggleRenderer: jest.fn(),
resetAllRenderersState: jest.fn(),
getRenderersState: jest.fn().mockReturnValue({}),
executeAudioRenderers: jest.fn().mockReturnValue(new MockAudioBuffer(2, 1000, 44100))
} as unknown as RendererManagerInterface;

const mockContextManager = {
currentContext: createMockAudioContext(),
currentSampleRate: 44100,
Expand Down Expand Up @@ -89,6 +102,8 @@ const mockBufferPlayer = {
start: jest.fn(),
compatibilityMode: false,
loop: false,
loadBuffer: jest.fn(),
setCompatibilityMode: jest.fn()
} as unknown as BufferPlayerInterface;

const mockEventEmitter = {
Expand All @@ -105,4 +120,4 @@ const mockRecorder = {
exportWAV: jest.fn((callback) => callback(new Blob()))
};

export { mockAudioProcessor, mockBufferManager, mockContextManager, mockFilterManager, mockRendererManager, mockSaveBufferManager, mockBufferPlayer, mockEventEmitter, mockFilterManagerWithoutEntrypoint, mockRecorder };
export { mockAudioProcessor, mockBufferManager, mockContextManager, mockFilterManager, mockRendererManager, mockSaveBufferManager, mockBufferPlayer, mockEventEmitter, mockFilterManagerWithoutEntrypoint, mockRecorder, mockRendererManagerWithFakeRendererBuffer };
112 changes: 107 additions & 5 deletions tests/AudioProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import "reflect-metadata";
import AudioProcessor from "../lib/audioEditor/AudioProcessor";
import { mockBufferManager, mockContextManager, mockFilterManager, mockRendererManager, mockBufferPlayer, mockFilterManagerWithoutEntrypoint } from "./AudioEditorObjectsMock";
import { mockBufferManager, mockContextManager, mockFilterManager, mockRendererManager, mockBufferPlayer, mockFilterManagerWithoutEntrypoint, mockRendererManagerWithFakeRendererBuffer } from "./AudioEditorObjectsMock";
import { MockAudioContext, createMockAudioContext } from "./AudioContextMock";
import { MockAudioBuffer } from "./AudioBufferMock";
import Constants from "../lib/model/Constants";
import GenericConfigService from "../lib/services/GenericConfigService";
import EventEmitter from "../lib/utils/EventEmitter";
import { EventType } from "../lib/model/EventTypeEnum";

(OfflineAudioContext as any) = MockAudioContext;

Expand All @@ -30,7 +33,7 @@ describe("AudioProcessor", () => {
});

test("renderAudio should throw error if required dependencies are not available", async () => {
const audioProcessor2 = audioProcessor = new AudioProcessor(
const audioProcessor2 = new AudioProcessor(
mockFilterManager,
mockRendererManager,
undefined,
Expand All @@ -46,7 +49,7 @@ describe("AudioProcessor", () => {
});

test("renderAudio should throw error if no filter manager available", async () => {
const audioProcessor2 = audioProcessor = new AudioProcessor(
const audioProcessor2 = new AudioProcessor(
undefined,
mockRendererManager,
mockContextManager,
Expand All @@ -58,7 +61,7 @@ describe("AudioProcessor", () => {
});

test("renderAudio should throw error if no renderer manager available", async () => {
const audioProcessor2 = audioProcessor = new AudioProcessor(
const audioProcessor2 = new AudioProcessor(
mockFilterManager,
undefined,
mockContextManager,
Expand All @@ -70,7 +73,7 @@ describe("AudioProcessor", () => {
});

test("renderAudio should throw error if no entrypoint filter available", async () => {
const audioProcessor2 = audioProcessor = new AudioProcessor(
const audioProcessor2 = new AudioProcessor(
mockFilterManagerWithoutEntrypoint,
mockRendererManager,
mockContextManager,
Expand All @@ -81,6 +84,105 @@ describe("AudioProcessor", () => {
await expect(audioProcessor2.renderAudio(null)).rejects.toThrow("Entrypoint filter is not available");
});

test("Render audio - initial rendering disabled", async () => {
const audioProcessor2 = new AudioProcessor(
mockFilterManager,
mockRendererManager,
mockContextManager,
mockBufferPlayer,
mockBufferManager
);
const genericConfigService = new GenericConfigService();

(audioProcessor2 as any).injectDependencies(null, null, genericConfigService, new EventEmitter());

genericConfigService.setConfig(Constants.PREFERENCES_KEYS.DISABLE_INITIAL_RENDERING, "true");

jest.spyOn(audioProcessor2, "setupOutput");

expect(await audioProcessor2.renderAudio(new MockAudioBuffer(2, 10000, 44100))).toBe(true);
expect(audioProcessor2.setupOutput).not.toHaveBeenCalled();
});

test("Render audio - switching between compatibility and normal mode", async () => {
const audioProcessor2 = new AudioProcessor(
mockFilterManager,
mockRendererManager,
mockContextManager,
mockBufferPlayer,
mockBufferManager
);
const genericConfigService = new GenericConfigService();

(audioProcessor2 as any).injectDependencies(null, null, genericConfigService, new EventEmitter());

genericConfigService.setConfig(Constants.PREFERENCES_KEYS.DISABLE_INITIAL_RENDERING, "false");
genericConfigService.setConfig(Constants.PREFERENCES_KEYS.COMPATIBILITY_MODE_ENABLED, "false");

mockBufferPlayer.compatibilityMode = true;

jest.spyOn(mockBufferPlayer, "stop");

await audioProcessor2.renderAudio(new MockAudioBuffer(2, 10000, 44100));

expect(mockBufferPlayer.stop).toHaveBeenCalled(); // Should stop the buffer player
});

test("Render audio - Setup output", async () => {
const audioProcessor2 = new AudioProcessor(
mockFilterManager,
mockRendererManagerWithFakeRendererBuffer,
mockContextManager,
mockBufferPlayer,
mockBufferManager
);
const genericConfigService = new GenericConfigService();

(audioProcessor2 as any).injectDependencies(null, null, genericConfigService, new EventEmitter());

genericConfigService.setConfig(Constants.PREFERENCES_KEYS.DISABLE_INITIAL_RENDERING, "false");

jest.spyOn(audioProcessor2, "setupOutput");
jest.spyOn(mockFilterManager, "initializeWorklets");
jest.spyOn(mockFilterManager, "connectNodes");

expect(await audioProcessor2.renderAudio(new MockAudioBuffer(2, 10000, 44100))).toBe(true);

expect(audioProcessor2.setupOutput).toHaveBeenCalled();
expect(mockFilterManager.initializeWorklets).toHaveBeenCalled();
expect(mockFilterManager.connectNodes).toHaveBeenCalled();
});

test("Render audio - Setup output - Compatibility mode", async () => {
const audioProcessor2 = new AudioProcessor(
mockFilterManager,
mockRendererManagerWithFakeRendererBuffer,
mockContextManager,
mockBufferPlayer,
mockBufferManager
);
const genericConfigService = new GenericConfigService();

(audioProcessor2 as any).injectDependencies(null, null, genericConfigService, new EventEmitter());

genericConfigService.setConfig(Constants.PREFERENCES_KEYS.DISABLE_INITIAL_RENDERING, "false");
genericConfigService.setConfig(Constants.PREFERENCES_KEYS.COMPATIBILITY_MODE_ENABLED, "true");

mockBufferPlayer.compatibilityMode = true;

jest.spyOn(audioProcessor2, "setupOutput");
jest.spyOn(mockFilterManager, "initializeWorklets");
jest.spyOn(mockFilterManager, "connectNodes");
jest.spyOn(mockBufferPlayer, "setCompatibilityMode");

expect(await audioProcessor2.renderAudio(new MockAudioBuffer(2, 10000, 44100))).toBe(true);

expect(audioProcessor2.setupOutput).toHaveBeenCalled();
expect(mockFilterManager.initializeWorklets).toHaveBeenCalled();
expect(mockFilterManager.connectNodes).toHaveBeenCalled();
expect(mockBufferPlayer.setCompatibilityMode).toHaveBeenCalled();
});

test("setupOutput should return false if required dependencies are missing", async () => {
const result = await audioProcessor.setupOutput(null, createMockAudioContext());
expect(result).toBe(false);
Expand Down
1 change: 0 additions & 1 deletion tests/SaveBufferManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import GenericConfigService from "../lib/services/GenericConfigService";
import { MockEntrypointFilter } from "./MockAudioFilter";
import EventEmitter from "../lib/utils/EventEmitter";
import { EventType } from "../lib/model/EventTypeEnum";
import { Recorder } from "../lib/recorder/Recorder";
import { mockRecorder } from "./AudioEditorObjectsMock";
import Constants from "../lib/model/Constants";

Expand Down

0 comments on commit 5983e5b

Please sign in to comment.