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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-keyboard-utils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,62 @@
     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 utils = require("sdk/keyboard/utils");
    1.11 +const runtime = require("sdk/system/runtime");
    1.12 +
    1.13 +const isMac = runtime.OS === "Darwin";
    1.14 +
    1.15 +exports["test toString"] = function(assert) {
    1.16 +  assert.equal(utils.toString({
    1.17 +    key: "B",
    1.18 +    modifiers: [ "Shift", "Ctrl" ]
    1.19 +  }), "Shift-Ctrl-B", "toString does not normalizes JSON");
    1.20 +  
    1.21 +  assert.equal(utils.toString({
    1.22 +    key: "C",
    1.23 +    modifiers: [],
    1.24 +  }), "C", "Works with objects with empty array of modifiers");
    1.25 +
    1.26 +  assert.equal(utils.toString(Object.create((function Type() {}).prototype, {
    1.27 +    key: { value: "d" },
    1.28 +    modifiers: { value: [ "alt" ] },
    1.29 +    method: { value: function() {} }
    1.30 +  })), "alt-d", "Works with non-json objects");
    1.31 +
    1.32 +  assert.equal(utils.toString({ 
    1.33 +    modifiers: [ "shift", "alt" ]
    1.34 +  }), "shift-alt-", "works with only modifiers");
    1.35 +};
    1.36 +
    1.37 +exports["test toJSON"] = function(assert) {
    1.38 +  assert.deepEqual(utils.toJSON("Shift-Ctrl-B"), {
    1.39 +    key: "b",
    1.40 +    modifiers: [ "control", "shift" ]
    1.41 +  }, "toJSON normalizes input");
    1.42 +
    1.43 +  assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), {
    1.44 +    key: "c",
    1.45 +    modifiers: [ "alt", "meta" ]
    1.46 +  }, "removes dublicates");
    1.47 +
    1.48 +  assert.deepEqual(utils.toJSON("AccEl+sHiFt+Z", "+"), {
    1.49 +    key: "z",
    1.50 +    modifiers: isMac ? [ "meta", "shift" ] : [ "control", "shift" ]
    1.51 +  }, "normalizes OS specific keys and adjustes seperator");
    1.52 +};
    1.53 +
    1.54 +exports["test normalize"] = function assert(assert) {
    1.55 +  assert.equal(utils.normalize("Shift Ctrl A control ctrl", " "),
    1.56 +               "control shift a", "removes reapeted modifiers");
    1.57 +  assert.equal(utils.normalize("shift-ctrl-left"), "control-shift-left",
    1.58 +               "normilizes non printed characters");
    1.59 +
    1.60 +  assert.throws(function() {
    1.61 +    utils.normalize("shift-alt-b-z");
    1.62 +  }, "throws if contains more then on non-modifier key");
    1.63 +};
    1.64 +
    1.65 +require("test").run(exports);

mercurial