test_attributes.js (3480B)
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 var ObjectReadWrite = { 6 QueryInterface: ChromeUtils.generateQI(["nsIXPCTestObjectReadWrite"]), 7 8 /* nsIXPCTestObjectReadWrite */ 9 stringProperty: "XPConnect Read-Writable String", 10 booleanProperty: true, 11 shortProperty: 32767, 12 longProperty: 2147483647, 13 floatProperty: 5.5, 14 charProperty: "X", 15 // timeProperty is PRTime and signed type. 16 // So it has to allow negative value. 17 timeProperty: -1, 18 }; 19 20 var ObjectReadOnly = { 21 QueryInterface: ChromeUtils.generateQI(["nsIXPCTestObjectReadOnly"]), 22 23 /* nsIXPCTestObjectReadOnly */ 24 strReadOnly: "XPConnect Read-Only String", 25 boolReadOnly: true, 26 shortReadOnly: 32767, 27 longReadOnly: 2147483647, 28 floatReadOnly: 5.5, 29 charReadOnly: "X", 30 // timeProperty is PRTime and signed type. 31 // So it has to allow negative value. 32 timeReadOnly: -1, 33 }; 34 35 function run_test() { 36 // Load the component manifests. 37 registerXPCTestComponents(); 38 39 // Test for each component. 40 test_component_readwrite(Cc["@mozilla.org/js/xpc/test/native/ObjectReadWrite;1"].createInstance()); 41 test_component_readwrite(xpcWrap(ObjectReadWrite)); 42 test_component_readonly(Cc["@mozilla.org/js/xpc/test/native/ObjectReadOnly;1"].createInstance()); 43 test_component_readonly(xpcWrap(ObjectReadOnly)); 44 } 45 46 function test_component_readwrite(obj) { 47 // Instantiate the object. 48 var o = obj.QueryInterface(Ci.nsIXPCTestObjectReadWrite); 49 50 // Test the initial values. 51 Assert.equal("XPConnect Read-Writable String", o.stringProperty); 52 Assert.equal(true, o.booleanProperty); 53 Assert.equal(32767, o.shortProperty); 54 Assert.equal(2147483647, o.longProperty); 55 Assert.ok(5.25 < o.floatProperty && 5.75 > o.floatProperty); 56 Assert.equal("X", o.charProperty); 57 Assert.equal(-1, o.timeProperty); 58 59 // Write new values. 60 o.stringProperty = "another string"; 61 o.booleanProperty = false; 62 o.shortProperty = -12345; 63 o.longProperty = 1234567890; 64 o.floatProperty = 10.2; 65 o.charProperty = "Z"; 66 o.timeProperty = 1; 67 68 // Test the new values. 69 Assert.equal("another string", o.stringProperty); 70 Assert.equal(false, o.booleanProperty); 71 Assert.equal(-12345, o.shortProperty); 72 Assert.equal(1234567890, o.longProperty); 73 Assert.ok(10.15 < o.floatProperty && 10.25 > o.floatProperty); 74 Assert.equal("Z", o.charProperty); 75 Assert.equal(1, o.timeProperty); 76 77 // Assign values that differ from the expected type to verify conversion. 78 79 function SetAndTestBooleanProperty(newValue, expectedValue) { 80 o.booleanProperty = newValue; 81 Assert.equal(expectedValue, o.booleanProperty); 82 }; 83 SetAndTestBooleanProperty(false, false); 84 SetAndTestBooleanProperty(1, true); 85 SetAndTestBooleanProperty(null, false); 86 SetAndTestBooleanProperty("A", true); 87 SetAndTestBooleanProperty(undefined, false); 88 SetAndTestBooleanProperty([], true); 89 SetAndTestBooleanProperty({}, true); 90 } 91 92 function test_component_readonly(obj) { 93 var o = obj.QueryInterface(Ci.nsIXPCTestObjectReadOnly); 94 95 // Test the initial values. 96 Assert.equal("XPConnect Read-Only String", o.strReadOnly); 97 Assert.equal(true, o.boolReadOnly); 98 Assert.equal(32767, o.shortReadOnly); 99 Assert.equal(2147483647, o.longReadOnly); 100 Assert.ok(5.25 < o.floatReadOnly && 5.75 > o.floatReadOnly); 101 Assert.equal("X", o.charReadOnly); 102 Assert.equal(-1, o.timeReadOnly); 103 }