mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-24 12:05:22 +08:00
172 lines
6.3 KiB
JavaScript
172 lines
6.3 KiB
JavaScript
// Test the widget framework.
|
|
casper.notebook_test(function () {
|
|
var index;
|
|
|
|
// Test widget dependencies ////////////////////////////////////////////////
|
|
this.then(function () {
|
|
|
|
// Check if the WidgetManager class is defined.
|
|
this.test.assert(this.evaluate(function() {
|
|
return IPython.WidgetManager != undefined;
|
|
}), '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.wait(500); // Wait for require.js async callbacks to load dependencies.
|
|
|
|
this.then(function () {
|
|
// Check if the widget manager has been instanciated.
|
|
this.test.assert(this.evaluate(function() {
|
|
return IPython.widget_manager != undefined;
|
|
}), 'Notebook widget manager instanciated');
|
|
});
|
|
|
|
|
|
// Check widget mapping ////////////////////////////////////////////////////
|
|
index = this.append_cell(
|
|
'names = [name for name in dir(widgets)' +
|
|
' if name.endswith("Widget") and name!= "Widget"]\n' +
|
|
'for name in names:\n' +
|
|
' print(name)\n');
|
|
this.execute_cell_then(index, function(index){
|
|
|
|
// Get the widget names that are registered with the widget manager. Assume
|
|
// a 1 to 1 mapping of model and widgets names (model names just have 'model'
|
|
// suffixed).
|
|
var javascript_names = this.evaluate(function () {
|
|
names = [];
|
|
for (var name in IPython.widget_manager.widget_model_types) {
|
|
names.push(name.replace('Model',''));
|
|
}
|
|
return names;
|
|
});
|
|
|
|
// Get the widget names registered in python.
|
|
var python_names = this.get_output_cell(index).text.split('\n');
|
|
|
|
// Make sure the two lists have the same items.
|
|
for (var i in javascript_names) {
|
|
var javascript_name = javascript_names[i];
|
|
var found = false;
|
|
for (var j in python_names) {
|
|
var python_name = python_names[j];
|
|
if (python_name==javascript_name) {
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
this.test.assert(found, javascript_name + ' exists in python');
|
|
}
|
|
for (var i in python_names) {
|
|
var python_name = python_names[i];
|
|
if (python_name.length > 0) {
|
|
var found = false;
|
|
for (var j in javascript_names) {
|
|
var javascript_name = javascript_names[j];
|
|
if (python_name==javascript_name) {
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
this.test.assert(found, python_name + ' exists in javascript');
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
// Test bool widget ////////////////////////////////////////////////////////
|
|
var bool_index = this.append_cell(
|
|
'bool_widget = widgets.BoolWidget(description="Title")\n' +
|
|
'display(bool_widget)\n'+
|
|
'display(bool_widget, view_name="ToggleButtonView")\n' +
|
|
'print("Success")');
|
|
this.execute_cell_then(bool_index, function(index){
|
|
|
|
var button_output = this.get_output_cell(index).text;
|
|
this.test.assert(button_output == 'Success\n',
|
|
'Create bool widget 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,
|
|
'.widget-area .widget-subarea .widget-hbox-single input'),
|
|
'Checkbox exists.');
|
|
|
|
this.test.assert(this.cell_element_exists(index,
|
|
'.widget-area .widget-subarea .widget-hbox-single .widget-hlabel'),
|
|
'Checkbox label exists.');
|
|
|
|
this.test.assert(this.cell_element_function(index,
|
|
'.widget-area .widget-subarea .widget-hbox-single .widget-hlabel', 'html')=="Title",
|
|
'Checkbox labeled correctly.');
|
|
|
|
this.test.assert(this.cell_element_exists(index,
|
|
'.widget-area .widget-subarea div button'),
|
|
'Toggle button exists.');
|
|
|
|
this.test.assert(this.cell_element_function(index,
|
|
'.widget-area .widget-subarea div button', 'html')=="Title",
|
|
'Toggle button labeled correctly.');
|
|
|
|
});
|
|
|
|
// Test button widget //////////////////////////////////////////////////////
|
|
var button_index = this.append_cell(
|
|
'button = widgets.ButtonWidget(description="Title")\n' +
|
|
'display(button)\n'+
|
|
'print("Success")\n' +
|
|
'def handle_click(sender):\n' +
|
|
' print("Clicked")\n' +
|
|
'button.on_click(handle_click)');
|
|
this.execute_cell_then(button_index, function(index){
|
|
|
|
var button_output = this.get_output_cell(index).text;
|
|
this.test.assert(button_output == 'Success\n',
|
|
'Create button 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,
|
|
'.widget-area .widget-subarea button'),
|
|
'Widget button exists.');
|
|
|
|
this.test.assert(this.cell_element_function(index,
|
|
'.widget-area .widget-subarea button', 'html')=='Title',
|
|
'Set button description.');
|
|
|
|
this.cell_element_function(index,
|
|
'.widget-area .widget-subarea button', 'click');
|
|
});
|
|
|
|
this.wait(500); // Wait for click to execute in kernel and write output
|
|
|
|
this.then(function () {
|
|
this.test.assert(this.get_output_cell(button_index, 1).text == 'Clicked\n',
|
|
'Button click event fires.');
|
|
});
|
|
|
|
index = this.append_cell(
|
|
'button.close()\n'+
|
|
'print("Success")\n');
|
|
this.execute_cell_then(index, function(index){
|
|
|
|
var button_output = this.get_output_cell(index).text;
|
|
this.test.assert(button_output == 'Success\n',
|
|
'Close button cell executed with correct output.');
|
|
|
|
this.test.assert(! this.cell_element_exists(button_index,
|
|
'.widget-area .widget-subarea button'),
|
|
'Widget button doesn\'t exists.');
|
|
});
|
|
});
|
|
|