##// END OF EJS Templates
Added test that shows the problem.
Jonathan Frederic -
Show More
@@ -1,134 +1,171 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]" || toString.call(a) === "[object RuntimeArray]";};
2 var isArray = function (a) {return toString.call(a) === "[object Array]" || toString.call(a) === "[object RuntimeArray]";};
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 var key;
12 for (key in a) {
12 if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
13 if (a.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
13 same = false;
14 same = false;
14 break;
15 break;
15 }
16 }
16 }
17 }
17 for (var key in b) {
18 for (key in b) {
18 if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
19 if (b.hasOwnProperty(key) && !recursive_compare(a[key], b[key])) {
19 same = false;
20 same = false;
20 break;
21 break;
21 }
22 }
22 }
23 }
23 } else {
24 } else {
24 return a === b;
25 return a === b;
25 }
26 }
26 }
27 }
27
28
28 return same;
29 return same;
29 }
30 };
30
31
31 // Test the widget framework.
32 // Test the widget framework.
32 casper.notebook_test(function () {
33 casper.notebook_test(function () {
33 var index;
34 var index;
34
35
35 this.then(function () {
36 this.then(function () {
36
37
37 // Check if the WidgetManager class is defined.
38 // Check if the WidgetManager class is defined.
38 this.test.assert(this.evaluate(function() {
39 this.test.assert(this.evaluate(function() {
39 return IPython.WidgetManager !== undefined;
40 return IPython.WidgetManager !== undefined;
40 }), 'WidgetManager class is defined');
41 }), 'WidgetManager class is defined');
41 });
42 });
42
43
43 index = this.append_cell(
44 index = this.append_cell(
44 'from IPython.html import widgets\n' +
45 'from IPython.html import widgets\n' +
45 'from IPython.display import display, clear_output\n' +
46 'from IPython.display import display, clear_output\n' +
46 'print("Success")');
47 'print("Success")');
47 this.execute_cell_then(index);
48 this.execute_cell_then(index);
48
49
49 this.then(function () {
50 this.then(function () {
50 // Check if the widget manager has been instantiated.
51 // Check if the widget manager has been instantiated.
51 this.test.assert(this.evaluate(function() {
52 this.test.assert(this.evaluate(function() {
52 return IPython.notebook.kernel.widget_manager !== undefined;
53 return IPython.notebook.kernel.widget_manager !== undefined;
53 }), 'Notebook widget manager instantiated');
54 }), 'Notebook widget manager instantiated');
54
55
55 // Functions that can be used to test the packing and unpacking APIs
56 // Functions that can be used to test the packing and unpacking APIs
56 var that = this;
57 var that = this;
57 var test_pack = function (input) {
58 var test_pack = function (input) {
58 var output = that.evaluate(function(input) {
59 var output = that.evaluate(function(input) {
59 var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined);
60 var model = new IPython.WidgetModel(IPython.notebook.kernel.widget_manager, undefined);
60 var results = model._pack_models(input);
61 var results = model._pack_models(input);
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;
72 return results;
71 return results;
73 }, {input: input});
72 }, {input: input});
74 that.test.assert(recursive_compare(input, output),
73 that.test.assert(recursive_compare(input, output),
75 JSON.stringify(input) + ' passed through Model._unpack_model unchanged');
74 JSON.stringify(input) + ' passed through Model._unpack_model unchanged');
76 };
75 };
77 var test_packing = function(input) {
76 var test_packing = function(input) {
78 test_pack(input);
77 test_pack(input);
79 test_unpack(input);
78 test_unpack(input);
80 };
79 };
81
80
82 test_packing({0: 'hi', 1: 'bye'})
81 test_packing({0: 'hi', 1: 'bye'});
83 test_packing(['hi', 'bye'])
82 test_packing(['hi', 'bye']);
84 test_packing(['hi', 5])
83 test_packing(['hi', 5]);
85 test_packing(['hi', '5'])
84 test_packing(['hi', '5']);
86 test_packing([1.0, 0])
85 test_packing([1.0, 0]);
87 test_packing([1.0, false])
86 test_packing([1.0, false]);
88 test_packing([1, false])
87 test_packing([1, false]);
89 test_packing([1, false, {a: 'hi'}])
88 test_packing([1, false, {a: 'hi'}]);
90 test_packing([1, false, ['hi']])
89 test_packing([1, false, ['hi']]);
90
91 // Test multi-set, single touch code. First create a custom widget.
92 this.evaluate(function() {
93 var MultiSetView = IPython.DOMWidgetView.extend({
94 render: function(){
95 this.model.set('a', 1);
96 this.model.set('b', 2);
97 this.model.set('c', 3);
98 this.touch();
99 },
100 });
101 WidgetManager.register_widget_view('MultiSetView', MultiSetView);
102 }, {});
103 });
104
105 // Try creating the multiset widget, verify that sets the values correctly.
106 var multiset = {};
107 multiset.index = this.append_cell(
108 'from IPython.utils.traitlets import Unicode, CInt\n' +
109 'class MultiSetWidget(widgets.Widget):\n' +
110 ' _view_name = Unicode("MultiSetView", sync=True)\n' +
111 ' a = CInt(0, sync=True)\n' +
112 ' b = CInt(0, sync=True)\n' +
113 ' c = CInt(0, sync=True)\n' +
114 'multiset = MultiSetWidget()\n' +
115 'display(multiset)\n' +
116 'print(multiset.model_id)');
117 this.execute_cell_then(multiset.index, function(index) {
118 multiset.model_id = this.get_output_cell(index).text.trim();
119 });
120
121 this.wait_for_widget(multiset);
122
123 index = this.append_cell(
124 'print("%d%d%d" % (multiset.a, multiset.b, multiset.c))');
125 this.execute_cell_then(index, function(index) {
126 this.test.assertEquals(this.get_output_cell(index).text.trim(), '123',
127 'Multiple model.set calls and one view.touch update state in back-end.');
91 });
128 });
92
129
93 var textbox = {};
130 var textbox = {};
94 throttle_index = this.append_cell(
131 throttle_index = this.append_cell(
95 'import time\n' +
132 'import time\n' +
96 'textbox = widgets.TextWidget()\n' +
133 'textbox = widgets.TextWidget()\n' +
97 'display(textbox)\n' +
134 'display(textbox)\n' +
98 'textbox.add_class("my-throttle-textbox")\n' +
135 'textbox.add_class("my-throttle-textbox")\n' +
99 'def handle_change(name, old, new):\n' +
136 'def handle_change(name, old, new):\n' +
100 ' print(len(new))\n' +
137 ' print(len(new))\n' +
101 ' time.sleep(0.5)\n' +
138 ' time.sleep(0.5)\n' +
102 'textbox.on_trait_change(handle_change, "value")\n' +
139 'textbox.on_trait_change(handle_change, "value")\n' +
103 'print(textbox.model_id)');
140 'print(textbox.model_id)');
104 this.execute_cell_then(throttle_index, function(index){
141 this.execute_cell_then(throttle_index, function(index){
105 textbox.model_id = this.get_output_cell(index).text.trim();
142 textbox.model_id = this.get_output_cell(index).text.trim();
106
143
107 this.test.assert(this.cell_element_exists(index,
144 this.test.assert(this.cell_element_exists(index,
108 '.widget-area .widget-subarea'),
145 '.widget-area .widget-subarea'),
109 'Widget subarea exists.');
146 'Widget subarea exists.');
110
147
111 this.test.assert(this.cell_element_exists(index,
148 this.test.assert(this.cell_element_exists(index,
112 '.my-throttle-textbox'), 'Textbox exists.');
149 '.my-throttle-textbox'), 'Textbox exists.');
113
150
114 // Send 20 characters
151 // Send 20 characters
115 this.sendKeys('.my-throttle-textbox', '....................');
152 this.sendKeys('.my-throttle-textbox', '....................');
116 });
153 });
117
154
118 this.wait_for_widget(textbox);
155 this.wait_for_widget(textbox);
119
156
120 this.then(function () {
157 this.then(function () {
121 var outputs = this.evaluate(function(i) {
158 var outputs = this.evaluate(function(i) {
122 return IPython.notebook.get_cell(i).output_area.outputs;
159 return IPython.notebook.get_cell(i).output_area.outputs;
123 }, {i : throttle_index});
160 }, {i : throttle_index});
124
161
125 // Only 4 outputs should have printed, but because of timing, sometimes
162 // 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
163 // 5 outputs will print. All we need to do is verify num outputs <= 5
127 // because that is much less than 20.
164 // because that is much less than 20.
128 this.test.assert(outputs.length <= 5, 'Messages throttled.');
165 this.test.assert(outputs.length <= 5, 'Messages throttled.');
129
166
130 // We also need to verify that the last state sent was correct.
167 // We also need to verify that the last state sent was correct.
131 var last_state = outputs[outputs.length-1].text;
168 var last_state = outputs[outputs.length-1].text;
132 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
169 this.test.assertEquals(last_state, "20\n", "Last state sent when throttling.");
133 });
170 });
134 });
171 });
General Comments 0
You need to be logged in to leave comments. Login now