gecko/js/xpconnect/tests/chrome/test_bug760109.xul
2013-03-11 14:45:21 -07:00

97 lines
3.9 KiB
XML

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=760109
-->
<window title="Mozilla Bug 760109"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=760109"
target="_blank">Mozilla Bug 760109</a>
</body>
<!-- test code goes here -->
<script type="application/javascript">
<![CDATA[
/** Test for COW prototype remapping.**/
// This gets decompiled and run inside the sandbox.
function sandboxCode() {
// Check that COWs for objects with standard prototypes use the standard
// prototype in the home compartment.
ok(Object.getPrototypeOf(chromeArray) === Array.prototype,
"Array prototype remapped properly");
var protoProto = Object.getPrototypeOf(Object.getPrototypeOf(chromeObject));
ok(protoProto === Object.prototype,
"Object prototype remapped properly");
// Check |constructor|.
// Note that the 'constructor' property of the underlying chrome object
// will be resolved on SomeConstructor.prototype, which has an empty
// __exposedProps__. This means that we shouldn't remap the property, even
// though we'd also be able to find it on Object.prototype. Some recent
// refactoring has made it possible to do the right thing here.
is(typeof chromeObject.constructor, "undefined", "Object constructor does what we expect");
ok(chromeArray.constructor === Array, "Array constructor remapped properly");
// We should be able to .forEach on the Array.
var concat = '';
chromeArray.forEach(function(val) { concat += val; });
is(concat, 'abz', "Should be able to .forEach COW-ed Array");
// Try other Array operations.
is(chromeArray.indexOf('b'), 1, "indexOf works correctly");
is(chromeArray.join(''), concat, "join works correctly");
is(chromeArray.slice(1).join(''), 'bz', "slice works correctly");
is(chromeArray.length, 3, "Able to check array length");
// Try some operations that modify the array.
is(chromeArray.pop(), 'z', "Able to call pop");
is(chromeArray.push('z'), 3, "Able to call push");
chromeArray.reverse();
is(chromeArray.join(''), 'zba', "Able to call reverse");
chromeArray.sort();
is(chromeArray.join(''), 'abz', "Able to call sort");
// Make sure we can see the data on the typed array
is(chromeTypedArray.length, 3, "Able to check typed array length");
is(chromeTypedArray[0], 10, "Able to access typed array data");
// We should be able to .hasOwnProperty on the Object, and have
// it filter the objects we can see.
ok(chromeObject.hasOwnProperty('foo'), "Should see r property");
ok(!chromeObject.hasOwnProperty('bar'), "Shouldn't see non-exposed property");
ok(chromeObject.hasOwnProperty('baz'), "Should see rw property");
}
// We use a constructor to create the test object so that there's an
// intermediate object on the prototype chain between the instance and the
// standard prototype.
function SomeConstructor() {
this.foo = 2;
this.bar = 3;
this.baz = 4;
this.__exposedProps__ = {foo: 'r', baz: 'rw'};
}
SomeConstructor.prototype.__exposedProps__ = {};
const Cu = Components.utils;
var sb = new Cu.Sandbox('http://www.example.org');
sb.chromeArray = ['a', 'b', 'z'];
sb.chromeArray.__exposedProps__ = {};
sb.chromeTypedArray = new Uint8Array([10, 20, 30]);
sb.chromeObject = new SomeConstructor();
sb.ok = ok;
sb.is = is;
Cu.evalInSandbox('(' + sandboxCode.toSource() + ')();', sb);
]]>
</script>
</window>