tenfourfox/dom/apps/tests/test_iac.html
Cameron Kaiser c9b2922b70 hello FPR
2017-04-19 00:56:45 -07:00

238 lines
6.2 KiB
HTML

<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=915880
-->
<head>
<title>Test for IAC API</title>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/chrome-harness.js"></script>
<script type="application/javascript"
src="http://test/chrome/dom/activities/tests/mochi/common.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id={915880}">Mozilla Bug {915880}</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
SimpleTest.waitForExplicitFinish();
const gURL = "http://test/chrome/dom/apps/tests/iac/";
const IAC_UI_GLUE_CID =
Components.ID("{384afeee-f1d2-4819-9d2e-9b62f6b0e382}");
function registerComponent(aObject, aDescription, aContract, aCid) {
info("Registering " + aCid);
var componentManager =
Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
componentManager.registerFactory(aCid, aDescription, aContract, aObject);
// Keep the id on the object so we can unregister later.
aObject.cid = aCid;
}
function unregisterComponent(aObject) {
info("Unregistering " + aObject.cid);
var componentManager =
Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
componentManager.unregisterFactory(aObject.cid, aObject);
}
let InterAppCommUIGlue = {
// nsISupports implementation.
QueryInterface: function(iid) {
if (iid.equals(Ci.nsISupports) ||
iid.equals(Ci.nsIFactory) ||
iid.equals(Ci.nsIInterAppCommUIGlue)) {
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
},
// nsIFactory implementation.
createInstance: function(outer, iid) {
return this.QueryInterface(iid);
},
// nsIInterAppCommUIGlue implementation
selectApps(aCallerID, aPubAppManifestURL, aKeyword, aAppsToSelect) {
return Promise.resolve({
callerID: aCallerID,
keyword: aKeyword,
manifestURL: aPubAppManifestURL,
selectedApps: aAppsToSelect
});
}
};
registerComponent(InterAppCommUIGlue,
"InterAppComm UI Glue",
"@mozilla.org/dom/apps/inter-app-comm-ui-glue;1",
IAC_UI_GLUE_CID);
function finish() {
unregisterComponent(InterAppCommUIGlue);
SimpleTest.finish();
}
function cbError(aEvent) {
ok(false, "Error callback invoked " +
aEvent.target.error.name + " " + aEvent.target.error.message);
finish();
}
let subscriber = null;
let publisher = null;
function installApp(path) {
return new Promise((resolve, reject) => {
let request = navigator.mozApps.installPackage(gURL + path);
request.onerror = () => {
ok(false, request.error.name);
reject();
};
request.onsuccess = () => {
let app = request.result;
ok(app, "App is not null");
if (app.installState == "installed") {
return resolve(app);
}
app.ondownloadapplied = () => {
resolve(app);
};
app.ondownloaderror = () => {
ok(false, "Unexpected download error");
reject();
};
};
});
}
function launchApp(app) {
if (!app) {
ok(false, "No app to launch");
return Promise.reject();
}
return new Promise((resolve, reject) => {
let iframe = document.createElement("iframe");
iframe.setAttribute("mozbrowser", "true");
iframe.setAttribute("mozapp", app.manifestURL);
iframe.addEventListener("mozbrowsershowmodalprompt", e => {
let 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, "Message from app: " + message);
resolve();
} else {
ok(false, "Unexpected message received: " + message);
}
}, false);
let domParent = document.getElementById("container");
domParent.appendChild(iframe);
SpecialPowers.wrap(iframe.contentWindow).location =
app.origin + app.manifest.launch_path;
});
}
const tests = [() => {
info("Test start");
SpecialPowers.setAllAppsLaunchable(true);
SpecialPowers.autoConfirmAppInstall(() => {
SpecialPowers.autoConfirmAppUninstall(next);
});
}, () => {
info("Installing subscriber app");
installApp("subscriber/update.webapp").then(app => {
subscriber = app;
next();
}).catch(() => {
ok(false, "Unable to install app");
finish();
});
}, () => {
info("Launching " + subscriber.manifest.name);
launchApp(subscriber).then(next);
}, () => {
info("Installing publisher app");
installApp("publisher/update.webapp").then(app => {
publisher = app;
next();
}).catch(() => {
ok(false, "Unable to install app");
finish();
});
}, () => {
info("Launching " + publisher.manifest.name);
launchApp(publisher).then(next);
}, () => {
navigator.mozApps.mgmt.onuninstall = event => {
let app = event.application;
next();
};
let request = navigator.mozApps.mgmt.uninstall(subscriber);
request.onerror = cbError;
}, () => {
navigator.mozApps.mgmt.onuninstall = event => {
let app = event.application;
next();
};
let request = navigator.mozApps.mgmt.uninstall(publisher);
request.onerror = cbError;
}];
const next = () => {
let step = tests.shift();
if (!step) {
return finish();
}
try {
step();
} catch(e) {
ok(false, "Test threw: " + e);
}
}
SpecialPowers.pushPermissions([{
"type": "webapps-manage",
"allow": 1,
"context": document
}, {
"type": "browser",
"allow": 1,
"context": document
}, {
"type": "embed-apps",
"allow": 1,
"context": document
}], () => {
// IAC is only allowed for certified apps. We use dev mode to
// skip the security checks.
SpecialPowers.pushPrefEnv({
"set": [
["dom.mozApps.debug", true],
["dom.apps.developer_mode", true],
["dom.mozBrowserFramesEnabled", true],
["dom.sysmsg.enabled", true]
]
}, next);
});
</script>
<div id="container"></div>
</pre>
</body>
</html>