|
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("@mozilla.org/js/xpc/test/native/Params;1"); |
|
16 test_component("@mozilla.org/js/xpc/test/js/Params;1"); |
|
17 } |
|
18 |
|
19 function test_component(contractid) { |
|
20 |
|
21 // Instantiate the object. |
|
22 var o = Cc[contractid].createInstance(Ci["nsIXPCTestParams"]); |
|
23 |
|
24 // Possible comparator functions. |
|
25 var standardComparator = function(a,b) {return a == b;}; |
|
26 var dotEqualsComparator = function(a,b) {return a.equals(b); } |
|
27 var fuzzComparator = function(a,b) {return Math.abs(a - b) < 0.1;}; |
|
28 var interfaceComparator = function(a,b) {return a.name == b.name; } |
|
29 var arrayComparator = function(innerComparator) { |
|
30 return function(a,b) { |
|
31 if (a.length != b.length) |
|
32 return false; |
|
33 for (var i = 0; i < a.length; ++i) |
|
34 if (!innerComparator(a[i], b[i])) |
|
35 return false; |
|
36 return true; |
|
37 }; |
|
38 }; |
|
39 |
|
40 // Helper test function - takes the name of test method and two values of |
|
41 // the given type. |
|
42 // |
|
43 // The optional comparator argument can be used for alternative notions of |
|
44 // equality. The comparator should return true on equality. |
|
45 function doTest(name, val1, val2, comparator) { |
|
46 if (!comparator) |
|
47 comparator = standardComparator; |
|
48 var a = val1; |
|
49 var b = {value: val2}; |
|
50 var rv = o[name].call(o, a, b); |
|
51 do_check_true(comparator(rv, val2)); |
|
52 do_check_true(comparator(val1, b.value)); |
|
53 }; |
|
54 |
|
55 function doIsTest(name, val1, val1Is, val2, val2Is, valComparator, isComparator) { |
|
56 if (!isComparator) |
|
57 isComparator = standardComparator; |
|
58 var a = val1; |
|
59 var aIs = val1Is; |
|
60 var b = {value: val2}; |
|
61 var bIs = {value: val2Is}; |
|
62 var rvIs = {}; |
|
63 var rv = o[name].call(o, aIs, a, bIs, b, rvIs); |
|
64 do_check_true(valComparator(rv, val2)); |
|
65 do_check_true(isComparator(rvIs.value, val2Is)); |
|
66 do_check_true(valComparator(val1, b.value)); |
|
67 do_check_true(isComparator(val1Is, bIs.value)); |
|
68 } |
|
69 |
|
70 // Special-purpose function for testing arrays of iid_is interfaces, where we |
|
71 // have 2 distinct sets of dependent parameters. |
|
72 function doIs2Test(name, val1, val1Size, val1IID, val2, val2Size, val2IID) { |
|
73 var a = val1; |
|
74 var aSize = val1Size; |
|
75 var aIID = val1IID; |
|
76 var b = {value: val2}; |
|
77 var bSize = {value: val2Size}; |
|
78 var bIID = {value: val2IID}; |
|
79 var rvSize = {}; |
|
80 var rvIID = {}; |
|
81 var rv = o[name].call(o, aSize, aIID, a, bSize, bIID, b, rvSize, rvIID); |
|
82 do_check_true(arrayComparator(interfaceComparator)(rv, val2)); |
|
83 do_check_true(standardComparator(rvSize.value, val2Size)); |
|
84 do_check_true(dotEqualsComparator(rvIID.value, val2IID)); |
|
85 do_check_true(arrayComparator(interfaceComparator)(val1, b.value)); |
|
86 do_check_true(standardComparator(val1Size, bSize.value)); |
|
87 do_check_true(dotEqualsComparator(val1IID, bIID.value)); |
|
88 } |
|
89 |
|
90 // Check that the given call (type mismatch) results in an exception being thrown. |
|
91 function doTypedArrayMismatchTest(name, val1, val1Size, val2, val2Size) { |
|
92 var comparator = arrayComparator(standardComparator); |
|
93 var error = false; |
|
94 try { |
|
95 doIsTest(name, val1, val1Size, val2, val2Size, comparator); |
|
96 |
|
97 // An exception was not thrown as would have been expected. |
|
98 do_check_true(false); |
|
99 } |
|
100 catch (e) { |
|
101 // An exception was thrown as expected. |
|
102 do_check_true(true); |
|
103 } |
|
104 } |
|
105 |
|
106 // Workaround for bug 687612 (inout parameters broken for dipper types). |
|
107 // We do a simple test of copying a into b, and ignore the rv. |
|
108 function doTestWorkaround(name, val1) { |
|
109 var a = val1; |
|
110 var b = {value: ""}; |
|
111 o[name].call(o, a, b); |
|
112 do_check_eq(val1, b.value); |
|
113 } |
|
114 |
|
115 // Test all the different types |
|
116 doTest("testBoolean", true, false); |
|
117 doTest("testOctet", 4, 156); |
|
118 doTest("testShort", -456, 1299); |
|
119 doTest("testLong", 50060, -12121212); |
|
120 doTest("testLongLong", 12345, -10000000000); |
|
121 doTest("testUnsignedShort", 1532, 65000); |
|
122 doTest("testUnsignedLong", 0, 4000000000); |
|
123 doTest("testUnsignedLongLong", 215435, 3453492580348535809); |
|
124 doTest("testFloat", 4.9, -11.2, fuzzComparator); |
|
125 doTest("testDouble", -80.5, 15000.2, fuzzComparator); |
|
126 doTest("testChar", "a", "2"); |
|
127 doTest("testString", "someString", "another string"); |
|
128 doTest("testWstring", "Why wasnt this", "turned on before? ಠ_ಠ"); |
|
129 doTest("testWchar", "z", "ア"); |
|
130 doTestWorkaround("testDOMString", "Beware: ☠ s"); |
|
131 doTestWorkaround("testAString", "Frosty the ☃ ;-)"); |
|
132 doTestWorkaround("testAUTF8String", "We deliver 〠!"); |
|
133 doTestWorkaround("testACString", "Just a regular C string."); |
|
134 doTest("testJsval", {aprop: 12, bprop: "str"}, 4.22); |
|
135 |
|
136 // Helpers to instantiate various test XPCOM objects. |
|
137 var numAsMade = 0; |
|
138 function makeA() { |
|
139 var a = Cc["@mozilla.org/js/xpc/test/js/InterfaceA;1"].createInstance(Ci['nsIXPCTestInterfaceA']); |
|
140 a.name = 'testA' + numAsMade++; |
|
141 return a; |
|
142 }; |
|
143 var numBsMade = 0; |
|
144 function makeB() { |
|
145 var b = Cc["@mozilla.org/js/xpc/test/js/InterfaceB;1"].createInstance(Ci['nsIXPCTestInterfaceB']); |
|
146 b.name = 'testB' + numBsMade++; |
|
147 return b; |
|
148 }; |
|
149 |
|
150 // Test arrays. |
|
151 doIsTest("testShortArray", [2, 4, 6], 3, [1, 3, 5, 7], 4, arrayComparator(standardComparator)); |
|
152 doIsTest("testDoubleArray", [-10, -0.5], 2, [1, 3, 1e11, -8e-5 ], 4, arrayComparator(fuzzComparator)); |
|
153 |
|
154 doIsTest("testStringArray", ["mary", "hat", "hey", "lid", "tell", "lam"], 6, |
|
155 ["ids", "fleas", "woes", "wide", "has", "know", "!"], 7, arrayComparator(standardComparator)); |
|
156 doIsTest("testWstringArray", ["沒有語言", "的偉大嗎?]"], 2, |
|
157 ["we", "are", "being", "sooo", "international", "right", "now"], 7, arrayComparator(standardComparator)); |
|
158 doIsTest("testInterfaceArray", [makeA(), makeA()], 2, |
|
159 [makeA(), makeA(), makeA(), makeA(), makeA(), makeA()], 6, arrayComparator(interfaceComparator)); |
|
160 |
|
161 // Test typed arrays and ArrayBuffer aliasing. |
|
162 var arrayBuffer = new ArrayBuffer(16); |
|
163 var int16Array = new Int16Array(arrayBuffer, 2, 3); |
|
164 int16Array.set([-32768, 0, 32767]); |
|
165 doIsTest("testShortArray", int16Array, 3, new Int16Array([1773, -32768, 32767, 7]), 4, arrayComparator(standardComparator)); |
|
166 doIsTest("testDoubleArray", new Float64Array([-10, -0.5]), 2, new Float64Array([0, 3.2, 1.0e10, -8.33 ]), 4, arrayComparator(fuzzComparator)); |
|
167 |
|
168 // Test sized strings. |
|
169 var ssTests = ["Tis not possible, I muttered", "give me back my free hardcore!", "quoth the server:", "4〠4"]; |
|
170 doIsTest("testSizedString", ssTests[0], ssTests[0].length, ssTests[1], ssTests[1].length, standardComparator); |
|
171 doIsTest("testSizedWstring", ssTests[2], ssTests[2].length, ssTests[3], ssTests[3].length, standardComparator); |
|
172 |
|
173 // Test iid_is. |
|
174 doIsTest("testInterfaceIs", makeA(), Ci['nsIXPCTestInterfaceA'], |
|
175 makeB(), Ci['nsIXPCTestInterfaceB'], |
|
176 interfaceComparator, dotEqualsComparator); |
|
177 |
|
178 // Test arrays of iids. |
|
179 doIs2Test("testInterfaceIsArray", [makeA(), makeA(), makeA(), makeA(), makeA()], 5, Ci['nsIXPCTestInterfaceA'], |
|
180 [makeB(), makeB(), makeB()], 3, Ci['nsIXPCTestInterfaceB']); |
|
181 |
|
182 // Test incorrect (too big) array size parameter; this should throw NOT_ENOUGH_ELEMENTS. |
|
183 doTypedArrayMismatchTest("testShortArray", new Int16Array([-3, 7, 4]), 4, |
|
184 new Int16Array([1, -32, 6]), 3); |
|
185 |
|
186 // Test type mismatch (int16 <-> uint16); this should throw BAD_CONVERT_JS. |
|
187 doTypedArrayMismatchTest("testShortArray", new Uint16Array([0, 7, 4, 3]), 4, |
|
188 new Uint16Array([1, 5, 6]), 3); |
|
189 } |