Initial import.
[featherfall2.git] / test / spec / yuu / input.js
1 var JS = this.JS || require('jstest');
2 var yuu = require('yuu/input');
3
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));
8 });
9 }});
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));
13 });
14 }});
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"));
18 });
19 }});
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"));
23 });
24 }});
25
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);
32 }});
33
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);
39 }});
40
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);
50 }});
51
52 it("unbinds", function () { with (this) {
53 var bindset = new yuu.KeyBindSet({ a: 'a', b: 'b' });
54 assertEqual(2, bindset.binds.length);
55 bindset.unbind('a');
56 assertEqual(1, bindset.binds.length);
57 bindset.unbind('x');
58 assertEqual(1, bindset.binds.length);
59 bindset.bind('a', 'a');
60 assertEqual(2, bindset.binds.length);
61 }});
62 }});
63
64 JS.Test.describe('InputState', function () { with (this) {
65 var bindset1 = new yuu.KeyBindSet({
66 "a": "a",
67 "b": "++b",
68 "c": "+c",
69 "a+b": "a and b",
70 });
71 var bindset2 = new yuu.KeyBindSet({
72 "a": "a different a",
73 "a+b+c": "a and b and c",
74 "mousemove": "mouse moved",
75 "d+mousemove": "mouse moved and d",
76 });
77
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"));
84 }});
85
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"));
90 }});
91
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"));
96 input.keyup("a");
97 assertEqual(["a"], input.keydown("a"));
98 }})
99
100 it("activates toggle binds", function () { with (this) {
101 var input = new yuu.InputState([bindset1]);
102 assertEqual(["++b"], input.keydown("b"));
103 }});
104
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"));
110 }});
111
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"));
116 }});
117
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());
122 }});
123
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());
131 }});
132
133 it("masks lower sets", function () { with (this) {
134 var input = new yuu.InputState([bindset1, bindset2]);
135 assertEqual(["a different a"], input.keydown("a"));
136 }});
137
138 it("always picks the longest command", function () { with (this) {
139 var input = new yuu.InputState([bindset1, bindset2]);
140 input.keydown("b");
141 assertEqual(["a and b"], input.keydown("a"));
142 assertEqual(["a and b and c"], input.keydown("c"));
143 }});
144
145 }});
146 }});