Merge mozilla-b2g34 to 2.1s. a=merge
[gecko.git] / addon-sdk / source / test / test-keyboard-utils.js
blob19981dec3d6843319acd7a2cdc9b543cc332333c
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/. */
5 "use strict";
7 const utils = require("sdk/keyboard/utils");
8 const runtime = require("sdk/system/runtime");
10 const isMac = runtime.OS === "Darwin";
12 exports["test toString"] = function(assert) {
13   assert.equal(utils.toString({
14     key: "B",
15     modifiers: [ "Shift", "Ctrl" ]
16   }), "Shift-Ctrl-B", "toString does not normalizes JSON");
17   
18   assert.equal(utils.toString({
19     key: "C",
20     modifiers: [],
21   }), "C", "Works with objects with empty array of modifiers");
23   assert.equal(utils.toString(Object.create((function Type() {}).prototype, {
24     key: { value: "d" },
25     modifiers: { value: [ "alt" ] },
26     method: { value: function() {} }
27   })), "alt-d", "Works with non-json objects");
29   assert.equal(utils.toString({ 
30     modifiers: [ "shift", "alt" ]
31   }), "shift-alt-", "works with only modifiers");
34 exports["test toJSON"] = function(assert) {
35   assert.deepEqual(utils.toJSON("Shift-Ctrl-B"), {
36     key: "b",
37     modifiers: [ "control", "shift" ]
38   }, "toJSON normalizes input");
40   assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), {
41     key: "c",
42     modifiers: [ "alt", "meta" ]
43   }, "removes dublicates");
45   assert.deepEqual(utils.toJSON("AccEl+sHiFt+Z", "+"), {
46     key: "z",
47     modifiers: isMac ? [ "meta", "shift" ] : [ "control", "shift" ]
48   }, "normalizes OS specific keys and adjustes seperator");
51 exports["test normalize"] = function assert(assert) {
52   assert.equal(utils.normalize("Shift Ctrl A control ctrl", " "),
53                "control shift a", "removes reapeted modifiers");
54   assert.equal(utils.normalize("shift-ctrl-left"), "control-shift-left",
55                "normilizes non printed characters");
57   assert.throws(function() {
58     utils.normalize("shift-alt-b-z");
59   }, "throws if contains more then on non-modifier key");
62 require("test").run(exports);