|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * MusicBlocks v3.4.1 |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + */ |
| 10 | + |
| 11 | +describe("p5-sound-adapter", () => { |
| 12 | + let adapterFunction; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + jest.resetModules(); |
| 16 | + |
| 17 | + global.define = jest.fn((deps, callback) => { |
| 18 | + adapterFunction = callback; |
| 19 | + }); |
| 20 | + window.OriginalAudioContext = "mock-original-audio"; |
| 21 | + window.AudioContext = "broken-audio"; |
| 22 | + window.OriginalWebkitAudioContext = "mock-original-webkit"; |
| 23 | + window.webkitAudioContext = "broken-webkit"; |
| 24 | + window.OriginalTone = "mock-original-tone"; |
| 25 | + window.Tone = "broken-tone"; |
| 26 | + |
| 27 | + window.AudioNode = { |
| 28 | + prototype: { |
| 29 | + connect: jest.fn().mockReturnValue(undefined) |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + jest.spyOn(console, "log").mockImplementation(() => {}); |
| 34 | + jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + delete global.define; |
| 39 | + delete window.OriginalAudioContext; |
| 40 | + delete window.AudioContext; |
| 41 | + delete window.OriginalWebkitAudioContext; |
| 42 | + delete window.webkitAudioContext; |
| 43 | + delete window.OriginalTone; |
| 44 | + delete window.Tone; |
| 45 | + delete window.AudioNode; |
| 46 | + |
| 47 | + jest.restoreAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("restores AudioContext, webkitAudioContext, and Tone", () => { |
| 51 | + require("../p5-sound-adapter"); |
| 52 | + adapterFunction(); |
| 53 | + expect(window.AudioContext).toBe("mock-original-audio"); |
| 54 | + expect(window.webkitAudioContext).toBe("mock-original-webkit"); |
| 55 | + expect(window.Tone).toBe("mock-original-tone"); |
| 56 | + }); |
| 57 | + it("does not restore AudioContext if already correct", () => { |
| 58 | + window.AudioContext = "mock-original-audio"; |
| 59 | + |
| 60 | + require("../p5-sound-adapter"); |
| 61 | + adapterFunction(); |
| 62 | + |
| 63 | + expect(window.AudioContext).toBe("mock-original-audio"); |
| 64 | + }); |
| 65 | + it("patches AudioNode.prototype.connect to return destination if undefined", () => { |
| 66 | + require("../p5-sound-adapter"); |
| 67 | + adapterFunction(); |
| 68 | + |
| 69 | + const patchedConnect = window.AudioNode.prototype.connect; |
| 70 | + expect(patchedConnect.isP5AdapterPatched).toBe(true); |
| 71 | + |
| 72 | + const mockDestination = "destination-node"; |
| 73 | + const result = patchedConnect(mockDestination); |
| 74 | + |
| 75 | + expect(result).toBe(mockDestination); |
| 76 | + }); |
| 77 | + it("does nothing if AudioNode is missing", () => { |
| 78 | + delete window.AudioNode; |
| 79 | + |
| 80 | + require("../p5-sound-adapter"); |
| 81 | + adapterFunction(); |
| 82 | + |
| 83 | + expect(window.AudioNode).toBeUndefined(); |
| 84 | + }); |
| 85 | + it("returns original result from connect if it is NOT undefined", () => { |
| 86 | + window.AudioNode.prototype.connect = jest.fn().mockReturnValue("valid-connection"); |
| 87 | + |
| 88 | + require("../p5-sound-adapter"); |
| 89 | + adapterFunction(); |
| 90 | + |
| 91 | + const patchedConnect = window.AudioNode.prototype.connect; |
| 92 | + const result = patchedConnect("destination-node"); |
| 93 | + |
| 94 | + expect(result).toBe("valid-connection"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("warns if OriginalTone is missing", () => { |
| 98 | + window.OriginalTone = undefined; |
| 99 | + require("../p5-sound-adapter"); |
| 100 | + adapterFunction(); |
| 101 | + |
| 102 | + expect(console.warn).toHaveBeenCalledWith("p5-sound-adapter: No OriginalTone to restore!"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("does not double-patch AudioNode.prototype.connect", () => { |
| 106 | + require("../p5-sound-adapter"); |
| 107 | + |
| 108 | + adapterFunction(); |
| 109 | + const patchedConnect = window.AudioNode.prototype.connect; |
| 110 | + |
| 111 | + adapterFunction(); |
| 112 | + |
| 113 | + expect(window.AudioNode.prototype.connect).toBe(patchedConnect); |
| 114 | + expect(console.log).toHaveBeenCalledWith( |
| 115 | + "p5-sound-adapter: AudioNode.prototype.connect already patched" |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
0 commit comments