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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-hotkeys.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const { Hotkey } = require("sdk/hotkeys");
    1.11 +const { keyDown } = require("sdk/dom/events/keys");
    1.12 +const { Loader } = require('sdk/test/loader');
    1.13 +const timer = require("sdk/timers");
    1.14 +const winUtils = require("sdk/deprecated/window-utils");
    1.15 +
    1.16 +exports["test hotkey: function key"] = function(assert, done) {
    1.17 +  var element = winUtils.activeBrowserWindow.document.documentElement;
    1.18 +  var showHotKey = Hotkey({
    1.19 +    combo: "f1",
    1.20 +    onPress: function() {
    1.21 +      assert.pass("first callback is called");
    1.22 +      assert.equal(this, showHotKey,
    1.23 +        'Context `this` in `onPress` should be the hotkey object');
    1.24 +      keyDown(element, "f2");
    1.25 +      showHotKey.destroy();
    1.26 +    }
    1.27 +  });
    1.28 +
    1.29 +  var hideHotKey = Hotkey({
    1.30 +    combo: "f2",
    1.31 +    onPress: function() {
    1.32 +      assert.pass("second callback is called");
    1.33 +      hideHotKey.destroy();
    1.34 +      done();
    1.35 +    }
    1.36 +  });
    1.37 +
    1.38 +  keyDown(element, "f1");
    1.39 +};
    1.40 +
    1.41 +exports["test hotkey: accel alt shift"] = function(assert, done) {
    1.42 +  var element = winUtils.activeBrowserWindow.document.documentElement;
    1.43 +  var showHotKey = Hotkey({
    1.44 +    combo: "accel-shift-6",
    1.45 +    onPress: function() {
    1.46 +      assert.pass("first callback is called");
    1.47 +      keyDown(element, "accel-alt-shift-6");
    1.48 +      showHotKey.destroy();
    1.49 +    }
    1.50 +  });
    1.51 +
    1.52 +  var hideHotKey = Hotkey({
    1.53 +    combo: "accel-alt-shift-6",
    1.54 +    onPress: function() {
    1.55 +      assert.pass("second callback is called");
    1.56 +      hideHotKey.destroy();
    1.57 +      done();
    1.58 +    }
    1.59 +  });
    1.60 +
    1.61 +  keyDown(element, "accel-shift-6");
    1.62 +};
    1.63 +
    1.64 +exports["test hotkey meta & control"] = function(assert, done) {
    1.65 +  var element = winUtils.activeBrowserWindow.document.documentElement;
    1.66 +  var showHotKey = Hotkey({
    1.67 +    combo: "meta-3",
    1.68 +    onPress: function() {
    1.69 +      assert.pass("first callback is called");
    1.70 +      keyDown(element, "alt-control-shift-b");
    1.71 +      showHotKey.destroy();
    1.72 +    }
    1.73 +  });
    1.74 +
    1.75 +  var hideHotKey = Hotkey({
    1.76 +    combo: "Ctrl-Alt-Shift-B",
    1.77 +    onPress: function() {
    1.78 +      assert.pass("second callback is called");
    1.79 +      hideHotKey.destroy();
    1.80 +      done();
    1.81 +    }
    1.82 +  });
    1.83 +
    1.84 +  keyDown(element, "meta-3");
    1.85 +};
    1.86 +
    1.87 +exports["test hotkey: control-1 / meta--"] = function(assert, done) {
    1.88 +  var element = winUtils.activeBrowserWindow.document.documentElement;
    1.89 +  var showHotKey = Hotkey({
    1.90 +    combo: "control-1",
    1.91 +    onPress: function() {
    1.92 +      assert.pass("first callback is called");
    1.93 +      keyDown(element, "meta--");
    1.94 +      showHotKey.destroy();
    1.95 +    }
    1.96 +  });
    1.97 +
    1.98 +  var hideHotKey = Hotkey({
    1.99 +    combo: "meta--",
   1.100 +    onPress: function() {
   1.101 +      assert.pass("second callback is called");
   1.102 +      hideHotKey.destroy();
   1.103 +      done();
   1.104 +    }
   1.105 +  });
   1.106 +
   1.107 +  keyDown(element, "control-1");
   1.108 +};
   1.109 +
   1.110 +exports["test invalid combos"] = function(assert) {
   1.111 +  assert.throws(function() {
   1.112 +    Hotkey({
   1.113 +      combo: "d",
   1.114 +      onPress: function() {}
   1.115 +    });
   1.116 +  }, "throws if no modifier is present");
   1.117 +  assert.throws(function() {
   1.118 +    Hotkey({
   1.119 +      combo: "alt",
   1.120 +      onPress: function() {}
   1.121 +    });
   1.122 +  }, "throws if no key is present");
   1.123 +  assert.throws(function() {
   1.124 +    Hotkey({
   1.125 +      combo: "alt p b",
   1.126 +      onPress: function() {}
   1.127 +    });
   1.128 +  }, "throws if more then one key is present");
   1.129 +};
   1.130 +
   1.131 +exports["test no exception on unmodified keypress"] = function(assert) {
   1.132 +  var element = winUtils.activeBrowserWindow.document.documentElement;
   1.133 +  var someHotkey = Hotkey({
   1.134 +    combo: "control-alt-1",
   1.135 +    onPress: function() {
   1.136 +    }
   1.137 +  });
   1.138 +  keyDown(element, "a");
   1.139 +  assert.pass("No exception throw, unmodified keypress passed");
   1.140 +};
   1.141 +
   1.142 +exports["test hotkey: automatic destroy"] = function(assert, done) {
   1.143 +  // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
   1.144 +  let loader = Loader(module);
   1.145 +  
   1.146 +  var called = false;
   1.147 +  var element = loader.require("sdk/deprecated/window-utils").activeBrowserWindow.document.documentElement;
   1.148 +  var hotkey = loader.require("sdk/hotkeys").Hotkey({
   1.149 +    combo: "accel-shift-x",
   1.150 +    onPress: function() {
   1.151 +      called = true;
   1.152 +    }
   1.153 +  });
   1.154 +  
   1.155 +  // Unload the module so that previous hotkey is automatically destroyed
   1.156 +  loader.unload();
   1.157 +  
   1.158 +  // Ensure that the hotkey is really destroyed
   1.159 +  keyDown(element, "accel-shift-x");
   1.160 +  
   1.161 +  timer.setTimeout(function () {
   1.162 +    assert.ok(!called, "Hotkey is destroyed and not called.");
   1.163 +    done();
   1.164 +  }, 0);
   1.165 +};
   1.166 +
   1.167 +require("test").run(exports);

mercurial