michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const utils = require("sdk/keyboard/utils"); michael@0: const runtime = require("sdk/system/runtime"); michael@0: michael@0: const isMac = runtime.OS === "Darwin"; michael@0: michael@0: exports["test toString"] = function(assert) { michael@0: assert.equal(utils.toString({ michael@0: key: "B", michael@0: modifiers: [ "Shift", "Ctrl" ] michael@0: }), "Shift-Ctrl-B", "toString does not normalizes JSON"); michael@0: michael@0: assert.equal(utils.toString({ michael@0: key: "C", michael@0: modifiers: [], michael@0: }), "C", "Works with objects with empty array of modifiers"); michael@0: michael@0: assert.equal(utils.toString(Object.create((function Type() {}).prototype, { michael@0: key: { value: "d" }, michael@0: modifiers: { value: [ "alt" ] }, michael@0: method: { value: function() {} } michael@0: })), "alt-d", "Works with non-json objects"); michael@0: michael@0: assert.equal(utils.toString({ michael@0: modifiers: [ "shift", "alt" ] michael@0: }), "shift-alt-", "works with only modifiers"); michael@0: }; michael@0: michael@0: exports["test toJSON"] = function(assert) { michael@0: assert.deepEqual(utils.toJSON("Shift-Ctrl-B"), { michael@0: key: "b", michael@0: modifiers: [ "control", "shift" ] michael@0: }, "toJSON normalizes input"); michael@0: michael@0: assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), { michael@0: key: "c", michael@0: modifiers: [ "alt", "meta" ] michael@0: }, "removes dublicates"); michael@0: michael@0: assert.deepEqual(utils.toJSON("AccEl+sHiFt+Z", "+"), { michael@0: key: "z", michael@0: modifiers: isMac ? [ "meta", "shift" ] : [ "control", "shift" ] michael@0: }, "normalizes OS specific keys and adjustes seperator"); michael@0: }; michael@0: michael@0: exports["test normalize"] = function assert(assert) { michael@0: assert.equal(utils.normalize("Shift Ctrl A control ctrl", " "), michael@0: "control shift a", "removes reapeted modifiers"); michael@0: assert.equal(utils.normalize("shift-ctrl-left"), "control-shift-left", michael@0: "normilizes non printed characters"); michael@0: michael@0: assert.throws(function() { michael@0: utils.normalize("shift-alt-b-z"); michael@0: }, "throws if contains more then on non-modifier key"); michael@0: }; michael@0: michael@0: require("test").run(exports);