##// END OF EJS Templates
Added bool widget value toggle tests
Jonathan Frederic -
Show More
@@ -1,179 +1,220 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 var button_output = this.get_output_cell(index).text;
91 91 this.test.assert(button_output == 'Success\n',
92 92 'Create bool widget cell executed with correct output.');
93 93
94 94 this.test.assert(this.cell_element_exists(index,
95 95 '.widget-area .widget-subarea'),
96 96 'Widget subarea exists.');
97 97
98 98 this.test.assert(this.cell_element_exists(index,
99 99 '.widget-area .widget-subarea .widget-hbox-single input'),
100 100 'Checkbox exists.');
101 101
102 102 this.test.assert(this.cell_element_function(index,
103 '.widget-area .widget-subarea .widget-hbox-single input', 'val')==true,
103 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
104 104 'Checkbox is checked.');
105 105
106 106 this.test.assert(this.cell_element_exists(index,
107 107 '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel'),
108 108 'Checkbox label exists.');
109 109
110 110 this.test.assert(this.cell_element_function(index,
111 111 '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel', 'html')=="Title",
112 112 'Checkbox labeled correctly.');
113 113
114 114 this.test.assert(this.cell_element_exists(index,
115 115 '.widget-area .widget-subarea div button'),
116 116 'Toggle button exists.');
117 117
118 118 this.test.assert(this.cell_element_function(index,
119 119 '.widget-area .widget-subarea div button', 'html')=="Title",
120 120 'Toggle button labeled correctly.');
121 121
122 122 this.test.assert(this.cell_element_function(index,
123 123 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
124 124 'Toggle button is toggled.');
125 125
126 126 });
127 127
128 index = this.append_cell(
129 'bool_widget.value = False\n' +
130 'print("Success")');
131 this.execute_cell_then(index, function(index){
132
133 var button_output = this.get_output_cell(index).text;
134 this.test.assert(button_output == 'Success\n',
135 'Change bool widget value cell executed with correct output.');
136
137 this.test.assert(!this.cell_element_function(bool_index,
138 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
139 'Checkbox is not checked. (1)');
140
141 this.test.assert(!this.cell_element_function(bool_index,
142 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
143 'Toggle button is not toggled. (1)');
144
145 // Try toggling the bool by clicking on the toggle button.
146 this.cell_element_function(bool_index, '.widget-area .widget-subarea div button', 'click');
147
148 this.test.assert(this.cell_element_function(bool_index,
149 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
150 'Checkbox is checked. (2)');
151
152 this.test.assert(this.cell_element_function(bool_index,
153 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
154 'Toggle button is toggled. (2)');
155
156 // Try toggling the bool by clicking on the checkbox.
157 this.cell_element_function(bool_index, '.widget-area .widget-subarea .widget-hbox-single input', 'click');
158
159 this.test.assert(!this.cell_element_function(bool_index,
160 '.widget-area .widget-subarea .widget-hbox-single input', 'prop', ['checked']),
161 'Checkbox is not checked. (3)');
162
163 this.test.assert(!this.cell_element_function(bool_index,
164 '.widget-area .widget-subarea div button', 'hasClass', ['active']),
165 'Toggle button is not toggled. (3)');
166
167 });
168
128 169 // Test button widget //////////////////////////////////////////////////////
129 170 var button_index = this.append_cell(
130 171 'button = widgets.ButtonWidget(description="Title")\n' +
131 172 'display(button)\n'+
132 173 'print("Success")\n' +
133 174 'def handle_click(sender):\n' +
134 175 ' print("Clicked")\n' +
135 176 'button.on_click(handle_click)');
136 177 this.execute_cell_then(button_index, function(index){
137 178
138 179 var button_output = this.get_output_cell(index).text;
139 180 this.test.assert(button_output == 'Success\n',
140 181 'Create button cell executed with correct output.');
141 182
142 183 this.test.assert(this.cell_element_exists(index,
143 184 '.widget-area .widget-subarea'),
144 185 'Widget subarea exists.');
145 186
146 187 this.test.assert(this.cell_element_exists(index,
147 188 '.widget-area .widget-subarea button'),
148 189 'Widget button exists.');
149 190
150 191 this.test.assert(this.cell_element_function(index,
151 192 '.widget-area .widget-subarea button', 'html')=='Title',
152 193 'Set button description.');
153 194
154 195 this.cell_element_function(index,
155 196 '.widget-area .widget-subarea button', 'click');
156 197 });
157 198
158 199 this.wait(500); // Wait for click to execute in kernel and write output
159 200
160 201 this.then(function () {
161 202 this.test.assert(this.get_output_cell(button_index, 1).text == 'Clicked\n',
162 203 'Button click event fires.');
163 204 });
164 205
165 206 index = this.append_cell(
166 207 'button.close()\n'+
167 208 'print("Success")\n');
168 209 this.execute_cell_then(index, function(index){
169 210
170 211 var button_output = this.get_output_cell(index).text;
171 212 this.test.assert(button_output == 'Success\n',
172 213 'Close button cell executed with correct output.');
173 214
174 215 this.test.assert(! this.cell_element_exists(button_index,
175 216 '.widget-area .widget-subarea button'),
176 217 'Widget button doesn\'t exists.');
177 218 });
178 219 });
179 220
General Comments 0
You need to be logged in to leave comments. Login now