js/xpconnect/tests/chrome/test_bug760109.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/chrome/test_bug760109.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     1.6 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
     1.7 +<!--
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=760109
     1.9 +-->
    1.10 +<window title="Mozilla Bug 760109"
    1.11 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.13 +
    1.14 +  <!-- test results are displayed in the html:body -->
    1.15 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.16 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=760109"
    1.17 +     target="_blank">Mozilla Bug 760109</a>
    1.18 +  </body>
    1.19 +
    1.20 +  <!-- test code goes here -->
    1.21 +  <script type="application/javascript">
    1.22 +  <![CDATA[
    1.23 +
    1.24 +  /** Test for COW prototype remapping.**/
    1.25 +
    1.26 +  // This gets decompiled and run inside the sandbox.
    1.27 +  function sandboxCode() {
    1.28 +
    1.29 +    // Check that COWs for objects with standard prototypes use the standard
    1.30 +    // prototype in the home compartment.
    1.31 +    ok(Object.getPrototypeOf(chromeArray) === Array.prototype,
    1.32 +       "Array prototype remapped properly");
    1.33 +    var protoProto = Object.getPrototypeOf(Object.getPrototypeOf(chromeObject));
    1.34 +    ok(protoProto === Object.prototype,
    1.35 +       "Object prototype remapped properly");
    1.36 +
    1.37 +    // Check |constructor|.
    1.38 +    // Note that the 'constructor' property of the underlying chrome object
    1.39 +    // will be resolved on SomeConstructor.prototype, which has an empty
    1.40 +    // __exposedProps__. This means that we shouldn't remap the property, even
    1.41 +    // though we'd also be able to find it on Object.prototype. Some recent
    1.42 +    // refactoring has made it possible to do the right thing here.
    1.43 +    is(typeof chromeObject.constructor, "undefined", "Object constructor does what we expect");
    1.44 +    ok(chromeArray.constructor === Array, "Array constructor remapped properly");
    1.45 +
    1.46 +    // We should be able to .forEach on the Array.
    1.47 +    var concat = '';
    1.48 +    chromeArray.forEach(function(val) { concat += val; });
    1.49 +    is(concat, 'abz', "Should be able to .forEach COW-ed Array");
    1.50 +
    1.51 +    // Try other Array operations.
    1.52 +    is(chromeArray.indexOf('b'), 1, "indexOf works correctly");
    1.53 +    is(chromeArray.join(''), concat, "join works correctly");
    1.54 +    is(chromeArray.slice(1).join(''), 'bz', "slice works correctly");
    1.55 +    is(chromeArray.length, 3, "Able to check array length");
    1.56 +
    1.57 +    // Try some operations that modify the array.
    1.58 +    is(chromeArray.pop(), 'z', "Able to call pop");
    1.59 +    is(chromeArray.push('z'), 3, "Able to call push");
    1.60 +    chromeArray.reverse();
    1.61 +    is(chromeArray.join(''), 'zba', "Able to call reverse");
    1.62 +    chromeArray.sort();
    1.63 +    is(chromeArray.join(''), 'abz', "Able to call sort");
    1.64 +
    1.65 +    // Make sure we can see the data on the typed array
    1.66 +    is(chromeTypedArray.length, 3, "Able to check typed array length");
    1.67 +    is(chromeTypedArray[0], 10, "Able to access typed array data");
    1.68 +
    1.69 +    // We should be able to .hasOwnProperty on the Object, and have
    1.70 +    // it filter the objects we can see.
    1.71 +    ok(chromeObject.hasOwnProperty('foo'), "Should see r property");
    1.72 +    ok(!chromeObject.hasOwnProperty('bar'), "Shouldn't see non-exposed property");
    1.73 +    ok(chromeObject.hasOwnProperty('baz'), "Should see rw property");
    1.74 +  }
    1.75 +
    1.76 +  // We use a constructor to create the test object so that there's an
    1.77 +  // intermediate object on the prototype chain between the instance and the
    1.78 +  // standard prototype.
    1.79 +  function SomeConstructor() {
    1.80 +    this.foo = 2;
    1.81 +    this.bar = 3;
    1.82 +    this.baz = 4;
    1.83 +    this.__exposedProps__ = {foo: 'r', baz: 'rw'};
    1.84 +  }
    1.85 +  SomeConstructor.prototype.__exposedProps__ = {};
    1.86 +
    1.87 +  const Cu = Components.utils;
    1.88 +  var sb = new Cu.Sandbox('http://www.example.org');
    1.89 +  sb.chromeArray = ['a', 'b', 'z'];
    1.90 +  sb.chromeArray.__exposedProps__ = {};
    1.91 +  sb.chromeTypedArray = new Uint8Array([10, 20, 30]);
    1.92 +  sb.chromeObject = new SomeConstructor();
    1.93 +  sb.ok = ok;
    1.94 +  sb.is = is;
    1.95 +  Cu.evalInSandbox('(' + sandboxCode.toSource() + ')();', sb);
    1.96 +
    1.97 +  ]]>
    1.98 +  </script>
    1.99 +</window>

mercurial