aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.3/packages/addon-kit/tests/test-hotkeys.js
blob: b4c6b37f9394bab832317292c7db9f1a97853fd4 (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
"use strict";

const { Hotkey } = require("hotkeys");
const { keyDown } = require("dom/events/keys");

exports["test hotkey: function key"] = function(assert, done) {
  var element = require("window-utils").activeBrowserWindow.document.documentElement;
  var showHotKey = Hotkey({
    combo: "f1",
    onPress: function() {
      assert.pass("first callback is called");
      keyDown(element, "f2");
      showHotKey.destroy();
    }
  });

  var hideHotKey = Hotkey({
    combo: "f2",
    onPress: function() {
      assert.pass("second callback is called");
      hideHotKey.destroy();
      done();
    }
  });

  keyDown(element, "f1");
};

exports["test hotkey: accel alt shift"] = function(assert, done) {
  var element = require("window-utils").activeBrowserWindow.document.documentElement;
  var showHotKey = Hotkey({
    combo: "accel-shift-6",
    onPress: function() {
      assert.pass("first callback is called");
      keyDown(element, "accel-alt-shift-6");
      showHotKey.destroy();
    }
  });

  var hideHotKey = Hotkey({
    combo: "accel-alt-shift-6",
    onPress: function() {
      assert.pass("second callback is called");
      hideHotKey.destroy();
      done();
    }
  });

  keyDown(element, "accel-shift-6");
};

exports["test hotkey meta & control"] = function(assert, done) {
  var element = require("window-utils").activeBrowserWindow.document.documentElement;
  var showHotKey = Hotkey({
    combo: "meta-3",
    onPress: function() {
      assert.pass("first callback is called");
      keyDown(element, "alt-control-shift-b");
      showHotKey.destroy();
    }
  });

  var hideHotKey = Hotkey({
    combo: "Ctrl-Alt-Shift-B",
    onPress: function() {
      assert.pass("second callback is called");
      hideHotKey.destroy();
      done();
    }
  });

  keyDown(element, "meta-3");
};

exports["test hotkey: control-1 / meta--"] = function(assert, done) {
  var element = require("window-utils").activeBrowserWindow.document.documentElement;
  var showHotKey = Hotkey({
    combo: "control-1",
    onPress: function() {
      assert.pass("first callback is called");
      keyDown(element, "meta--");
      showHotKey.destroy();
    }
  });

  var hideHotKey = Hotkey({
    combo: "meta--",
    onPress: function() {
      assert.pass("second callback is called");
      hideHotKey.destroy();
      done();
    }
  });

  keyDown(element, "control-1");
};

exports["test invalid combos"] = function(assert) {
  assert.throws(function() {
    Hotkey({
      combo: "d",
      onPress: function() {}
    });
  }, "throws if no modifier is present");
  assert.throws(function() {
    Hotkey({
      combo: "alt",
      onPress: function() {}
    });
  }, "throws if no key is present");
  assert.throws(function() {
    Hotkey({
      combo: "alt p b",
      onPress: function() {}
    });
  }, "throws if more then one key is present");
};

exports["test no exception on unmodified keypress"] = function(assert) {
  var element = require("window-utils").activeBrowserWindow.document.documentElement;
  var someHotkey = Hotkey({
    combo: "control-alt-1",
    onPress: function() {
    }
  });
  keyDown(element, "a");
  assert.pass("No exception throw, unmodified keypress passed");
};

exports["test hotkey: automatic destroy"] = function(assert, done) {
  // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
  let loader = assert._log.makeSandboxedLoader();
  
  var called = false;
  var element = loader.require("window-utils").activeBrowserWindow.document.documentElement;
  var hotkey = loader.require("hotkeys").Hotkey({
    combo: "accel-shift-x",
    onPress: function() {
      called = true;
    }
  });
  
  // Unload the module so that previous hotkey is automatically destroyed
  loader.unload();
  
  // Ensure that the hotkey is really destroyed
  keyDown(element, "accel-shift-x");
  
  require("timer").setTimeout(function () {
    assert.ok(!called, "Hotkey is destroyed and not called.");
    done();
  }, 0);
};

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