summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/context-menu/util.js
blob: af9de938d7f4e9537384ec5d7b638fbaa0106c74 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { Cc, Ci } = require("chrome");
const { getMostRecentBrowserWindow } = require("sdk/window/utils");
const { open: openWindow, close: closeWindow } = require("sdk/window/helpers");
const tabUtils = require("sdk/tabs/utils");
const { map, filter, object, reduce, keys, symbols,
        pairs, values, each, some, isEvery, count } = require("sdk/util/sequence");
const { when } = require("sdk/dom/events");

const { Task } = require("resource://gre/modules/Task.jsm");

var observerService = Cc["@mozilla.org/observer-service;1"]
                      .getService(Ci.nsIObserverService);

const framescriptURI = require.resolve("./framescript");

const _target = ({target}) => target;

exports.openWindow = openWindow;
exports.closeWindow = closeWindow;

const getActiveTab = (window=getMostRecentBrowserWindow()) =>
  tabUtils.getActiveTab(window)

const openTab = (url, window=getMostRecentBrowserWindow()) => {
  const tab = tabUtils.openTab(window, url);
  const browser = tabUtils.getBrowserForTab(tab);
  browser.messageManager.loadFrameScript(framescriptURI, false);

  return when(browser, "load", true).then(_ => tab);
};
exports.openTab = openTab;

const openContextMenu = (selector, tab=getActiveTab()) => {
  const browser = tabUtils.getBrowserForTab(tab);
  browser.
    messageManager.
    sendAsyncMessage("sdk/test/context-menu/open",
                     {target: selector});

  return when(tab.ownerDocument.defaultView, "popupshown").
          then(_target);
};
exports.openContextMenu = openContextMenu;

const closeContextMenu = (menu) => {
  const result = when(menu.ownerDocument.defaultView, "popuphidden").
                  then(_target);

  menu.hidePopup();
  return result;
};
exports.closeContextMenu = closeContextMenu;

const closeTab = (tab) => {
  const result = when(tab, "TabClose").then(_ => tab);
  tabUtils.closeTab(tab);

  return result;
};
exports.closeTab = closeTab;

const select = (target, tab=getActiveTab()) =>
  new Promise(resolve => {
    const {messageManager} = tabUtils.getBrowserForTab(tab);
    messageManager.
      sendAsyncMessage("sdk/test/context-menu/select",
                       target);

    messageManager.addMessageListener("sdk/test/context-menu/selected", {
      receiveMessage({name}) {
        messageManager.removeMessageListener(name, this);
        resolve();
      }
    });
  });
exports.select = select;

const attributeBlocklist = new Set(["data-component-path"]);
const attributeRenameTable = Object.assign(Object.create(null), {
  class: "className"
});
const readAttributes = node =>
  object(...map(({name, value}) => [attributeRenameTable[name] || name, value],
                filter(({name}) => !attributeBlocklist.has(name),
                       node.attributes)));
exports.readAttributes = readAttributes;

const readNode = node =>
  Object.assign(readAttributes(node),
                {tagName: node.tagName, namespaceURI: node.namespaceURI},
                node.children.length ?
                  {children: [...map(readNode, node.children)]} :
                  {});
exports.readNode = readNode;

const captureContextMenu = (target=":root", options={}) => Task.spawn(function*() {
  const window = options.window || getMostRecentBrowserWindow();
  const tab = options.tab || tabUtils.getActiveTab(window);

  const menu = yield openContextMenu(target, tab);
  const tree = readNode(menu.querySelector(".sdk-context-menu-extension"));
  yield closeContextMenu(menu);
  return tree;
});
exports.captureContextMenu = captureContextMenu;

const withTab = (test, uri="about:blank") => function*(assert) {
  const tab = yield openTab(uri);
  try {
    yield* test(assert, tab);
  }
  finally {
    yield closeTab(tab);
  }
};
exports.withTab = withTab;

const withWindow = () => function*(assert) {
  const window = yield openWindow();
  try {
    yield* test(assert, window);
  }
  finally {
    yield closeWindow(window);
  }
};
exports.withWindow = withWindow;

const withItems = (items, body) => function*() {
  try {
    yield* body(items);
  } finally {
      Object.keys(items).forEach(key => items[key].destroy());
  }
}();
exports.withItems = withItems;