dom/datastore/tests/test_oop_events.html
author Sean Lin <selin@mozilla.com>
Fri, 27 Jun 2014 15:58:47 +0800
changeset 200262 721eb8e94100c8947b6c00e22ea69b4462d6050b
parent 199043 ddcf4f4af7d932e67127711b37040a485151d815
child 200273 087e8fbc1cf9e26fd414197fe3af7bf64c311324
permissions -rw-r--r--
Bug 1014023 - [Datastore] Notify apps of changes in datastore without being opened. r=baku, r=gene

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for DataStore - basic operation on a readonly db</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;version=1.7">

  var gHostedManifestURL1 = 'http://test/tests/dom/datastore/tests/file_app.sjs?template=file_app2.template.webapp&testToken=file_event_receiver.html';
  var gHostedManifestURL2 = 'http://example.com/tests/dom/datastore/tests/file_app.sjs?testToken=file_event_maker.html';
  var gApps = [];

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

  function installApp(manifest) {
    var request = navigator.mozApps.install(manifest);
    request.onerror = cbError;
    request.onsuccess = function() {
      gApps.push(request.result);
      runTest();
    }
  }

  function uninstallApp(app) {
    // Uninstall the app
    var request = navigator.mozApps.mgmt.uninstall(app);
    request.onerror = cbError;
    request.onsuccess = function() {
      runTest();
    }
  }

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

    // Set us up to listen for messages from the app.
    var listener = function(e) {
      var message = e.detail.message;
      if (/^OK/.exec(message)) {
        ok(true, "Message from app: " + message);
      } else if (/KO/.exec(message)) {
        ok(false, "Message from app: " + message);
      } else if (/READY/.exec(message)) {
        ok(true, "App ready");
        testApp2();
      } else if (/DONE/.exec(message)) {
        ok(true, "Messaging from app complete");
        ifr.removeEventListener('mozbrowsershowmodalprompt', listener);
        domParent.removeChild(ifr);
        runTest();
      }
    }

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

  function testApp2() {
    var ifr = document.createElement('iframe');
    ifr.setAttribute('mozbrowser', 'true');
    ifr.setAttribute('mozapp', gApps[1].manifestURL);
    ifr.setAttribute('src', gApps[1].manifest.launch_path);
    var domParent = document.getElementById('container');

    // Set us up to listen for messages from the app.
    var listener = function(e) {
      var message = e.detail.message;
      if (/^OK/.exec(message)) {
        ok(true, "Message from app: " + message);
      } else if (/KO/.exec(message)) {
        ok(false, "Message from app: " + message);
      } else if (/DONE/.exec(message)) {
        ok(true, "Messaging from app complete");
        ifr.removeEventListener('mozbrowsershowmodalprompt', listener);
        domParent.removeChild(ifr);
      }
    }

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

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

    // Preferences
    function() {
      SpecialPowers.pushPrefEnv({"set": [["dom.datastore.enabled", true],
                                         ["dom.datastore.sysMsgOnChangeShortTimeoutSec", 1],
                                         ["dom.datastore.sysMsgOnChangeLongTimeoutSec", 3],
                                         ["dom.ipc.browser_frames.oop_by_default", true],
                                         ["dom.testing.ignore_ipc_principal", true],
                                         ["dom.testing.datastore_enabled_for_hosted_apps", true]]}, runTest);
    },

    function() {
      if (SpecialPowers.isMainProcess()) {
        SpecialPowers.Cu.import("resource://gre/modules/DataStoreChangeNotifier.jsm");
      }

      SpecialPowers.setAllAppsLaunchable(true);
      SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
      runTest();
    },

    // No confirmation needed when an app is installed
    function() {
      SpecialPowers.autoConfirmAppInstall(runTest);
    },

    // Installing the apps
    function() { installApp(gHostedManifestURL1); },
    function() { installApp(gHostedManifestURL2); },

    // Run tests in apps
    testApp,

    // Uninstall the apps
    function() { uninstallApp(gApps[0]); },
    function() { uninstallApp(gApps[1]); },
  ];

  function runTest() {
    if (!tests.length) {
      finish();
      return;
    }

    var test = tests.shift();
    test();
  }

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

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