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 affa4b5 commit 1ed23b8
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/bufferPlayer/BufferPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ 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
201 changes: 199 additions & 2 deletions tests/BufferPlayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ import { describe, expect, test } from "@jest/globals";
import "reflect-metadata";
import EventEmitter from "../lib/utils/EventEmitter";
import BufferPlayer from "../lib/bufferPlayer/BufferPlayer";
import Constants from "../lib/model/Constants";
import MockAudioNode from "./MockAudioNode";
import { EventType } from "../lib/model/EventTypeEnum";

(window as any) = {};
(window as any).setInterval

jest.spyOn(window, "setInterval");

describe("BufferPlayer tests", () => {
let bufferPlayer: BufferPlayer;
let mockEventEmitter: EventEmitter;

beforeEach(() => {
jest.useFakeTimers();
jest.clearAllTimers();

// Mock AudioContextManagerInterface
const mockAudioContextManager = {
currentContext: {
Expand All @@ -27,7 +35,7 @@ describe("BufferPlayer tests", () => {
destination: {} // Mock destination
}
};
const mockEventEmitter = new EventEmitter();
mockEventEmitter = new EventEmitter();

bufferPlayer = new BufferPlayer(mockAudioContextManager);
bufferPlayer.injectDependencies(null, null, null, mockEventEmitter);
Expand Down Expand Up @@ -72,6 +80,7 @@ describe("BufferPlayer tests", () => {
test("Pause playing", async () => {
bufferPlayer.loadBuffer({} as AudioBuffer);
await bufferPlayer.start();
expect(bufferPlayer.playing).toBe(true);
bufferPlayer.pause();
expect(bufferPlayer.playing).toBe(false);
});
Expand Down Expand Up @@ -107,4 +116,192 @@ describe("BufferPlayer tests", () => {
bufferPlayer.toggleLoop();
expect(bufferPlayer.loop).toBe(false);
});

test("Get current time display", () => {
const mockAudioBuffer = {
duration: 60
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(30);
expect(bufferPlayer.currentTimeDisplay).toBe("00:30");
});

test("Get current time display - 2", () => {
const mockAudioBuffer = {
duration: 350
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(320);
expect(bufferPlayer.currentTimeDisplay).toBe("05:20");
});

test("Get max time display", () => {
const mockAudioBuffer = {
duration: 45
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
expect(bufferPlayer.maxTimeDisplay).toBe("00:45");
});

test("Get max time display - 2", () => {
const mockAudioBuffer = {
duration: 490
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
expect(bufferPlayer.maxTimeDisplay).toBe("08:10");
});

test("Get percent", () => {
const mockAudioBuffer = {
duration: 500
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(250);
expect(bufferPlayer.percent).toBe(50);
});

test("Get percent - 2", () => {
const mockAudioBuffer = {
duration: 345
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(345);
expect(bufferPlayer.percent).toBe(100);
});

test("Get percent - 3", () => {
const mockAudioBuffer = {
duration: 500
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(0);
expect(bufferPlayer.percent).toBe(0);
});

test("Get remaining time display", () => {
const mockAudioBuffer = {
duration: 60
} as AudioBuffer;
bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(35);
expect(bufferPlayer.remainingTimeDisplay).toBe("00:25");
});

test("Set time while playing", async () => {
const mockAudioBuffer = {
duration: 60
} as AudioBuffer;

jest.spyOn(bufferPlayer, "pause");
jest.spyOn(bufferPlayer, "start");

bufferPlayer.loadBuffer(mockAudioBuffer);
await bufferPlayer.start();
bufferPlayer.setTime(20);

expect(bufferPlayer.pause).toHaveBeenCalled();
expect(bufferPlayer.start).toHaveBeenCalled();
expect(bufferPlayer.remainingTimeDisplay).toBe("00:40");
expect(bufferPlayer.percent).toBe(33);
expect(bufferPlayer.currentTimeDisplay).toBe("00:20");
});

test("Set percent time while playing", async () => {
const mockAudioBuffer = {
duration: 60
} as AudioBuffer;

jest.spyOn(bufferPlayer, "pause");
jest.spyOn(bufferPlayer, "start");

bufferPlayer.loadBuffer(mockAudioBuffer);
await bufferPlayer.start();
bufferPlayer.setTimePercent(50);

expect(bufferPlayer.pause).toHaveBeenCalled();
expect(bufferPlayer.start).toHaveBeenCalled();
expect(bufferPlayer.remainingTimeDisplay).toBe("00:30");
expect(bufferPlayer.percent).toBe(50);
expect(bufferPlayer.currentTimeDisplay).toBe("00:30");
});

test("Play direct - not compatibility mode", async () => {
const mockAudioBuffer = {
duration: 60
} as AudioBuffer;

jest.spyOn(bufferPlayer, "start");

bufferPlayer.loadBuffer(mockAudioBuffer);
await bufferPlayer.playDirect();

expect(bufferPlayer.start).toHaveBeenCalledWith(true);
});

test("Play direct - compatibility mode", async () => {
jest.spyOn(bufferPlayer, "start");

bufferPlayer.setCompatibilityMode(new MockAudioNode(), 60);
await bufferPlayer.playDirect();

expect(bufferPlayer.start).toHaveBeenCalledWith(false);
});

test("Play - end playing", async () => {
jest.spyOn(bufferPlayer, "reset");
jest.spyOn(mockEventEmitter, "emit");

bufferPlayer.setCompatibilityMode(new MockAudioNode(), 60);
bufferPlayer.setTime(60);

await bufferPlayer.start();

jest.runAllTimers();

expect(bufferPlayer.reset).toHaveBeenCalled();
expect(mockEventEmitter.emit).toHaveBeenCalledWith(EventType.PLAYING_FINISHED);
});

test("Play - end playing - Loop (compatibility mode)", async () => {
jest.spyOn(bufferPlayer, "start");
jest.spyOn(mockEventEmitter, "emit");

bufferPlayer.setCompatibilityMode(new MockAudioNode(), 30);
bufferPlayer.setTime(0);
bufferPlayer.loop = true;

await bufferPlayer.start();

jest.advanceTimersByTime(31000);

expect(bufferPlayer.start).toHaveBeenCalledTimes(1);
expect(mockEventEmitter.emit).toHaveBeenCalledWith(EventType.PLAYING_FINISHED);
});

test("Play - end playing - Loop (not compatibility mode)", async () => {
const mockAudioBuffer = {
duration: 120
} as AudioBuffer;

jest.spyOn(bufferPlayer, "start");
jest.spyOn(bufferPlayer, "reset");
jest.spyOn(mockEventEmitter, "emit");

bufferPlayer.loadBuffer(mockAudioBuffer);
bufferPlayer.setTime(0);
bufferPlayer.loop = true;

await bufferPlayer.start();

jest.advanceTimersByTime(121000);

expect(bufferPlayer.start).toHaveBeenCalledTimes(2);
expect(bufferPlayer.reset).toHaveBeenCalledTimes(2);
expect(mockEventEmitter.emit).not.toHaveBeenCalledWith(EventType.PLAYING_FINISHED);
});

test("Should return order and id correctly", () => {
expect(bufferPlayer.order).toBe(-1);
expect(bufferPlayer.id).toBe(Constants.BUFFER_PLAYER);
});
});
22 changes: 22 additions & 0 deletions tests/SaveBufferManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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";

(AudioContext as any) = MockAudioContext;
(AudioBuffer as any) = MockAudioBuffer;
Expand Down Expand Up @@ -113,6 +114,9 @@ describe("SaveBufferManager tests", () => {
jest.spyOn(mockRecorder, "stop");
jest.spyOn(mockRecorder, "record");

jest.spyOn(eventEmitter, "off");
jest.spyOn(eventEmitter, "on");

const savePromise = saveBufferManager.saveBuffer(mockRenderedBuffer, saveBufferOptions, mockRecorder as any);

setTimeout(() => {
Expand All @@ -124,6 +128,8 @@ describe("SaveBufferManager tests", () => {
expect(mockRecorder.stop).toHaveBeenCalled();
expect(mockRecorder.exportWAV).toHaveBeenCalled();
expect(mockRecorder.exportMP3).not.toHaveBeenCalled();
expect(eventEmitter.off).toHaveBeenCalledWith(EventType.PLAYING_FINISHED, expect.any(Function));
expect(eventEmitter.off).toHaveBeenCalledWith(EventType.PLAYING_STOPPED, expect.any(Function));
});

test("Save buffer direct - compatibility mode - success (MP3)", async () => {
Expand All @@ -134,6 +140,9 @@ describe("SaveBufferManager tests", () => {
bitrate: 128
};

jest.spyOn(eventEmitter, "off");
jest.spyOn(eventEmitter, "on");

mockFilterManagerInstance.connectNodes(createMockAudioContext(), new AudioBuffer({ length: 44100, numberOfChannels: 2, sampleRate: 44100 }), false, true);
mockBufferPlayerInstance.compatibilityMode = true;

Expand All @@ -149,6 +158,8 @@ describe("SaveBufferManager tests", () => {
expect(await savePromise).toBe(true);
expect(mockRecorder.exportMP3).toHaveBeenCalled();
expect(mockRecorder.exportWAV).not.toHaveBeenCalled();
expect(eventEmitter.off).toHaveBeenCalledWith(EventType.PLAYING_FINISHED, expect.any(Function));
expect(eventEmitter.off).toHaveBeenCalledWith(EventType.PLAYING_STOPPED, expect.any(Function));
});

test("Save buffer direct - compatibility mode - cancel", async () => {
Expand All @@ -160,6 +171,9 @@ describe("SaveBufferManager tests", () => {
bitrate: 128
};

jest.spyOn(eventEmitter, "off");
jest.spyOn(eventEmitter, "on");

mockFilterManagerInstance.connectNodes(createMockAudioContext(), new AudioBuffer({ length: 44100, numberOfChannels: 2, sampleRate: 44100 }), false, true);

mockBufferPlayerInstanceOther.compatibilityMode = true;
Expand All @@ -180,6 +194,8 @@ describe("SaveBufferManager tests", () => {
expect(mockRecorder.exportMP3).not.toHaveBeenCalled();
expect(mockRecorder.exportWAV).not.toHaveBeenCalled();
expect(mockRecorder.stop).not.toHaveBeenCalled();
expect(eventEmitter.off).toHaveBeenCalledWith(EventType.PLAYING_FINISHED, expect.any(Function));
expect(eventEmitter.off).toHaveBeenCalledWith(EventType.PLAYING_STOPPED, expect.any(Function));
});

test("Save buffer direct - failure (cannot save if we are already saving)", async () => {
Expand All @@ -200,4 +216,10 @@ describe("SaveBufferManager tests", () => {

await expect(saveBufferManager.saveBuffer(mockRenderedBuffer, saveBufferOptions)).rejects.toThrowError("The buffer is currently saving");
});

test("should return order and id correctly", () => {
const saveBufferManager = new SaveBufferManager(mockFilterManagerInstance, mockAudioContextManagerInstance, mockBufferPlayerInstance);
expect(saveBufferManager.order).toBe(-1);
expect(saveBufferManager.id).toBe(Constants.SAVE_BUFFER_MANAGER);
});
});

0 comments on commit 1ed23b8

Please sign in to comment.