diff --git a/src/utilities/HiFiUtilities.ts b/src/utilities/HiFiUtilities.ts index e3ee80fe..b125729b 100644 --- a/src/utilities/HiFiUtilities.ts +++ b/src/utilities/HiFiUtilities.ts @@ -195,21 +195,21 @@ export class HiFiUtilities { } static checkBrowserCompatibility(): Boolean { - let requiredFeatures: Array = [ + let requiredFeatures: Array<{ name: string, value: () => any }> = [ // Navigator mediaDevices - "navigator", // Found on source code HiFiMixerSession.ts, RaviStreamController.ts - "navigator.mediaDevices.getUserMedia", // Found on source code HiFiMixerSession.ts (ln.590) - "navigator.mediaDevices.getSupportedConstraints", // Found on source code HiFiUtilities (ln. 130, 134, 138) + { name: "navigator", value: () => typeof navigator !== "undefined" ? navigator : undefined }, // Found on source code HiFiMixerSession.ts, RaviStreamController.ts + { name: "navigator.mediaDevices.getUserMedia", value: () => typeof navigator !== "undefined" && navigator.mediaDevices ? navigator.mediaDevices.getUserMedia : undefined }, // Found on source code HiFiMixerSession.ts (ln.590) + { name: "navigator.mediaDevices.getSupportedConstraints", value: () => typeof navigator !== "undefined" && navigator.mediaDevices ? navigator.mediaDevices.getSupportedConstraints : undefined }, // Found on source code HiFiUtilities (ln. 130, 134, 138) // WebRTC - "window.MediaStream", // Found on source code HiFiCommunicator.ts, HiFiMixerSession.ts, RaviSession.ts, RaviStreamController.ts - "window.RTCDataChannel", // Found on source code RaviCommandController.ts (ln.151) - "window.RTCPeerConnection", // Found on source code RaviSession.ts (ln.603) - "window.RTCSessionDescription" // Found on source code RaviSession.ts (ln.604) + { name: "window.MediaStream", value: () => typeof window !== "undefined" ? window.MediaStream : undefined }, // Found on source code HiFiCommunicator.ts, HiFiMixerSession.ts, RaviSession.ts, RaviStreamController.ts + { name: "window.RTCDataChannel", value: () => typeof window !== "undefined" ? window.RTCDataChannel : undefined }, // Found on source code RaviCommandController.ts (ln.151) + { name: "window.RTCPeerConnection", value: () => typeof window !== "undefined" ? window.RTCPeerConnection : undefined }, // Found on source code RaviSession.ts (ln.603) + { name: "window.RTCSessionDescription", value: () => typeof window !== "undefined" ? window.RTCSessionDescription : undefined } // Found on source code RaviSession.ts (ln.604) ] for (let i = 0; i < requiredFeatures.length; i++) { - if (typeof (eval(requiredFeatures[i])) === "undefined") { - HiFiLogger.error("HiFi Audio API: The browser does not support: " + requiredFeatures[i]); - if (requiredFeatures[i] === "navigator.mediaDevices.getUserMedia") { + if (typeof (requiredFeatures[i].value()) === "undefined") { + HiFiLogger.error("HiFi Audio API: The browser does not support: " + requiredFeatures[i].name); + if (requiredFeatures[i].name === "navigator.mediaDevices.getUserMedia") { HiFiLogger.error("HiFi Audio API: Your browser may be preventing access to this feature if you are running in an insecure context, i.e. an `http` server."); } return false; diff --git a/tests/unit/src/utilities/HiFiUtilities.unit.test.ts b/tests/unit/src/utilities/HiFiUtilities.unit.test.ts new file mode 100644 index 00000000..c9de5aa6 --- /dev/null +++ b/tests/unit/src/utilities/HiFiUtilities.unit.test.ts @@ -0,0 +1,62 @@ +import { HiFiUtilities } from "../../../../src/utilities/HiFiUtilities"; + +describe("checkBrowserCompatibility", () => { + let navigatorDescriptor: PropertyDescriptor | undefined; + let windowDescriptor: PropertyDescriptor | undefined; + + beforeEach(() => { + navigatorDescriptor = Object.getOwnPropertyDescriptor(global, "navigator"); + windowDescriptor = Object.getOwnPropertyDescriptor(global, "window"); + jest.spyOn(console, "error").mockImplementation(() => {}); + }); + + afterEach(() => { + jest.restoreAllMocks(); + + if (navigatorDescriptor) { + Object.defineProperty(global, "navigator", navigatorDescriptor); + } else { + delete (global as any).navigator; + } + + if (windowDescriptor) { + Object.defineProperty(global, "window", windowDescriptor); + } else { + delete (global as any).window; + } + }); + + test("returns false instead of throwing when mediaDevices is unavailable", () => { + Object.defineProperty(global, "navigator", { + configurable: true, + value: {} + }); + + let compatibilityResult: Boolean | undefined; + expect(() => compatibilityResult = HiFiUtilities.checkBrowserCompatibility()).not.toThrow(); + expect(compatibilityResult).toBe(false); + }); + + test("returns true when every required browser feature is available", () => { + Object.defineProperty(global, "navigator", { + configurable: true, + value: { + mediaDevices: { + getUserMedia: (): any => undefined, + getSupportedConstraints: () => ({}) + } + } + }); + Object.defineProperty(global, "window", { + configurable: true, + value: { + MediaStream: function () {}, + RTCDataChannel: function () {}, + RTCPeerConnection: function () {}, + RTCSessionDescription: function () {} + } + }); + + expect(HiFiUtilities.checkBrowserCompatibility()).toBe(true); + }); +});