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