dom/tests/mochitest/whatwg/postMessage_structured_clone_helper.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/whatwg/postMessage_structured_clone_helper.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>postMessage structured clone page</title>
     1.8 +  <script type="application/javascript;version=1.7"
     1.9 +          src="postMessage_structured_clone_helper.js"></script>
    1.10 +  <script type="application/javascript;version=1.7">
    1.11 +    var generator = new getTestContent()
    1.12 +
    1.13 +    function isClone(a, b) {
    1.14 +        window.dump("Object a: " + a + "\n");
    1.15 +        window.dump("Object b: " + b + "\n");
    1.16 +        var stack = [[a, b]];
    1.17 +        var memory = new WeakMap();
    1.18 +        var rmemory = new WeakMap();
    1.19 +
    1.20 +        while (stack.length > 0) {
    1.21 +            var pair = stack.pop();
    1.22 +            var x = pair[0], y = pair[1];
    1.23 +            if (typeof x !== "object" || x === null) {
    1.24 +                // x is primitive.
    1.25 +                if (x !== y) {
    1.26 +                    window.dump("Primitives not equal!\n");
    1.27 +                    return false;
    1.28 +                }
    1.29 +            } else if (x instanceof Date) {
    1.30 +                if (x.getTime() == y.getTime())
    1.31 +                    return true;
    1.32 +                window.dump("Dates not equal!\n");
    1.33 +                return false;
    1.34 +            } else if (memory.has(x)) {
    1.35 +                // x is an object we have seen before in a.
    1.36 +                if (y !== memory.get(x)) {
    1.37 +                    window.dump("Already seen!?\n");
    1.38 +                    return false;
    1.39 +                }
    1.40 +                if (!(rmemory.get(y) == x)) {
    1.41 +                    window.dump("Not equal??\n");
    1.42 +                    return false;
    1.43 +                }
    1.44 +            } else {
    1.45 +                // x is an object we have not seen before.
    1.46 +                // Check that we have not seen y before either.
    1.47 +                if (rmemory.has(y)) {
    1.48 +                    // If we have seen y before, the only possible outcome
    1.49 +                    // is that x and y are literally the same object.
    1.50 +                    if (y == x)
    1.51 +                        continue;
    1.52 +                    window.dump("Already seen y!?\n");
    1.53 +                    window.dump(y.toString() + "\n");
    1.54 +                    return false;
    1.55 +                }
    1.56 +
    1.57 +                // x and y must be of the same [[Class]].
    1.58 +                var xcls = Object.prototype.toString.call(x);
    1.59 +                var ycls = Object.prototype.toString.call(y);
    1.60 +                if (xcls !== ycls) {
    1.61 +                    window.dump("Failing on proto\n");
    1.62 +                    return false;
    1.63 +                }
    1.64 +
    1.65 +                // This function is only designed to check Objects and Arrays.
    1.66 +                if (!(xcls === "[object Object]" || xcls === "[object Array]")) {
    1.67 +                    window.dump("Not an object!\n");
    1.68 +                    window.dump(xcls + "\n");
    1.69 +                    return false;
    1.70 +                }
    1.71 +
    1.72 +                // Compare objects.
    1.73 +                var xk = Object.keys(x), yk = Object.keys(y);
    1.74 +                if (xk.length !== yk.length) {
    1.75 +                    window.dump("Length mismatch!\n");
    1.76 +                    return false;
    1.77 +                }
    1.78 +                for (var i = 0; i < xk.length; i++) {
    1.79 +                    // We must see the same property names in the same order.
    1.80 +                    if (xk[i] !== yk[i]) {
    1.81 +                        window.dump("wrong order\n");
    1.82 +                        return false;
    1.83 +                    }
    1.84 +
    1.85 +                    // Put the property values on the stack to compare later.
    1.86 +                    stack.push([x[xk[i]], y[yk[i]]]);
    1.87 +                }
    1.88 +
    1.89 +                // Record that we have seen this pair of objects.
    1.90 +                memory.set(x, y);
    1.91 +                rmemory.set(y, x);
    1.92 +            }
    1.93 +        }
    1.94 +        return true;
    1.95 +    }
    1.96 +
    1.97 +    function receiveMessage(evt)
    1.98 +    {
    1.99 +      if (isClone(evt.data, generator.next()))
   1.100 +        window.parent.postMessage("TEST-PASS", "*");
   1.101 +      else
   1.102 +        window.parent.postMessage("TEST-FAIL", "*");
   1.103 +    }
   1.104 +    window.addEventListener("message", receiveMessage, false);
   1.105 +  </script>
   1.106 +</head>
   1.107 +<body>
   1.108 +</body>
   1.109 +</html>

mercurial