Bug 933681 - Tests. r=jorendorff
authorBobby Holley <bobbyholley@gmail.com>
Fri, 22 Nov 2013 10:55:43 -0800
changeset 157068 061c63d261e51e8a5f2e2418c638494dc771b590
parent 157067 176caf61a97545ce3c5637b59ba9989394db94a7
child 157069 b14fa789432315ed34417ec2c6edeeeea8cbdf4c
push id36628
push userbobbyholley@gmail.com
push dateFri, 22 Nov 2013 18:55:55 +0000
treeherdermozilla-inbound@061c63d261e5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs933681
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 933681 - Tests. r=jorendorff
js/xpconnect/tests/chrome/chrome.ini
js/xpconnect/tests/chrome/test_xrayToJS.xul
--- a/js/xpconnect/tests/chrome/chrome.ini
+++ b/js/xpconnect/tests/chrome/chrome.ini
@@ -45,16 +45,17 @@ support-files =
 [test_bug801241.xul]
 [test_bug812415.xul]
 [test_bug853283.xul]
 [test_bug853571.xul]
 [test_bug858101.xul]
 [test_bug860494.xul]
 [test_bug866823.xul]
 [test_bug895340.xul]
+[test_xrayToJS.xul]
 [test_chrometoSource.xul]
 [test_cows.xul]
 [test_documentdomain.xul]
 [test_doublewrappedcompartments.xul]
 [test_evalInSandbox.xul]
 [test_evalInWindow.xul]
 [test_exnstack.xul]
 [test_expandosharing.xul]
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/chrome/test_xrayToJS.xul
@@ -0,0 +1,117 @@
+<?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=933681
+-->
+<window title="Mozilla Bug 933681"
+        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=933681"
+     target="_blank">Mozilla Bug 933681</a>
+  </body>
+
+  <!-- test code goes here -->
+  <script type="application/javascript">
+  <![CDATA[
+
+  /** Test for ES constructors on Xrayed globals. **/
+  SimpleTest.waitForExplicitFinish();
+  const Cu = Components.utils;
+  let global = Cu.getGlobalForObject.bind(Cu);
+
+  simpleConstructors = ['Object', 'Function', 'Array', 'Boolean', 'Date', 'Number',
+                        'String', 'RegExp', 'Error', 'InternalError', 'EvalError',
+                        'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError',
+                        'URIError', 'ArrayBuffer', 'Int8Array', 'Uint8Array',
+                        'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array',
+                        'Float32Array', 'Float64Array', 'Uint8ClampedArray',
+                        'WeakMap', 'Map', 'Set'];
+
+  function go() {
+    var iwin = document.getElementById('ifr').contentWindow;
+
+    // Test constructors that can be instantiated with zero arguments.
+    for (var c of simpleConstructors) {
+      ok(iwin[c], "Constructors appear: " + c);
+      is(iwin[c], Cu.unwaiveXrays(iwin.wrappedJSObject[c]),
+         "we end up with the appropriate constructor: " + c);
+      is(Cu.unwaiveXrays(Cu.waiveXrays(new iwin[c]).constructor), iwin[c],
+         "constructor property is set up right: " + c);
+      is(Object.getPrototypeOf(new iwin[c]),
+         Cu.unwaiveXrays(Cu.waiveXrays(iwin[c]).prototype),
+         "prototype is correct: " + c);
+      is(global(new iwin[c]), iwin, "Got the right global: " + c);
+    }
+
+    // Test Object in more detail.
+    var num = new iwin.Object(4);
+    is(num.valueOf(), 4, "primitive object construction works");
+    is(global(num), iwin, "correct global for num");
+    var obj = new iwin.Object();
+    obj.foo = 2;
+    var withProto = iwin.Object.create(obj);
+    is(global(withProto), iwin, "correct global for withProto");
+    is(withProto.foo, 2, "Inherits properly");
+
+    // Test Function.
+    var primitiveFun = new iwin.Function('return 2');
+    is(global(primitiveFun), iwin, "function construction works");
+    is(primitiveFun(), 2, "basic function works");
+    var doSetFoo = new iwin.Function('arg', 'arg.foo = 2;');
+    is(global(doSetFoo), iwin, "function with args works");
+    try {
+      doSetFoo(new Object());
+      ok(false, "should have thrown while setting property on object");
+    } catch (e) {
+      ok(!!/denied/.test(e), "Threw correctly: " + e);
+    }
+    var factoryFun = new iwin.Function('return {foo: 32}');
+    is(global(factoryFun), iwin, "proper global for factoryFun");
+    is(factoryFun().foo, 32, "factoryFun invokable");
+    is(global(factoryFun()), iwin, "minted objects live in the content scope");
+
+    // Test interface objects that don't actually construct things.
+    is(iwin.Math.tan(4.5), Math.tan(4.5), "Math.tan works");
+    is(iwin.Math.E, Math.E, "Math.E works");
+    var json = JSON.stringify({a: 2, b: 'hi', c: {d: 'there'}});
+    is(global(iwin.JSON.parse(json)), iwin, "JSON rehydrated into the right context");
+    is(iwin.JSON.stringify(iwin.JSON.parse(json)), json, "JSON composition identity holds");
+
+    // Test proxies.
+    var targetObject = new iwin.Object();
+    targetObject.foo = 9;
+    var forwardingProxy = new iwin.Proxy(targetObject, new iwin.Object());
+    is(global(forwardingProxy), iwin, "proxy global correct");
+    is(forwardingProxy.foo, 9, "forwards correctly");
+    // NB: COW-implemented proxy handlers are super dangerous, and we should not
+    // encourage them.
+    var handler = {get: function(target, name) { return name * 2; }, __exposedProps__: {get: 'r'}};
+    var doublingProxy = new iwin.Proxy(targetObject, handler);
+    is(global(doublingProxy), iwin, "doubling proxy global correct");
+    is(doublingProxy[3], 6, "Doubles correctly");
+    is(doublingProxy[20], 40, "Doubles correctly");
+
+    // Test eval.
+    var toEval = "({a: 2, b: {foo: 'bar'}, f: function() { return window; }})";
+    is(global(iwin.eval(toEval)), iwin, "eval creates objects in the correct global");
+    is(iwin.eval(toEval).b.foo, 'bar', "eval-ed object looks right");
+    is(iwin.eval(toEval).f(), iwin, "evaled function works right");
+
+    // We could also test DataView and Iterator here for completeness, but it's
+    // more trouble than it's worth.
+
+
+    SimpleTest.finish();
+  }
+
+
+
+
+  ]]>
+  </script>
+  <iframe id="ifr" onload="go();" src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html" />
+</window>