js/xpconnect/tests/unit/test_params.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/test_params.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,189 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +
    1.11 +function run_test() {
    1.12 +
    1.13 +  // Load the component manifests.
    1.14 +  Components.manager.autoRegister(do_get_file('../components/native/xpctest.manifest'));
    1.15 +  Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
    1.16 +
    1.17 +  // Test for each component.
    1.18 +  test_component("@mozilla.org/js/xpc/test/native/Params;1");
    1.19 +  test_component("@mozilla.org/js/xpc/test/js/Params;1");
    1.20 +}
    1.21 +
    1.22 +function test_component(contractid) {
    1.23 +
    1.24 +  // Instantiate the object.
    1.25 +  var o = Cc[contractid].createInstance(Ci["nsIXPCTestParams"]);
    1.26 +
    1.27 +  // Possible comparator functions.
    1.28 +  var standardComparator = function(a,b) {return a == b;};
    1.29 +  var dotEqualsComparator = function(a,b) {return a.equals(b); }
    1.30 +  var fuzzComparator = function(a,b) {return Math.abs(a - b) < 0.1;};
    1.31 +  var interfaceComparator = function(a,b) {return a.name == b.name; }
    1.32 +  var arrayComparator = function(innerComparator) {
    1.33 +    return function(a,b) {
    1.34 +      if (a.length != b.length)
    1.35 +        return false;
    1.36 +      for (var i = 0; i < a.length; ++i)
    1.37 +        if (!innerComparator(a[i], b[i]))
    1.38 +          return false;
    1.39 +      return true;
    1.40 +    };
    1.41 +  };
    1.42 +
    1.43 +  // Helper test function - takes the name of test method and two values of
    1.44 +  // the given type.
    1.45 +  //
    1.46 +  // The optional comparator argument can be used for alternative notions of
    1.47 +  // equality. The comparator should return true on equality.
    1.48 +  function doTest(name, val1, val2, comparator) {
    1.49 +    if (!comparator)
    1.50 +      comparator = standardComparator;
    1.51 +    var a = val1;
    1.52 +    var b = {value: val2};
    1.53 +    var rv = o[name].call(o, a, b);
    1.54 +    do_check_true(comparator(rv, val2));
    1.55 +    do_check_true(comparator(val1, b.value));
    1.56 +  };
    1.57 +
    1.58 +  function doIsTest(name, val1, val1Is, val2, val2Is, valComparator, isComparator) {
    1.59 +    if (!isComparator)
    1.60 +      isComparator = standardComparator;
    1.61 +    var a = val1;
    1.62 +    var aIs = val1Is;
    1.63 +    var b = {value: val2};
    1.64 +    var bIs = {value: val2Is};
    1.65 +    var rvIs = {};
    1.66 +    var rv = o[name].call(o, aIs, a, bIs, b, rvIs);
    1.67 +    do_check_true(valComparator(rv, val2));
    1.68 +    do_check_true(isComparator(rvIs.value, val2Is));
    1.69 +    do_check_true(valComparator(val1, b.value));
    1.70 +    do_check_true(isComparator(val1Is, bIs.value));
    1.71 +  }
    1.72 +
    1.73 +  // Special-purpose function for testing arrays of iid_is interfaces, where we
    1.74 +  // have 2 distinct sets of dependent parameters.
    1.75 +  function doIs2Test(name, val1, val1Size, val1IID, val2, val2Size, val2IID) {
    1.76 +    var a = val1;
    1.77 +    var aSize = val1Size;
    1.78 +    var aIID = val1IID;
    1.79 +    var b = {value: val2};
    1.80 +    var bSize = {value: val2Size};
    1.81 +    var bIID = {value: val2IID};
    1.82 +    var rvSize = {};
    1.83 +    var rvIID = {};
    1.84 +    var rv = o[name].call(o, aSize, aIID, a, bSize, bIID, b, rvSize, rvIID);
    1.85 +    do_check_true(arrayComparator(interfaceComparator)(rv, val2));
    1.86 +    do_check_true(standardComparator(rvSize.value, val2Size));
    1.87 +    do_check_true(dotEqualsComparator(rvIID.value, val2IID));
    1.88 +    do_check_true(arrayComparator(interfaceComparator)(val1, b.value));
    1.89 +    do_check_true(standardComparator(val1Size, bSize.value));
    1.90 +    do_check_true(dotEqualsComparator(val1IID, bIID.value));
    1.91 +  }
    1.92 +
    1.93 +  // Check that the given call (type mismatch) results in an exception being thrown.
    1.94 +  function doTypedArrayMismatchTest(name, val1, val1Size, val2, val2Size) {
    1.95 +    var comparator = arrayComparator(standardComparator);
    1.96 +    var error = false;
    1.97 +    try {
    1.98 +      doIsTest(name, val1, val1Size, val2, val2Size, comparator);
    1.99 +      
   1.100 +      // An exception was not thrown as would have been expected.
   1.101 +      do_check_true(false);
   1.102 +    }
   1.103 +    catch (e) {
   1.104 +      // An exception was thrown as expected.
   1.105 +      do_check_true(true);
   1.106 +    }
   1.107 +  }
   1.108 +
   1.109 +  // Workaround for bug 687612 (inout parameters broken for dipper types).
   1.110 +  // We do a simple test of copying a into b, and ignore the rv.
   1.111 +  function doTestWorkaround(name, val1) {
   1.112 +    var a = val1;
   1.113 +    var b = {value: ""};
   1.114 +    o[name].call(o, a, b);
   1.115 +    do_check_eq(val1, b.value);
   1.116 +  }
   1.117 +
   1.118 +  // Test all the different types
   1.119 +  doTest("testBoolean", true, false);
   1.120 +  doTest("testOctet", 4, 156);
   1.121 +  doTest("testShort", -456, 1299);
   1.122 +  doTest("testLong", 50060, -12121212);
   1.123 +  doTest("testLongLong", 12345, -10000000000);
   1.124 +  doTest("testUnsignedShort", 1532, 65000);
   1.125 +  doTest("testUnsignedLong", 0, 4000000000);
   1.126 +  doTest("testUnsignedLongLong", 215435, 3453492580348535809);
   1.127 +  doTest("testFloat", 4.9, -11.2, fuzzComparator);
   1.128 +  doTest("testDouble", -80.5, 15000.2, fuzzComparator);
   1.129 +  doTest("testChar", "a", "2");
   1.130 +  doTest("testString", "someString", "another string");
   1.131 +  doTest("testWstring", "Why wasnt this", "turned on before? ಠ_ಠ");
   1.132 +  doTest("testWchar", "z", "ア");
   1.133 +  doTestWorkaround("testDOMString", "Beware: ☠ s");
   1.134 +  doTestWorkaround("testAString", "Frosty the ☃ ;-)");
   1.135 +  doTestWorkaround("testAUTF8String", "We deliver 〠!");
   1.136 +  doTestWorkaround("testACString", "Just a regular C string.");
   1.137 +  doTest("testJsval", {aprop: 12, bprop: "str"}, 4.22);
   1.138 +
   1.139 +  // Helpers to instantiate various test XPCOM objects.
   1.140 +  var numAsMade = 0;
   1.141 +  function makeA() {
   1.142 +    var a = Cc["@mozilla.org/js/xpc/test/js/InterfaceA;1"].createInstance(Ci['nsIXPCTestInterfaceA']);
   1.143 +    a.name = 'testA' + numAsMade++;
   1.144 +    return a;
   1.145 +  };
   1.146 +  var numBsMade = 0;
   1.147 +  function makeB() {
   1.148 +    var b = Cc["@mozilla.org/js/xpc/test/js/InterfaceB;1"].createInstance(Ci['nsIXPCTestInterfaceB']);
   1.149 +    b.name = 'testB' + numBsMade++;
   1.150 +    return b;
   1.151 +  };
   1.152 +
   1.153 +  // Test arrays.
   1.154 +  doIsTest("testShortArray", [2, 4, 6], 3, [1, 3, 5, 7], 4, arrayComparator(standardComparator));
   1.155 +  doIsTest("testDoubleArray", [-10, -0.5], 2, [1, 3, 1e11, -8e-5 ], 4, arrayComparator(fuzzComparator));
   1.156 +
   1.157 +  doIsTest("testStringArray", ["mary", "hat", "hey", "lid", "tell", "lam"], 6,
   1.158 +                              ["ids", "fleas", "woes", "wide", "has", "know", "!"], 7, arrayComparator(standardComparator));
   1.159 +  doIsTest("testWstringArray", ["沒有語言", "的偉大嗎?]"], 2,
   1.160 +                               ["we", "are", "being", "sooo", "international", "right", "now"], 7, arrayComparator(standardComparator));
   1.161 +  doIsTest("testInterfaceArray", [makeA(), makeA()], 2,
   1.162 +                                 [makeA(), makeA(), makeA(), makeA(), makeA(), makeA()], 6, arrayComparator(interfaceComparator));
   1.163 +
   1.164 +  // Test typed arrays and ArrayBuffer aliasing.
   1.165 +  var arrayBuffer = new ArrayBuffer(16);
   1.166 +  var int16Array = new Int16Array(arrayBuffer, 2, 3);
   1.167 +  int16Array.set([-32768, 0, 32767]);
   1.168 +  doIsTest("testShortArray", int16Array, 3, new Int16Array([1773, -32768, 32767, 7]), 4, arrayComparator(standardComparator));
   1.169 +  doIsTest("testDoubleArray", new Float64Array([-10, -0.5]), 2, new Float64Array([0, 3.2, 1.0e10, -8.33 ]), 4, arrayComparator(fuzzComparator));
   1.170 +
   1.171 +  // Test sized strings.
   1.172 +  var ssTests = ["Tis not possible, I muttered", "give me back my free hardcore!", "quoth the server:", "4〠4"];
   1.173 +  doIsTest("testSizedString", ssTests[0], ssTests[0].length, ssTests[1], ssTests[1].length, standardComparator);
   1.174 +  doIsTest("testSizedWstring", ssTests[2], ssTests[2].length, ssTests[3], ssTests[3].length, standardComparator);
   1.175 +
   1.176 +  // Test iid_is.
   1.177 +  doIsTest("testInterfaceIs", makeA(), Ci['nsIXPCTestInterfaceA'],
   1.178 +                              makeB(), Ci['nsIXPCTestInterfaceB'],
   1.179 +                              interfaceComparator, dotEqualsComparator);
   1.180 +
   1.181 +  // Test arrays of iids.
   1.182 +  doIs2Test("testInterfaceIsArray", [makeA(), makeA(), makeA(), makeA(), makeA()], 5, Ci['nsIXPCTestInterfaceA'],
   1.183 +                                    [makeB(), makeB(), makeB()], 3, Ci['nsIXPCTestInterfaceB']);
   1.184 +
   1.185 +  // Test incorrect (too big) array size parameter; this should throw NOT_ENOUGH_ELEMENTS.
   1.186 +  doTypedArrayMismatchTest("testShortArray", new Int16Array([-3, 7, 4]), 4,
   1.187 +                                             new Int16Array([1, -32, 6]), 3);
   1.188 +
   1.189 +  // Test type mismatch (int16 <-> uint16); this should throw BAD_CONVERT_JS.
   1.190 +  doTypedArrayMismatchTest("testShortArray", new Uint16Array([0, 7, 4, 3]), 4,
   1.191 +                                             new Uint16Array([1, 5, 6]), 3);
   1.192 +}

mercurial