addon-sdk/source/test/test-hotkeys.js

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:7568b69b6a72
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 const { Hotkey } = require("sdk/hotkeys");
8 const { keyDown } = require("sdk/dom/events/keys");
9 const { Loader } = require('sdk/test/loader');
10 const timer = require("sdk/timers");
11 const winUtils = require("sdk/deprecated/window-utils");
12
13 exports["test hotkey: function key"] = function(assert, done) {
14 var element = winUtils.activeBrowserWindow.document.documentElement;
15 var showHotKey = Hotkey({
16 combo: "f1",
17 onPress: function() {
18 assert.pass("first callback is called");
19 assert.equal(this, showHotKey,
20 'Context `this` in `onPress` should be the hotkey object');
21 keyDown(element, "f2");
22 showHotKey.destroy();
23 }
24 });
25
26 var hideHotKey = Hotkey({
27 combo: "f2",
28 onPress: function() {
29 assert.pass("second callback is called");
30 hideHotKey.destroy();
31 done();
32 }
33 });
34
35 keyDown(element, "f1");
36 };
37
38 exports["test hotkey: accel alt shift"] = function(assert, done) {
39 var element = winUtils.activeBrowserWindow.document.documentElement;
40 var showHotKey = Hotkey({
41 combo: "accel-shift-6",
42 onPress: function() {
43 assert.pass("first callback is called");
44 keyDown(element, "accel-alt-shift-6");
45 showHotKey.destroy();
46 }
47 });
48
49 var hideHotKey = Hotkey({
50 combo: "accel-alt-shift-6",
51 onPress: function() {
52 assert.pass("second callback is called");
53 hideHotKey.destroy();
54 done();
55 }
56 });
57
58 keyDown(element, "accel-shift-6");
59 };
60
61 exports["test hotkey meta & control"] = function(assert, done) {
62 var element = winUtils.activeBrowserWindow.document.documentElement;
63 var showHotKey = Hotkey({
64 combo: "meta-3",
65 onPress: function() {
66 assert.pass("first callback is called");
67 keyDown(element, "alt-control-shift-b");
68 showHotKey.destroy();
69 }
70 });
71
72 var hideHotKey = Hotkey({
73 combo: "Ctrl-Alt-Shift-B",
74 onPress: function() {
75 assert.pass("second callback is called");
76 hideHotKey.destroy();
77 done();
78 }
79 });
80
81 keyDown(element, "meta-3");
82 };
83
84 exports["test hotkey: control-1 / meta--"] = function(assert, done) {
85 var element = winUtils.activeBrowserWindow.document.documentElement;
86 var showHotKey = Hotkey({
87 combo: "control-1",
88 onPress: function() {
89 assert.pass("first callback is called");
90 keyDown(element, "meta--");
91 showHotKey.destroy();
92 }
93 });
94
95 var hideHotKey = Hotkey({
96 combo: "meta--",
97 onPress: function() {
98 assert.pass("second callback is called");
99 hideHotKey.destroy();
100 done();
101 }
102 });
103
104 keyDown(element, "control-1");
105 };
106
107 exports["test invalid combos"] = function(assert) {
108 assert.throws(function() {
109 Hotkey({
110 combo: "d",
111 onPress: function() {}
112 });
113 }, "throws if no modifier is present");
114 assert.throws(function() {
115 Hotkey({
116 combo: "alt",
117 onPress: function() {}
118 });
119 }, "throws if no key is present");
120 assert.throws(function() {
121 Hotkey({
122 combo: "alt p b",
123 onPress: function() {}
124 });
125 }, "throws if more then one key is present");
126 };
127
128 exports["test no exception on unmodified keypress"] = function(assert) {
129 var element = winUtils.activeBrowserWindow.document.documentElement;
130 var someHotkey = Hotkey({
131 combo: "control-alt-1",
132 onPress: function() {
133 }
134 });
135 keyDown(element, "a");
136 assert.pass("No exception throw, unmodified keypress passed");
137 };
138
139 exports["test hotkey: automatic destroy"] = function(assert, done) {
140 // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
141 let loader = Loader(module);
142
143 var called = false;
144 var element = loader.require("sdk/deprecated/window-utils").activeBrowserWindow.document.documentElement;
145 var hotkey = loader.require("sdk/hotkeys").Hotkey({
146 combo: "accel-shift-x",
147 onPress: function() {
148 called = true;
149 }
150 });
151
152 // Unload the module so that previous hotkey is automatically destroyed
153 loader.unload();
154
155 // Ensure that the hotkey is really destroyed
156 keyDown(element, "accel-shift-x");
157
158 timer.setTimeout(function () {
159 assert.ok(!called, "Hotkey is destroyed and not called.");
160 done();
161 }, 0);
162 };
163
164 require("test").run(exports);

mercurial