diff --git a/src/test/js/media_manager.test.js b/src/test/js/media_manager.test.js index 44d82100..b959d294 100644 --- a/src/test/js/media_manager.test.js +++ b/src/test/js/media_manager.test.js @@ -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(); + }); + + }); });