|
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 |
|
5 "use strict"; |
|
6 |
|
7 const utils = require("sdk/keyboard/utils"); |
|
8 const runtime = require("sdk/system/runtime"); |
|
9 |
|
10 const isMac = runtime.OS === "Darwin"; |
|
11 |
|
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"); |
|
22 |
|
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"); |
|
28 |
|
29 assert.equal(utils.toString({ |
|
30 modifiers: [ "shift", "alt" ] |
|
31 }), "shift-alt-", "works with only modifiers"); |
|
32 }; |
|
33 |
|
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"); |
|
39 |
|
40 assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), { |
|
41 key: "c", |
|
42 modifiers: [ "alt", "meta" ] |
|
43 }, "removes dublicates"); |
|
44 |
|
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"); |
|
49 }; |
|
50 |
|
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"); |
|
56 |
|
57 assert.throws(function() { |
|
58 utils.normalize("shift-alt-b-z"); |
|
59 }, "throws if contains more then on non-modifier key"); |
|
60 }; |
|
61 |
|
62 require("test").run(exports); |