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