test-keyboard-utils.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 utils = require("sdk/keyboard/utils");
  6. const runtime = require("sdk/system/runtime");
  7. const isMac = runtime.OS === "Darwin";
  8. exports["test toString"] = function(assert) {
  9. assert.equal(utils.toString({
  10. key: "B",
  11. modifiers: [ "Shift", "Ctrl" ]
  12. }), "Shift-Ctrl-B", "toString does not normalizes JSON");
  13. assert.equal(utils.toString({
  14. key: "C",
  15. modifiers: [],
  16. }), "C", "Works with objects with empty array of modifiers");
  17. assert.equal(utils.toString(Object.create((function Type() {}).prototype, {
  18. key: { value: "d" },
  19. modifiers: { value: [ "alt" ] },
  20. method: { value: function() {} }
  21. })), "alt-d", "Works with non-json objects");
  22. assert.equal(utils.toString({
  23. modifiers: [ "shift", "alt" ]
  24. }), "shift-alt-", "works with only modifiers");
  25. };
  26. exports["test toJSON"] = function(assert) {
  27. assert.deepEqual(utils.toJSON("Shift-Ctrl-B"), {
  28. key: "b",
  29. modifiers: [ "control", "shift" ]
  30. }, "toJSON normalizes input");
  31. assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), {
  32. key: "c",
  33. modifiers: [ "alt", "meta" ]
  34. }, "removes dublicates");
  35. assert.deepEqual(utils.toJSON("AccEl+sHiFt+Z", "+"), {
  36. key: "z",
  37. modifiers: isMac ? [ "meta", "shift" ] : [ "control", "shift" ]
  38. }, "normalizes OS specific keys and adjustes seperator");
  39. };
  40. exports["test normalize"] = function assert(assert) {
  41. assert.equal(utils.normalize("Shift Ctrl A control ctrl", " "),
  42. "control shift a", "removes reapeted modifiers");
  43. assert.equal(utils.normalize("shift-ctrl-left"), "control-shift-left",
  44. "normilizes non printed characters");
  45. assert.throws(function() {
  46. utils.normalize("shift-alt-b-z");
  47. }, "throws if contains more then on non-modifier key");
  48. };
  49. require("test").run(exports);