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/. */
5 var ObjectReadWrite = {
6 QueryInterface: ChromeUtils.generateQI(["nsIXPCTestObjectReadWrite"]),
8 /* nsIXPCTestObjectReadWrite */
9 stringProperty: "XPConnect Read-Writable String",
10 booleanProperty: true,
12 longProperty: 2147483647,
15 // timeProperty is PRTime and signed type.
16 // So it has to allow negative value.
20 var ObjectReadOnly = {
21 QueryInterface: ChromeUtils.generateQI(["nsIXPCTestObjectReadOnly"]),
23 /* nsIXPCTestObjectReadOnly */
24 strReadOnly: "XPConnect Read-Only String",
27 longReadOnly: 2147483647,
30 // timeProperty is PRTime and signed type.
31 // So it has to allow negative value.
36 // Load the component manifests.
37 registerXPCTestComponents();
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));
46 function test_component_readwrite(obj) {
47 // Instantiate the object.
48 var o = obj.QueryInterface(Ci.nsIXPCTestObjectReadWrite);
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);
60 o.stringProperty = "another string";
61 o.booleanProperty = false;
62 o.shortProperty = -12345;
63 o.longProperty = 1234567890;
64 o.floatProperty = 10.2;
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);
77 // Assign values that differ from the expected type to verify conversion.
79 function SetAndTestBooleanProperty(newValue, expectedValue) {
80 o.booleanProperty = newValue;
81 Assert.equal(expectedValue, o.booleanProperty);
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);
92 function test_component_readonly(obj) {
93 var o = obj.QueryInterface(Ci.nsIXPCTestObjectReadOnly);
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);