|
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 Cc = Components.classes; |
|
6 const Ci = Components.interfaces; |
|
7 |
|
8 function run_test() { |
|
9 |
|
10 // Load the component manifests. |
|
11 Components.manager.autoRegister(do_get_file('../components/native/xpctest.manifest')); |
|
12 Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest')); |
|
13 |
|
14 // Test for each component. |
|
15 test_component_readwrite("@mozilla.org/js/xpc/test/native/ObjectReadWrite;1"); |
|
16 test_component_readwrite("@mozilla.org/js/xpc/test/js/ObjectReadWrite;1"); |
|
17 test_component_readonly("@mozilla.org/js/xpc/test/native/ObjectReadOnly;1"); |
|
18 test_component_readonly("@mozilla.org/js/xpc/test/js/ObjectReadOnly;1"); |
|
19 } |
|
20 |
|
21 function test_component_readwrite(contractid) { |
|
22 |
|
23 // Instantiate the object. |
|
24 var o = Cc[contractid].createInstance(Ci["nsIXPCTestObjectReadWrite"]); |
|
25 |
|
26 // Test the initial values. |
|
27 do_check_eq("XPConnect Read-Writable String", o.stringProperty); |
|
28 do_check_eq(true, o.booleanProperty); |
|
29 do_check_eq(32767, o.shortProperty); |
|
30 do_check_eq(2147483647, o.longProperty); |
|
31 do_check_true(5.25 < o.floatProperty && 5.75 > o.floatProperty); |
|
32 do_check_eq("X", o.charProperty); |
|
33 do_check_eq(-1, o.timeProperty); |
|
34 |
|
35 // Write new values. |
|
36 o.stringProperty = "another string"; |
|
37 o.booleanProperty = false; |
|
38 o.shortProperty = -12345; |
|
39 o.longProperty = 1234567890; |
|
40 o.floatProperty = 10.2; |
|
41 o.charProperty = "Z"; |
|
42 o.timeProperty = 1; |
|
43 |
|
44 // Test the new values. |
|
45 do_check_eq("another string", o.stringProperty); |
|
46 do_check_eq(false, o.booleanProperty); |
|
47 do_check_eq(-12345, o.shortProperty); |
|
48 do_check_eq(1234567890, o.longProperty); |
|
49 do_check_true(10.15 < o.floatProperty && 10.25 > o.floatProperty); |
|
50 do_check_eq("Z", o.charProperty); |
|
51 do_check_eq(1, o.timeProperty); |
|
52 |
|
53 // Assign values that differ from the expected type to verify conversion. |
|
54 |
|
55 function SetAndTestBooleanProperty(newValue, expectedValue) { |
|
56 o.booleanProperty = newValue; |
|
57 do_check_eq(expectedValue, o.booleanProperty); |
|
58 }; |
|
59 SetAndTestBooleanProperty(false, false); |
|
60 SetAndTestBooleanProperty(1, true); |
|
61 SetAndTestBooleanProperty(null, false); |
|
62 SetAndTestBooleanProperty("A", true); |
|
63 SetAndTestBooleanProperty(undefined, false); |
|
64 SetAndTestBooleanProperty([], true); |
|
65 SetAndTestBooleanProperty({}, true); |
|
66 } |
|
67 |
|
68 function test_component_readonly(contractid) { |
|
69 |
|
70 // Instantiate the object. |
|
71 var o = Cc[contractid].createInstance(Ci["nsIXPCTestObjectReadOnly"]); |
|
72 |
|
73 // Test the initial values. |
|
74 do_check_eq("XPConnect Read-Only String", o.strReadOnly); |
|
75 do_check_eq(true, o.boolReadOnly); |
|
76 do_check_eq(32767, o.shortReadOnly); |
|
77 do_check_eq(2147483647, o.longReadOnly); |
|
78 do_check_true(5.25 < o.floatReadOnly && 5.75 > o.floatReadOnly); |
|
79 do_check_eq("X", o.charReadOnly); |
|
80 do_check_eq(-1, o.timeReadOnly); |
|
81 } |