Show More
@@ -1,67 +1,134 b'' | |||||
|
1 | var xor = function (a, b) {return !a ^ !b;}; | |||
|
2 | var isArray = function (a) {return toString.call(a) === "[object Array]";}; | |||
|
3 | var recursive_compare = function(a, b) { | |||
|
4 | // Recursively compare two objects. | |||
|
5 | var same = true; | |||
|
6 | same = same && !xor(a instanceof Object, b instanceof Object); | |||
|
7 | same = same && !xor(isArray(a), isArray(b)); | |||
|
8 | ||||
|
9 | if (same) { | |||
|
10 | if (a instanceof Object) { | |||
|
11 | for (var key in a) { | |||
|
12 | if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { | |||
|
13 | same = false; | |||
|
14 | break; | |||
|
15 | } | |||
|
16 | } | |||
|
17 | for (var key in b) { | |||
|
18 | if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) { | |||
|
19 | same = false; | |||
|
20 | break; | |||
|
21 | } | |||
|
22 | } | |||
|
23 | } else { | |||
|
24 | return a === b; | |||
|
25 | } | |||
|
26 | } | |||
|
27 | ||||
|
28 | return same; | |||
|
29 | } | |||
|
30 | ||||
1 | // Test the widget framework. |
|
31 | // Test the widget framework. | |
2 | casper.notebook_test(function () { |
|
32 | casper.notebook_test(function () { | |
3 | var index; |
|
33 | var index; | |
4 |
|
34 | |||
5 | this.then(function () { |
|
35 | this.then(function () { | |
6 |
|
36 | |||
7 | // Check if the WidgetManager class is defined. |
|
37 | // Check if the WidgetManager class is defined. | |
8 | this.test.assert(this.evaluate(function() { |
|
38 | this.test.assert(this.evaluate(function() { | |
9 | return IPython.WidgetManager !== undefined; |
|
39 | return IPython.WidgetManager !== undefined; | |
10 | }), 'WidgetManager class is defined'); |
|
40 | }), 'WidgetManager class is defined'); | |
11 | }); |
|
41 | }); | |
12 |
|
42 | |||
13 | index = this.append_cell( |
|
43 | index = this.append_cell( | |
14 | 'from IPython.html import widgets\n' + |
|
44 | 'from IPython.html import widgets\n' + | |
15 | 'from IPython.display import display, clear_output\n' + |
|
45 | 'from IPython.display import display, clear_output\n' + | |
16 | 'print("Success")'); |
|
46 | 'print("Success")'); | |
17 | this.execute_cell_then(index); |
|
47 | this.execute_cell_then(index); | |
18 |
|
48 | |||
19 | this.then(function () { |
|
49 | this.then(function () { | |
20 | // Check if the widget manager has been instantiated. |
|
50 | // Check if the widget manager has been instantiated. | |
21 | this.test.assert(this.evaluate(function() { |
|
51 | this.test.assert(this.evaluate(function() { | |
22 | return IPython.notebook.kernel.widget_manager !== undefined; |
|
52 | return IPython.notebook.kernel.widget_manager !== undefined; | |
23 | }), 'Notebook widget manager instantiated'); |
|
53 | }), 'Notebook widget manager instantiated'); | |
|
54 | ||||
|
55 | // Functions that can be used to test the packing and unpacking APIs | |||
|
56 | var that = this; | |||
|
57 | var test_pack = function (input) { | |||
|
58 | var output = that.evaluate(function(input) { | |||
|
59 | var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined); | |||
|
60 | var results = model._pack_models(input); | |||
|
61 | delete model; | |||
|
62 | return results; | |||
|
63 | }, {input: input}); | |||
|
64 | that.test.assert(recursive_compare(input, output), | |||
|
65 | JSON.stringify(input) + ' passed through Model._pack_model unchanged'); | |||
|
66 | }; | |||
|
67 | var test_unpack = function (input) { | |||
|
68 | var output = that.evaluate(function(input) { | |||
|
69 | var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined); | |||
|
70 | var results = model._unpack_models(input); | |||
|
71 | delete model; | |||
|
72 | return results; | |||
|
73 | }, {input: input}); | |||
|
74 | that.test.assert(recursive_compare(input, output), | |||
|
75 | JSON.stringify(input) + ' passed through Model._unpack_model unchanged'); | |||
|
76 | }; | |||
|
77 | var test_packing = function(input) { | |||
|
78 | test_pack(input); | |||
|
79 | test_unpack(input); | |||
|
80 | }; | |||
|
81 | ||||
|
82 | test_packing({0: 'hi', 1: 'bye'}) | |||
|
83 | test_packing(['hi', 'bye']) | |||
|
84 | test_packing(['hi', 5]) | |||
|
85 | test_packing(['hi', '5']) | |||
|
86 | test_packing([1.0, 0]) | |||
|
87 | test_packing([1.0, false]) | |||
|
88 | test_packing([1, false]) | |||
|
89 | test_packing([1, false, {a: 'hi'}]) | |||
|
90 | test_packing([1, false, ['hi']]) | |||
24 | }); |
|
91 | }); | |
25 |
|
92 | |||
26 | var textbox = {}; |
|
93 | var textbox = {}; | |
27 | throttle_index = this.append_cell( |
|
94 | throttle_index = this.append_cell( | |
28 | 'import time\n' + |
|
95 | 'import time\n' + | |
29 | 'textbox = widgets.TextWidget()\n' + |
|
96 | 'textbox = widgets.TextWidget()\n' + | |
30 | 'display(textbox)\n' + |
|
97 | 'display(textbox)\n' + | |
31 | 'textbox.add_class("my-throttle-textbox")\n' + |
|
98 | 'textbox.add_class("my-throttle-textbox")\n' + | |
32 | 'def handle_change(name, old, new):\n' + |
|
99 | 'def handle_change(name, old, new):\n' + | |
33 | ' print(len(new))\n' + |
|
100 | ' print(len(new))\n' + | |
34 | ' time.sleep(0.5)\n' + |
|
101 | ' time.sleep(0.5)\n' + | |
35 | 'textbox.on_trait_change(handle_change, "value")\n' + |
|
102 | 'textbox.on_trait_change(handle_change, "value")\n' + | |
36 | 'print(textbox.model_id)'); |
|
103 | 'print(textbox.model_id)'); | |
37 | this.execute_cell_then(throttle_index, function(index){ |
|
104 | this.execute_cell_then(throttle_index, function(index){ | |
38 | textbox.model_id = this.get_output_cell(index).text.trim(); |
|
105 | textbox.model_id = this.get_output_cell(index).text.trim(); | |
39 |
|
106 | |||
40 | this.test.assert(this.cell_element_exists(index, |
|
107 | this.test.assert(this.cell_element_exists(index, | |
41 | '.widget-area .widget-subarea'), |
|
108 | '.widget-area .widget-subarea'), | |
42 | 'Widget subarea exists.'); |
|
109 | 'Widget subarea exists.'); | |
43 |
|
110 | |||
44 | this.test.assert(this.cell_element_exists(index, |
|
111 | this.test.assert(this.cell_element_exists(index, | |
45 | '.my-throttle-textbox'), 'Textbox exists.'); |
|
112 | '.my-throttle-textbox'), 'Textbox exists.'); | |
46 |
|
113 | |||
47 | // Send 20 characters |
|
114 | // Send 20 characters | |
48 | this.sendKeys('.my-throttle-textbox', '....................'); |
|
115 | this.sendKeys('.my-throttle-textbox', '....................'); | |
49 | }); |
|
116 | }); | |
50 |
|
117 | |||
51 | this.wait_for_widget(textbox); |
|
118 | this.wait_for_widget(textbox); | |
52 |
|
119 | |||
53 | this.then(function () { |
|
120 | this.then(function () { | |
54 | var outputs = this.evaluate(function(i) { |
|
121 | var outputs = this.evaluate(function(i) { | |
55 | return IPython.notebook.get_cell(i).output_area.outputs; |
|
122 | return IPython.notebook.get_cell(i).output_area.outputs; | |
56 | }, {i : throttle_index}); |
|
123 | }, {i : throttle_index}); | |
57 |
|
124 | |||
58 | // Only 4 outputs should have printed, but because of timing, sometimes |
|
125 | // Only 4 outputs should have printed, but because of timing, sometimes | |
59 | // 5 outputs will print. All we need to do is verify num outputs <= 5 |
|
126 | // 5 outputs will print. All we need to do is verify num outputs <= 5 | |
60 | // because that is much less than 20. |
|
127 | // because that is much less than 20. | |
61 | this.test.assert(outputs.length <= 5, 'Messages throttled.'); |
|
128 | this.test.assert(outputs.length <= 5, 'Messages throttled.'); | |
62 |
|
129 | |||
63 | // We also need to verify that the last state sent was correct. |
|
130 | // We also need to verify that the last state sent was correct. | |
64 | var last_state = outputs[outputs.length-1].text; |
|
131 | var last_state = outputs[outputs.length-1].text; | |
65 | this.test.assertEquals(last_state, "20\n", "Last state sent when throttling."); |
|
132 | this.test.assertEquals(last_state, "20\n", "Last state sent when throttling."); | |
66 | }); |
|
133 | }); | |
67 | }); |
|
134 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now