1 var JS
= this.JS
|| require('jstest');
2 var yuu
= require('yuu/input');
4 JS
.Test
.describe('yuu input', function () { with (this) {
5 it("has consistent key names", function () { with (this) {
6 Object
.keys(yuu
.KEY_NAMES
).forEach(function (code
) {
7 assertEqual(yuu
.KEY_NAMES
[code
], yuu
.keyCodeName(code
));
10 it("handles unknown key names", function () { with (this) {
11 Object
.keys(yuu
.KEY_NAMES
).forEach(function (name
) {
12 assertEqual("key:123456", yuu
.keyCodeName(123456));
15 it("handles event-known key names", function () { with (this) {
16 Object
.keys(yuu
.KEY_NAMES
).forEach(function (name
) {
17 assertEqual("bogus", yuu
.keyCodeName(123456, "BOGUS"));
20 it("ignores browser default names", function () { with (this) {
21 Object
.keys(yuu
.KEY_NAMES
).forEach(function (name
) {
22 assertEqual("key:123456", yuu
.keyCodeName(123456, "Unidentified"));
26 JS
.Test
.describe('KeyBindSet', function () { with (this) {
27 it("holds binds", function () { with (this) {
28 var bindset
= new yuu
.KeyBindSet();
29 assertEqual(0, bindset
.binds
.length
);
30 bindset
= new yuu
.KeyBindSet({ a
: 'a', b
: 'b' });
31 assertEqual(2, bindset
.binds
.length
);
34 it("binds new binds", function () { with (this) {
35 var bindset
= new yuu
.KeyBindSet({ a
: 'a', b
: 'b' });
36 assertEqual(2, bindset
.binds
.length
);
37 bindset
.bind('c', 'c');
38 assertEqual(3, bindset
.binds
.length
);
41 it("doesn't bind duplicates", function () { with (this) {
42 var bindset
= new yuu
.KeyBindSet({ a
: 'a', b
: 'b' });
43 assertEqual(2, bindset
.binds
.length
);
44 bindset
.bind("a", "a different a");
45 assertEqual(2, bindset
.binds
.length
);
46 bindset
.bind("a+b", "a and b");
47 assertEqual(3, bindset
.binds
.length
);
48 bindset
.bind("b+a", "b and a");
49 assertEqual(3, bindset
.binds
.length
);
52 it("unbinds", function () { with (this) {
53 var bindset
= new yuu
.KeyBindSet({ a
: 'a', b
: 'b' });
54 assertEqual(2, bindset
.binds
.length
);
56 assertEqual(1, bindset
.binds
.length
);
58 assertEqual(1, bindset
.binds
.length
);
59 bindset
.bind('a', 'a');
60 assertEqual(2, bindset
.binds
.length
);
64 JS
.Test
.describe('InputState', function () { with (this) {
65 var bindset1
= new yuu
.KeyBindSet({
71 var bindset2
= new yuu
.KeyBindSet({
73 "a+b+c": "a and b and c",
74 "mousemove": "mouse moved",
75 "d+mousemove": "mouse moved and d",
78 it("activates simple binds", function () { with (this) {
79 var input
= new yuu
.InputState([bindset1
]);
80 assertEqual(["a"], input
.keydown("a"));
81 assertNot(input
.keyup("a"));
82 assertEqual(["a"], input
.keydown("a"));
83 assertNot(input
.keyup("a"));
86 it("doesn't activate nothing", function () { with (this) {
87 var input
= new yuu
.InputState([bindset1
]);
88 assertEqual(null, input
.keydown("x"));
89 assertEqual(null, input
.keyup("x"));
92 it("doesn't repeat", function () { with (this) {
93 var input
= new yuu
.InputState([bindset1
]);
94 assertEqual(["a"], input
.keydown("a"));
95 assertEqual([], input
.keydown("a"));
97 assertEqual(["a"], input
.keydown("a"));
100 it("activates toggle binds", function () { with (this) {
101 var input
= new yuu
.InputState([bindset1
]);
102 assertEqual(["++b"], input
.keydown("b"));
105 it("activates flag binds", function () { with (this) {
106 var input
= new yuu
.InputState([bindset1
]);
107 assertEqual(["+c"], input
.keydown("c"));
108 assertEqual([], input
.keydown("c"));
109 assertEqual(["-c"], input
.keyup("c"));
112 it("handles multiple keys", function () { with (this) {
113 var input
= new yuu
.InputState([bindset1
]);
114 assertEqual(["a"], input
.keydown("a"));
115 assertEqual(["a and b"], input
.keydown("b"));
118 it("handles change states", function () { with (this) {
119 var input
= new yuu
.InputState([bindset2
]);
120 assertEqual(["mouse moved"], input
.mousemove());
121 assertEqual(["mouse moved"], input
.mousemove());
124 it("handles change states with a key", function () { with (this) {
125 var input
= new yuu
.InputState([bindset2
]);
126 assertEqual(null, input
.keydown("d"));
127 assertEqual(["mouse moved and d"], input
.mousemove());
128 assertEqual(["mouse moved and d"], input
.mousemove());
129 assertEqual(null, input
.keyup("d"));
130 assertEqual(["mouse moved"], input
.mousemove());
133 it("masks lower sets", function () { with (this) {
134 var input
= new yuu
.InputState([bindset1
, bindset2
]);
135 assertEqual(["a different a"], input
.keydown("a"));
138 it("always picks the longest command", function () { with (this) {
139 var input
= new yuu
.InputState([bindset1
, bindset2
]);
141 assertEqual(["a and b"], input
.keydown("a"));
142 assertEqual(["a and b and c"], input
.keydown("c"));