Show More
@@ -0,0 +1,98 | |||
|
1 | // | |
|
2 | // Various output tests | |
|
3 | // | |
|
4 | ||
|
5 | casper.notebook_test(function () { | |
|
6 | ||
|
7 | this.test_coalesced_output = function (msg, code, expected) { | |
|
8 | this.then(function () { | |
|
9 | this.echo("Test coalesced output: " + msg); | |
|
10 | }); | |
|
11 | ||
|
12 | this.thenEvaluate(function (code) { | |
|
13 | IPython.notebook.insert_cell_at_index(0, "code"); | |
|
14 | var cell = IPython.notebook.get_cell(0); | |
|
15 | cell.set_text(code); | |
|
16 | cell.execute(); | |
|
17 | }, {code: code}); | |
|
18 | ||
|
19 | this.wait_for_output(0); | |
|
20 | ||
|
21 | this.then(function () { | |
|
22 | var results = this.evaluate(function () { | |
|
23 | var cell = IPython.notebook.get_cell(0); | |
|
24 | return cell.output_area.outputs; | |
|
25 | }); | |
|
26 | this.test.assertEquals(results.length, expected.length, "correct number of outputs"); | |
|
27 | for (var i = 0; i < results.length; i++) { | |
|
28 | var r = results[i]; | |
|
29 | var ex = expected[i]; | |
|
30 | this.test.assertEquals(r.output_type, ex.output_type, "output " + i); | |
|
31 | if (r.output_type === 'stream') { | |
|
32 | this.test.assertEquals(r.stream, ex.stream, "stream " + i); | |
|
33 | this.test.assertEquals(r.text, ex.text, "content " + i); | |
|
34 | } | |
|
35 | } | |
|
36 | }); | |
|
37 | ||
|
38 | }; | |
|
39 | ||
|
40 | this.thenEvaluate(function () { | |
|
41 | IPython.notebook.insert_cell_at_index(0, "code"); | |
|
42 | var cell = IPython.notebook.get_cell(0); | |
|
43 | cell.set_text([ | |
|
44 | "from __future__ import print_function", | |
|
45 | "import sys", | |
|
46 | "from IPython.display import display" | |
|
47 | ].join("\n") | |
|
48 | ); | |
|
49 | cell.execute(); | |
|
50 | }); | |
|
51 | ||
|
52 | this.test_coalesced_output("stdout", [ | |
|
53 | "print(1)", | |
|
54 | "sys.stdout.flush()", | |
|
55 | "print(2)", | |
|
56 | "sys.stdout.flush()", | |
|
57 | "print(3)" | |
|
58 | ].join("\n"), [{ | |
|
59 | output_type: "stream", | |
|
60 | stream: "stdout", | |
|
61 | text: "1\n2\n3\n" | |
|
62 | }] | |
|
63 | ); | |
|
64 | ||
|
65 | this.test_coalesced_output("stdout+sdterr", [ | |
|
66 | "print(1)", | |
|
67 | "sys.stdout.flush()", | |
|
68 | "print(2)", | |
|
69 | "print(3, file=sys.stderr)" | |
|
70 | ].join("\n"), [{ | |
|
71 | output_type: "stream", | |
|
72 | stream: "stdout", | |
|
73 | text: "1\n2\n" | |
|
74 | },{ | |
|
75 | output_type: "stream", | |
|
76 | stream: "stderr", | |
|
77 | text: "3\n" | |
|
78 | }] | |
|
79 | ); | |
|
80 | ||
|
81 | this.test_coalesced_output("display splits streams", [ | |
|
82 | "print(1)", | |
|
83 | "sys.stdout.flush()", | |
|
84 | "display(2)", | |
|
85 | "print(3)" | |
|
86 | ].join("\n"), [{ | |
|
87 | output_type: "stream", | |
|
88 | stream: "stdout", | |
|
89 | text: "1\n" | |
|
90 | },{ | |
|
91 | output_type: "display_data", | |
|
92 | },{ | |
|
93 | output_type: "stream", | |
|
94 | stream: "stdout", | |
|
95 | text: "3\n" | |
|
96 | }] | |
|
97 | ); | |
|
98 | }); |
@@ -1,188 +1,188 | |||
|
1 | 1 | var xor = function (a, b) {return !a ^ !b;}; |
|
2 | 2 | var isArray = function (a) { |
|
3 | 3 | try { |
|
4 | 4 | return Object.toString.call(a) === "[object Array]" || Object.toString.call(a) === "[object RuntimeArray]"; |
|
5 | 5 | } catch (e) { |
|
6 | 6 | return Array.isArray(a); |
|
7 | 7 | } |
|
8 | 8 | }; |
|
9 | 9 | var recursive_compare = function(a, b) { |
|
10 | 10 | // Recursively compare two objects. |
|
11 | 11 | var same = true; |
|
12 | 12 | same = same && !xor(a instanceof Object || typeof a == 'object', b instanceof Object || typeof b == 'object'); |
|
13 | 13 | same = same && !xor(isArray(a), isArray(b)); |
|
14 | 14 | |
|
15 | 15 | if (same) { |
|
16 | 16 | if (a instanceof Object) { |
|
17 | 17 | var key; |
|
18 | 18 | for (key in a) { |
|
19 | 19 | if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { |
|
20 | 20 | same = false; |
|
21 | 21 | break; |
|
22 | 22 | } |
|
23 | 23 | } |
|
24 | 24 | for (key in b) { |
|
25 | 25 | if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { |
|
26 | 26 | same = false; |
|
27 | 27 | break; |
|
28 | 28 | } |
|
29 | 29 | } |
|
30 | 30 | } else { |
|
31 | 31 | return a === b; |
|
32 | 32 | } |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | return same; |
|
36 | 36 | }; |
|
37 | 37 | |
|
38 | 38 | // Test the widget framework. |
|
39 | 39 | casper.notebook_test(function () { |
|
40 | 40 | var index; |
|
41 | 41 | |
|
42 | 42 | this.then(function () { |
|
43 | 43 | |
|
44 | 44 | // Check if the WidgetManager class is defined. |
|
45 | 45 | this.test.assert(this.evaluate(function() { |
|
46 | 46 | return IPython.WidgetManager !== undefined; |
|
47 | 47 | }), 'WidgetManager class is defined'); |
|
48 | 48 | }); |
|
49 | 49 | |
|
50 | 50 | index = this.append_cell( |
|
51 | 51 | 'from IPython.html import widgets\n' + |
|
52 | 52 | 'from IPython.display import display, clear_output\n' + |
|
53 | 53 | 'print("Success")'); |
|
54 | 54 | this.execute_cell_then(index); |
|
55 | 55 | |
|
56 | 56 | this.then(function () { |
|
57 | 57 | // Check if the widget manager has been instantiated. |
|
58 | 58 | this.test.assert(this.evaluate(function() { |
|
59 | 59 | return IPython.notebook.kernel.widget_manager !== undefined; |
|
60 | 60 | }), 'Notebook widget manager instantiated'); |
|
61 | 61 | |
|
62 | 62 | // Functions that can be used to test the packing and unpacking APIs |
|
63 | 63 | var that = this; |
|
64 | 64 | var test_pack = function (input) { |
|
65 | 65 | var output = that.evaluate(function(input) { |
|
66 | 66 | var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined); |
|
67 | 67 | var results = model._pack_models(input); |
|
68 | 68 | return results; |
|
69 | 69 | }, {input: input}); |
|
70 | 70 | that.test.assert(recursive_compare(input, output), |
|
71 | 71 | JSON.stringify(input) + ' passed through Model._pack_model unchanged'); |
|
72 | 72 | }; |
|
73 | 73 | var test_unpack = function (input) { |
|
74 | 74 | var output = that.evaluate(function(input) { |
|
75 | 75 | var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined); |
|
76 | 76 | var results = model._unpack_models(input); |
|
77 | 77 | return results; |
|
78 | 78 | }, {input: input}); |
|
79 | 79 | that.test.assert(recursive_compare(input, output), |
|
80 | 80 | JSON.stringify(input) + ' passed through Model._unpack_model unchanged'); |
|
81 | 81 | }; |
|
82 | 82 | var test_packing = function(input) { |
|
83 | 83 | test_pack(input); |
|
84 | 84 | test_unpack(input); |
|
85 | 85 | }; |
|
86 | 86 | |
|
87 | 87 | test_packing({0: 'hi', 1: 'bye'}); |
|
88 | 88 | test_packing(['hi', 'bye']); |
|
89 | 89 | test_packing(['hi', 5]); |
|
90 | 90 | test_packing(['hi', '5']); |
|
91 | 91 | test_packing([1.0, 0]); |
|
92 | 92 | test_packing([1.0, false]); |
|
93 | 93 | test_packing([1, false]); |
|
94 | 94 | test_packing([1, false, {a: 'hi'}]); |
|
95 | 95 | test_packing([1, false, ['hi']]); |
|
96 | 96 | |
|
97 | 97 | // Test multi-set, single touch code. First create a custom widget. |
|
98 | 98 | this.evaluate(function() { |
|
99 | 99 | var MultiSetView = IPython.DOMWidgetView.extend({ |
|
100 | 100 | render: function(){ |
|
101 | 101 | this.model.set('a', 1); |
|
102 | 102 | this.model.set('b', 2); |
|
103 | 103 | this.model.set('c', 3); |
|
104 | 104 | this.touch(); |
|
105 | 105 | }, |
|
106 | 106 | }); |
|
107 | 107 | IPython.WidgetManager.register_widget_view('MultiSetView', MultiSetView); |
|
108 | 108 | }, {}); |
|
109 | 109 | }); |
|
110 | 110 | |
|
111 | 111 | // Try creating the multiset widget, verify that sets the values correctly. |
|
112 | 112 | var multiset = {}; |
|
113 | 113 | multiset.index = this.append_cell( |
|
114 | 114 | 'from IPython.utils.traitlets import Unicode, CInt\n' + |
|
115 | 115 | 'class MultiSetWidget(widgets.Widget):\n' + |
|
116 | 116 | ' _view_name = Unicode("MultiSetView", sync=True)\n' + |
|
117 | 117 | ' a = CInt(0, sync=True)\n' + |
|
118 | 118 | ' b = CInt(0, sync=True)\n' + |
|
119 | 119 | ' c = CInt(0, sync=True)\n' + |
|
120 | 120 | ' d = CInt(-1, sync=True)\n' + // See if it sends a full state. |
|
121 | 121 | ' def _handle_receive_state(self, sync_data):\n' + |
|
122 | 122 | ' widgets.Widget._handle_receive_state(self, sync_data)\n'+ |
|
123 | 123 | ' self.d = len(sync_data)\n' + |
|
124 | 124 | 'multiset = MultiSetWidget()\n' + |
|
125 | 125 | 'display(multiset)\n' + |
|
126 | 126 | 'print(multiset.model_id)'); |
|
127 | 127 | this.execute_cell_then(multiset.index, function(index) { |
|
128 | 128 | multiset.model_id = this.get_output_cell(index).text.trim(); |
|
129 | 129 | }); |
|
130 | 130 | |
|
131 | 131 | this.wait_for_widget(multiset); |
|
132 | 132 | |
|
133 | 133 | index = this.append_cell( |
|
134 | 134 | 'print("%d%d%d" % (multiset.a, multiset.b, multiset.c))'); |
|
135 | 135 | this.execute_cell_then(index, function(index) { |
|
136 | 136 | this.test.assertEquals(this.get_output_cell(index).text.trim(), '123', |
|
137 | 137 | 'Multiple model.set calls and one view.touch update state in back-end.'); |
|
138 | 138 | }); |
|
139 | 139 | |
|
140 | 140 | index = this.append_cell( |
|
141 | 141 | 'print("%d" % (multiset.d))'); |
|
142 | 142 | this.execute_cell_then(index, function(index) { |
|
143 | 143 | this.test.assertEquals(this.get_output_cell(index).text.trim(), '3', |
|
144 | 144 | 'Multiple model.set calls sent a partial state.'); |
|
145 | 145 | }); |
|
146 | 146 | |
|
147 | 147 | var textbox = {}; |
|
148 | 148 | throttle_index = this.append_cell( |
|
149 | 149 | 'import time\n' + |
|
150 | 150 | 'textbox = widgets.TextWidget()\n' + |
|
151 | 151 | 'display(textbox)\n' + |
|
152 | 152 | 'textbox.add_class("my-throttle-textbox")\n' + |
|
153 | 153 | 'def handle_change(name, old, new):\n' + |
|
154 |
' |
|
|
154 | ' display(len(new))\n' + | |
|
155 | 155 | ' time.sleep(0.5)\n' + |
|
156 | 156 | 'textbox.on_trait_change(handle_change, "value")\n' + |
|
157 | 157 | 'print(textbox.model_id)'); |
|
158 | 158 | this.execute_cell_then(throttle_index, function(index){ |
|
159 | 159 | textbox.model_id = this.get_output_cell(index).text.trim(); |
|
160 | 160 | |
|
161 | 161 | this.test.assert(this.cell_element_exists(index, |
|
162 | 162 | '.widget-area .widget-subarea'), |
|
163 | 163 | 'Widget subarea exists.'); |
|
164 | 164 | |
|
165 | 165 | this.test.assert(this.cell_element_exists(index, |
|
166 | 166 | '.my-throttle-textbox'), 'Textbox exists.'); |
|
167 | 167 | |
|
168 | 168 | // Send 20 characters |
|
169 | 169 | this.sendKeys('.my-throttle-textbox', '....................'); |
|
170 | 170 | }); |
|
171 | 171 | |
|
172 | 172 | this.wait_for_widget(textbox); |
|
173 | 173 | |
|
174 | 174 | this.then(function () { |
|
175 | 175 | var outputs = this.evaluate(function(i) { |
|
176 | 176 | return IPython.notebook.get_cell(i).output_area.outputs; |
|
177 | 177 | }, {i : throttle_index}); |
|
178 | 178 | |
|
179 | 179 | // Only 4 outputs should have printed, but because of timing, sometimes |
|
180 | 180 | // 5 outputs will print. All we need to do is verify num outputs <= 5 |
|
181 | 181 | // because that is much less than 20. |
|
182 | 182 | this.test.assert(outputs.length <= 5, 'Messages throttled.'); |
|
183 | 183 | |
|
184 | 184 | // We also need to verify that the last state sent was correct. |
|
185 |
var last_state = outputs[outputs.length-1] |
|
|
186 |
this.test.assertEquals(last_state, "20 |
|
|
185 | var last_state = outputs[outputs.length-1]['text/plain']; | |
|
186 | this.test.assertEquals(last_state, "20", "Last state sent when throttling."); | |
|
187 | 187 | }); |
|
188 | 188 | }); |
@@ -1,43 +1,43 | |||
|
1 | 1 | // Test widget button class |
|
2 | 2 | casper.notebook_test(function () { |
|
3 | 3 | index = this.append_cell( |
|
4 | 4 | 'from IPython.html import widgets\n' + |
|
5 | 5 | 'from IPython.display import display, clear_output\n' + |
|
6 | 6 | 'print("Success")'); |
|
7 | 7 | this.execute_cell_then(index); |
|
8 | 8 | |
|
9 | 9 | var button_index = this.append_cell( |
|
10 | 10 | 'button = widgets.ButtonWidget(description="Title")\n' + |
|
11 | 'display(button)\n'+ | |
|
11 | 'display(button)\n' + | |
|
12 | 12 | 'print("Success")\n' + |
|
13 | 13 | 'def handle_click(sender):\n' + |
|
14 |
' |
|
|
14 | ' display("Clicked")\n' + | |
|
15 | 15 | 'button.on_click(handle_click)'); |
|
16 | 16 | this.execute_cell_then(button_index, function(index){ |
|
17 | 17 | |
|
18 |
this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', |
|
|
18 | this.test.assertEquals(this.get_output_cell(index).text, 'Success\n', | |
|
19 | 19 | 'Create button cell executed with correct output.'); |
|
20 | 20 | |
|
21 | 21 | this.test.assert(this.cell_element_exists(index, |
|
22 | 22 | '.widget-area .widget-subarea'), |
|
23 | 23 | 'Widget subarea exists.'); |
|
24 | 24 | |
|
25 | 25 | this.test.assert(this.cell_element_exists(index, |
|
26 | 26 | '.widget-area .widget-subarea button'), |
|
27 | 27 | 'Widget button exists.'); |
|
28 | 28 | |
|
29 | 29 | this.test.assert(this.cell_element_function(index, |
|
30 | 30 | '.widget-area .widget-subarea button', 'html')=='Title', |
|
31 | 31 | 'Set button description.'); |
|
32 | 32 | |
|
33 | 33 | this.cell_element_function(index, |
|
34 | 34 | '.widget-area .widget-subarea button', 'click'); |
|
35 | 35 | }); |
|
36 | 36 | |
|
37 | 37 | this.wait_for_output(button_index, 1); |
|
38 | 38 | |
|
39 | 39 | this.then(function () { |
|
40 |
this.test.assertEquals(this.get_output_cell(button_index, 1) |
|
|
40 | this.test.assertEquals(this.get_output_cell(button_index, 1)['text/plain'], "'Clicked'", | |
|
41 | 41 | 'Button click event fires.'); |
|
42 | 42 | }); |
|
43 | 43 | }); No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now