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