Bug 693341 - Test iid_is(). r=khuey
authorBobby Holley <bobbyholley@gmail.com>
Thu, 27 Oct 2011 12:43:38 -0700
changeset 79974 5aa96243e15d1e90e886992e1fea8d48fad77e8c
parent 79973 b4e6d15b836edad270b9263fda48838fe0decf85
child 79975 cb81dcf21c85e8881d5227a1945556de3aa1b263
push id506
push userclegnitto@mozilla.com
push dateWed, 09 Nov 2011 02:03:18 +0000
treeherdermozilla-aurora@63587fc7bb93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs693341
milestone10.0a1
Bug 693341 - Test iid_is(). r=khuey
js/xpconnect/tests/components/js/xpctest_params.js
js/xpconnect/tests/components/native/xpctest_params.cpp
js/xpconnect/tests/idl/xpctest_params.idl
js/xpconnect/tests/unit/test_params.js
--- a/js/xpconnect/tests/components/js/xpctest_params.js
+++ b/js/xpconnect/tests/components/js/xpctest_params.js
@@ -88,11 +88,12 @@ TestParams.prototype = {
   testJsval: f,
   testShortArray: f_is,
   testLongLongArray: f_is,
   testStringArray: f_is,
   testWstringArray: f_is,
   testInterfaceArray: f_is,
   testSizedString: f_is,
   testSizedWstring: f_is,
+  testInterfaceIs: f_is,
 };
 
 var NSGetFactory = XPCOMUtils.generateNSGetFactory([TestParams]);
--- a/js/xpconnect/tests/components/native/xpctest_params.cpp
+++ b/js/xpconnect/tests/components/native/xpctest_params.cpp
@@ -300,8 +300,44 @@ NS_IMETHODIMP nsXPCTestParams::TestSized
  *                        inout unsigned long bLength, [size_is (bLength)] inout wstring b,
  *                        out unsigned long rvLength, [size_is (rvLength), retval] out wstring rv); */
 NS_IMETHODIMP nsXPCTestParams::TestSizedWstring(PRUint32 aLength, const PRUnichar * a,
                                                 PRUint32 *bLength NS_INOUTPARAM, PRUnichar * *b NS_INOUTPARAM,
                                                 PRUint32 *rvLength NS_OUTPARAM, PRUnichar * *rv NS_OUTPARAM)
 {
     BUFFER_METHOD_IMPL(PRUnichar, 1, TAKE_OWNERSHIP_NOOP);
 }
+
+/* void testInterfaceIs (in nsIIDPtr aIID, [iid_is (aIID)] in nsQIResult a,
+ *                       inout nsIIDPtr bIID, [iid_is (bIID)] inout nsQIResult b,
+ *                       out nsIIDPtr rvIID, [iid_is (rvIID), retval] out nsQIResult rv); */
+NS_IMETHODIMP nsXPCTestParams::TestInterfaceIs(const nsIID *aIID, void *a,
+                                               nsIID **bIID NS_INOUTPARAM, void **b NS_INOUTPARAM,
+                                               nsIID **rvIID NS_OUTPARAM, void **rv NS_OUTPARAM)
+{
+    //
+    // Getting the buffers and ownership right here can be a little tricky.
+    //
+
+    // The interface pointers are heap-allocated, and b has been AddRef'd
+    // by XPConnect for the duration of the call. If we snatch it away from b
+    // and leave no trace, XPConnect won't Release it. Since we also need to
+    // return an already-AddRef'd pointer in rv, we don't need to do anything
+    // special here.
+    *rv = *b;
+
+    // rvIID is out-only, so nobody allocated an IID buffer for us. Do that now,
+    // and store b's IID in the new buffer.
+    *rvIID = static_cast<nsIID*>(NS_Alloc(sizeof(nsID)));
+    if (!*rvIID)
+        return NS_ERROR_OUT_OF_MEMORY;
+    **rvIID = **bIID;
+
+    // Copy the interface pointer from a to b. Since a is in-only, XPConnect will
+    // release it upon completion of the call. AddRef it for b.
+    *b = a;
+    static_cast<nsISupports*>(*b)->AddRef();
+
+    // We already had a buffer allocated for b's IID, so we can re-use it.
+    **bIID = *aIID;
+
+    return NS_OK;
+}
--- a/js/xpconnect/tests/idl/xpctest_params.idl
+++ b/js/xpconnect/tests/idl/xpctest_params.idl
@@ -94,9 +94,14 @@ interface nsIXPCTestParams : nsISupports
 
   // Test sized strings.
   void                  testSizedString(in unsigned long aLength, [size_is(aLength)] in string a,
                                         inout unsigned long bLength, [size_is(bLength)] inout string b,
                                         out unsigned long rvLength, [retval, size_is(rvLength)] out string rv);
   void                  testSizedWstring(in unsigned long aLength, [size_is(aLength)] in wstring a,
                                          inout unsigned long bLength, [size_is(bLength)] inout wstring b,
                                          out unsigned long rvLength, [retval, size_is(rvLength)] out wstring rv);
