js/xpconnect/tests/chrome/test_scriptSettings.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
michael@0 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=937317
michael@0 6 -->
michael@0 7 <window title="Mozilla Bug 937317"
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
michael@0 10
michael@0 11 <!-- test results are displayed in the html:body -->
michael@0 12 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 13 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=937317"
michael@0 14 target="_blank">Mozilla Bug 937317</a>
michael@0 15 </body>
michael@0 16
michael@0 17 <!-- test code goes here -->
michael@0 18 <iframe src="data:text/html,"></iframe>
michael@0 19 <script type="application/javascript">
michael@0 20 <![CDATA[
michael@0 21
michael@0 22 /** Test for the script settings stack. **/
michael@0 23 SimpleTest.waitForExplicitFinish();
michael@0 24 addLoadEvent(function() {
michael@0 25 const Cu = Components.utils;
michael@0 26 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 27 iwin = window[0];
michael@0 28
michael@0 29 // Smoketest.
michael@0 30 is(Cu.getIncumbentGlobal(), window, "smoketest");
michael@0 31
michael@0 32 // Calling a cross-compartment non-scripted function changes the
michael@0 33 // compartment, but not the incumbent script settings object.
michael@0 34 var sb = new Cu.Sandbox(window, { wantComponents: true });
michael@0 35 is(sb.Components.utils.getIncumbentGlobal(), window, "cross-compartment sb non-scripted");
michael@0 36 is(iwin.Components.utils.getIncumbentGlobal(), window, "cross-compartment win non-scripted");
michael@0 37
michael@0 38 // If we call a scripted function in the other compartment, that becomes
michael@0 39 // the incumbent script.
michael@0 40 function gib() { return Components.utils.getIncumbentGlobal(); };
michael@0 41 Cu.evalInSandbox(gib.toSource(), sb);
michael@0 42 iwin.eval(gib.toSource());
michael@0 43 is(sb.gib(), sb, "cross-compartment sb scripted");
michael@0 44 is(iwin.gib(), iwin, "cross-compartment win scripted");
michael@0 45
michael@0 46 // Eval-ing top-level script in another component makes that compartment the
michael@0 47 // incumbent script.
michael@0 48 is(Cu.evalInSandbox('Components.utils.getIncumbentGlobal()', sb), sb, 'eval sb');
michael@0 49 is(iwin.eval('Components.utils.getIncumbentGlobal()'), iwin, 'eval iwin');
michael@0 50
michael@0 51 // Make sure that the callback mechanism works.
michael@0 52 function makeCallback(expectedGlobal, deferred, kind) {
michael@0 53 function cb(incumbentGlobal) {
michael@0 54 is(incumbentGlobal, expectedGlobal, "Callback got the right incumbent global: " + kind);
michael@0 55 if (deferred)
michael@0 56 deferred.resolve();
michael@0 57 }
michael@0 58 info("Generated callback: " + kind);
michael@0 59 return cb;
michael@0 60 }
michael@0 61
michael@0 62 var bound = Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, undefined, "simple bound"));
michael@0 63 is(bound(), window, "Bound method returns the right global");
michael@0 64
michael@0 65 // Callbacks grab the incumbent script at the time of invocation.
michael@0 66 //
michael@0 67 // Note - We avoid calling the initial defer |d| so that it's not in-scope for everything below.
michael@0 68 let initialDefer = Promise.defer();
michael@0 69 setTimeout(Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, initialDefer, "same-global setTimeout")), 0);
michael@0 70 initialDefer.promise.then(function() {
michael@0 71
michael@0 72 // Try cross-global setTimeout where |window| is the incumbent script when the callback is created.
michael@0 73 let d = Promise.defer();
michael@0 74 iwin.setTimeout(Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, d, "cross-global setTimeout by |window|")), 0);
michael@0 75 return d.promise;
michael@0 76 }).then(function() {
michael@0 77
michael@0 78 // Try cross-global setTimeout where |iwin| is the incumbent script when the callback is created.
michael@0 79 let d = Promise.defer();
michael@0 80 iwin.wrappedJSObject.timeoutFun = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global setTimeout by |iwin|"));
michael@0 81 iwin.eval('setTimeout(timeoutFun, 0);');
michael@0 82 return d.promise;
michael@0 83 }).then(function() {
michael@0 84
michael@0 85 // The incumbent script override doesn't take effect if the callback is scripted.
michael@0 86 let d = Promise.defer();
michael@0 87 iwin.wrappedJSObject.timeoutFun2 = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global setTimeout of scripted function"));
michael@0 88 iwin.eval('let timeoutFun2Wrapper = function() { timeoutFun2(); }');
michael@0 89 setTimeout(iwin.wrappedJSObject.timeoutFun2Wrapper, 0);
michael@0 90 return d.promise;
michael@0 91 }).then(function() {
michael@0 92
michael@0 93 // Try event listeners.
michael@0 94 let d = Promise.defer();
michael@0 95 let body = iwin.document.body;
michael@0 96 body.addEventListener('click', Cu.getIncumbentGlobal.bind(Cu, makeCallback(window, d, "cross-global event listener")));
michael@0 97 body.dispatchEvent(new iwin.MouseEvent('click'));
michael@0 98 return d.promise;
michael@0 99
michael@0 100 }).then(function() {
michael@0 101
michael@0 102 // Try an event handler set by |iwin|.
michael@0 103 let d = Promise.defer();
michael@0 104 let body = iwin.document.body;
michael@0 105 iwin.wrappedJSObject.handler = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global event handler"));
michael@0 106 iwin.eval('document.body.onmousemove = handler');
michael@0 107 body.dispatchEvent(new iwin.MouseEvent('mousemove'));
michael@0 108 return d.promise;
michael@0 109
michael@0 110 }).then(function() {
michael@0 111
michael@0 112 // Try an event handler compiled by a content attribute.
michael@0 113 let d = Promise.defer();
michael@0 114 let body = iwin.document.body;
michael@0 115 iwin.wrappedJSObject.innerHandler = Cu.getIncumbentGlobal.bind(Cu, makeCallback(iwin, d, "cross-global compiled event handler"));
michael@0 116 iwin.eval("document.body.setAttribute('onmouseout', 'innerHandler()')");
michael@0 117 body.dispatchEvent(new iwin.MouseEvent('mouseout'));
michael@0 118 return d.promise;
michael@0 119 }).then(function() {
michael@0 120
michael@0 121 SimpleTest.finish();
michael@0 122 });
michael@0 123 });
michael@0 124
michael@0 125 ]]>
michael@0 126 </script>
michael@0 127 </window>

mercurial