Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the public domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Test that an <iframe mozbrowser> is a window.{top,parent,frameElement} barrier. |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 8 | browserElementTestHelpers.setEnabledPref(true); |
michael@0 | 9 | browserElementTestHelpers.addPermission(); |
michael@0 | 10 | |
michael@0 | 11 | var iframe; |
michael@0 | 12 | function runTest() { |
michael@0 | 13 | iframe = document.createElement('iframe'); |
michael@0 | 14 | iframe.addEventListener('mozbrowserloadend', function() { |
michael@0 | 15 | try { |
michael@0 | 16 | outerIframeLoaded(); |
michael@0 | 17 | } catch(e) { |
michael@0 | 18 | dump("Got error: " + e + '\n'); |
michael@0 | 19 | } |
michael@0 | 20 | }); |
michael@0 | 21 | SpecialPowers.wrap(iframe).mozbrowser = true; |
michael@0 | 22 | iframe.src = 'data:text/html,Outer iframe <iframe id="inner-iframe"></iframe>'; |
michael@0 | 23 | // For kicks, this test uses a display:none iframe. This shouldn't make a |
michael@0 | 24 | // difference in anything. |
michael@0 | 25 | iframe.style.display = 'none'; |
michael@0 | 26 | document.body.appendChild(iframe); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | var numMsgReceived = 0; |
michael@0 | 30 | function outerIframeLoaded() { |
michael@0 | 31 | var injectedScript = |
michael@0 | 32 | "data:,function is(a, b, desc) { \ |
michael@0 | 33 | if (a == b) { \ |
michael@0 | 34 | sendAsyncMessage('test:test-pass', desc); \ |
michael@0 | 35 | } else { \ |
michael@0 | 36 | sendAsyncMessage('test:test-fail', desc + ' ' + a + ' != ' + b); \ |
michael@0 | 37 | } \ |
michael@0 | 38 | } \ |
michael@0 | 39 | is(content.window.top, content.window, 'top'); \ |
michael@0 | 40 | is(content.window.content, content.window, 'content'); \ |
michael@0 | 41 | is(content.window.parent, content.window, 'parent'); \ |
michael@0 | 42 | is(content.window.frameElement, null, 'frameElement'); \ |
michael@0 | 43 | var innerIframe = content.document.getElementById('inner-iframe'); \ |
michael@0 | 44 | var innerWindow = innerIframe.contentWindow; \ |
michael@0 | 45 | is(innerWindow.top, content.window, 'inner top'); \ |
michael@0 | 46 | is(innerWindow.content, content.window, 'inner content'); \ |
michael@0 | 47 | is(innerWindow.parent, content.window, 'inner parent'); \ |
michael@0 | 48 | is(innerWindow.frameElement, innerIframe, 'inner frameElement');" |
michael@0 | 49 | |
michael@0 | 50 | var mm = SpecialPowers.getBrowserFrameMessageManager(iframe); |
michael@0 | 51 | |
michael@0 | 52 | function onRecvTestPass(msg) { |
michael@0 | 53 | numMsgReceived++; |
michael@0 | 54 | ok(true, msg.json); |
michael@0 | 55 | } |
michael@0 | 56 | mm.addMessageListener('test:test-pass', onRecvTestPass); |
michael@0 | 57 | |
michael@0 | 58 | function onRecvTestFail(msg) { |
michael@0 | 59 | numMsgReceived++; |
michael@0 | 60 | ok(false, msg.json); |
michael@0 | 61 | } |
michael@0 | 62 | mm.addMessageListener('test:test-fail', onRecvTestFail); |
michael@0 | 63 | |
michael@0 | 64 | mm.loadFrameScript(injectedScript, /* allowDelayedLoad = */ false); |
michael@0 | 65 | |
michael@0 | 66 | waitForMessages(6); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | function waitForMessages(num) { |
michael@0 | 70 | if (numMsgReceived < num) { |
michael@0 | 71 | SimpleTest.executeSoon(function() { waitForMessages(num); }); |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | SimpleTest.finish(); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | addEventListener('testready', runTest); |