+
+  // Test iid_is.
+  void                  testInterfaceIs(in nsIIDPtr aIID, [iid_is(aIID)] in nsQIResult a,
+                                        inout nsIIDPtr bIID, [iid_is(bIID)] inout nsQIResult b,
+                                        out nsIIDPtr rvIID, [retval, iid_is(rvIID)] out nsQIResult rv);
 };
--- a/js/xpconnect/tests/unit/test_params.js
+++ b/js/xpconnect/tests/unit/test_params.js
@@ -50,17 +50,19 @@ function run_test() {
 
 function test_component(contractid) {
 
   // Instantiate the object.
   var o = Cc[contractid].createInstance(Ci["nsIXPCTestParams"]);
 
   // Possible comparator functions.
   var standardComparator = function(a,b) {return a == b;};
+  var dotEqualsComparator = function(a,b) {return a.equals(b); }
   var fuzzComparator = function(a,b) {return Math.abs(a - b) < 0.1;};
+  var interfaceComparator = function(a,b) {return a.name == b.name; }
   var arrayComparator = function(a,b) {
     if (a.length != b.length)
       return false;
     for (var i = 0; i < a.length; ++i)
       if (a[i] != b[i])
         return false;
     return true;
   };
@@ -75,27 +77,29 @@ function test_component(contractid) {
       comparator = standardComparator;
     var a = val1;
     var b = {value: val2};
     var rv = o[name].call(o, a, b);
     do_check_true(comparator(rv, val2));
     do_check_true(comparator(val1, b.value));
   };
 
-  function doIsTest(name, val1, val1Is, val2, val2Is, comparator) {
+  function doIsTest(name, val1, val1Is, val2, val2Is, valComparator, isComparator) {
+    if (!isComparator)
+      isComparator = standardComparator;
     var a = val1;
     var aIs = val1Is;
     var b = {value: val2};
     var bIs = {value: val2Is};
     var rvIs = {};
     var rv = o[name].call(o, aIs, a, bIs, b, rvIs);
-    do_check_true(comparator(rv, val2));
-    do_check_eq(rvIs.value, val2Is);
-    do_check_true(comparator(val1, b.value));
-    do_check_eq(val1Is, bIs.value);
+    do_check_true(valComparator(rv, val2));
+    do_check_true(isComparator(rvIs.value, val2Is));
+    do_check_true(valComparator(val1, b.value));
+    do_check_true(isComparator(val1Is, bIs.value));
   }
 
   // Workaround for bug 687612 (inout parameters broken for dipper types).
   // We do a simple test of copying a into b, and ignore the rv.
   function doTestWorkaround(name, val1) {
     var a = val1;
     var b = {value: ""};
     o[name].call(o, a, b);
@@ -112,17 +116,16 @@ function test_component(contractid) {
   doTest("testUnsignedLong", 0, 4000000000);
   doTest("testUnsignedLongLong", 215435, 3453492580348535809);
   doTest("testFloat", 4.9, -11.2, fuzzComparator);
   doTest("testDouble", -80.5, 15000.2, fuzzComparator);
   doTest("testChar", "a", "2");
   doTest("testString", "someString", "another string");
   // TODO: Fix bug 687679 and use the second argument listed below
   doTest("testWchar", "z", "q");// "ア");
-  // TODO - Test nsIID in bug 687662
   doTestWorkaround("testDOMString", "Beware: ☠ s");
   doTestWorkaround("testAString", "Frosty the ☃ ;-)");
   doTestWorkaround("testAUTF8String", "We deliver 〠!");
   doTestWorkaround("testACString", "Just a regular C string.");
   doTest("testJsval", {aprop: 12, bprop: "str"}, 4.22);
 
   // Helpers to instantiate various test XPCOM objects.
   function makeA() { return Cc["@mozilla.org/js/xpc/test/js/InterfaceA;1"].createInstance(Ci['nsIXPCTestInterfaceA']); };
@@ -137,9 +140,14 @@ function test_component(contractid) {
                                ["we", "are", "being", "sooo", "international", "right", "now"], 7, arrayComparator);
   doIsTest("testInterfaceArray", [makeA(), makeA()], 2,
                                  [makeA(), makeA(), makeA(), makeA(), makeA(), makeA()], 6, arrayComparator);
 
   // Test sized strings.
   var ssTests = ["Tis not possible, I muttered", "give me back my free hardcore!", "quoth the server:", "4〠4"];
   doIsTest("testSizedString", ssTests[0], ssTests[0].length, ssTests[1], ssTests[1].length, standardComparator);
   doIsTest("testSizedWstring", ssTests[2], ssTests[2].length, ssTests[3], ssTests[3].length, standardComparator);
+
+  // Test iid_is.
+  doIsTest("testInterfaceIs", makeA(), Ci['nsIXPCTestInterfaceA'],
+                              makeB(), Ci['nsIXPCTestInterfaceB'],
+                              interfaceComparator, dotEqualsComparator);
 }