summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-events.js
blob: 4d4eae34700ffcfe7530ddaef45704be14fd440c (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* 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 { Loader } = require("sdk/test/loader");
const utils = require("sdk/tabs/utils");
const { open, close } = require("sdk/window/helpers");
const { getMostRecentBrowserWindow } = require("sdk/window/utils");
const { events } = require("sdk/tab/events");
const { on, off } = require("sdk/event/core");
const { resolve, defer } = require("sdk/core/promise");

var isFennec = require("sdk/system/xul-app").is("Fennec");

function test(options) {
  return function(assert, done) {
    let tabEvents = [];
    let tabs = [];
    let { promise, resolve: resolveP } = defer();
    let win = isFennec ? resolve(getMostRecentBrowserWindow()) :
      open(null, {
        features: { private: true, toolbar:true, chrome: true }
      });
    let window = null;

    // Firefox events are fired sync; Fennec events async
    // this normalizes the tests
    function handler (event) {
      tabEvents.push(event);
      runIfReady();
    }

    function runIfReady () {
      let releventEvents = getRelatedEvents(tabEvents, tabs);
      if (options.readyWhen(releventEvents))
        options.end({
          tabs: tabs,
          events: releventEvents,
          assert: assert,
          done: resolveP
        });
    }

    win.then(function(w) {
      window = w;
      on(events, "data", handler);
      options.start({ tabs: tabs, window: window });

      // Execute here for synchronous FF events, as the handlers
      // were called before tabs were pushed to `tabs`
      runIfReady(); 
      return promise;
    }).then(function() {
      off(events, "data", handler);
      return isFennec ? null : close(window);
    }).then(done, assert.fail);
  };
}

// Just making sure that tab events work for already opened tabs not only
// for new windows.
exports["test current window"] = test({
  readyWhen: events => events.length === 3,
  start: ({ tabs, window }) => {
    let tab = utils.openTab(window, 'data:text/plain,open');
    tabs.push(tab);
    utils.closeTab(tab);
  },
  end: ({ tabs, events, assert, done }) => {
    let [open, select, close] = events;
    let tab = tabs[0];

    assert.equal(open.type, "TabOpen");
    assert.equal(open.target, tab);

    assert.equal(select.type, "TabSelect");
    assert.equal(select.target, tab);

    assert.equal(close.type, "TabClose");
    assert.equal(close.target, tab);
    done();
  }
});

exports["test open"] = test({
  readyWhen: events => events.length === 2,
  start: ({ tabs, window }) => {
    tabs.push(utils.openTab(window, 'data:text/plain,open'));
  },
  end: ({ tabs, events, assert, done }) => {
    let [open, select] = events;
    let tab = tabs[0];

    assert.equal(open.type, "TabOpen");
    assert.equal(open.target, tab);

    assert.equal(select.type, "TabSelect");
    assert.equal(select.target, tab);
    done();
  }
});

exports["test open -> close"] = test({
  readyWhen: events => events.length === 3,
  start: ({ tabs, window }) => {
    // First tab is useless we just open it so that closing second tab won't
    // close window on some platforms.
    utils.openTab(window, 'data:text/plain,ignore');
    let tab = utils.openTab(window, 'data:text/plain,open-close');
    tabs.push(tab);
    utils.closeTab(tab);
  },
  end: ({ tabs, events, assert, done }) => {
    let [open, select, close] = events;
    let tab = tabs[0];

    assert.equal(open.type, "TabOpen");
    assert.equal(open.target, tab);

    assert.equal(select.type, "TabSelect");
    assert.equal(select.target, tab);

    assert.equal(close.type, "TabClose");
    assert.equal(close.target, tab);
    done();
  }
});

exports["test open -> open -> select"] = test({
  readyWhen: events => events.length === 5,
  start: ({tabs, window}) => {
    tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
    tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
    utils.activateTab(tabs[0], window);
  },
  end: ({ tabs, events, assert, done }) => {
    let [ tab1, tab2 ] = tabs;
    let tab1Events = 0;
    getRelatedEvents(events, tab1).map(event => {
      tab1Events++;
      if (tab1Events === 1)
        assert.equal(event.type, "TabOpen", "first tab opened");
      else
        assert.equal(event.type, "TabSelect", "first tab selected");
      assert.equal(event.target, tab1);
    });
    assert.equal(tab1Events, 3, "first tab has 3 events");

    let tab2Opened;
    getRelatedEvents(events, tab2).map(event => {
      if (!tab2Opened)
        assert.equal(event.type, "TabOpen", "second tab opened");
      else
        assert.equal(event.type, "TabSelect", "second tab selected");
      tab2Opened = true;
      assert.equal(event.target, tab2);
    });
    done();
  }
});

exports["test open -> pin -> unpin"] = test({
  readyWhen: events => events.length === (isFennec ? 2 : 5),
  start: ({ tabs, window }) => {
    tabs.push(utils.openTab(window, 'data:text/plain,pin-unpin'));
    utils.pin(tabs[0]);
    utils.unpin(tabs[0]);
  },
  end: ({ tabs, events, assert, done }) => {
    let [open, select, move, pin, unpin] = events;
    let tab = tabs[0];

    assert.equal(open.type, "TabOpen");
    assert.equal(open.target, tab);

    assert.equal(select.type, "TabSelect");
    assert.equal(select.target, tab);

    if (isFennec) {
      assert.pass("Tab pin / unpin is not supported by Fennec");
    }
    else {
      assert.equal(move.type, "TabMove");
      assert.equal(move.target, tab);

      assert.equal(pin.type, "TabPinned");
      assert.equal(pin.target, tab);

      assert.equal(unpin.type, "TabUnpinned");
      assert.equal(unpin.target, tab);
    }
    done();
  }
});

exports["test open -> open -> move "] = test({
  readyWhen: events => events.length === (isFennec ? 4 : 5),
  start: ({tabs, window}) => {
    tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
    tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
    utils.move(tabs[0], 2);
  },
  end: ({ tabs, events, assert, done }) => {
    let [ tab1, tab2 ] = tabs;
    let tab1Events = 0;
    getRelatedEvents(events, tab1).map(event => {
      tab1Events++;
      if (tab1Events === 1)
        assert.equal(event.type, "TabOpen", "first tab opened");
      else if (tab1Events === 2)
        assert.equal(event.type, "TabSelect", "first tab selected");
      else if (tab1Events === 3 && isFennec)
        assert.equal(event.type, "TabMove", "first tab moved");
      assert.equal(event.target, tab1);
    });
    assert.equal(tab1Events, isFennec ? 2 : 3,
      "correct number of events for first tab");

    let tab2Events = 0;
    getRelatedEvents(events, tab2).map(event => {
      tab2Events++;
      if (tab2Events === 1)
        assert.equal(event.type, "TabOpen", "second tab opened");
      else
        assert.equal(event.type, "TabSelect", "second tab selected");
      assert.equal(event.target, tab2);
    });
    done();
  }
});

function getRelatedEvents (events, tabs) {
  return events.filter(({target}) => ~([].concat(tabs)).indexOf(target));
}

// require("sdk/test").run(exports);