##// END OF EJS Templates
Added float widget tests
Jonathan Frederic -
Show More
@@ -1,374 +1,424 b''
1 1 // Test the widget framework.
2 2 casper.notebook_test(function () {
3 3 var index;
4 4
5 5 // Test widget dependencies ////////////////////////////////////////////////
6 6 this.then(function () {
7 7
8 8 // Check if the WidgetManager class is defined.
9 9 this.test.assert(this.evaluate(function() {
10 10 return IPython.WidgetManager != undefined;
11 11 }), 'WidgetManager class is defined');
12 12 });
13 13
14 14 index = this.append_cell(
15 15 'from IPython.html import widgets\n' +
16 16 'from IPython.display import display, clear_output\n' +
17 17 'print("Success")');
18 18 this.execute_cell_then(index);
19 19
20 20 this.wait(500); // Wait for require.js async callbacks to load dependencies.
21 21
22 22 this.then(function () {
23 23 // Check if the widget manager has been instanciated.
24 24 this.test.assert(this.evaluate(function() {
25 25 return IPython.widget_manager != undefined;
26 26 }), 'Notebook widget manager instanciated');
27 27 });
28 28
29 29
30 30 // Check widget mapping ////////////////////////////////////////////////////
31 31 index = this.append_cell(
32 32 'names = [name for name in dir(widgets)' +
33 33 ' if name.endswith("Widget") and name!= "Widget"]\n' +
34 34 'for name in names:\n' +
35 35 ' print(name)\n');
36 36 this.execute_cell_then(index, function(index){
37 37
38 38 // Get the widget names that are registered with the widget manager. Assume
39 39 // a 1 to 1 mapping of model and widgets names (model names just have 'model'
40 40 // suffixed).
41 41 var javascript_names = this.evaluate(function () {
42 42 names = [];
43 43 for (var name in IPython.widget_manager.widget_model_types) {
44 44 names.push(name.replace('Model',''));
45 45 }
46 46 return names;
47 47 });
48 48
49 49 // Get the widget names registered in python.
50 50 var python_names = this.get_output_cell(index).text.split('\n');
51 51
52 52 // Make sure the two lists have the same items.
53 53 for (var i in javascript_names) {
54 54 var javascript_name = javascript_names[i];
55 55 var found = false;
56 56 for (var j in python_names) {
57 57 var python_name = python_names[j];
58 58 if (python_name==javascript_name) {
59 59 found = true;
60 60 break;
61 61 }
62 62 }
63 63 this.test.assert(found, javascript_name + ' exists in python');
64 64 }
65 65 for (var i in python_names) {
66 66 var python_name = python_names[i];
67 67 if (python_name.length > 0) {
68 68 var found = false;
69 69 for (var j in javascript_names) {
70 70 var javascript_name = javascript_names[j];
71 71 if (python_name==javascript_name) {
72 72 found = true;
73 73 break;
74 74 }
75 75 }
76 76 this.test.assert(found, python_name + ' exists in javascript');
77 77 }
78 78 }
79 79 });
80 80
81 81
82 82 // Test bool widget ////////////////////////////////////////////////////////
83 83 var bool_index = this.append_cell(
84 84 'bool_widget = widgets.BoolWidget(description="Title", value=True)\n' +
85 85 'display(bool_widget)\n'+
86 86 'display(bool_widget, view_name="ToggleButtonView")\n' +
87 87 'print("Success")');
88 88 this.execute_cell_then(bool_index, function(index){
89 89
90 90 this.test.assert(this.get_output_cell(index).text == 'Success\n',
91 91 'Create bool widget cell executed with correct output.');
92 92
93 93 this.test.assert(this.cell_element_exists(index,
94 94 '.widget-area .widget-subarea'),
95 95 'Widget subarea exists.');
96 96
97 97 this.test.assert(this.cell_element_exists(index,
98 98 '.widget-area .widget-subarea .widget-hbox-single input'),
99 99 'Checkbox exists.');
100 100
101 101 this.test.assert(this.cell_element_function(index,
102 102 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
103 103 'Checkbox is checked.');
104 104
105 105 this.test.assert(this.cell_element_exists(index,
106 106 '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel'),
107 107 'Checkbox label exists.');
108 108
109 109 this.test.assert(this.cell_element_function(index,
110 110 '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel', 'html')=="Title",
111 111 'Checkbox labeled correctly.');
112 112
113 113 this.test.assert(this.cell_element_exists(index,
114 114 '.widget-area .widget-subarea div button'),
115 115 'Toggle button exists.');
116 116
117 117 this.test.assert(this.cell_element_function(index,
118 118 '.widget-area .widget-subarea div button', 'html')=="Title",
119 119 'Toggle button labeled correctly.');
120 120
121 121 this.test.assert(this.cell_element_function(index,
122 122 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
123 123 'Toggle button is toggled.');
124 124
125 125 });
126 126
127 127 index = this.append_cell(
128 128 'bool_widget.value = False\n' +
129 129 'print("Success")');
130 130 this.execute_cell_then(index, function(index){
131 131
132 132 this.test.assert(this.get_output_cell(index).text == 'Success\n',
133 133 'Change bool widget value cell executed with correct output.');
134 134
135 135 this.test.assert(! this.cell_element_function(bool_index,
136 136 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
137 137 'Checkbox is not checked. (1)');
138 138
139 139 this.test.assert(! this.cell_element_function(bool_index,
140 140 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
141 141 'Toggle button is not toggled. (1)');
142 142
143 143 // Try toggling the bool by clicking on the toggle button.
144 144 this.cell_element_function(bool_index, '.widget-area .widget-subarea div button', 'click');
145 145
146 146 this.test.assert(this.cell_element_function(bool_index,
147 147 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
148 148 'Checkbox is checked. (2)');
149 149
150 150 this.test.assert(this.cell_element_function(bool_index,
151 151 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
152 152 'Toggle button is toggled. (2)');
153 153
154 154 // Try toggling the bool by clicking on the checkbox.
155 155 this.cell_element_function(bool_index, '.widget-area .widget-subarea .widget-hbox-single input', 'click');
156 156
157 157 this.test.assert(! this.cell_element_function(bool_index,
158 158 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
159 159 'Checkbox is not checked. (3)');
160 160
161 161 this.test.assert(! this.cell_element_function(bool_index,
162 162 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
163 163 'Toggle button is not toggled. (3)');
164 164
165 165 });
166 166
167 167 // Test button widget //////////////////////////////////////////////////////
168 168 var button_index = this.append_cell(
169 169 'button = widgets.ButtonWidget(description="Title")\n' +
170 170 'display(button)\n'+
171 171 'print("Success")\n' +
172 172 'def handle_click(sender):\n' +
173 173 ' print("Clicked")\n' +
174 174 'button.on_click(handle_click)');
175 175 this.execute_cell_then(button_index, function(index){
176 176
177 177 this.test.assert(this.get_output_cell(index).text == 'Success\n',
178 178 'Create button cell executed with correct output.');
179 179
180 180 this.test.assert(this.cell_element_exists(index,
181 181 '.widget-area .widget-subarea'),
182 182 'Widget subarea exists.');
183 183
184 184 this.test.assert(this.cell_element_exists(index,
185 185 '.widget-area .widget-subarea button'),
186 186 'Widget button exists.');
187 187
188 188 this.test.assert(this.cell_element_function(index,
189 189 '.widget-area .widget-subarea button', 'html')=='Title',
190 190 'Set button description.');
191 191
192 192 this.cell_element_function(index,
193 193 '.widget-area .widget-subarea button', 'click');
194 194 });
195 195
196 196 this.wait(500); // Wait for click to execute in kernel and write output
197 197
198 198 this.then(function () {
199 199 this.test.assert(this.get_output_cell(button_index, 1).text == 'Clicked\n',
200 200 'Button click event fires.');
201 201 });
202 202
203 index = this.append_cell(
204 'button.close()\n'+
205 'print("Success")');
206 this.execute_cell_then(index, function(index){
203 // Close the button widget asynchronisly.
204 index = this.append_cell('button.close()\n');
205 this.execute_cell(index);
207 206
208 this.test.assert(this.get_output_cell(index).text == 'Success\n',
209 'Close button cell executed with correct output.');
207 this.wait(500); // Wait for the button to close.
210 208
209 this.then(function(){
211 210 this.test.assert(! this.cell_element_exists(button_index,
212 211 '.widget-area .widget-subarea button'),
213 212 'Widget button doesn\'t exists.');
214 213 });
215 214
216 215 // Test container widget ///////////////////////////////////////////////////
217 216 var container_index = this.append_cell(
218 217 'container = widgets.ContainerWidget()\n' +
219 218 'button = widgets.ButtonWidget(parent=container)\n'+
220 219 'display(container)\n'+
221 220 'container.add_class("my-test-class")\n'+
222 221 'print("Success")\n');
223 222 this.execute_cell_then(container_index, function(index){
224 223
225 224 this.test.assert(this.get_output_cell(index).text == 'Success\n',
226 225 'Create container cell executed with correct output.');
227 226
228 227 this.test.assert(this.cell_element_exists(index,
229 228 '.widget-area .widget-subarea'),
230 229 'Widget subarea exists.');
231 230
232 231 this.test.assert(this.cell_element_exists(index,
233 232 '.widget-area .widget-subarea .widget-container'),
234 233 'Widget container exists.');
235 234
236 235 this.test.assert(this.cell_element_exists(index,
237 236 '.widget-area .widget-subarea .my-test-class'),
238 237 'add_class works.');
239 238
240 239 this.test.assert(this.cell_element_exists(index,
241 240 '.widget-area .widget-subarea .my-test-class button'),
242 241 'Container parent/child relationship works.');
243 242 });
244 243
245 244 index = this.append_cell(
246 245 'container.set_css("display", "none")\n'+
247 246 'print("Success")\n');
248 247 this.execute_cell_then(index, function(index){
249 248
250 249 this.test.assert(this.get_output_cell(index).text == 'Success\n',
251 250 'Set container class CSS cell executed with correct output.');
252 251
253 252 this.test.assert(this.cell_element_function(container_index,
254 253 '.widget-area .widget-subarea .my-test-class', 'css', ['display'])=='none',
255 254 'set_css works.');
256 255 });
257 256
258 257 index = this.append_cell(
259 258 'container.remove_class("my-test-class")\n'+
260 259 'print("Success")\n');
261 260 this.execute_cell_then(index, function(index){
262 261
263 262 this.test.assert(this.get_output_cell(index).text == 'Success\n',
264 263 'Remove container class cell executed with correct output.');
265 264
266 265 this.test.assert(! this.cell_element_exists(container_index,
267 266 '.widget-area .widget-subarea .my-test-class'),
268 267 'remove_class works.');
269 268 });
270 269
271 270 index = this.append_cell(
272 271 'display(button)\n'+
273 272 'print("Success")\n');
274 273 this.execute_cell_then(index, function(index){
275 274
276 275 this.test.assert(this.get_output_cell(index).text == 'Success\n',
277 276 'Display container child executed with correct output.');
278 277
279 278 this.test.assert(! this.cell_element_exists(index,
280 279 '.widget-area .widget-subarea .widget-container'),
281 280 'Parent container not displayed.');
282 281
283 282 this.test.assert(this.cell_element_exists(index,
284 283 '.widget-area .widget-subarea button'),
285 284 'Child displayed.');
286 285 });
287 286
288 287 // Test float range widget /////////////////////////////////////////////////
289 288 var slider_query = '.widget-area .widget-subarea .widget-hbox-single .slider';
290 289 var float_text_query = '.widget-area .widget-subarea .widget-hbox-single .widget-numeric-text';
291 290
292 291 var floatrange_index = this.append_cell(
293 292 'floatrange = widgets.FloatRangeWidget()\n' +
294 293 'display(floatrange)\n' +
295 294 'display(floatrange, view_name="FloatTextView")\n' +
296 295 'print("Success")\n');
297 296 this.execute_cell_then(floatrange_index, function(index){
298 297
299 298 this.test.assert(this.get_output_cell(index).text == 'Success\n',
300 299 'Create float range cell executed with correct output.');
301 300
302 301 this.test.assert(this.cell_element_exists(index,
303 302 '.widget-area .widget-subarea'),
304 303 'Widget subarea exists.');
305 304
306 305 this.test.assert(this.cell_element_exists(index, slider_query),
307 306 'Widget slider exists.');
308 307
309 308 this.test.assert(this.cell_element_exists(index, float_text_query),
310 309 'Widget float textbox exists.');
311 310 });
312 311
313 312 index = this.append_cell(
314 313 'floatrange.max = 50.0\n' +
315 314 'floatrange.min = -50.0\n' +
316 315 'floatrange.value = 25.0\n' +
317 316 'print("Success")\n');
318 317 this.execute_cell_then(index, function(index){
319 318
320 319 this.test.assert(this.get_output_cell(index).text == 'Success\n',
321 320 'Float range properties cell executed with correct output.');
322 321
323 322 this.test.assert(this.cell_element_exists(floatrange_index, slider_query),
324 323 'Widget slider exists.');
325 324
326 325 this.test.assert(this.cell_element_function(floatrange_index, slider_query,
327 326 'slider', ['value']) == 25.0,
328 327 'Slider set to Python value.');
329 328
330 329 this.test.assert(this.cell_element_function(floatrange_index, float_text_query,
331 330 'val') == 25.0, 'Float textbox set to Python value.');
332 331
333 332 // Clear the float textbox value and then set it to 1 by emulating
334 333 // keyboard presses.
335 334 this.cell_element_function(floatrange_index, float_text_query, 'val', ['']);
336 335 this.sendKeys(float_text_query, '1');
337 336 });
338 337
339 this.wait(500); // Wait for slide to execute in kernel
338 this.wait(500); // Wait for change to execute in kernel
340 339
341 340 index = this.append_cell('print(floatrange.value)\n');
342 341 this.execute_cell_then(index, function(index){
343 342 this.test.assert(this.get_output_cell(index).text == '1.0\n',
344 343 'Float textbox set float range value');
345 344
346 345 // Clear the float textbox value and then set it to 120 by emulating
347 346 // keyboard presses.
348 347 this.cell_element_function(floatrange_index, float_text_query, 'val', ['']);
349 348 this.sendKeys(float_text_query, '120');
350 349 });
351 350
352 this.wait(500); // Wait for slide to execute in kernel
351 this.wait(500); // Wait for change to execute in kernel
353 352
354 353 index = this.append_cell('print(floatrange.value)\n');
355 354 this.execute_cell_then(index, function(index){
356 355 this.test.assert(this.get_output_cell(index).text == '50.0\n',
357 356 'Float textbox value bound');
358 357
359 358 // Clear the float textbox value and then set it to 'hello world' by
360 359 // emulating keyboard presses. 'hello world' should get filtered...
361 360 this.cell_element_function(floatrange_index, float_text_query, 'val', ['']);
362 361 this.sendKeys(float_text_query, 'hello world');
363 362 });
364 363
365 this.wait(500); // Wait for slide to execute in kernel
364 this.wait(500); // Wait for change to execute in kernel
366 365
367 366 index = this.append_cell('print(floatrange.value)\n');
368 367 this.execute_cell_then(index, function(index){
369 368 this.test.assert(this.get_output_cell(index).text == '50.0\n',
370 369 'Invalid float textbox characters ignored');
371 370 });
372 371
372 // Test float widget ///////////////////////////////////////////////////////
373 var float_text_query_2 = '.widget-area .widget-subarea .widget-hbox-single .my-second-float-text';
374
375 var float_index = this.append_cell(
376 'float_widget = widgets.FloatWidget()\n' +
377 'display(float_widget)\n' +
378 'float_widget.add_class("my-second-float-text")\n' +
379 'print("Success")\n');
380 this.execute_cell_then(float_index, function(index){
381
382 this.test.assert(this.get_output_cell(index).text == 'Success\n',
383 'Create float cell executed with correct output.');
384
385 this.test.assert(this.cell_element_exists(index,
386 '.widget-area .widget-subarea'),
387 'Widget subarea exists.');
388
389 this.test.assert(this.cell_element_exists(index, float_text_query_2),
390 'Widget float textbox exists.');
391
392 this.cell_element_function(float_index, float_text_query_2, 'val', ['']);
393 this.sendKeys(float_text_query_2, '1.05');
394 });
395
396 this.wait(500); // Wait for change to execute in kernel
397
398 index = this.append_cell('print(float_widget.value)\n');
399 this.execute_cell_then(index, function(index){
400 this.test.assert(this.get_output_cell(index).text == '1.05\n',
401 'Float textbox value set.');
402 this.cell_element_function(float_index, float_text_query_2, 'val', ['']);
403 this.sendKeys(float_text_query_2, '123456789.0');
404 });
405
406 this.wait(500); // Wait for change to execute in kernel
407
408 index = this.append_cell('print(float_widget.value)\n');
409 this.execute_cell_then(index, function(index){
410 this.test.assert(this.get_output_cell(index).text == '123456789.0\n',
411 'Long float textbox value set (probably triggers throttling).');
412 this.cell_element_function(float_index, float_text_query_2, 'val', ['']);
413 this.sendKeys(float_text_query_2, '12hello');
414 });
415
416 this.wait(500); // Wait for change to execute in kernel
417
418 index = this.append_cell('print(float_widget.value)\n');
419 this.execute_cell_then(index, function(index){
420 this.test.assert(this.get_output_cell(index).text == '12.0\n',
421 'Float textbox value set.');
422 });
373 423 });
374 424
General Comments 0
You need to be logged in to leave comments. Login now