test_chromeutils_defineLazyGetter.js (1619B)
1 "use strict"; 2 3 add_task(function test_defineLazyGetter() { 4 let accessCount = 0; 5 let obj = { 6 inScope: false, 7 }; 8 const TEST_VALUE = "test value"; 9 ChromeUtils.defineLazyGetter(obj, "foo", function () { 10 accessCount++; 11 this.inScope = true; 12 return TEST_VALUE; 13 }); 14 Assert.equal(accessCount, 0); 15 16 // Get the property, making sure the access count has increased. 17 Assert.equal(obj.foo, TEST_VALUE); 18 Assert.equal(accessCount, 1); 19 Assert.ok(obj.inScope); 20 21 // Get the property once more, making sure the access count has not 22 // increased. 23 Assert.equal(obj.foo, TEST_VALUE); 24 Assert.equal(accessCount, 1); 25 }); 26 27 add_task(function test_defineLazyGetter_name() { 28 // Properties that are convertible to NonIntAtom should be reflected to 29 // the getter name. 30 for (const name of [ 31 "foo", 32 "\u3042", 33 true, 34 false, 35 -1, 36 1.1, 37 0.1, 38 null, 39 undefined, 40 {}, 41 [], 42 /a/, 43 ]) { 44 const obj = {}; 45 const v = {}; 46 ChromeUtils.defineLazyGetter(obj, name, () => v); 47 Assert.equal( 48 Object.getOwnPropertyDescriptor(obj, name).get.name, 49 String(name) 50 ); 51 Assert.equal(obj[name], v); 52 Assert.equal(obj[name], v); 53 } 54 55 // Int and Symbol properties are not reflected to the getter name. 56 for (const name of [ 57 0, 58 10, 59 Symbol.iterator, 60 Symbol("foo"), 61 Symbol.for("foo"), 62 ]) { 63 const obj = {}; 64 const v = {}; 65 ChromeUtils.defineLazyGetter(obj, name, () => v); 66 Assert.equal(Object.getOwnPropertyDescriptor(obj, name).get.name, ""); 67 Assert.equal(obj[name], v); 68 Assert.equal(obj[name], v); 69 } 70 });