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