MockSound.sys.mjs (1858B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const Cm = Components.manager; 6 7 const CONTRACT_ID = "@mozilla.org/sound;1"; 8 9 Cu.crashIfNotInAutomation(); 10 11 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); 12 var oldClassID = ""; 13 var newClassID = Services.uuid.generateUUID(); 14 var newFactory = function (window) { 15 return { 16 createInstance(aIID) { 17 return new MockSoundInstance(window).QueryInterface(aIID); 18 }, 19 QueryInterface: ChromeUtils.generateQI(["nsIFactory"]), 20 }; 21 }; 22 23 export var MockSound = { 24 _played: [], 25 26 get played() { 27 return this._played; 28 }, 29 30 init() { 31 this.reset(); 32 this.factory = newFactory(); 33 if (!registrar.isCIDRegistered(newClassID)) { 34 try { 35 oldClassID = registrar.contractIDToCID(CONTRACT_ID); 36 } catch (ex) { 37 oldClassID = ""; 38 dump( 39 "TEST-INFO | can't get sound registered component, " + 40 "assuming there is none" 41 ); 42 } 43 registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory); 44 } 45 }, 46 47 reset() { 48 this._played = []; 49 }, 50 51 cleanup() { 52 var previousFactory = this.factory; 53 this.reset(); 54 this.factory = null; 55 56 registrar.unregisterFactory(newClassID, previousFactory); 57 if (oldClassID != "") { 58 registrar.registerFactory(oldClassID, "", CONTRACT_ID, null); 59 } 60 }, 61 }; 62 63 function MockSoundInstance() {} 64 MockSoundInstance.prototype = { 65 QueryInterface: ChromeUtils.generateQI(["nsISound"]), 66 67 play(aURL) { 68 MockSound._played.push(`(uri)${aURL.spec}`); 69 }, 70 71 beep() { 72 MockSound._played.push("beep"); 73 }, 74 75 init() {}, 76 77 playEventSound(aEventId) { 78 MockSound._played.push(`(event)${aEventId}`); 79 }, 80 };