summaryrefslogtreecommitdiffstats
path: root/dom/xbl/test/file_bug946815.xhtml
blob: ca8718b7ba6e2e17e09e9497ca5dad8a894e3e45 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=946815
-->
<head>
  <bindings xmlns="http://www.mozilla.org/xbl">
    <binding id="testBinding">
      <implementation>
        <constructor>
          // Grab some basic infrastructure off the content window.
          var win = XPCNativeWrapper.unwrap(window);
          SpecialPowers = win.SpecialPowers;
          Cu = SpecialPowers.Cu;
          is = win.is;
          ok = win.ok;
          SimpleTest = win.SimpleTest;

          var bound = document.getElementById('bound');

          // This gets invoked by an event handler.
          window.finish = function() {
            // XBL scope, with 'wifi-manage' scope
            testWifiPermissionFromXbl(true, true /* with wifi-manage permission */);
            SimpleTest.finish();
          }

          eval('var testWifiPermissionFromXbl = ' + win.testWifiPermissionFromContent.toSource());

          // XBL scope, with no 'wifi-manage' permission
          testWifiPermissionFromXbl(true, false /* without wifi-manage permission */);

          // Hand things off to content. Content will call us back.
          win.go();
        </constructor>
      </implementation>
      <handlers>
        <handler event="testevent" action="ok(true, 'called event handler'); finish();" allowuntrusted="true"/>
      </handlers>
    </binding>
  </bindings>
  <script type="application/javascript">
  <![CDATA[

  ok = parent.ok;
  is = parent.is;
  SimpleTest = parent.SimpleTest;
  SpecialPowers = parent.SpecialPowers;

  function go() {
    "use strict";

    // Content scope, with no 'wifi-manage' permission
    testWifiPermissionFromContent(false, false /* without wifi-manage permission */);

    SpecialPowers.pushPermissions([{ "type": "wifi-manage", "allow": 1, "context": window.document }], function() {
      testWifiPermissionFromContent(false, true /* with wifi-manage permission */);
      // Hand control back to the XBL scope by dispatching an event on the bound element.
      bound.dispatchEvent(new CustomEvent('testevent'));
    });
  }

  function testWifiPermissionFromContent(aIsXblScope, aExpectedWifiPermission) {
    // Even though the function name has suggested we are either in content or
    // XBL scope, the argument |aIsXblScope| is still required to print
    // descriptive enough message for the test.

    // If this test fails, something must be wrong with the permission manipulation.
    // Check test_bug946815.html to see if we removed wifi-manage permission in
    // the beginning and if we forgot to add permission back during testing.
    is(aExpectedWifiPermission, SpecialPowers.hasPermission("wifi-manage", window.document),
       "'wifi-manage' permission is not as expected! Expected: " + aExpectedWifiPermission);

    is(typeof window.MozWifiP2pManager, (aExpectedWifiPermission ? "function" : "undefined"),
       (aIsXblScope ? "XBL" : "Content") + " should" + (aExpectedWifiPermission ? "" : " NOT") +
       " see MozWifiP2pManager with" + (aExpectedWifiPermission ? "" : "out") +
       " 'wifi-manage' permission." );
  }

  function setup() {
    // When the bindings are applied, the constructor will be invoked and the
    // test will continue.
    document.getElementById('bound').style.MozBinding = 'url(#testBinding)';
  }

  ]]>
</script>
</head>
<body onload="setup()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=946815">Mozilla Bug 946815</a>
<p id="display"></p>
<div id="content">
  <div id="bound">Bound element</div>
</div>
<pre id="test">
</pre>
</body>
</html>