Show More
@@ -1,171 +1,179 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 | 'bool_widget = widgets.BoolWidget(description="Title")\n' + | |
|
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 | 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 | 106 | this.test.assert(this.cell_element_exists(index, |
|
103 | 107 | '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel'), |
|
104 | 108 | 'Checkbox label exists.'); |
|
105 | 109 | |
|
106 | 110 | this.test.assert(this.cell_element_function(index, |
|
107 | 111 | '.widget-area .widget-subarea .widget-hbox-single .widget-hlabel', 'html')=="Title", |
|
108 | 112 | 'Checkbox labeled correctly.'); |
|
109 | 113 | |
|
110 | 114 | this.test.assert(this.cell_element_exists(index, |
|
111 | 115 | '.widget-area .widget-subarea div button'), |
|
112 | 116 | 'Toggle button exists.'); |
|
113 | 117 | |
|
114 | 118 | this.test.assert(this.cell_element_function(index, |
|
115 | 119 | '.widget-area .widget-subarea div button', 'html')=="Title", |
|
116 | 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 | 128 | // Test button widget ////////////////////////////////////////////////////// |
|
121 | 129 | var button_index = this.append_cell( |
|
122 | 130 | 'button = widgets.ButtonWidget(description="Title")\n' + |
|
123 | 131 | 'display(button)\n'+ |
|
124 | 132 | 'print("Success")\n' + |
|
125 | 133 | 'def handle_click(sender):\n' + |
|
126 | 134 | ' print("Clicked")\n' + |
|
127 | 135 | 'button.on_click(handle_click)'); |
|
128 | 136 | this.execute_cell_then(button_index, function(index){ |
|
129 | 137 | |
|
130 | 138 | var button_output = this.get_output_cell(index).text; |
|
131 | 139 | this.test.assert(button_output == 'Success\n', |
|
132 | 140 | 'Create button cell executed with correct output.'); |
|
133 | 141 | |
|
134 | 142 | this.test.assert(this.cell_element_exists(index, |
|
135 | 143 | '.widget-area .widget-subarea'), |
|
136 | 144 | 'Widget subarea exists.'); |
|
137 | 145 | |
|
138 | 146 | this.test.assert(this.cell_element_exists(index, |
|
139 | 147 | '.widget-area .widget-subarea button'), |
|
140 | 148 | 'Widget button exists.'); |
|
141 | 149 | |
|
142 | 150 | this.test.assert(this.cell_element_function(index, |
|
143 | 151 | '.widget-area .widget-subarea button', 'html')=='Title', |
|
144 | 152 | 'Set button description.'); |
|
145 | 153 | |
|
146 | 154 | this.cell_element_function(index, |
|
147 | 155 | '.widget-area .widget-subarea button', 'click'); |
|
148 | 156 | }); |
|
149 | 157 | |
|
150 | 158 | this.wait(500); // Wait for click to execute in kernel and write output |
|
151 | 159 | |
|
152 | 160 | this.then(function () { |
|
153 | 161 | this.test.assert(this.get_output_cell(button_index, 1).text == 'Clicked\n', |
|
154 | 162 | 'Button click event fires.'); |
|
155 | 163 | }); |
|
156 | 164 | |
|
157 | 165 | index = this.append_cell( |
|
158 | 166 | 'button.close()\n'+ |
|
159 | 167 | 'print("Success")\n'); |
|
160 | 168 | this.execute_cell_then(index, function(index){ |
|
161 | 169 | |
|
162 | 170 | var button_output = this.get_output_cell(index).text; |
|
163 | 171 | this.test.assert(button_output == 'Success\n', |
|
164 | 172 | 'Close button cell executed with correct output.'); |
|
165 | 173 | |
|
166 | 174 | this.test.assert(! this.cell_element_exists(button_index, |
|
167 | 175 | '.widget-area .widget-subarea button'), |
|
168 | 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