summaryrefslogtreecommitdiffstats
path: root/dom/base/test/mozbrowser_api_utils.js
blob: 92b080939024c0442f36ccc3b0776e44c33627a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const FRAME_URL = "http://example.org/";

const METHODS = {
  setVisible: {},
  getVisible: {},
  setActive: {},
  getActive: {},
  addNextPaintListener: {},
  removeNextPaintListener: {},
  sendMouseEvent: {},
  sendTouchEvent: {},
  goBack: {},
  goForward: {},
  reload: {},
  stop: {},
  download: {},
  purgeHistory: {},
  getScreenshot: {},
  zoom: {},
  getCanGoBack: {},
  getCanGoForward: {},
  getContentDimensions: {},
  setInputMethodActive: {},
  findAll: {},
  findNext: {},
  clearMatch: {},
  executeScript: {},
  getWebManifest: {},
  mute: {},
  unmute: {},
  getMuted: {},
  setVolume: {},
  getVolume: {},
};

const ATTRIBUTES = [
  "allowedAudioChannels",
];

function once(target, eventName, useCapture = false) {
  info("Waiting for event: '" + JSON.stringify(eventName) + "' on " + target + ".");

  return new Promise(resolve => {
    for (let [add, remove] of [
      ["addEventListener", "removeEventListener"],
      ["addMessageListener", "removeMessageListener"],
    ]) {
      if ((add in target) && (remove in target)) {
        eventName.forEach(evName => {
          target[add](evName, function onEvent(...aArgs) {
            info("Got event: '" + evName + "' on " + target + ".");
            target[remove](evName, onEvent, useCapture);
            resolve(aArgs);
          }, useCapture);
	});
        break;
      }
    }
  });
}

function* loadFrame(attributes = {}) {
  let iframe = document.createElement("iframe");
  iframe.setAttribute("src", FRAME_URL);
  for (let key in attributes) {
    iframe.setAttribute(key, attributes[key]);
  }
  let loaded = once(iframe, [ "load", "mozbrowserloadend" ]);
  document.body.appendChild(iframe);
  yield loaded;
  return iframe;
}