Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | const Cu = Components.utils; |
michael@0 | 2 | |
michael@0 | 3 | const { Services } = Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 4 | |
michael@0 | 5 | // Load a duplicated copy of the jsm to prevent messing with the currently running one |
michael@0 | 6 | let scope = {}; |
michael@0 | 7 | Services.scriptloader.loadSubScript("resource://gre/modules/SystemAppProxy.jsm", scope); |
michael@0 | 8 | const { SystemAppProxy } = scope; |
michael@0 | 9 | |
michael@0 | 10 | let frame; |
michael@0 | 11 | |
michael@0 | 12 | let index = -1; |
michael@0 | 13 | function next() { |
michael@0 | 14 | index++; |
michael@0 | 15 | if (index >= steps.length) { |
michael@0 | 16 | assert.ok(false, "Shouldn't get here!"); |
michael@0 | 17 | return; |
michael@0 | 18 | } |
michael@0 | 19 | try { |
michael@0 | 20 | steps[index](); |
michael@0 | 21 | } catch(ex) { |
michael@0 | 22 | assert.ok(false, "Caught exception: " + ex); |
michael@0 | 23 | } |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | // Listen for events received by the system app document |
michael@0 | 27 | // to ensure that we receive all of them, in an expected order and time |
michael@0 | 28 | let isLoaded = false; |
michael@0 | 29 | let n = 0; |
michael@0 | 30 | function listener(event) { |
michael@0 | 31 | if (!isLoaded) { |
michael@0 | 32 | assert.ok(false, "Received event before the iframe is ready"); |
michael@0 | 33 | return; |
michael@0 | 34 | } |
michael@0 | 35 | n++; |
michael@0 | 36 | if (n == 1) { |
michael@0 | 37 | assert.equal(event.type, "mozChromeEvent"); |
michael@0 | 38 | assert.equal(event.detail.name, "first"); |
michael@0 | 39 | } else if (n == 2) { |
michael@0 | 40 | assert.equal(event.type, "custom"); |
michael@0 | 41 | assert.equal(event.detail.name, "second"); |
michael@0 | 42 | |
michael@0 | 43 | next(); // call checkEventDispatching |
michael@0 | 44 | } else if (n == 3) { |
michael@0 | 45 | assert.equal(event.type, "custom"); |
michael@0 | 46 | assert.equal(event.detail.name, "third"); |
michael@0 | 47 | } else if (n == 4) { |
michael@0 | 48 | assert.equal(event.type, "mozChromeEvent"); |
michael@0 | 49 | assert.equal(event.detail.name, "fourth"); |
michael@0 | 50 | |
michael@0 | 51 | next(); // call checkEventListening(); |
michael@0 | 52 | } else { |
michael@0 | 53 | assert.ok(false, "Unexpected event of type " + event.type); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | let steps = [ |
michael@0 | 59 | function waitForWebapps() { |
michael@0 | 60 | // We are using webapps API later in this test and we need to ensure |
michael@0 | 61 | // it is fully initialized before trying to use it |
michael@0 | 62 | let { DOMApplicationRegistry } = Cu.import('resource://gre/modules/Webapps.jsm', {}); |
michael@0 | 63 | DOMApplicationRegistry.registryReady.then(function () { |
michael@0 | 64 | next(); |
michael@0 | 65 | }); |
michael@0 | 66 | }, |
michael@0 | 67 | |
michael@0 | 68 | function earlyEvents() { |
michael@0 | 69 | // Immediately try to send events |
michael@0 | 70 | SystemAppProxy.dispatchEvent({ name: "first" }); |
michael@0 | 71 | SystemAppProxy._sendCustomEvent("custom", { name: "second" }); |
michael@0 | 72 | next(); |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | function createFrame() { |
michael@0 | 76 | // Create a fake system app frame |
michael@0 | 77 | let win = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 78 | let doc = win.document; |
michael@0 | 79 | frame = doc.createElement("iframe"); |
michael@0 | 80 | doc.documentElement.appendChild(frame); |
michael@0 | 81 | |
michael@0 | 82 | // Ensure that events are correctly sent to the frame. |
michael@0 | 83 | // `listener` is going to call next() |
michael@0 | 84 | frame.contentWindow.addEventListener("mozChromeEvent", listener); |
michael@0 | 85 | frame.contentWindow.addEventListener("custom", listener); |
michael@0 | 86 | |
michael@0 | 87 | // Ensure that listener being registered before the system app is ready |
michael@0 | 88 | // are correctly removed from the pending list |
michael@0 | 89 | function removedListener() { |
michael@0 | 90 | assert(false, "Listener isn't correctly removed from the pending list"); |
michael@0 | 91 | } |
michael@0 | 92 | SystemAppProxy.addEventListener("mozChromeEvent", removedListener); |
michael@0 | 93 | SystemAppProxy.removeEventListener("mozChromeEvent", removedListener); |
michael@0 | 94 | |
michael@0 | 95 | // Register it to the JSM |
michael@0 | 96 | SystemAppProxy.registerFrame(frame); |
michael@0 | 97 | assert.ok(true, "Frame created and registered"); |
michael@0 | 98 | |
michael@0 | 99 | frame.contentWindow.addEventListener("load", function onload() { |
michael@0 | 100 | frame.contentWindow.removeEventListener("load", onload); |
michael@0 | 101 | assert.ok(true, "Frame document loaded"); |
michael@0 | 102 | |
michael@0 | 103 | // Declare that the iframe is now loaded. |
michael@0 | 104 | // That should dispatch early events |
michael@0 | 105 | isLoaded = true; |
michael@0 | 106 | SystemAppProxy.setIsReady(); |
michael@0 | 107 | assert.ok(true, "Frame declared as loaded"); |
michael@0 | 108 | |
michael@0 | 109 | // Once pending events are received, |
michael@0 | 110 | // we will run checkEventDispatching from `listener` function |
michael@0 | 111 | }); |
michael@0 | 112 | |
michael@0 | 113 | frame.setAttribute("src", "data:text/html,system app"); |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | function checkEventDispatching() { |
michael@0 | 117 | // Send events after the iframe is ready, |
michael@0 | 118 | // they should be dispatched right away |
michael@0 | 119 | SystemAppProxy._sendCustomEvent("custom", { name: "third" }); |
michael@0 | 120 | SystemAppProxy.dispatchEvent({ name: "fourth" }); |
michael@0 | 121 | // Once this 4th event is received, we will run checkEventListening |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | function checkEventListening() { |
michael@0 | 125 | SystemAppProxy.addEventListener("mozContentEvent", function onContentEvent(event) { |
michael@0 | 126 | assert.equal(event.detail.name, "first-content", "received a system app event"); |
michael@0 | 127 | SystemAppProxy.removeEventListener("mozContentEvent", onContentEvent); |
michael@0 | 128 | |
michael@0 | 129 | next(); |
michael@0 | 130 | }); |
michael@0 | 131 | let win = frame.contentWindow; |
michael@0 | 132 | win.dispatchEvent(new win.CustomEvent("mozContentEvent", { detail: {name: "first-content"} })); |
michael@0 | 133 | }, |
michael@0 | 134 | |
michael@0 | 135 | function endOfTest() { |
michael@0 | 136 | frame.remove(); |
michael@0 | 137 | sendAsyncMessage("finish"); |
michael@0 | 138 | } |
michael@0 | 139 | ]; |
michael@0 | 140 | |
michael@0 | 141 | next(); |