summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_TopBarrier.js
blob: 186622b33865a98baa909f6f917691b36be8cbf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that an <iframe mozbrowser> is a window.{top,parent,frameElement} barrier.
"use strict";

SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();
browserElementTestHelpers.allowTopLevelDataURINavigation();

var iframe;
function runTest() {
  iframe = document.createElement('iframe');
  iframe.addEventListener('mozbrowserloadend', function() {
    try {
      outerIframeLoaded();
    } catch(e) {
      dump("Got error: " + e + '\n');
    }
  });
  iframe.setAttribute('mozbrowser', 'true');
  iframe.src = 'data:text/html,Outer iframe <iframe id="inner-iframe"></iframe>';
  // For kicks, this test uses a display:none iframe.  This shouldn't make a
  // difference in anything.
  iframe.style.display = 'none';
  document.body.appendChild(iframe);
}

var numMsgReceived = 0;
function outerIframeLoaded() {
  // If you're changing the amount of is() calls in injectedScript,
  // also change the number in waitForMessages accordingly
  var injectedScript =
    "data:,function is(a, b, desc) {                                     \
      if (a == b) {                                                      \
        sendAsyncMessage('test:test-pass', desc);                        \
      } else {                                                           \
        sendAsyncMessage('test:test-fail', desc + ' ' + a + ' != ' + b); \
      }                                                                  \
    }                                                                    \
    is(content.window.top, content.window, 'top');                       \
    is(content.window.content, content.window, 'content');               \
    is(content.window.parent, content.window, 'parent');                 \
    is(content.window.frameElement, null, 'frameElement');               \
    var innerIframe = content.document.getElementById('inner-iframe');   \
    var innerWindow = innerIframe.contentWindow;                         \
    is(innerWindow.top, content.window, 'inner top');                    \
    is(innerWindow.content, content.window, 'inner content');            \
    is(innerWindow.parent, content.window, 'inner parent');              \
    is(innerWindow.frameElement, innerIframe, 'inner frameElement');"

  var mm = SpecialPowers.getBrowserFrameMessageManager(iframe);

  function onRecvTestPass(msg) {
    numMsgReceived++;
    ok(true, msg.json);
  }
  mm.addMessageListener('test:test-pass', onRecvTestPass);

  function onRecvTestFail(msg) {
    numMsgReceived++;
    ok(false, msg.json);
  }
  mm.addMessageListener('test:test-fail', onRecvTestFail);

  mm.loadFrameScript(injectedScript, /* allowDelayedLoad = */ false);

  // 8 is the number of is() calls in injectedScript
  waitForMessages(8);
}

function waitForMessages(num) {
  if (numMsgReceived < num) {
    SimpleTest.executeSoon(function() { waitForMessages(num); });
    return;
  }

  SimpleTest.finish();
}

addEventListener('testready', runTest);