]> git.proxmox.com Git - extjs.git/blob - extjs/classic/classic/test/specs/util/KeyboardInteractive.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / test / specs / util / KeyboardInteractive.js
1 describe("Ext.util.KeyboardInteractive", function() {
2 var Event = Ext.event.Event,
3 createSpy = jasmine.createSpy,
4 focusAndWait = jasmine.focusAndWait,
5 waitAWhile = jasmine.waitAWhile,
6 pressArrowKey = jasmine.pressArrowKey,
7 fireKeyEvent = jasmine.fireKeyEvent,
8 c, focusEl;
9
10 function stdComponent(config) {
11 return Ext.apply({
12 xtype: 'component',
13 renderTo: Ext.getBody(),
14 width: 100,
15 height: 100,
16 focusable: true,
17 tabIndex: 0,
18 getFocusEl: function() {
19 return this.el;
20 },
21 onKeyDefault: Ext.emptyFn
22 }, config);
23 }
24
25 function makeComponent(config) {
26 var cmpCfg = stdComponent(config);
27
28 c = new Ext.Component(cmpCfg);
29
30 return c;
31 }
32
33 afterEach(function() {
34 if (c) {
35 c.destroy();
36 }
37
38 c = null;
39 });
40
41 describe("config handling", function() {
42 beforeEach(function() {
43 makeComponent();
44 });
45
46 it("should accept binding as function", function() {
47 spyOn(Ext.log, 'warn');
48
49 c.setKeyHandlers({ UP: Ext.emptyFn });
50
51 expect(Ext.log.warn).not.toHaveBeenCalled();
52
53 var handlers = c.getKeyHandlers();
54
55 expect(handlers.UP).toBe(Ext.emptyFn);
56 });
57
58 it("should accept binding as fn name", function() {
59 c.setKeyHandlers({ DOWN: 'onKeyDefault' });
60
61 var handlers = c.getKeyHandlers();
62
63 expect(handlers.DOWN).toBe(Ext.emptyFn);
64 });
65
66 it("should throw on unknown keycode", function() {
67 var err = "Unknown key: FOO in keyHandlers config for " + c.id +
68 ". Key names should be in UPPER CASE.";
69
70 expect(function() {
71 c.setKeyHandlers({ FOO: 'onKeyFoo' });
72 }).toThrow(err);
73 });
74
75 it("should warn on undefined binding", function() {
76 // The warning is expected
77 spyOn(Ext.log, 'warn');
78
79 c.setKeyHandlers({ UP: 'onKeyUp' });
80
81 var have = Ext.log.warn.mostRecentCall.args[0],
82 want = "Undefined binding onKeyUp for UP key " +
83 "in keyHandlers config for " + c.id;
84
85 expect(have).toBe(want);
86 });
87 });
88
89 describe("keydown listener", function() {
90 describe("w/o config", function() {
91 beforeEach(function() {
92 makeComponent();
93
94 focusEl = c.getFocusEl();
95 });
96
97 it("should not attach listener initially", function() {
98 expect(focusEl.hasListener('keydown')).toBe(false);
99 });
100
101 it("should attach listener on config update", function() {
102 c.setKeyHandlers({ HOME: 'onKeyDefault' });
103
104 expect(focusEl.hasListener('keydown')).toBe(true);
105 });
106 });
107
108 describe("with config", function() {
109 beforeEach(function() {
110 makeComponent({
111 keyHandlers: {
112 LEFT: 'onKeyDefault'
113 }
114 });
115
116 focusEl = c.getFocusEl();
117 });
118
119 it("should attach listener after render", function() {
120 expect(focusEl.hasListener('keydown')).toBe(true);
121 });
122
123 it("should not attach listener more than once", function() {
124 c.setKeyHandlers({ RIGHT: 'onKeyDefault' });
125
126 expect(focusEl.hasListeners.keydown).toBe(1);
127 });
128 });
129 });
130
131 describe("handlers", function() {
132 var leftSpy, rightSpy;
133
134 beforeEach(function() {
135 leftSpy = createSpy('left');
136 rightSpy = createSpy('right');
137
138 makeComponent({
139 keyHandlers: {
140 LEFT: 'onKeyLeft',
141 RIGHT: 'onKeyRight'
142 },
143
144 onKeyLeft: leftSpy,
145 onKeyRight: rightSpy
146 });
147 });
148
149 afterEach(function() {
150 leftSpy = rightSpy = null;
151 });
152
153 describe("resolving", function() {
154 it("should resolve handler name to function", function() {
155 var handlers = c.getKeyHandlers();
156
157 expect(handlers.LEFT).toBe(leftSpy);
158 });
159 });
160
161 describe("invoking", function() {
162 describe("matching a handler", function() {
163 it("should invoke the handler", function() {
164 pressArrowKey(c, 'left');
165
166 runs(function() {
167 expect(leftSpy).toHaveBeenCalled();
168 });
169 });
170
171 it("should pass the key event", function() {
172 focusAndWait(c);
173
174 runs(function() {
175 fireKeyEvent(c.getFocusEl(), 'keydown', Event.RIGHT);
176 });
177
178 waitAWhile();
179
180 runs(function() {
181 var args = rightSpy.mostRecentCall.args,
182 ev = args[0];
183
184 expect(ev.getKey()).toBe(Event.RIGHT);
185 });
186 });
187 });
188
189 describe("not matching a handler", function() {
190 it("should not throw", function() {
191 focusAndWait(c);
192
193 runs(function() {
194 expect(function() {
195 fireKeyEvent(c.getFocusEl(), 'keydown', Event.UP);
196 }).not.toThrow();
197 });
198 });
199 });
200 });
201 });
202 });