test_utils_catch.js (3150B)
1 const { Service } = ChromeUtils.importESModule( 2 "resource://services-sync/service.sys.mjs" 3 ); 4 5 add_task(async function run_test() { 6 _("Make sure catch when copied to an object will correctly catch stuff"); 7 let ret, rightThis, didCall, didThrow, wasCovfefe, wasLocked; 8 let obj = { 9 _catch: Utils.catch, 10 _log: { 11 debug(str) { 12 didThrow = str.search(/^Exception/) == 0; 13 }, 14 info(str) { 15 wasLocked = str.indexOf("Cannot start sync: already syncing?") == 0; 16 }, 17 }, 18 19 func() { 20 return this._catch(async function () { 21 rightThis = this == obj; 22 didCall = true; 23 return 5; 24 })(); 25 }, 26 27 throwy() { 28 return this._catch(async function () { 29 rightThis = this == obj; 30 didCall = true; 31 throw new Error("covfefe"); 32 })(); 33 }, 34 35 callbacky() { 36 return this._catch( 37 async function () { 38 rightThis = this == obj; 39 didCall = true; 40 throw new Error("covfefe"); 41 }, 42 async function (ex) { 43 wasCovfefe = ex && ex.message == "covfefe"; 44 } 45 )(); 46 }, 47 48 lockedy() { 49 return this._catch(async function () { 50 rightThis = this == obj; 51 didCall = true; 52 Utils.throwLockException(null); 53 })(); 54 }, 55 56 lockedy_chained() { 57 return this._catch(async function () { 58 rightThis = this == obj; 59 didCall = true; 60 Utils.throwLockException(null); 61 })(); 62 }, 63 }; 64 65 _("Make sure a normal call will call and return"); 66 rightThis = didCall = didThrow = wasLocked = false; 67 ret = await obj.func(); 68 Assert.equal(ret, 5); 69 Assert.ok(rightThis); 70 Assert.ok(didCall); 71 Assert.ok(!didThrow); 72 Assert.equal(wasCovfefe, undefined); 73 Assert.ok(!wasLocked); 74 75 _( 76 "Make sure catch/throw results in debug call and caller doesn't need to handle exception" 77 ); 78 rightThis = didCall = didThrow = wasLocked = false; 79 ret = await obj.throwy(); 80 Assert.equal(ret, undefined); 81 Assert.ok(rightThis); 82 Assert.ok(didCall); 83 Assert.ok(didThrow); 84 Assert.equal(wasCovfefe, undefined); 85 Assert.ok(!wasLocked); 86 87 _("Test callback for exception testing."); 88 rightThis = didCall = didThrow = wasLocked = false; 89 ret = await obj.callbacky(); 90 Assert.equal(ret, undefined); 91 Assert.ok(rightThis); 92 Assert.ok(didCall); 93 Assert.ok(didThrow); 94 Assert.ok(wasCovfefe); 95 Assert.ok(!wasLocked); 96 97 _("Test the lock-aware catch that Service uses."); 98 obj._catch = Service._catch; 99 rightThis = didCall = didThrow = wasLocked = false; 100 wasCovfefe = undefined; 101 ret = await obj.lockedy(); 102 Assert.equal(ret, undefined); 103 Assert.ok(rightThis); 104 Assert.ok(didCall); 105 Assert.ok(didThrow); 106 Assert.equal(wasCovfefe, undefined); 107 Assert.ok(wasLocked); 108 109 _("Test the lock-aware catch that Service uses with a chained promise."); 110 rightThis = didCall = didThrow = wasLocked = false; 111 wasCovfefe = undefined; 112 ret = await obj.lockedy_chained(); 113 Assert.equal(ret, undefined); 114 Assert.ok(rightThis); 115 Assert.ok(didCall); 116 Assert.ok(didThrow); 117 Assert.equal(wasCovfefe, undefined); 118 Assert.ok(wasLocked); 119 });