michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const { Hotkey } = require("sdk/hotkeys"); michael@0: const { keyDown } = require("sdk/dom/events/keys"); michael@0: const { Loader } = require('sdk/test/loader'); michael@0: const timer = require("sdk/timers"); michael@0: const winUtils = require("sdk/deprecated/window-utils"); michael@0: michael@0: exports["test hotkey: function key"] = function(assert, done) { michael@0: var element = winUtils.activeBrowserWindow.document.documentElement; michael@0: var showHotKey = Hotkey({ michael@0: combo: "f1", michael@0: onPress: function() { michael@0: assert.pass("first callback is called"); michael@0: assert.equal(this, showHotKey, michael@0: 'Context `this` in `onPress` should be the hotkey object'); michael@0: keyDown(element, "f2"); michael@0: showHotKey.destroy(); michael@0: } michael@0: }); michael@0: michael@0: var hideHotKey = Hotkey({ michael@0: combo: "f2", michael@0: onPress: function() { michael@0: assert.pass("second callback is called"); michael@0: hideHotKey.destroy(); michael@0: done(); michael@0: } michael@0: }); michael@0: michael@0: keyDown(element, "f1"); michael@0: }; michael@0: michael@0: exports["test hotkey: accel alt shift"] = function(assert, done) { michael@0: var element = winUtils.activeBrowserWindow.document.documentElement; michael@0: var showHotKey = Hotkey({ michael@0: combo: "accel-shift-6", michael@0: onPress: function() { michael@0: assert.pass("first callback is called"); michael@0: keyDown(element, "accel-alt-shift-6"); michael@0: showHotKey.destroy(); michael@0: } michael@0: }); michael@0: michael@0: var hideHotKey = Hotkey({ michael@0: combo: "accel-alt-shift-6", michael@0: onPress: function() { michael@0: assert.pass("second callback is called"); michael@0: hideHotKey.destroy(); michael@0: done(); michael@0: } michael@0: }); michael@0: michael@0: keyDown(element, "accel-shift-6"); michael@0: }; michael@0: michael@0: exports["test hotkey meta & control"] = function(assert, done) { michael@0: var element = winUtils.activeBrowserWindow.document.documentElement; michael@0: var showHotKey = Hotkey({ michael@0: combo: "meta-3", michael@0: onPress: function() { michael@0: assert.pass("first callback is called"); michael@0: keyDown(element, "alt-control-shift-b"); michael@0: showHotKey.destroy(); michael@0: } michael@0: }); michael@0: michael@0: var hideHotKey = Hotkey({ michael@0: combo: "Ctrl-Alt-Shift-B", michael@0: onPress: function() { michael@0: assert.pass("second callback is called"); michael@0: hideHotKey.destroy(); michael@0: done(); michael@0: } michael@0: }); michael@0: michael@0: keyDown(element, "meta-3"); michael@0: }; michael@0: michael@0: exports["test hotkey: control-1 / meta--"] = function(assert, done) { michael@0: var element = winUtils.activeBrowserWindow.document.documentElement; michael@0: var showHotKey = Hotkey({ michael@0: combo: "control-1", michael@0: onPress: function() { michael@0: assert.pass("first callback is called"); michael@0: keyDown(element, "meta--"); michael@0: showHotKey.destroy(); michael@0: } michael@0: }); michael@0: michael@0: var hideHotKey = Hotkey({ michael@0: combo: "meta--", michael@0: onPress: function() { michael@0: assert.pass("second callback is called"); michael@0: hideHotKey.destroy(); michael@0: done(); michael@0: } michael@0: }); michael@0: michael@0: keyDown(element, "control-1"); michael@0: }; michael@0: michael@0: exports["test invalid combos"] = function(assert) { michael@0: assert.throws(function() { michael@0: Hotkey({ michael@0: combo: "d", michael@0: onPress: function() {} michael@0: }); michael@0: }, "throws if no modifier is present"); michael@0: assert.throws(function() { michael@0: Hotkey({ michael@0: combo: "alt", michael@0: onPress: function() {} michael@0: }); michael@0: }, "throws if no key is present"); michael@0: assert.throws(function() { michael@0: Hotkey({ michael@0: combo: "alt p b", michael@0: onPress: function() {} michael@0: }); michael@0: }, "throws if more then one key is present"); michael@0: }; michael@0: michael@0: exports["test no exception on unmodified keypress"] = function(assert) { michael@0: var element = winUtils.activeBrowserWindow.document.documentElement; michael@0: var someHotkey = Hotkey({ michael@0: combo: "control-alt-1", michael@0: onPress: function() { michael@0: } michael@0: }); michael@0: keyDown(element, "a"); michael@0: assert.pass("No exception throw, unmodified keypress passed"); michael@0: }; michael@0: michael@0: exports["test hotkey: automatic destroy"] = function(assert, done) { michael@0: // Hacky way to be able to create unloadable modules via makeSandboxedLoader. michael@0: let loader = Loader(module); michael@0: michael@0: var called = false; michael@0: var element = loader.require("sdk/deprecated/window-utils").activeBrowserWindow.document.documentElement; michael@0: var hotkey = loader.require("sdk/hotkeys").Hotkey({ michael@0: combo: "accel-shift-x", michael@0: onPress: function() { michael@0: called = true; michael@0: } michael@0: }); michael@0: michael@0: // Unload the module so that previous hotkey is automatically destroyed michael@0: loader.unload(); michael@0: michael@0: // Ensure that the hotkey is really destroyed michael@0: keyDown(element, "accel-shift-x"); michael@0: michael@0: timer.setTimeout(function () { michael@0: assert.ok(!called, "Hotkey is destroyed and not called."); michael@0: done(); michael@0: }, 0); michael@0: }; michael@0: michael@0: require("test").run(exports);