michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: function run_test() { michael@0: michael@0: // Load the component manifests. michael@0: Components.manager.autoRegister(do_get_file('../components/native/xpctest.manifest')); michael@0: Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest')); michael@0: michael@0: // Test for each component. michael@0: test_component("@mozilla.org/js/xpc/test/native/Params;1"); michael@0: test_component("@mozilla.org/js/xpc/test/js/Params;1"); michael@0: } michael@0: michael@0: function test_component(contractid) { michael@0: michael@0: // Instantiate the object. michael@0: var o = Cc[contractid].createInstance(Ci["nsIXPCTestParams"]); michael@0: michael@0: // Possible comparator functions. michael@0: var standardComparator = function(a,b) {return a == b;}; michael@0: var dotEqualsComparator = function(a,b) {return a.equals(b); } michael@0: var fuzzComparator = function(a,b) {return Math.abs(a - b) < 0.1;}; michael@0: var interfaceComparator = function(a,b) {return a.name == b.name; } michael@0: var arrayComparator = function(innerComparator) { michael@0: return function(a,b) { michael@0: if (a.length != b.length) michael@0: return false; michael@0: for (var i = 0; i < a.length; ++i) michael@0: if (!innerComparator(a[i], b[i])) michael@0: return false; michael@0: return true; michael@0: }; michael@0: }; michael@0: michael@0: // Helper test function - takes the name of test method and two values of michael@0: // the given type. michael@0: // michael@0: // The optional comparator argument can be used for alternative notions of michael@0: // equality. The comparator should return true on equality. michael@0: function doTest(name, val1, val2, comparator) { michael@0: if (!comparator) michael@0: comparator = standardComparator; michael@0: var a = val1; michael@0: var b = {value: val2}; michael@0: var rv = o[name].call(o, a, b); michael@0: do_check_true(comparator(rv, val2)); michael@0: do_check_true(comparator(val1, b.value)); michael@0: }; michael@0: michael@0: function doIsTest(name, val1, val1Is, val2, val2Is, valComparator, isComparator) { michael@0: if (!isComparator) michael@0: isComparator = standardComparator; michael@0: var a = val1; michael@0: var aIs = val1Is; michael@0: var b = {value: val2}; michael@0: var bIs = {value: val2Is}; michael@0: var rvIs = {}; michael@0: var rv = o[name].call(o, aIs, a, bIs, b, rvIs); michael@0: do_check_true(valComparator(rv, val2)); michael@0: do_check_true(isComparator(rvIs.value, val2Is)); michael@0: do_check_true(valComparator(val1, b.value)); michael@0: do_check_true(isComparator(val1Is, bIs.value)); michael@0: } michael@0: michael@0: // Special-purpose function for testing arrays of iid_is interfaces, where we michael@0: // have 2 distinct sets of dependent parameters. michael@0: function doIs2Test(name, val1, val1Size, val1IID, val2, val2Size, val2IID) { michael@0: var a = val1; michael@0: var aSize = val1Size; michael@0: var aIID = val1IID; michael@0: var b = {value: val2}; michael@0: var bSize = {value: val2Size}; michael@0: var bIID = {value: val2IID}; michael@0: var rvSize = {}; michael@0: var rvIID = {}; michael@0: var rv = o[name].call(o, aSize, aIID, a, bSize, bIID, b, rvSize, rvIID); michael@0: do_check_true(arrayComparator(interfaceComparator)(rv, val2)); michael@0: do_check_true(standardComparator(rvSize.value, val2Size)); michael@0: do_check_true(dotEqualsComparator(rvIID.value, val2IID)); michael@0: do_check_true(arrayComparator(interfaceComparator)(val1, b.value)); michael@0: do_check_true(standardComparator(val1Size, bSize.value)); michael@0: do_check_true(dotEqualsComparator(val1IID, bIID.value)); michael@0: } michael@0: michael@0: // Check that the given call (type mismatch) results in an exception being thrown. michael@0: function doTypedArrayMismatchTest(name, val1, val1Size, val2, val2Size) { michael@0: var comparator = arrayComparator(standardComparator); michael@0: var error = false; michael@0: try { michael@0: doIsTest(name, val1, val1Size, val2, val2Size, comparator); michael@0: michael@0: // An exception was not thrown as would have been expected. michael@0: do_check_true(false); michael@0: } michael@0: catch (e) { michael@0: // An exception was thrown as expected. michael@0: do_check_true(true); michael@0: } michael@0: } michael@0: michael@0: // Workaround for bug 687612 (inout parameters broken for dipper types). michael@0: // We do a simple test of copying a into b, and ignore the rv. michael@0: function doTestWorkaround(name, val1) { michael@0: var a = val1; michael@0: var b = {value: ""}; michael@0: o[name].call(o, a, b); michael@0: do_check_eq(val1, b.value); michael@0: } michael@0: michael@0: // Test all the different types michael@0: doTest("testBoolean", true, false); michael@0: doTest("testOctet", 4, 156); michael@0: doTest("testShort", -456, 1299); michael@0: doTest("testLong", 50060, -12121212); michael@0: doTest("testLongLong", 12345, -10000000000); michael@0: doTest("testUnsignedShort", 1532, 65000); michael@0: doTest("testUnsignedLong", 0, 4000000000); michael@0: doTest("testUnsignedLongLong", 215435, 3453492580348535809); michael@0: doTest("testFloat", 4.9, -11.2, fuzzComparator); michael@0: doTest("testDouble", -80.5, 15000.2, fuzzComparator); michael@0: doTest("testChar", "a", "2"); michael@0: doTest("testString", "someString", "another string"); michael@0: doTest("testWstring", "Why wasnt this", "turned on before? ಠ_ಠ"); michael@0: doTest("testWchar", "z", "ア"); michael@0: doTestWorkaround("testDOMString", "Beware: ☠ s"); michael@0: doTestWorkaround("testAString", "Frosty the ☃ ;-)"); michael@0: doTestWorkaround("testAUTF8String", "We deliver 〠!"); michael@0: doTestWorkaround("testACString", "Just a regular C string."); michael@0: doTest("testJsval", {aprop: 12, bprop: "str"}, 4.22); michael@0: michael@0: // Helpers to instantiate various test XPCOM objects. michael@0: var numAsMade = 0; michael@0: function makeA() { michael@0: var a = Cc["@mozilla.org/js/xpc/test/js/InterfaceA;1"].createInstance(Ci['nsIXPCTestInterfaceA']); michael@0: a.name = 'testA' + numAsMade++; michael@0: return a; michael@0: }; michael@0: var numBsMade = 0; michael@0: function makeB() { michael@0: var b = Cc["@mozilla.org/js/xpc/test/js/InterfaceB;1"].createInstance(Ci['nsIXPCTestInterfaceB']); michael@0: b.name = 'testB' + numBsMade++; michael@0: return b; michael@0: }; michael@0: michael@0: // Test arrays. michael@0: doIsTest("testShortArray", [2, 4, 6], 3, [1, 3, 5, 7], 4, arrayComparator(standardComparator)); michael@0: doIsTest("testDoubleArray", [-10, -0.5], 2, [1, 3, 1e11, -8e-5 ], 4, arrayComparator(fuzzComparator)); michael@0: michael@0: doIsTest("testStringArray", ["mary", "hat", "hey", "lid", "tell", "lam"], 6, michael@0: ["ids", "fleas", "woes", "wide", "has", "know", "!"], 7, arrayComparator(standardComparator)); michael@0: doIsTest("testWstringArray", ["沒有語言", "的偉大嗎?]"], 2, michael@0: ["we", "are", "being", "sooo", "international", "right", "now"], 7, arrayComparator(standardComparator)); michael@0: doIsTest("testInterfaceArray", [makeA(), makeA()], 2, michael@0: [makeA(), makeA(), makeA(), makeA(), makeA(), makeA()], 6, arrayComparator(interfaceComparator)); michael@0: michael@0: // Test typed arrays and ArrayBuffer aliasing. michael@0: var arrayBuffer = new ArrayBuffer(16); michael@0: var int16Array = new Int16Array(arrayBuffer, 2, 3); michael@0: int16Array.set([-32768, 0, 32767]); michael@0: doIsTest("testShortArray", int16Array, 3, new Int16Array([1773, -32768, 32767, 7]), 4, arrayComparator(standardComparator)); michael@0: doIsTest("testDoubleArray", new Float64Array([-10, -0.5]), 2, new Float64Array([0, 3.2, 1.0e10, -8.33 ]), 4, arrayComparator(fuzzComparator)); michael@0: michael@0: // Test sized strings. michael@0: var ssTests = ["Tis not possible, I muttered", "give me back my free hardcore!", "quoth the server:", "4〠4"]; michael@0: doIsTest("testSizedString", ssTests[0], ssTests[0].length, ssTests[1], ssTests[1].length, standardComparator); michael@0: doIsTest("testSizedWstring", ssTests[2], ssTests[2].length, ssTests[3], ssTests[3].length, standardComparator); michael@0: michael@0: // Test iid_is. michael@0: doIsTest("testInterfaceIs", makeA(), Ci['nsIXPCTestInterfaceA'], michael@0: makeB(), Ci['nsIXPCTestInterfaceB'], michael@0: interfaceComparator, dotEqualsComparator); michael@0: michael@0: // Test arrays of iids. michael@0: doIs2Test("testInterfaceIsArray", [makeA(), makeA(), makeA(), makeA(), makeA()], 5, Ci['nsIXPCTestInterfaceA'], michael@0: [makeB(), makeB(), makeB()], 3, Ci['nsIXPCTestInterfaceB']); michael@0: michael@0: // Test incorrect (too big) array size parameter; this should throw NOT_ENOUGH_ELEMENTS. michael@0: doTypedArrayMismatchTest("testShortArray", new Int16Array([-3, 7, 4]), 4, michael@0: new Int16Array([1, -32, 6]), 3); michael@0: michael@0: // Test type mismatch (int16 <-> uint16); this should throw BAD_CONVERT_JS. michael@0: doTypedArrayMismatchTest("testShortArray", new Uint16Array([0, 7, 4, 3]), 4, michael@0: new Uint16Array([1, 5, 6]), 3); michael@0: }