Show More
@@ -1,99 +1,120 b'' | |||||
1 | // |
|
1 | // | |
2 | // Test the widget framework. |
|
2 | // Test the widget framework. | |
3 | // |
|
3 | // | |
4 | casper.notebook_test(function () { |
|
4 | casper.notebook_test(function () { | |
5 | //this.test.begin("widget tests (notebook)", 2, function(test) { |
|
5 | //this.test.begin("widget tests (notebook)", 2, function(test) { | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | // Utility function that allows us to easily execute a cell of python code |
|
8 | // Utility function that allows us to easily execute a cell of python code | |
9 | // and wait for the results. |
|
9 | // and wait for the results. | |
10 | var that = this; |
|
10 | var that = this; | |
11 | var run_python_code = function(code){ |
|
11 | var run_python_code = function(code){ | |
12 | var index = that.evaluate(function (code) { |
|
12 | var index = that.evaluate(function (code) { | |
13 | var index = IPython.notebook.ncells(); |
|
13 | var index = IPython.notebook.ncells(); | |
14 | var cell = IPython.notebook.insert_cell_at_index('code', index); |
|
14 | var cell = IPython.notebook.insert_cell_at_index('code', index); | |
15 | cell.set_text(code); |
|
15 | cell.set_text(code); | |
16 | cell.execute(); |
|
16 | cell.execute(); | |
17 | return index; |
|
17 | return index; | |
18 | }, code); |
|
18 | }, code); | |
19 |
|
19 | |||
20 | that.wait_for_output(index); |
|
20 | that.wait_for_output(index); | |
21 | return index; |
|
21 | return index; | |
22 | }; |
|
22 | }; | |
23 |
|
23 | |||
24 | // Test widget dependencies //////////////////////////////////////////////// |
|
24 | // Test widget dependencies //////////////////////////////////////////////// | |
25 | this.then(function () { |
|
25 | this.then(function () { | |
26 |
|
26 | |||
27 | // Check if the WidgetManager class is defined. |
|
27 | // Check if the WidgetManager class is defined. | |
28 | this.test.assert(this.evaluate(function() { |
|
28 | this.test.assert(this.evaluate(function() { | |
29 | return IPython.WidgetManager != undefined; |
|
29 | return IPython.WidgetManager != undefined; | |
30 | }), 'WidgetManager class is defined'); |
|
30 | }), 'WidgetManager class is defined'); | |
31 | }); |
|
31 | }); | |
32 |
|
32 | |||
33 | run_python_code('from IPython.html import widgets\n' + |
|
33 | run_python_code('from IPython.html import widgets\n' + | |
34 | 'from IPython.display import display, clear_output\n' + |
|
34 | 'from IPython.display import display, clear_output\n' + | |
35 | 'print("Success")'); |
|
35 | 'print("Success")'); | |
36 | this.wait(500); // Wait for require.js async callbacks to load dependencies. |
|
36 | this.wait(500); // Wait for require.js async callbacks to load dependencies. | |
37 |
|
37 | |||
38 | this.then(function () { |
|
38 | this.then(function () { | |
39 | // Check if the widget manager has been instanciated. |
|
39 | // Check if the widget manager has been instanciated. | |
40 | this.test.assert(this.evaluate(function() { |
|
40 | this.test.assert(this.evaluate(function() { | |
41 | return IPython.widget_manager != undefined; |
|
41 | return IPython.widget_manager != undefined; | |
42 | }), 'Notebook widget manager instanciated'); |
|
42 | }), 'Notebook widget manager instanciated'); | |
43 | }); |
|
43 | }); | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | // Check widget mapping //////////////////////////////////////////////////// |
|
46 | // Check widget mapping //////////////////////////////////////////////////// | |
47 | var cell_index = run_python_code('names = [name for name in dir(widgets)' + |
|
47 | var cell_index = run_python_code('names = [name for name in dir(widgets)' + | |
48 | ' if name.endswith("Widget") and name!= "Widget"]\n' + |
|
48 | ' if name.endswith("Widget") and name!= "Widget"]\n' + | |
49 | 'for name in names:\n' + |
|
49 | 'for name in names:\n' + | |
50 | ' print(name)\n'); |
|
50 | ' print(name)\n'); | |
51 |
|
51 | |||
52 | this.then(function () { |
|
52 | this.then(function () { | |
53 | // Get the widget names that are registered with the widget manager. Assume |
|
53 | // Get the widget names that are registered with the widget manager. Assume | |
54 | // a 1 to 1 mapping of model and widgets names (model names just have 'model' |
|
54 | // a 1 to 1 mapping of model and widgets names (model names just have 'model' | |
55 | // suffixed). |
|
55 | // suffixed). | |
56 | var javascript_names = this.evaluate(function () { |
|
56 | var javascript_names = this.evaluate(function () { | |
57 | names = []; |
|
57 | names = []; | |
58 | for (var name in IPython.widget_manager.widget_model_types) { |
|
58 | for (var name in IPython.widget_manager.widget_model_types) { | |
59 | names.push(name.replace('Model','')); |
|
59 | names.push(name.replace('Model','')); | |
60 | } |
|
60 | } | |
61 | return names; |
|
61 | return names; | |
62 | }); |
|
62 | }); | |
63 |
|
63 | |||
64 | // Get the widget names registered in python. |
|
64 | // Get the widget names registered in python. | |
65 | var python_names = this.get_output_cell(cell_index).text.split('\n'); |
|
65 | var python_names = this.get_output_cell(cell_index).text.split('\n'); | |
66 |
|
66 | |||
67 | // Make sure the two lists have the same items. |
|
67 | // Make sure the two lists have the same items. | |
68 | for (var i in javascript_names) { |
|
68 | for (var i in javascript_names) { | |
69 | var javascript_name = javascript_names[i]; |
|
69 | var javascript_name = javascript_names[i]; | |
70 | var found = false; |
|
70 | var found = false; | |
71 | for (var j in python_names) { |
|
71 | for (var j in python_names) { | |
72 | var python_name = python_names[j]; |
|
72 | var python_name = python_names[j]; | |
73 | if (python_name==javascript_name) { |
|
73 | if (python_name==javascript_name) { | |
74 | found = true; |
|
74 | found = true; | |
75 | break; |
|
75 | break; | |
76 | } |
|
76 | } | |
77 | } |
|
77 | } | |
78 | this.test.assert(found, javascript_name + ' exists in python'); |
|
78 | this.test.assert(found, javascript_name + ' exists in python'); | |
79 | } |
|
79 | } | |
80 | for (var i in python_names) { |
|
80 | for (var i in python_names) { | |
81 | var python_name = python_names[i]; |
|
81 | var python_name = python_names[i]; | |
82 | if (python_name.length > 0) { |
|
82 | if (python_name.length > 0) { | |
83 | var found = false; |
|
83 | var found = false; | |
84 | for (var j in javascript_names) { |
|
84 | for (var j in javascript_names) { | |
85 | var javascript_name = javascript_names[j]; |
|
85 | var javascript_name = javascript_names[j]; | |
86 | if (python_name==javascript_name) { |
|
86 | if (python_name==javascript_name) { | |
87 | found = true; |
|
87 | found = true; | |
88 | break; |
|
88 | break; | |
89 | } |
|
89 | } | |
90 | } |
|
90 | } | |
91 | this.test.assert(found, python_name + ' exists in javascript'); |
|
91 | this.test.assert(found, python_name + ' exists in javascript'); | |
92 | } |
|
92 | } | |
93 | } |
|
93 | } | |
|
94 | ||||
|
95 | // Try to create a button widget | |||
|
96 | cell_index = run_python_code('button = widgets.ButtonWidget()\n' + | |||
|
97 | 'display(button)\n'+ | |||
|
98 | 'print("Success")'); | |||
|
99 | ||||
|
100 | this.then(function () { | |||
|
101 | ||||
|
102 | // Check if the WidgetManager class is defined. | |||
|
103 | var $widget_subarea = this.evaluate(function() { | |||
|
104 | var $cell = IPython.notebook.get_cell_element(cell_index); | |||
|
105 | return $cell.find('.widget-area.widget-subarea'); | |||
|
106 | }); | |||
|
107 | ||||
|
108 | // Make sure the widget subarea was found. | |||
|
109 | this.test.assert($widget_subarea.length > 0, 'Create button widget, widget subarea exist?'); | |||
|
110 | ||||
|
111 | var $widget_button = $widget_subarea.find('button'); | |||
|
112 | this.test.assert($$widget_button.length > 0, 'Create button widget, widget button exist?'); | |||
|
113 | } | |||
|
114 | ||||
94 | }); |
|
115 | }); | |
95 |
|
116 | |||
96 |
|
117 | |||
97 | //}); // end of test.begin |
|
118 | //}); // end of test.begin | |
98 | }); |
|
119 | }); | |
99 |
|
120 |
General Comments 0
You need to be logged in to leave comments.
Login now