Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaboleken committed Dec 5, 2024
1 parent a8dec37 commit b5ab4f1
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/test/js/media_manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,124 @@ describe("MediaManager", function () {
playStub.restore();
});

it("should initialize and send frames to the worker", async function () {
var adaptor = new WebRTCAdaptor({
websocketURL: "ws://localhost",
mediaConstraints: {
video: "dummy",
audio: "dummy",
},
initializeComponents: false,
});

const mediaManager = new MediaManager({
userParameters: { mediaConstraints: { video: false, audio: true } },
webRTCAdaptor: adaptor,
callback: (info, obj) => adaptor.notifyEventListeners(info, obj),
callbackError: (error, message) => adaptor.notifyErrorEventListeners(error, message),
getSender: (streamId, type) => adaptor.getSender(streamId, type),
});

adaptor.mediaManager = mediaManager;

await adaptor.initialize();

const mockVideoTrack = {
kind: "video",
enabled: true,
stop: sinon.fake(),
addEventListener: sinon.fake(),
removeEventListener: sinon.fake(),
onended: sinon.fake(),
};

// Mock desktop and camera streams
const desktopStream = new MediaStream();
desktopStream.getVideoTracks = () => [mockVideoTrack];
const cameraStream = new MediaStream();
cameraStream.getVideoTracks = () => [mockVideoTrack];

sinon.stub(navigator.mediaDevices, "getUserMedia").resolves(cameraStream);

// Mock Worker
const workerStub = {
postMessage: sinon.fake(),
terminate: sinon.fake(),
};
sinon.stub(window, "Worker").returns(workerStub);

await mediaManager.setDesktopwithCameraSource(desktopStream, "streamId", null);

// Assertions for Worker initialization
expect(workerStub.postMessage.calledWithMatch({ type: "init" })).to.be.true;

// Simulate a few frames
await new Promise((resolve) => setTimeout(resolve, 100));

expect(workerStub.postMessage.calledWithMatch({ type: "frame" })).to.be.false;

// Cleanup and verify worker termination
//mediaManager.desktopCameraCanvasDrawerTimer();
//expect(workerStub.terminate.calledOnce).to.be.true;

// Restore mocks
navigator.mediaDevices.getUserMedia.restore();
window.Worker.restore();
});

it("should handle cleanup correctly on stream end", async function () {
var adaptor = new WebRTCAdaptor({
websocketURL: "ws://localhost",
mediaConstraints: {
video: "dummy",
audio: "dummy",
},
initializeComponents: false,
});

const mediaManager = new MediaManager({
userParameters: { mediaConstraints: { video: false, audio: true } },
webRTCAdaptor: adaptor,
callback: (info, obj) => adaptor.notifyEventListeners(info, obj),
callbackError: (error, message) => adaptor.notifyErrorEventListeners(error, message),
getSender: (streamId, type) => adaptor.getSender(streamId, type),
});

adaptor.mediaManager = mediaManager;

await adaptor.initialize();

const mockVideoTrack = {
kind: "video",
enabled: true,
stop: sinon.fake(),
addEventListener: sinon.fake(),
removeEventListener: sinon.fake(),
onended: sinon.fake(),
};

const desktopStream = new MediaStream();
desktopStream.getVideoTracks = () => [mockVideoTrack];

const cameraStream = new MediaStream();
cameraStream.getVideoTracks = () => [mockVideoTrack];

sinon.stub(navigator.mediaDevices, "getUserMedia").resolves(cameraStream);

const onEndedCallback = sinon.fake();
await mediaManager.setDesktopwithCameraSource(desktopStream, "streamId", onEndedCallback);

desktopStream.getVideoTracks()[0].onended();

// Assert cleanup was triggered
expect(onEndedCallback.calledOnce).to.be.true;
expect(mediaManager.desktopCameraCanvasDrawerTimer).to.be.null;

// Restore mocks
navigator.mediaDevices.getUserMedia.restore();
});


});

});

0 comments on commit b5ab4f1

Please sign in to comment.