aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/spinner/spinner_events.js
blob: 610f7a2ecddfd4a8ae31689761cbca20bb0b06e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
(function( $ ) {

var simulateKeyDownUp = TestHelpers.spinner.simulateKeyDownUp;

module( "spinner: events" );

test( "start", function() {
	expect( 10 );
	var element = $( "#spin" ).spinner();

	function shouldStart( expectation, msg ) {
		element.spinner( "option", "start", function() {
			ok( expectation, msg );
		});
	}

	shouldStart( true, "key UP" );
	simulateKeyDownUp( element, $.ui.keyCode.UP );
	shouldStart( true, "key DOWN" );
	simulateKeyDownUp( element, $.ui.keyCode.DOWN );

	shouldStart( true, "key PAGE_UP" );
	simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
	shouldStart( true, "key PAGE_DOWN" );
	simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );

	shouldStart( true, "button up" );
	element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown().mouseup();
	shouldStart( true, "button down" );
	element.spinner( "widget" ).find( ".ui-spinner-down" ).mousedown().mouseup();

	shouldStart( true, "stepUp" );
	element.spinner( "stepUp" );
	shouldStart( true, "stepDown" );
	element.spinner( "stepDown" );

	shouldStart( true, "pageUp" );
	element.spinner( "pageUp" );
	shouldStart( true, "pageDown" );
	element.spinner( "pageDown" );

	shouldStart( false, "value" );
	element.spinner( "value", 999 );
});

test( "spin", function() {
	expect( 10 );
	var element = $( "#spin" ).spinner();

	function shouldSpin( expectation, msg ) {
		element.spinner( "option", "spin", function() {
			ok( expectation, msg );
		});
	}

	shouldSpin( true, "key UP" );
	simulateKeyDownUp( element, $.ui.keyCode.UP );
	shouldSpin( true, "key DOWN" );
	simulateKeyDownUp( element, $.ui.keyCode.DOWN );

	shouldSpin( true, "key PAGE_UP" );
	simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
	shouldSpin( true, "key PAGE_DOWN" );
	simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );

	shouldSpin( true, "button up" );
	element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown().mouseup();
	shouldSpin( true, "button down" );
	element.spinner( "widget" ).find( ".ui-spinner-down" ).mousedown().mouseup();

	shouldSpin( true, "stepUp" );
	element.spinner( "stepUp" );
	shouldSpin( true, "stepDown" );
	element.spinner( "stepDown" );

	shouldSpin( true, "pageUp" );
	element.spinner( "pageUp" );
	shouldSpin( true, "pageDown" );
	element.spinner( "pageDown" );

	shouldSpin( false, "value" );
	element.spinner( "value", 999 );
});

test( "stop", function() {
	expect( 10 );
	var element = $( "#spin" ).spinner();

	function shouldStop( expectation, msg ) {
		element.spinner( "option", "stop", function() {
			ok( expectation, msg );
		});
	}

	shouldStop( true, "key UP" );
	simulateKeyDownUp( element, $.ui.keyCode.UP );
	shouldStop( true, "key DOWN" );
	simulateKeyDownUp( element, $.ui.keyCode.DOWN );

	shouldStop( true, "key PAGE_UP" );
	simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
	shouldStop( true, "key PAGE_DOWN" );
	simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );

	shouldStop( true, "button up" );
	element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown().mouseup();
	shouldStop( true, "button down" );
	element.spinner( "widget" ).find( ".ui-spinner-down" ).mousedown().mouseup();

	shouldStop( true, "stepUp" );
	element.spinner( "stepUp" );
	shouldStop( true, "stepDown" );
	element.spinner( "stepDown" );

	shouldStop( true, "pageUp" );
	element.spinner( "pageUp" );
	shouldStop( true, "pageDown" );
	element.spinner( "pageDown" );

	shouldStop( false, "value" );
	element.spinner( "value", 999 );
});

