notebook/IPython/html/tests/casperjs/test_cases/widgets.js

73 lines
2.7 KiB
JavaScript
Raw Normal View History

2013-11-01 03:42:08 +08:00
// Test the widget framework.
casper.notebook_test(function () {
var index;
2013-11-01 03:42:08 +08:00
this.then(function () {
// Check if the WidgetManager class is defined.
this.test.assert(this.evaluate(function() {
2014-01-27 09:01:59 +08:00
return IPython.WidgetManager !== undefined;
2013-11-01 03:42:08 +08:00
}), 'WidgetManager class is defined');
});
index = this.append_cell(
'from IPython.html import widgets\n' +
'from IPython.display import display, clear_output\n' +
'print("Success")');
this.execute_cell_then(index);
this.then(function () {
2014-01-27 09:01:59 +08:00
// Check if the widget manager has been instantiated.
2013-11-01 03:42:08 +08:00
this.test.assert(this.evaluate(function() {
2014-01-27 09:01:59 +08:00
return IPython.notebook.kernel.widget_manager !== undefined;
}), 'Notebook widget manager instantiated');
2013-11-01 03:42:08 +08:00
});
2013-12-13 02:26:44 +08:00
throttle_index = this.append_cell(
'import time\n' +
'textbox = widgets.TextWidget()\n' +
2014-01-30 09:04:46 +08:00
'display(textbox)\n' +
2013-12-13 02:26:44 +08:00
'textbox.add_class("my-throttle-textbox")\n' +
'def handle_change(name, old, new):\n' +
' print(len(new))\n' +
' time.sleep(0.5)\n' +
2014-01-27 09:01:59 +08:00
'textbox.on_trait_change(handle_change, "value")\n' +
2013-12-13 02:26:44 +08:00
'print("Success")');
this.execute_cell_then(throttle_index, function(index){
2014-01-27 09:01:59 +08:00
this.test.assertEquals(this.get_output_cell(index).text, 'Success\n',
2013-12-13 02:26:44 +08:00
'Test throttling cell executed with correct output');
this.test.assert(this.cell_element_exists(index,
'.widget-area .widget-subarea'),
'Widget subarea exists.');
this.test.assert(this.cell_element_exists(index,
'.my-throttle-textbox'), 'Textbox exists.');
// Send 20 characters
this.sendKeys('.my-throttle-textbox', '....................');
2013-12-13 02:26:44 +08:00
});
2014-01-30 09:04:46 +08:00
this.waitFor(function check() {
var outputs = this.evaluate(function(i) {
return IPython.notebook.get_cell(i).output_area.outputs;
}, {i : throttle_index});
var output = outputs[outputs.length-1].text.trim();
return (output == '20');
2014-01-30 09:04:46 +08:00
}, function then() {
2014-01-27 09:01:59 +08:00
var outputs = this.evaluate(function(i) {
return IPython.notebook.get_cell(i).output_area.outputs;
}, {i : throttle_index});
2013-12-13 02:26:44 +08:00
// Only 4 outputs should have printed, but because of timing, sometimes
// 5 outputs will print. All we need to do is verify num outputs <= 5
// because that is much less than 20.
2014-01-27 09:01:59 +08:00
this.test.assert(outputs.length <= 5, 'Messages throttled.');
2013-12-13 02:26:44 +08:00
// We also need to verify that the last state sent was correct.
2014-01-27 09:01:59 +08:00
var last_state = outputs[outputs.length-1].text;
this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
2014-01-18 00:32:58 +08:00
});
2013-12-11 04:14:08 +08:00
});