addon-sdk/source/test/test-keyboard-utils.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const utils = require("sdk/keyboard/utils");
michael@0 8 const runtime = require("sdk/system/runtime");
michael@0 9
michael@0 10 const isMac = runtime.OS === "Darwin";
michael@0 11
michael@0 12 exports["test toString"] = function(assert) {
michael@0 13 assert.equal(utils.toString({
michael@0 14 key: "B",
michael@0 15 modifiers: [ "Shift", "Ctrl" ]
michael@0 16 }), "Shift-Ctrl-B", "toString does not normalizes JSON");
michael@0 17
michael@0 18 assert.equal(utils.toString({
michael@0 19 key: "C",
michael@0 20 modifiers: [],
michael@0 21 }), "C", "Works with objects with empty array of modifiers");
michael@0 22
michael@0 23 assert.equal(utils.toString(Object.create((function Type() {}).prototype, {
michael@0 24 key: { value: "d" },
michael@0 25 modifiers: { value: [ "alt" ] },
michael@0 26 method: { value: function() {} }
michael@0 27 })), "alt-d", "Works with non-json objects");
michael@0 28
michael@0 29 assert.equal(utils.toString({
michael@0 30 modifiers: [ "shift", "alt" ]
michael@0 31 }), "shift-alt-", "works with only modifiers");
michael@0 32 };
michael@0 33
michael@0 34 exports["test toJSON"] = function(assert) {
michael@0 35 assert.deepEqual(utils.toJSON("Shift-Ctrl-B"), {
michael@0 36 key: "b",
michael@0 37 modifiers: [ "control", "shift" ]
michael@0 38 }, "toJSON normalizes input");
michael@0 39
michael@0 40 assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), {
michael@0 41 key: "c",
michael@0 42 modifiers: [ "alt", "meta" ]
michael@0 43 }, "removes dublicates");
michael@0 44
michael@0 45 assert.deepEqual(utils.toJSON("AccEl+sHiFt+Z", "+"), {
michael@0 46 key: "z",
michael@0 47 modifiers: isMac ? [ "meta", "shift" ] : [ "control", "shift" ]
michael@0 48 }, "normalizes OS specific keys and adjustes seperator");
michael@0 49 };
michael@0 50
michael@0 51 exports["test normalize"] = function assert(assert) {
michael@0 52 assert.equal(utils.normalize("Shift Ctrl A control ctrl", " "),
michael@0 53 "control shift a", "removes reapeted modifiers");
michael@0 54 assert.equal(utils.normalize("shift-ctrl-left"), "control-shift-left",
michael@0 55 "normilizes non printed characters");
michael@0 56
michael@0 57 assert.throws(function() {
michael@0 58 utils.normalize("shift-alt-b-z");
michael@0 59 }, "throws if contains more then on non-modifier key");
michael@0 60 };
michael@0 61
michael@0 62 require("test").run(exports);

mercurial