dom/base/test/chrome/test_permission_isHandlingUserInput.xul
author Kris Maglione <maglione.k@gmail.com>
Mon, 29 Jan 2018 15:20:18 -0800
changeset 456545 e6a7b5e11ba856ee3535f76c6bcca17ea29e3d5f
parent 456423 c6e0fe339cb1e7c0c029e68be5332e52f53ab6b7
child 457640 b38d59f71915f78922b46a7c7bc65a48488c45f1
permissions -rw-r--r--
Bug 1431533: Part 5a - Auto-rewrite code to use ChromeUtils import methods. r=florian This was done using the following script: https://bitbucket.org/kmaglione/m-c-rewrites/src/37e3803c7a23385bab5ea077ba8692bcfe1608fc/processors/chromeutils-import.jsm MozReview-Commit-ID: 1Nc3XDu0wGl

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<!--
  Tests that the isHandlingUserInput attribute on permission requests is set correctly.
-->
<window title="isHandlingUserInput test" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

  <body xmlns="http://www.w3.org/1999/xhtml">
    <iframe id="frame" src="https://example.com/chrome/dom/base/test/chrome/dummy.html" />
  </body>

  <script type="application/javascript">
  <![CDATA[
  let Cu = Components.utils;

  ChromeUtils.import("resource://gre/modules/Integration.jsm");
  ChromeUtils.import("resource://gre/modules/E10SUtils.jsm");

  SimpleTest.waitForExplicitFinish();

  let frame = document.getElementById("frame");

  function checkPermissionRequest(permission, isHandlingUserInput) {
    return new Promise(function(resolve) {
      let TestIntegration = (base) => ({
        __proto__: base,
        createPermissionPrompt(type, request) {
          is(type, permission, `Has correct permission type ${permission}.`);
          is(request.isHandlingUserInput, isHandlingUserInput,
             "The isHandlingUserInput attribute is set correctly.");
          Integration.contentPermission.unregister(TestIntegration);
          resolve();
          return { prompt() {} };
        },
      });
      Integration.contentPermission.register(TestIntegration);
    });
  }

  async function runTest() {
    // Test programmatic request for persistent storage.
    let request = checkPermissionRequest("persistent-storage", false);
    navigator.storage.persist();
    await request;

    // Test user-initiated request for persistent storage.
    request = checkPermissionRequest("persistent-storage", true);
    E10SUtils.wrapHandlingUserInput(content, true, function() {
      navigator.storage.persist();
    });
    await request;

    // Test programmatic request for geolocation.
    request = checkPermissionRequest("geolocation", false);
    navigator.geolocation.getCurrentPosition(() => {});
    await request;

    // Test user-initiated request for geolocation.
    request = checkPermissionRequest("geolocation", true);
    E10SUtils.wrapHandlingUserInput(content, true, function() {
      navigator.geolocation.getCurrentPosition(() => {});
    });
    await request;

    // Notifications need to be tested in an HTTPS frame, because
    // chrome:// URLs are whitelisted.
    let frameWin = frame.contentWindow;

    // Test programmatic request for notifications.
    request = checkPermissionRequest("desktop-notification", false);
    frameWin.Notification.requestPermission();
    await request;

    // Test user-initiated request for notifications.
    request = checkPermissionRequest("desktop-notification", true);
    E10SUtils.wrapHandlingUserInput(content, true, function() {
      frameWin.Notification.requestPermission();
    });
    await request;
  }

  frame.addEventListener("load", function() {
    runTest().then(() => SimpleTest.finish());
  });
  ]]>
  </script>
</window>