Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/utilities/HiFiUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,21 @@ export class HiFiUtilities {
}

static checkBrowserCompatibility(): Boolean {
let requiredFeatures: Array<string> = [
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;
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/src/utilities/HiFiUtilities.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading