addon-sdk/source/test/test-tab-events.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const { Loader } = require("sdk/test/loader");
michael@0 8 const utils = require("sdk/tabs/utils");
michael@0 9 const { open, close } = require("sdk/window/helpers");
michael@0 10 const { getMostRecentBrowserWindow } = require("sdk/window/utils");
michael@0 11 const { events } = require("sdk/tab/events");
michael@0 12 const { on, off } = require("sdk/event/core");
michael@0 13 const { resolve, defer } = require("sdk/core/promise");
michael@0 14
michael@0 15 let isFennec = require("sdk/system/xul-app").is("Fennec");
michael@0 16
michael@0 17 function test(options) {
michael@0 18 return function(assert, done) {
michael@0 19 let tabEvents = [];
michael@0 20 let tabs = [];
michael@0 21 let { promise, resolve: resolveP } = defer();
michael@0 22 let win = isFennec ? resolve(getMostRecentBrowserWindow()) :
michael@0 23 open(null, {
michael@0 24 features: { private: true, toolbar:true, chrome: true }
michael@0 25 });
michael@0 26 let window = null;
michael@0 27
michael@0 28 // Firefox events are fired sync; Fennec events async
michael@0 29 // this normalizes the tests
michael@0 30 function handler (event) {
michael@0 31 tabEvents.push(event);
michael@0 32 runIfReady();
michael@0 33 }
michael@0 34
michael@0 35 function runIfReady () {
michael@0 36 let releventEvents = getRelatedEvents(tabEvents, tabs);
michael@0 37 if (options.readyWhen(releventEvents))
michael@0 38 options.end({
michael@0 39 tabs: tabs,
michael@0 40 events: releventEvents,
michael@0 41 assert: assert,
michael@0 42 done: resolveP
michael@0 43 });
michael@0 44 }
michael@0 45
michael@0 46 win.then(function(w) {
michael@0 47 window = w;
michael@0 48 on(events, "data", handler);
michael@0 49 options.start({ tabs: tabs, window: window });
michael@0 50
michael@0 51 // Execute here for synchronous FF events, as the handlers
michael@0 52 // were called before tabs were pushed to `tabs`
michael@0 53 runIfReady();
michael@0 54 return promise;
michael@0 55 }).then(function() {
michael@0 56 off(events, "data", handler);
michael@0 57 return isFennec ? null : close(window);
michael@0 58 }).then(done, assert.fail);
michael@0 59 };
michael@0 60 }
michael@0 61
michael@0 62 // Just making sure that tab events work for already opened tabs not only
michael@0 63 // for new windows.
michael@0 64 exports["test current window"] = test({
michael@0 65 readyWhen: events => events.length === 3,
michael@0 66 start: ({ tabs, window }) => {
michael@0 67 let tab = utils.openTab(window, 'data:text/plain,open');
michael@0 68 tabs.push(tab);
michael@0 69 utils.closeTab(tab);
michael@0 70 },
michael@0 71 end: ({ tabs, events, assert, done }) => {
michael@0 72 let [open, select, close] = events;
michael@0 73 let tab = tabs[0];
michael@0 74
michael@0 75 assert.equal(open.type, "TabOpen");
michael@0 76 assert.equal(open.target, tab);
michael@0 77
michael@0 78 assert.equal(select.type, "TabSelect");
michael@0 79 assert.equal(select.target, tab);
michael@0 80
michael@0 81 assert.equal(close.type, "TabClose");
michael@0 82 assert.equal(close.target, tab);
michael@0 83 done();
michael@0 84 }
michael@0 85 });
michael@0 86
michael@0 87 exports["test open"] = test({
michael@0 88 readyWhen: events => events.length === 2,
michael@0 89 start: ({ tabs, window }) => {
michael@0 90 tabs.push(utils.openTab(window, 'data:text/plain,open'));
michael@0 91 },
michael@0 92 end: ({ tabs, events, assert, done }) => {
michael@0 93 let [open, select] = events;
michael@0 94 let tab = tabs[0];
michael@0 95
michael@0 96 assert.equal(open.type, "TabOpen");
michael@0 97 assert.equal(open.target, tab);
michael@0 98
michael@0 99 assert.equal(select.type, "TabSelect");
michael@0 100 assert.equal(select.target, tab);
michael@0 101 done();
michael@0 102 }
michael@0 103 });
michael@0 104
michael@0 105 exports["test open -> close"] = test({
michael@0 106 readyWhen: events => events.length === 3,
michael@0 107 start: ({ tabs, window }) => {
michael@0 108 // First tab is useless we just open it so that closing second tab won't
michael@0 109 // close window on some platforms.
michael@0 110 utils.openTab(window, 'data:text/plain,ignore');
michael@0 111 let tab = utils.openTab(window, 'data:text/plain,open-close');
michael@0 112 tabs.push(tab);
michael@0 113 utils.closeTab(tab);
michael@0 114 },
michael@0 115 end: ({ tabs, events, assert, done }) => {
michael@0 116 let [open, select, close] = events;
michael@0 117 let tab = tabs[0];
michael@0 118
michael@0 119 assert.equal(open.type, "TabOpen");
michael@0 120 assert.equal(open.target, tab);
michael@0 121
michael@0 122 assert.equal(select.type, "TabSelect");
michael@0 123 assert.equal(select.target, tab);
michael@0 124
michael@0 125 assert.equal(close.type, "TabClose");
michael@0 126 assert.equal(close.target, tab);
michael@0 127 done();
michael@0 128 }
michael@0 129 });
michael@0 130
michael@0 131 exports["test open -> open -> select"] = test({
michael@0 132 readyWhen: events => events.length === 5,
michael@0 133 start: ({tabs, window}) => {
michael@0 134 tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
michael@0 135 tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
michael@0 136 utils.activateTab(tabs[0], window);
michael@0 137 },
michael@0 138 end: ({ tabs, events, assert, done }) => {
michael@0 139 let [ tab1, tab2 ] = tabs;
michael@0 140 let tab1Events = 0;
michael@0 141 getRelatedEvents(events, tab1).map(event => {
michael@0 142 tab1Events++;
michael@0 143 if (tab1Events === 1)
michael@0 144 assert.equal(event.type, "TabOpen", "first tab opened");
michael@0 145 else
michael@0 146 assert.equal(event.type, "TabSelect", "first tab selected");
michael@0 147 assert.equal(event.target, tab1);
michael@0 148 });
michael@0 149 assert.equal(tab1Events, 3, "first tab has 3 events");
michael@0 150
michael@0 151 let tab2Opened;
michael@0 152 getRelatedEvents(events, tab2).map(event => {
michael@0 153 if (!tab2Opened)
michael@0 154 assert.equal(event.type, "TabOpen", "second tab opened");
michael@0 155 else
michael@0 156 assert.equal(event.type, "TabSelect", "second tab selected");
michael@0 157 tab2Opened = true;
michael@0 158 assert.equal(event.target, tab2);
michael@0 159 });
michael@0 160 done();
michael@0 161 }
michael@0 162 });
michael@0 163
michael@0 164 exports["test open -> pin -> unpin"] = test({
michael@0 165 readyWhen: events => events.length === (isFennec ? 2 : 5),
michael@0 166 start: ({ tabs, window }) => {
michael@0 167 tabs.push(utils.openTab(window, 'data:text/plain,pin-unpin'));
michael@0 168 utils.pin(tabs[0]);
michael@0 169 utils.unpin(tabs[0]);
michael@0 170 },
michael@0 171 end: ({ tabs, events, assert, done }) => {
michael@0 172 let [open, select, move, pin, unpin] = events;
michael@0 173 let tab = tabs[0];
michael@0 174
michael@0 175 assert.equal(open.type, "TabOpen");
michael@0 176 assert.equal(open.target, tab);
michael@0 177
michael@0 178 assert.equal(select.type, "TabSelect");
michael@0 179 assert.equal(select.target, tab);
michael@0 180
michael@0 181 if (isFennec) {
michael@0 182 assert.pass("Tab pin / unpin is not supported by Fennec");
michael@0 183 }
michael@0 184 else {
michael@0 185 assert.equal(move.type, "TabMove");
michael@0 186 assert.equal(move.target, tab);
michael@0 187
michael@0 188 assert.equal(pin.type, "TabPinned");
michael@0 189 assert.equal(pin.target, tab);
michael@0 190
michael@0 191 assert.equal(unpin.type, "TabUnpinned");
michael@0 192 assert.equal(unpin.target, tab);
michael@0 193 }
michael@0 194 done();
michael@0 195 }
michael@0 196 });
michael@0 197
michael@0 198 exports["test open -> open -> move "] = test({
michael@0 199 readyWhen: events => events.length === (isFennec ? 4 : 5),
michael@0 200 start: ({tabs, window}) => {
michael@0 201 tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
michael@0 202 tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
michael@0 203 utils.move(tabs[0], 2);
michael@0 204 },
michael@0 205 end: ({ tabs, events, assert, done }) => {
michael@0 206 let [ tab1, tab2 ] = tabs;
michael@0 207 let tab1Events = 0;
michael@0 208 getRelatedEvents(events, tab1).map(event => {
michael@0 209 tab1Events++;
michael@0 210 if (tab1Events === 1)
michael@0 211 assert.equal(event.type, "TabOpen", "first tab opened");
michael@0 212 else if (tab1Events === 2)
michael@0 213 assert.equal(event.type, "TabSelect", "first tab selected");
michael@0 214 else if (tab1Events === 3 && isFennec)
michael@0 215 assert.equal(event.type, "TabMove", "first tab moved");
michael@0 216 assert.equal(event.target, tab1);
michael@0 217 });
michael@0 218 assert.equal(tab1Events, isFennec ? 2 : 3,
michael@0 219 "correct number of events for first tab");
michael@0 220
michael@0 221 let tab2Events = 0;
michael@0 222 getRelatedEvents(events, tab2).map(event => {
michael@0 223 tab2Events++;
michael@0 224 if (tab2Events === 1)
michael@0 225 assert.equal(event.type, "TabOpen", "second tab opened");
michael@0 226 else
michael@0 227 assert.equal(event.type, "TabSelect", "second tab selected");
michael@0 228 assert.equal(event.target, tab2);
michael@0 229 });
michael@0 230 done();
michael@0 231 }
michael@0 232 });
michael@0 233
michael@0 234 function getRelatedEvents (events, tabs) {
michael@0 235 return events.filter(({target}) => ~([].concat(tabs)).indexOf(target));
michael@0 236 }
michael@0 237
michael@0 238 require("sdk/test").run(exports);

mercurial