dom/datastore/tests/test_oop.html
author Andrea Marchesini <amarchesini@mozilla.com>
Thu, 05 Dec 2013 13:50:11 +0000
changeset 174597 9fb6d18116967ac51c8cc943a63fadf7a7d990de
parent 173908 bc50783a4cf7dd099e7c447adc17f67280fd78c3
child 174623 e805a81f0a1c88dbc61082a155d91b90d277c9d5
permissions -rw-r--r--
Bug 942639 - Make DataStore API Certified-only for 1.3, r=ehsan, r=fabrice

<!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 gHostedManifestURL = 'http://test/tests/dom/datastore/tests/file_app.sjs?testToken=file_basic.html';
  var gApp;

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

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

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

  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) {
      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);
        runTest();
      }
    }

    // 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.promise.enabled", true],
                                         ["dom.datastore.enabled", true],
                                         ["dom.ipc.browser_frames.oop_by_default", true],
                                         ["dom.testing.ignore_ipc_principal", true],
                                         ["dom.testing.datastore_enabled_for_hosted_apps", true]]}, runTest);
    },

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

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

    // Installing the app
    installApp,

    // Run tests in app
    testApp,

    // Uninstall the app
    uninstallApp
  ];

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

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

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

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

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