test-hotkeys.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "use strict";
  5. const { Hotkey } = require("sdk/hotkeys");
  6. const { keyDown } = require("sdk/dom/events/keys");
  7. const { Loader } = require('sdk/test/loader');
  8. const timer = require("sdk/timers");
  9. const winUtils = require("sdk/deprecated/window-utils");
  10. exports["test hotkey: function key"] = function(assert, done) {
  11. var element = winUtils.activeBrowserWindow.document.documentElement;
  12. var showHotKey = Hotkey({
  13. combo: "f1",
  14. onPress: function() {
  15. assert.pass("first callback is called");
  16. assert.equal(this, showHotKey,
  17. 'Context `this` in `onPress` should be the hotkey object');
  18. keyDown(element, "f2");
  19. showHotKey.destroy();
  20. }
  21. });
  22. var hideHotKey = Hotkey({
  23. combo: "f2",
  24. onPress: function() {
  25. assert.pass("second callback is called");
  26. hideHotKey.destroy();
  27. done();
  28. }
  29. });
  30. keyDown(element, "f1");
  31. };
  32. exports["test hotkey: accel alt shift"] = function(assert, done) {
  33. var element = winUtils.activeBrowserWindow.document.documentElement;
  34. var showHotKey = Hotkey({
  35. combo: "accel-shift-6",
  36. onPress: function() {
  37. assert.pass("first callback is called");
  38. keyDown(element, "accel-alt-shift-6");
  39. showHotKey.destroy();
  40. }
  41. });
  42. var hideHotKey = Hotkey({
  43. combo: "accel-alt-shift-6",
  44. onPress: function() {
  45. assert.pass("second callback is called");
  46. hideHotKey.destroy();
  47. done();
  48. }
  49. });
  50. keyDown(element, "accel-shift-6");
  51. };
  52. exports["test hotkey meta & control"] = function(assert, done) {
  53. var element = winUtils.activeBrowserWindow.document.documentElement;
  54. var showHotKey = Hotkey({
  55. combo: "meta-3",
  56. onPress: function() {
  57. assert.pass("first callback is called");
  58. keyDown(element, "alt-control-shift-b");
  59. showHotKey.destroy();
  60. }
  61. });
  62. var hideHotKey = Hotkey({
  63. combo: "Ctrl-Alt-Shift-B",
  64. onPress: function() {
  65. assert.pass("second callback is called");
  66. hideHotKey.destroy();
  67. done();
  68. }
  69. });
  70. keyDown(element, "meta-3");
  71. };
  72. exports["test hotkey: control-1 / meta--"] = function(assert, done) {
  73. var element = winUtils.activeBrowserWindow.document.documentElement;
  74. var showHotKey = Hotkey({
  75. combo: "control-1",
  76. onPress: function() {
  77. assert.pass("first callback is called");
  78. keyDown(element, "meta--");
  79. showHotKey.destroy();
  80. }
  81. });
  82. var hideHotKey = Hotkey({
  83. combo: "meta--",
  84. onPress: function() {
  85. assert.pass("second callback is called");
  86. hideHotKey.destroy();
  87. done();
  88. }
  89. });
  90. keyDown(element, "control-1");
  91. };
  92. exports["test invalid combos"] = function(assert) {
  93. assert.throws(function() {
  94. Hotkey({
  95. combo: "d",
  96. onPress: function() {}
  97. });
  98. }, "throws if no modifier is present");
  99. assert.throws(function() {
  100. Hotkey({
  101. combo: "alt",
  102. onPress: function() {}
  103. });
  104. }, "throws if no key is present");
  105. assert.throws(function() {
  106. Hotkey({
  107. combo: "alt p b",
  108. onPress: function() {}
  109. });
  110. }, "throws if more then one key is present");
  111. };
  112. exports["test no exception on unmodified keypress"] = function(assert) {
  113. var element = winUtils.activeBrowserWindow.document.documentElement;
  114. var someHotkey = Hotkey({
  115. combo: "control-alt-1",
  116. onPress: function() {
  117. }
  118. });
  119. keyDown(element, "a");
  120. assert.pass("No exception throw, unmodified keypress passed");
  121. };
  122. exports["test hotkey: automatic destroy"] = function(assert, done) {
  123. // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
  124. let loader = Loader(module);
  125. var called = false;
  126. var element = loader.require("sdk/deprecated/window-utils").activeBrowserWindow.document.documentElement;
  127. var hotkey = loader.require("sdk/hotkeys").Hotkey({
  128. combo: "accel-shift-x",
  129. onPress: function() {
  130. called = true;
  131. }
  132. });
  133. // Unload the module so that previous hotkey is automatically destroyed
  134. loader.unload();
  135. // Ensure that the hotkey is really destroyed
  136. keyDown(element, "accel-shift-x");
  137. timer.setTimeout(function () {
  138. assert.ok(!called, "Hotkey is destroyed and not called.");
  139. done();
  140. }, 0);
  141. };
  142. require("test").run(exports);