asyncTest( "change", function() {
	expect( 14 );
	var element = $( "#spin" ).spinner();

	function shouldChange( expectation, msg ) {
		element.spinner( "option", "change", function() {
			ok( expectation, msg );
		});
	}

	function focusWrap( fn, next ) {
		element[0].focus();
		setTimeout( function() {
			fn();
			setTimeout(function() {
				element[0].blur();
				setTimeout( next );
			});
		});
	}

	function step1() {
		focusWrap(function() {
			shouldChange( false, "key UP, before blur" );
			simulateKeyDownUp( element, $.ui.keyCode.UP );
			shouldChange( true, "blur after key UP" );
		}, step2 );
	}

	function step2() {
		focusWrap(function() {
			shouldChange( false, "key DOWN, before blur" );
			simulateKeyDownUp( element, $.ui.keyCode.DOWN );
			shouldChange( true, "blur after key DOWN" );
		}, step3 );
	}

	function step3() {
		focusWrap(function() {
			shouldChange( false, "key PAGE_UP, before blur" );
			simulateKeyDownUp( element, $.ui.keyCode.PAGE_UP );
			shouldChange( true, "blur after key PAGE_UP" );
		}, step4 );
	}

	function step4() {
		focusWrap(function() {
			shouldChange( false, "key PAGE_DOWN, before blur" );
			simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
			shouldChange( true, "blur after key PAGE_DOWN" );
		}, step5 );
	}

	function step5() {
		focusWrap(function() {
			shouldChange( false, "many keys, before blur" );
			simulateKeyDownUp( element, $.ui.keyCode.PAGE_DOWN );
			simulateKeyDownUp( element, $.ui.keyCode.UP );
			simulateKeyDownUp( element, $.ui.keyCode.UP );
			simulateKeyDownUp( element, $.ui.keyCode.UP );
			simulateKeyDownUp( element, $.ui.keyCode.DOWN );
			shouldChange( true, "blur after many keys" );
		}, step6 );
	}

	function step6() {
		focusWrap(function() {
			shouldChange( false, "many keys, same final value, before blur" );
			simulateKeyDownUp( element, $.ui.keyCode.UP );
			simulateKeyDownUp( element, $.ui.keyCode.UP );
			simulateKeyDownUp( element, $.ui.keyCode.DOWN );
			simulateKeyDownUp( element, $.ui.keyCode.DOWN );
			shouldChange( false, "blur after many keys, same final value" );

			shouldChange( false, "button up, before blur" );
			element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown().mouseup();
			shouldChange( true, "blur after button up" );
		}, step7 );
	}

	function step7() {
		focusWrap(function() {
			shouldChange( false, "button down, before blur" );
			element.spinner( "widget" ).find( ".ui-spinner-down" ).mousedown().mouseup();
			shouldChange( true, "blur after button down" );
		}, step8 );
	}

	function step8() {
		focusWrap(function() {
			shouldChange( false, "many buttons, same final value, before blur" );
			element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown().mouseup();
			element.spinner( "widget" ).find( ".ui-spinner-up" ).mousedown().mouseup();
			element.spinner( "widget" ).find( ".ui-spinner-down" ).mousedown().mouseup();
			element.spinner( "widget" ).find( ".ui-spinner-down" ).mousedown().mouseup();
			shouldChange( false, "blur after many buttons, same final value" );
		}, step9 );
	}

	function step9() {
		shouldChange( true, "stepUp" );
		element.spinner( "stepUp" );

		shouldChange( true, "stepDown" );
		element.spinner( "stepDown" );

		shouldChange( true, "pageUp" );
		element.spinner( "pageUp" );

		shouldChange( true, "pageDown" );
		element.spinner( "pageDown" );

		shouldChange( true, "value" );
		element.spinner( "value", 999 );

		shouldChange( false, "value, same value" );
		element.spinner( "value", 999 );

		shouldChange( true, "max, value changed" );
		element.spinner( "option", "max", 900 );

		shouldChange( false, "max, value not changed" );
		element.spinner( "option", "max", 1000 );

		shouldChange( true, "min, value changed" );
		element.spinner( "option", "min", 950 );

		shouldChange( false, "min, value not changed" );
		element.spinner( "option", "min", 200 );
		start();
	}

	setTimeout( step1 );
});

})( jQuery );