Bumping manifests a=b2g-bump
[gecko.git] / dom / apps / tests / test_widget.html
blob6b702ae9e397ad3499afaa6696dfa34ae8afc0fb
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Test for DataStore - basic operation on a readonly db</title>
6 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
8 </head>
9 <body>
10 <div id="container"></div>
11 <script type="application/javascript;version=1.7">
13 var gWidgetManifestURL = 'http://test/tests/dom/apps/tests/file_app.sjs?apptype=widget&getmanifest=true';
14 var gInvalidWidgetManifestURL = 'http://test/tests/dom/apps/tests/file_app.sjs?apptype=invalidWidget&getmanifest=true';
15 var gApp;
17 function onError() {
18 ok(false, "Error callback invoked");
19 finish();
22 function installApp(path) {
23 var request = navigator.mozApps.install(path);
24 request.onerror = onError;
25 request.onsuccess = function() {
26 gApp = request.result;
28 runTest();
32 function uninstallApp() {
33 // Uninstall the app.
34 var request = navigator.mozApps.mgmt.uninstall(gApp);
35 request.onerror = onError;
36 request.onsuccess = function() {
37 // All done.
38 info("All done");
40 runTest();
44 function testApp(isValidWidget) {
45 info("Test widget feature. IsValidWidget: " + isValidWidget);
47 var ifr = document.createElement('iframe');
48 ifr.setAttribute('mozbrowser', 'true');
49 ifr.setAttribute('mozwidget', gApp.manifestURL);
50 ifr.setAttribute('src', gApp.origin+gApp.manifest.launch_path);
52 var domParent = document.getElementById('container');
53 domParent.appendChild(ifr);
55 var mm = SpecialPowers.getBrowserFrameMessageManager(ifr);
56 mm.addMessageListener('OK', function(msg) {
57 ok(isValidWidget, "Message from widget: " + SpecialPowers.wrap(msg).json);
58 });
59 mm.addMessageListener('KO', function(msg) {
60 ok(!isValidWidget, "Message from widget: " + SpecialPowers.wrap(msg).json);
61 });
62 mm.addMessageListener('DONE', function(msg) {
63 ok(true, "Message from widget complete: "+SpecialPowers.wrap(msg).json);
64 domParent.removeChild(ifr);
65 runTest();
66 });
68 ifr.addEventListener('mozbrowserloadend', function() {
69 ok(true, "receive mozbrowserloadend");
71 // Test limited browser API feature only for valid widget case
72 if (isValidWidget) {
73 testLimitedBrowserAPI(ifr);
75 SimpleTest.executeSoon(()=>loadFrameScript(mm));
76 }, false);
78 // Test limited browser API feature only for valid widget case
79 if (!isValidWidget) {
80 return;
84 'mozbrowsertitlechange',
85 'mozbrowseropenwindow',
86 'mozbrowserscroll',
87 'mozbrowserasyncscroll'
88 ].forEach( function(topic) {
89 ifr.addEventListener(topic, function() {
90 ok(false, topic + " should be hidden");
91 }, false);
92 });
95 function testLimitedBrowserAPI(ifr) {
96 var securitySensitiveCalls = [
97 'sendMouseEvent',
98 'sendTouchEvent',
99 'goBack',
100 'goForward',
101 'reload',
102 'stop',
103 'download',
104 'purgeHistory',
105 'getScreenshot',
106 'zoom',
107 'getCanGoBack',
108 'getCanGoForward'
110 securitySensitiveCalls.forEach( function(call) {
111 is(typeof ifr[call], "undefined", call + " should be hidden for widget");
115 function loadFrameScript(mm) {
116 var script = 'data:,\
117 function ok(p, msg) { \
118 if (p) { \
119 sendAsyncMessage("OK", msg); \
120 } else { \
121 sendAsyncMessage("KO", msg); \
125 function is(a, b, msg) { \
126 if (a == b) { \
127 sendAsyncMessage("OK", a + " == " + b + " - " + msg); \
128 } else { \
129 sendAsyncMessage("KO", a + " != " + b + " - " + msg); \
133 function finish() { \
134 sendAsyncMessage("DONE",""); \
137 function onError() { \
138 ok(false, "Error callback invoked"); \
139 finish(); \
142 function checkWidget(widget) { \
143 /*For invalid widget case, ignore the following check*/\
144 if (widget) { \
145 var widgetName = "Really Rapid Release (APPTYPETOKEN)"; \
146 is(widget.origin, "http://test", "Widget origin should be correct"); \
147 is(widget.installOrigin, "http://mochi.test:8888", "Install origin should be correct"); \
149 finish(); \
152 var request = content.window.navigator.mozApps.getSelf(); \
153 request.onsuccess = function() { \
154 var widget = request.result; \
155 ok(widget,"Should be a widget"); \
156 checkWidget(widget); \
157 }; \
158 request.onerror = onError; \
159 content.window.open("about:blank"); /*test mozbrowseropenwindow*/ \
160 content.window.scrollTo(4000, 4000); /*test mozbrowser(async)scroll*/ \
162 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
165 var tests = [
166 // Permissions
167 function() {
168 SpecialPowers.pushPermissions(
169 [{ "type": "browser", "allow": 1, "context": document },
170 { "type": "embed-widgets", "allow": 1, "context": document },
171 { "type": "webapps-manage", "allow": 1, "context": document }], runTest);
174 // Preferences
175 function() {
176 SpecialPowers.pushPrefEnv({"set": [["dom.mozBrowserFramesEnabled", true],
177 ["dom.enable_widgets", true],
178 ["dom.datastore.sysMsgOnChangeShortTimeoutSec", 1],
179 ["dom.datastore.sysMsgOnChangeLongTimeoutSec", 3]]}, runTest);
182 function() {
183 if (SpecialPowers.isMainProcess()) {
184 SpecialPowers.Cu.import("resource://gre/modules/DataStoreChangeNotifier.jsm");
187 SpecialPowers.setAllAppsLaunchable(true);
188 runTest();
191 // No confirmation needed when an app is installed
192 function() {
193 SpecialPowers.autoConfirmAppInstall(() => {
194 SpecialPowers.autoConfirmAppUninstall(runTest);
198 // Installing the app
199 ()=>installApp(gWidgetManifestURL),
201 // Run tests in app
202 ()=>testApp(true),
204 // Uninstall the app
205 uninstallApp,
207 // Installing the app for invalid widget case
208 ()=>installApp(gInvalidWidgetManifestURL),
210 // Run tests in app for invalid widget case
211 ()=>testApp(false),
213 // Uninstall the app
214 uninstallApp
217 function runTest() {
218 if (!tests.length) {
219 finish();
220 return;
223 var test = tests.shift();
224 test();
227 function finish() {
228 SimpleTest.finish();
231 SimpleTest.waitForExplicitFinish();
232 runTest();
233 </script>
234 </body>
235 </html>