diff --git a/tests/integration/scenarios/mediaCapabilitiesProber/check_drm_configuration.js b/tests/integration/scenarios/mediaCapabilitiesProber/check_drm_configuration.js new file mode 100644 index 00000000000..e2bb12de5b1 --- /dev/null +++ b/tests/integration/scenarios/mediaCapabilitiesProber/check_drm_configuration.js @@ -0,0 +1,137 @@ +import { mediaCapabilitiesProber } from "../../../../dist/es2017/experimental/tools"; +import { describe, it, expect } from "vitest"; + +/** + * Mock requestMediaKeySystemAccess delivering mediaKeySystemAccess. + */ +function mockPositivesResultsRMKSA() { + const saveRMKSA = navigator.requestMediaKeySystemAccess; + navigator.requestMediaKeySystemAccess = (_, configurations) => { + return new Promise((resolve) => { + resolve({ + getConfiguration: () => { + return configurations[0]; + }, + }); + }); + }; + return function reset() { + navigator.requestMediaKeySystemAccess = saveRMKSA; + }; +} + +/** + * Mock requestMediaKeySystemAccess delivering either mediaKeySystemAccess + * or rejecting (start with rejection). + */ +function mockMixedResultsRMKSA() { + let i = 0; + const saveRMKSA = navigator.requestMediaKeySystemAccess; + navigator.requestMediaKeySystemAccess = (_, configurations) => { + return new Promise((resolve, reject) => { + i++; + if (i % 2) { + reject(new Error("Incompatible configuration")); + return; + } + resolve({ + getConfiguration: () => { + return configurations[0]; + }, + }); + }); + }; + return function reset() { + navigator.requestMediaKeySystemAccess = saveRMKSA; + }; +} + +/** + * Mock requestMediaKeySystemAccess rejecting. + */ +function mockNegativeResultsRMKSA() { + const saveRMKSA = navigator.requestMediaKeySystemAccess; + navigator.requestMediaKeySystemAccess = () => { + return Promise.reject(new Error("Incompatible configuration")); + }; + return function reset() { + navigator.requestMediaKeySystemAccess = saveRMKSA; + }; +} + +describe("mediaCapabilitiesProber - checkDrmConfiguration", () => { + const mksConfiguration = [ + { + initDataTypes: ["cenc"], + videoCapabilities: [ + { + contentType: 'video/mp4;codecs="avc1.4d401e"', // standard mp4 codec + robustness: "HW_SECURE_CRYPTO", + }, + { + contentType: 'video/mp4;codecs="avc1.4d401e"', + robustness: "SW_SECURE_DECODE", + }, + ], + }, + ]; + + const keySystems = [ + // Let's consider this one as a compatible key system configuration + ["com.widevine.alpha", mksConfiguration], + + // Let's consider this one as not compatible + ["com.microsoft.playready", mksConfiguration], + ]; + + it("Should resolve when the given configuration is supported.", async () => { + const resetRMKSA = mockPositivesResultsRMKSA(); + const results1 = await mediaCapabilitiesProber.checkDrmConfiguration( + ...keySystems[0], + ); + expect(results1).not.to.be.undefined; + const results2 = await mediaCapabilitiesProber.checkDrmConfiguration( + ...keySystems[1], + ); + expect(results2).not.to.be.undefined; + resetRMKSA(); + }); + + it("Should reject when configurations are not supported yet resolve when some are.", async () => { + const resetRMKSA = mockMixedResultsRMKSA(); + await mediaCapabilitiesProber.checkDrmConfiguration(...keySystems[0]).then( + () => { + throw new Error("Should not have resolved"); + }, + (err) => { + expect(err).toBeInstanceOf(Error); + }, + ); + const results2 = await mediaCapabilitiesProber.checkDrmConfiguration( + ...keySystems[1], + ); + expect(results2).not.to.be.undefined; + resetRMKSA(); + }); + + it("Should reject when no configuration is supported", async () => { + const resetRMKSA = mockNegativeResultsRMKSA(); + await mediaCapabilitiesProber.checkDrmConfiguration(...keySystems[0]).then( + () => { + throw new Error("Should not have resolved"); + }, + (err) => { + expect(err).toBeInstanceOf(Error); + }, + ); + await mediaCapabilitiesProber.checkDrmConfiguration(...keySystems[1]).then( + () => { + throw new Error("Should not have resolved"); + }, + (err) => { + expect(err).toBeInstanceOf(Error); + }, + ); + resetRMKSA(); + }); +}); diff --git a/tests/integration/scenarios/mediaCapabilitiesProber/getCompatibleDRM.js b/tests/integration/scenarios/mediaCapabilitiesProber/getCompatibleDRM.js deleted file mode 100644 index 321e95b8030..00000000000 --- a/tests/integration/scenarios/mediaCapabilitiesProber/getCompatibleDRM.js +++ /dev/null @@ -1,128 +0,0 @@ -import { mediaCapabilitiesProber } from "../../../../dist/es2017/experimental/tools"; -import { describe, it, expect } from "vitest"; - -/** - * Mock requestMediaKeySystemAccess delivering mediaKeySystemAccess. - */ -function mockPositivesResultsRMKSA() { - const saveRMKSA = navigator.requestMediaKeySystemAccess; - navigator.requestMediaKeySystemAccess = (_, configurations) => { - return new Promise((resolve) => { - resolve({ - getConfiguration: () => { - return configurations[0]; - }, - }); - }); - }; - return function reset() { - navigator.requestMediaKeySystemAccess = saveRMKSA; - }; -} - -/** - * Mock requestMediaKeySystemAccess delivering either mediaKeySystemAccess - * or rejecting (start with rejection). - */ -function mockMixedResultsRMKSA() { - let i = 0; - const saveRMKSA = navigator.requestMediaKeySystemAccess; - navigator.requestMediaKeySystemAccess = (_, configurations) => { - return new Promise((resolve, reject) => { - i++; - if (i % 2) { - reject(); - return; - } - resolve({ - getConfiguration: () => { - return configurations[0]; - }, - }); - }); - }; - return function reset() { - navigator.requestMediaKeySystemAccess = saveRMKSA; - }; -} - -/** - * Mock requestMediaKeySystemAccess rejecting. - */ -function mockNegativeResultsRMKSA() { - const saveRMKSA = navigator.requestMediaKeySystemAccess; - navigator.requestMediaKeySystemAccess = () => { - return Promise.reject(); - }; - return function reset() { - navigator.requestMediaKeySystemAccess = saveRMKSA; - }; -} - -describe("mediaCapabilitiesProber - getCompatibleDRMConfigurations", () => { - const mksConfiguration = { - initDataTypes: ["cenc"], - videoCapabilities: [ - { - contentType: 'video/mp4;codecs="avc1.4d401e"', // standard mp4 codec - robustness: "HW_SECURE_CRYPTO", - }, - { - contentType: 'video/mp4;codecs="avc1.4d401e"', - robustness: "SW_SECURE_DECODE", - }, - ], - }; - - const keySystems = [ - // Let's consider this one as a compatible key system configuration - { type: "com.widevine.alpha", configuration: mksConfiguration }, - - // Let's consider this one as not compatible - { type: "com.microsoft.playready", configuration: mksConfiguration }, - ]; - - it("Should support all configurations.", async () => { - const resetRMKSA = mockPositivesResultsRMKSA(); - const results = - await mediaCapabilitiesProber.getCompatibleDRMConfigurations(keySystems); - - expect(results.length).to.be.equal(2); - for (let i = 0; i < results.length; i++) { - expect(results[i].configuration).not.to.be.undefined; - expect(results[i].type).not.to.be.undefined; - expect(results[i].compatibleConfiguration).not.to.be.undefined; - } - resetRMKSA(); - }); - - it("Should support half of configurations only.", async () => { - const resetRMKSA = mockMixedResultsRMKSA(); - const results = - await mediaCapabilitiesProber.getCompatibleDRMConfigurations(keySystems); - - expect(results.length).to.be.equal(2); - expect(results[0].configuration).not.to.be.undefined; - expect(results[0].type).not.to.be.undefined; - expect(results[0].compatibleConfiguration).to.be.undefined; - expect(results[1].configuration).not.to.be.undefined; - expect(results[1].type).not.to.be.undefined; - expect(results[1].compatibleConfiguration).not.to.be.undefined; - resetRMKSA(); - }); - - it("Should not support configurations.", async () => { - const resetRMKSA = mockNegativeResultsRMKSA(); - const results = - await mediaCapabilitiesProber.getCompatibleDRMConfigurations(keySystems); - - expect(results.length).to.be.equal(2); - expect(results[0].configuration).not.to.be.undefined; - expect(results[0].type).not.to.be.undefined; - expect(results[0].compatibleConfiguration).to.be.undefined; - expect(results[1].configuration).not.to.be.undefined; - expect(results[1].type).not.to.be.undefined; - expect(results[1].compatibleConfiguration).to.be.undefined; - resetRMKSA(); - }); -});