dom/broadcastchannel/tests/test_broadcastchannel_mozbrowser2.html
author Ehsan Akhgari <ehsan@mozilla.com>
Mon, 20 Jul 2015 09:47:15 -0400
changeset 287131 ed821611548724bbde98387d7012ab22d7919a77
parent 268766 43c2bea152ce2c53e85a714a5efd415da0e70989
child 287921 4c0e2c42c17128fbe5c4a0af2dc474d4a79cf559
permissions -rw-r--r--
Bug 1185515 - Don't hold the observer service alive from nsDNSService; r=mcmanus

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for BroadcastChannel - iframe mozbrowser</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<div id="container"></div>

<script type="application/javascript">

var gHostedManifestURL = 'http://test/tests/dom/broadcastchannel/tests/server.sjs?testToken=file_mozbrowser2.html';
var gApp;

function cbError() {
  ok(false, "Error callback invoked");
  finish();
}

function createBC() {
  var bc = new BroadcastChannel('foobar');
  bc.onmessage = function(e) {
    is(e.data, "Done!", "A message has been received!");
    nextStep();
  }

  nextStep();
}

function completeOperation() {
  var bc = new BroadcastChannel('foobar');
  bc.postMessage("Done!");
}

function installApp() {
  var request = navigator.mozApps.install(gHostedManifestURL);
  request.onerror = cbError;
  request.onsuccess = function() {
    gApp = request.result;
    nextStep();
  }
}

function uninstallApp() {
  // Uninstall the app.
  var request = navigator.mozApps.mgmt.uninstall(gApp);
  request.onerror = cbError;
  request.onsuccess = function() {
    // All done.
    info("All done");
    nextStep();
  }
}

function testApp() {
  var ifr = document.createElement('iframe');
  ifr.setAttribute('mozbrowser', 'true');
  ifr.setAttribute('mozapp', gApp.manifestURL);
  ifr.setAttribute('src', gApp.manifest.launch_path);
  var domParent = document.getElementById('container');

  // Set us up to listen for messages from the app.
  var listener = function(e) {
    ok(true, "Messaging from app complete");
    ifr.removeEventListener('mozbrowsershowmodalprompt', listener);
    domParent.removeChild(ifr);
    nextStep();
  }

  // This event is triggered when the app calls "alert".
  ifr.addEventListener('mozbrowsershowmodalprompt', listener, false);
  domParent.appendChild(ifr);
}

var steps = [
  // Permissions
  function() {
    SpecialPowers.pushPermissions(
      [{ "type": "browser", "allow": 1, "context": document },
       { "type": "embed-apps", "allow": 1, "context": document },
       { "type": "webapps-manage", "allow": 1, "context": document }], nextStep);
  },

  function() {
    SpecialPowers.pushPrefEnv({"set": [["dom.broadcastChannel.enabled", true],
                                       ["network.disable.ipc.security", true],
                                       ["browser.pagethumbnails.capturing_disabled", true],
                                       ["dom.mozBrowserFramesEnabled", true],
                                       ["dom.ipc.browser_frames.oop_by_default", false],
                                       ["dom.ipc.tabs.disabled", false],
                                       ["dom.testing.ignore_ipc_principal", true],
                                       ["security.mixed_content.block_active_content", false]]},
                              nextStep);
  },

  // No confirmation needed when an app is installed
  function() {
    SpecialPowers.autoConfirmAppInstall(() =>
      SpecialPowers.autoConfirmAppUninstall(nextStep));
  },

  createBC,

  // Installing the app
  installApp,

  // Run tests in app
  testApp,

  // Uninstall the app
  uninstallApp,

  // finish
  completeOperation
];

function finish() {
  SimpleTest.finish();
}

function nextStep() {
  if (!steps.length) {
    SimpleTest.finish();
    return;
  }

  var step = steps.shift();
  step();
}

SimpleTest.waitForExplicitFinish();
nextStep();
</script>
</body>
</html>