Show More
@@ -1,171 +1,179 b'' | |||||
1 | // Test the widget framework. |
|
1 | // Test the widget framework. | |
2 | casper.notebook_test(function () { |
|
2 | casper.notebook_test(function () { | |
3 | var index; |
|
3 | var index; | |
4 |
|
4 | |||
5 | // Test widget dependencies //////////////////////////////////////////////// |
|
5 | // Test widget dependencies //////////////////////////////////////////////// | |
6 | this.then(function () { |
|
6 | this.then(function () { | |
7 |
|
7 | |||
8 | // Check if the WidgetManager class is defined. |
|
8 | // Check if the WidgetManager class is defined. | |
9 | this.test.assert(this.evaluate(function() { |
|
9 | this.test.assert(this.evaluate(function() { | |
10 | return IPython.WidgetManager != undefined; |
|
10 | return IPython.WidgetManager != undefined; | |
11 | }), 'WidgetManager class is defined'); |
|
11 | }), 'WidgetManager class is defined'); | |
12 | }); |
|
12 | }); | |
13 |
|
13 | |||
14 | index = this.append_cell( |
|
14 | index = this.append_cell( | |
15 | 'from IPython.html import widgets\n' + |
|
15 | 'from IPython.html import widgets\n' + | |
16 | 'from IPython.display import display, clear_output\n' + |
|
16 | 'from IPython.display import display, clear_output\n' + | |
17 | 'print("Success")'); |
|
17 | 'print("Success")'); | |
18 | this.execute_cell_then(index); |
|
18 | this.execute_cell_then(index); | |
19 |
|
19 | |||
20 | this.wait(500); // Wait for require.js async callbacks to load dependencies. |
|
20 | this.wait(500); // Wait for require.js async callbacks to load dependencies. | |
21 |
|
21 | |||
22 | this.then(function () { |
|
22 | this.then(function () { | |
23 | // Check if the widget manager has been instanciated. |
|
23 | // Check if the widget manager has been instanciated. | |
24 | this.test.assert(this.evaluate(function() { |
|
24 | this.test.assert(this.evaluate(function() { | |
25 | return IPython.widget_manager != undefined; |
|
25 | return IPython.widget_manager != undefined; | |
26 | }), 'Notebook widget manager instanciated'); |
|
26 | }), 'Notebook widget manager instanciated'); | |
27 | }); |
|
27 | }); | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | // Check widget mapping //////////////////////////////////////////////////// |
|
30 | // Check widget mapping //////////////////////////////////////////////////// | |
31 | index = this.append_cell( |
|
31 | index = this.append_cell( | |
32 | 'names = [name for name in dir(widgets)' + |
|
32 | 'names = [name for name in dir(widgets)' + | |
33 | ' if name.endswith("Widget") and name!= "Widget"]\n' + |
|
33 | ' if name.endswith("Widget") and name!= "Widget"]\n' + | |
34 | 'for name in names:\n' + |
|
34 | 'for name in names:\n' + | |
35 | ' print(name)\n'); |
|
35 | ' print(name)\n'); | |
36 | this.execute_cell_then(index, function(index){ |
|
36 | this.execute_cell_then(index, function(index){ | |
37 |
|
37 | |||
38 | // Get the widget names that are registered with the widget manager. Assume |
|
38 | // Get the widget names that are registered with the widget manager. Assume | |
39 | // a 1 to 1 mapping of model and widgets names (model names just have 'model' |
|
39 | // a 1 to 1 mapping of model and widgets names (model names just have 'model' | |
40 | // suffixed). |
|
40 | // suffixed). | |
41 | var javascript_names = this.evaluate(function () { |
|
41 | var javascript_names = this.evaluate(function () { | |
42 | names = []; |
|
42 | names = []; | |
43 | for (var name in IPython.widget_manager.widget_model_types) { |
|
43 | for (var name in IPython.widget_manager.widget_model_types) { | |
44 | names.push(name.replace('Model','')); |
|
44 | names.push(name.replace('Model','')); | |
45 | } |
|
45 | } | |
46 | return names; |
|
46 | return names; | |
47 | }); |
|
47 | }); | |
48 |
|
48 | |||
49 | // Get the widget names registered in python. |
|
49 | // Get the widget names registered in python. | |
50 | var python_names = this.get_output_cell(index).text.split('\n'); |
|
50 | var python_names = this.get_output_cell(index).text.split('\n'); | |
51 |
|
51 | |||
52 | // Make sure the two lists have the same items. |
|
52 | // Make sure the two lists have the same items. | |
53 | for (var i in javascript_names) { |
|
53 | for (var i in javascript_names) { | |
54 | var javascript_name = javascript_names[i]; |
|
54 | var javascript_name = javascript_names[i]; | |
55 | var found = false; |
|
55 | var found = false; | |
56 | for (var j in python_names) { |
|
56 | for (var j in python_names) { | |
57 | var python_name = python_names[j]; |
|
57 | var python_name = python_names[j]; | |
58 | if (python_name==javascript_name) { |
|
58 | if (python_name==javascript_name) { | |
59 | found = true; |
|
59 | found = true; | |
60 | break; |
|
60 | break; | |
61 | } |
|
61 | } | |
62 | } |
|
62 | } | |
63 | this.test.assert(found, javascript_name + ' exists in python'); |
|
63 | this.test.assert(found, javascript_name + ' exists in python'); | |
64 | } |
|
64 | } | |
65 | for (var i in python_names) { |
|
65 | for (var i in python_names) { | |
66 | var python_name = python_names[i]; |
|
66 | var python_name = python_names[i]; | |
67 | if (python_name.length > 0) { |
|
67 | if (python_name.length > 0) { | |
68 | var found = false; |
|
68 | var found = false; | |
69 | for (var j in javascript_names) { |
|
69 | for (var j in javascript_names) { | |
70 | var javascript_name = javascript_names[j]; |
|
70 | var javascript_name = javascript_names[j]; | |
71 | if (python_name==javascript_name) { |
|
71 | if (python_name==javascript_name) { | |
72 | found = true; |
|
72 | found = true; | |
73 | break; |
|
73 | break; | |
74 | } |
|
74 | } | |
75 | } |
|
75 | } | |
76 | this.test.assert(found, python_name + ' exists in javascript'); |
|
76 | this.test.assert(found, python_name + ' exists in javascript'); | |
77 | } |
|
77 | } | |
78 | } |
|
78 | } | |
79 | }); |
|
79 | }); | |
80 |
|
80 | |||
81 |
|
81 | |||
82 | // Test bool widget //////////////////////////////////////////////////////// |
|
82 | // Test bool widget //////////////////////////////////////////////////////// | |
83 | var bool_index = this.append_cell( |
|
83 | var bool_index = this.append_cell( | |
84 | 'bool_widget = widgets.BoolWidget(description="Title")\n' + |
|
84 | 'bool_widget = widgets.BoolWidget(description="Title", value=True)\n' + | |
85 | 'display(bool_widget)\n'+ |
|
85 | 'display(bool_widget)\n'+ | |
86 | 'display(bool_widget, view_name="ToggleButtonView")\n' + |
|
86 | 'display(bool_widget, view_name="ToggleButtonView")\n' + | |
87 | 'print("Success")'); |
|
87 | 'print("Success")'); | |
88 | this.execute_cell_then(bool_index, function(index){ |
|
88 | this.execute_cell_then(bool_index, function(index){ | |
89 |
|
89 | |||
90 | var button_output = this.get_output_cell(index).text; |
|
90 | var button_output = this.get_output_cell(index).text; | |
91 | this.test.assert(button_output == 'Success\n', |
|
91 | this.test.assert(button_output == 'Success\n', | |
92 | 'Create bool widget cell executed with correct output.'); |
|
92 | 'Create bool widget cell executed with correct output.'); | |
93 |
|
93 | |||
94 | this.test.assert(this.cell_element_exists(index, |
|
94 | this.test.assert(this.cell_element_exists(index, | |
95 | '.widget-area .widget-subarea'), |
|
95 | '.widget-area .widget-subarea'), | |
96 | 'Widget subarea exists.'); |
|
96 | 'Widget subarea exists.'); | |
97 |
|
97 | |||
98 | this.test.assert(this.cell_element_exists(index, |
|
98 | this.test.assert(this.cell_element_exists(index, | |
99 | '.widget-area .widget-subarea .widget-hbox-single input'), |
|
99 | '.widget-area .widget-subarea .widget-hbox-single input'), | |
100 | 'Checkbox exists.'); |
|
100 | 'Checkbox exists.'); | |
101 |
|
101 | |||
|
102 | this.test.assert(this.cell_element_function(index, | |||
|
103 | '.widget-area .widget-subarea .widget-hbox-single input', 'val')==true, | |||
|
104 | 'Checkbox is checked.'); | |||
|
105 | ||||
102 | this.test.assert(this.cell_element_exists(index, |
|
106 | this.test.assert(this.cell_element_exists(index, | |
103 | '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel'), |
|
107 | '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel'), | |
104 | 'Checkbox label exists.'); |
|
108 | 'Checkbox label exists.'); | |
105 |
|
109 | |||
106 | this.test.assert(this.cell_element_function(index, |
|
110 | this.test.assert(this.cell_element_function(index, | |
107 | '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel', 'html')=="Title", |
|
111 | '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel', 'html')=="Title", | |
108 | 'Checkbox labeled correctly.'); |
|
112 | 'Checkbox labeled correctly.'); | |
109 |
|
113 | |||
110 | this.test.assert(this.cell_element_exists(index, |
|
114 | this.test.assert(this.cell_element_exists(index, | |
111 | '.widget-area .widget-subarea div button'), |
|
115 | '.widget-area .widget-subarea div button'), | |
112 | 'Toggle button exists.'); |
|
116 | 'Toggle button exists.'); | |
113 |
|
117 | |||
114 | this.test.assert(this.cell_element_function(index, |
|
118 | this.test.assert(this.cell_element_function(index, | |
115 | '.widget-area .widget-subarea div button', 'html')=="Title", |
|
119 | '.widget-area .widget-subarea div button', 'html')=="Title", | |
116 | 'Toggle button labeled correctly.'); |
|
120 | 'Toggle button labeled correctly.'); | |
117 |
|
121 | |||
|
122 | this.test.assert(this.cell_element_function(index, | |||
|
123 | '.widget-area .widget-subarea div button', 'hasClass', ['active']), | |||
|
124 | 'Toggle button is toggled.'); | |||
|
125 | ||||
118 | }); |
|
126 | }); | |
119 |
|
127 | |||
120 | // Test button widget ////////////////////////////////////////////////////// |
|
128 | // Test button widget ////////////////////////////////////////////////////// | |
121 | var button_index = this.append_cell( |
|
129 | var button_index = this.append_cell( | |
122 | 'button = widgets.ButtonWidget(description="Title")\n' + |
|
130 | 'button = widgets.ButtonWidget(description="Title")\n' + | |
123 | 'display(button)\n'+ |
|
131 | 'display(button)\n'+ | |
124 | 'print("Success")\n' + |
|
132 | 'print("Success")\n' + | |
125 | 'def handle_click(sender):\n' + |
|
133 | 'def handle_click(sender):\n' + | |
126 | ' print("Clicked")\n' + |
|
134 | ' print("Clicked")\n' + | |
127 | 'button.on_click(handle_click)'); |
|
135 | 'button.on_click(handle_click)'); | |
128 | this.execute_cell_then(button_index, function(index){ |
|
136 | this.execute_cell_then(button_index, function(index){ | |
129 |
|
137 | |||
130 | var button_output = this.get_output_cell(index).text; |
|
138 | var button_output = this.get_output_cell(index).text; | |
131 | this.test.assert(button_output == 'Success\n', |
|
139 | this.test.assert(button_output == 'Success\n', | |
132 | 'Create button cell executed with correct output.'); |
|
140 | 'Create button cell executed with correct output.'); | |
133 |
|
141 | |||
134 | this.test.assert(this.cell_element_exists(index, |
|
142 | this.test.assert(this.cell_element_exists(index, | |
135 | '.widget-area .widget-subarea'), |
|
143 | '.widget-area .widget-subarea'), | |
136 | 'Widget subarea exists.'); |
|
144 | 'Widget subarea exists.'); | |
137 |
|
145 | |||
138 | this.test.assert(this.cell_element_exists(index, |
|
146 | this.test.assert(this.cell_element_exists(index, | |
139 | '.widget-area .widget-subarea button'), |
|
147 | '.widget-area .widget-subarea button'), | |
140 | 'Widget button exists.'); |
|
148 | 'Widget button exists.'); | |
141 |
|
149 | |||
142 | this.test.assert(this.cell_element_function(index, |
|
150 | this.test.assert(this.cell_element_function(index, | |
143 | '.widget-area .widget-subarea button', 'html')=='Title', |
|
151 | '.widget-area .widget-subarea button', 'html')=='Title', | |
144 | 'Set button description.'); |
|
152 | 'Set button description.'); | |
145 |
|
153 | |||
146 | this.cell_element_function(index, |
|
154 | this.cell_element_function(index, | |
147 | '.widget-area .widget-subarea button', 'click'); |
|
155 | '.widget-area .widget-subarea button', 'click'); | |
148 | }); |
|
156 | }); | |
149 |
|
157 | |||
150 | this.wait(500); // Wait for click to execute in kernel and write output |
|
158 | this.wait(500); // Wait for click to execute in kernel and write output | |
151 |
|
159 | |||
152 | this.then(function () { |
|
160 | this.then(function () { | |
153 | this.test.assert(this.get_output_cell(button_index, 1).text == 'Clicked\n', |
|
161 | this.test.assert(this.get_output_cell(button_index, 1).text == 'Clicked\n', | |
154 | 'Button click event fires.'); |
|
162 | 'Button click event fires.'); | |
155 | }); |
|
163 | }); | |
156 |
|
164 | |||
157 | index = this.append_cell( |
|
165 | index = this.append_cell( | |
158 | 'button.close()\n'+ |
|
166 | 'button.close()\n'+ | |
159 | 'print("Success")\n'); |
|
167 | 'print("Success")\n'); | |
160 | this.execute_cell_then(index, function(index){ |
|
168 | this.execute_cell_then(index, function(index){ | |
161 |
|
169 | |||
162 | var button_output = this.get_output_cell(index).text; |
|
170 | var button_output = this.get_output_cell(index).text; | |
163 | this.test.assert(button_output == 'Success\n', |
|
171 | this.test.assert(button_output == 'Success\n', | |
164 | 'Close button cell executed with correct output.'); |
|
172 | 'Close button cell executed with correct output.'); | |
165 |
|
173 | |||
166 | this.test.assert(! this.cell_element_exists(button_index, |
|
174 | this.test.assert(! this.cell_element_exists(button_index, | |
167 | '.widget-area .widget-subarea button'), |
|
175 | '.widget-area .widget-subarea button'), | |
168 | 'Widget button doesn\'t exists.'); |
|
176 | 'Widget button doesn\'t exists.'); | |
169 | }); |
|
177 | }); | |
170 | }); |
|
178 | }); | |
171 |
|
179 |
General Comments 0
You need to be logged in to leave comments.
Login now