##// END OF EJS Templates
Merge pull request #5062 from minrk/js-test-races...
Brian E. Granger -
r15246:51d8a38d merge
parent child Browse files
Show More
@@ -1,246 +1,245 b''
1 // Test opening a rich notebook, saving it, and reopening it again.
1 // Test opening a rich notebook, saving it, and reopening it again.
2 //
2 //
3 //toJSON fromJSON toJSON and do a string comparison
3 //toJSON fromJSON toJSON and do a string comparison
4
4
5
5
6 // this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js
6 // this is just a copy of OutputArea.mime_mape_r in IPython/html/static/notebook/js/outputarea.js
7 mime = {
7 mime = {
8 "text" : "text/plain",
8 "text" : "text/plain",
9 "html" : "text/html",
9 "html" : "text/html",
10 "svg" : "image/svg+xml",
10 "svg" : "image/svg+xml",
11 "png" : "image/png",
11 "png" : "image/png",
12 "jpeg" : "image/jpeg",
12 "jpeg" : "image/jpeg",
13 "latex" : "text/latex",
13 "latex" : "text/latex",
14 "json" : "application/json",
14 "json" : "application/json",
15 "javascript" : "application/javascript",
15 "javascript" : "application/javascript",
16 };
16 };
17
17
18 var black_dot_jpeg="u\"\"\"/9j/4AAQSkZJRgABAQEASABIAAD/2wBDACodICUgGiolIiUvLSoyP2lEPzo6P4FcYUxpmYagnpaG\nk5GovfLNqLPltZGT0v/V5fr/////o8v///////L/////2wBDAS0vLz83P3xERHz/rpOu////////\n////////////////////////////////////////////////////////////wgARCAABAAEDAREA\nAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAABP/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEA\nAhADEAAAARn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAEFAn//xAAUEQEAAAAAAAAAAAAA\nAAAAAAAA/9oACAEDAQE/AX//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/AX//xAAUEAEA\nAAAAAAAAAAAAAAAAAAAA/9oACAEBAAY/An//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/\nIX//2gAMAwEAAgADAAAAEB//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDAQE/EH//xAAUEQEA\nAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/EH//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/\nEH//2Q==\"\"\"";
18 var black_dot_jpeg="u\"\"\"/9j/4AAQSkZJRgABAQEASABIAAD/2wBDACodICUgGiolIiUvLSoyP2lEPzo6P4FcYUxpmYagnpaG\nk5GovfLNqLPltZGT0v/V5fr/////o8v///////L/////2wBDAS0vLz83P3xERHz/rpOu////////\n////////////////////////////////////////////////////////////wgARCAABAAEDAREA\nAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAABP/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEA\nAhADEAAAARn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAEFAn//xAAUEQEAAAAAAAAAAAAA\nAAAAAAAA/9oACAEDAQE/AX//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/AX//xAAUEAEA\nAAAAAAAAAAAAAAAAAAAA/9oACAEBAAY/An//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/\nIX//2gAMAwEAAgADAAAAEB//xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oACAEDAQE/EH//xAAUEQEA\nAAAAAAAAAAAAAAAAAAAA/9oACAECAQE/EH//xAAUEAEAAAAAAAAAAAAAAAAAAAAA/9oACAEBAAE/\nEH//2Q==\"\"\"";
19 var black_dot_png = 'u\"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QA\\niAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AA\\nAAACAAHiIbwzAAAAAElFTkSuQmCC\"';
19 var black_dot_png = 'u\"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QA\\niAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AA\\nAAACAAHiIbwzAAAAAElFTkSuQmCC\"';
20 var svg = "\"<svg width='1cm' height='1cm' viewBox='0 0 1000 500'><defs><style>rect {fill:red;}; </style></defs><rect id='r1' x='200' y='100' width='600' height='300' /></svg>\"";
20 var svg = "\"<svg width='1cm' height='1cm' viewBox='0 0 1000 500'><defs><style>rect {fill:red;}; </style></defs><rect id='r1' x='200' y='100' width='600' height='300' /></svg>\"";
21
21
22 // helper function to ensure that the short_name is found in the toJSON
22 // helper function to ensure that the short_name is found in the toJSON
23 // represetnation, while the original in-memory cell retains its long mimetype
23 // represetnation, while the original in-memory cell retains its long mimetype
24 // name, and that fromJSON also gets its long mimetype name
24 // name, and that fromJSON also gets its long mimetype name
25 function assert_has(short_name, json, result, result2) {
25 function assert_has(short_name, json, result, result2) {
26 long_name = mime[short_name];
26 long_name = mime[short_name];
27 this.test.assertTrue(json[0].hasOwnProperty(short_name),
27 this.test.assertTrue(json[0].hasOwnProperty(short_name),
28 'toJSON() representation uses ' + short_name);
28 'toJSON() representation uses ' + short_name);
29 this.test.assertTrue(result.hasOwnProperty(long_name),
29 this.test.assertTrue(result.hasOwnProperty(long_name),
30 'toJSON() original embedded JSON keeps ' + long_name);
30 'toJSON() original embedded JSON keeps ' + long_name);
31 this.test.assertTrue(result2.hasOwnProperty(long_name),
31 this.test.assertTrue(result2.hasOwnProperty(long_name),
32 'fromJSON() embedded ' + short_name + ' gets mime key ' + long_name);
32 'fromJSON() embedded ' + short_name + ' gets mime key ' + long_name);
33 }
33 }
34
34
35 // helper function for checkout that the first two cells have a particular
35 // helper function for checkout that the first two cells have a particular
36 // output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON
36 // output_type (either 'pyout' or 'display_data'), and checks the to/fromJSON
37 // for a set of mimetype keys, using their short names ('javascript', 'text',
37 // for a set of mimetype keys, using their short names ('javascript', 'text',
38 // 'png', etc).
38 // 'png', etc).
39 function check_output_area(output_type, keys) {
39 function check_output_area(output_type, keys) {
40 this.wait_for_output(0);
40 this.wait_for_output(0);
41 json = this.evaluate(function() {
41 json = this.evaluate(function() {
42 var json = IPython.notebook.get_cell(0).output_area.toJSON();
42 var json = IPython.notebook.get_cell(0).output_area.toJSON();
43 // appended cell will initially be empty, let's add some output
43 // appended cell will initially be empty, let's add some output
44 IPython.notebook.get_cell(1).output_area.fromJSON(json);
44 IPython.notebook.get_cell(1).output_area.fromJSON(json);
45 return json;
45 return json;
46 });
46 });
47 // The evaluate call above happens asynchronously: wait for cell[1] to have output
47 // The evaluate call above happens asynchronously: wait for cell[1] to have output
48 this.wait_for_output(1);
48 this.wait_for_output(1);
49 var result = this.get_output_cell(0);
49 var result = this.get_output_cell(0);
50 var result2 = this.get_output_cell(1);
50 var result2 = this.get_output_cell(1);
51 this.test.assertEquals(result.output_type, output_type,
51 this.test.assertEquals(result.output_type, output_type,
52 'testing ' + output_type + ' for ' + keys.join(' and '));
52 'testing ' + output_type + ' for ' + keys.join(' and '));
53
53
54 for (var idx in keys) {
54 for (var idx in keys) {
55 assert_has.apply(this, [keys[idx], json, result, result2]);
55 assert_has.apply(this, [keys[idx], json, result, result2]);
56 }
56 }
57 }
57 }
58
58
59
59
60 // helper function to clear the first two cells, set the text of and execute
60 // helper function to clear the first two cells, set the text of and execute
61 // the first one
61 // the first one
62 function clear_and_execute(that, code) {
62 function clear_and_execute(that, code) {
63 that.evaluate(function() {
63 that.evaluate(function() {
64 IPython.notebook.get_cell(0).clear_output();
64 IPython.notebook.get_cell(0).clear_output();
65 IPython.notebook.get_cell(1).clear_output();
65 IPython.notebook.get_cell(1).clear_output();
66 });
66 });
67 that.then(function () {
67 that.set_cell_text(0, code);
68 that.set_cell_text(0, code);
68 that.execute_cell(0);
69 that.execute_cell(0);
69 }
70 that.wait_for_idle();
71 });
72 };
70
73
71 casper.notebook_test(function () {
74 casper.notebook_test(function () {
72 this.evaluate(function () {
75 this.evaluate(function () {
73 var cell = IPython.notebook.get_cell(0);
76 var cell = IPython.notebook.get_cell(0);
74 // "we have to make messes to find out who we are"
77 // "we have to make messes to find out who we are"
75 cell.set_text([
78 cell.set_text([
76 "%%javascript",
79 "%%javascript",
77 "IPython.notebook.insert_cell_below('code')"
80 "IPython.notebook.insert_cell_below('code')"
78 ].join('\n')
81 ].join('\n')
79 );
82 );
80
81 cell.execute();
82 });
83 });
83
84
84 this.wait_for_output(0);
85 this.execute_cell_then(0, function () {
85
86 this.then(function ( ) {
87 var result = this.get_output_cell(0);
86 var result = this.get_output_cell(0);
88 var num_cells = this.get_cells_length();
87 var num_cells = this.get_cells_length();
89 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
88 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
90 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
89 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
91 'testing JS embedded with mime key');
90 'testing JS embedded with mime key');
92 });
91 });
93
92
94 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
93 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
95 this.then(function () {
94 this.then(function () {
96 clear_and_execute(this, [
95 clear_and_execute(this, [
97 "%%javascript",
96 "%%javascript",
98 "var a=5;"
97 "var a=5;"
99 ].join('\n'));
98 ].join('\n'));
100 });
99 });
101
100
102
101
103 this.then(function () {
102 this.then(function () {
104 check_output_area.apply(this, ['display_data', ['javascript']]);
103 check_output_area.apply(this, ['display_data', ['javascript']]);
105
104
106 });
105 });
107
106
108 this.then(function() {
107 this.then(function() {
109 clear_and_execute(this, '%lsmagic');
108 clear_and_execute(this, '%lsmagic');
110 });
109 });
111
110
112 this.then(function () {
111 this.then(function () {
113 check_output_area.apply(this, ['pyout', ['text', 'json']]);
112 check_output_area.apply(this, ['pyout', ['text', 'json']]);
114 });
113 });
115
114
116 this.then(function() {
115 this.then(function() {
117 clear_and_execute(this,
116 clear_and_execute(this,
118 "x = %lsmagic\nfrom IPython.display import display; display(x)");
117 "x = %lsmagic\nfrom IPython.display import display; display(x)");
119 });
118 });
120
119
121 this.then(function ( ) {
120 this.then(function ( ) {
122 check_output_area.apply(this, ['display_data', ['text', 'json']]);
121 check_output_area.apply(this, ['display_data', ['text', 'json']]);
123 });
122 });
124
123
125 this.then(function() {
124 this.then(function() {
126 clear_and_execute(this,
125 clear_and_execute(this,
127 "from IPython.display import Latex; Latex('$X^2$')");
126 "from IPython.display import Latex; Latex('$X^2$')");
128 });
127 });
129
128
130 this.then(function ( ) {
129 this.then(function ( ) {
131 check_output_area.apply(this, ['pyout', ['text', 'latex']]);
130 check_output_area.apply(this, ['pyout', ['text', 'latex']]);
132 });
131 });
133
132
134 this.then(function() {
133 this.then(function() {
135 clear_and_execute(this,
134 clear_and_execute(this,
136 "from IPython.display import Latex, display; display(Latex('$X^2$'))");
135 "from IPython.display import Latex, display; display(Latex('$X^2$'))");
137 });
136 });
138
137
139 this.then(function ( ) {
138 this.then(function ( ) {
140 check_output_area.apply(this, ['display_data', ['text', 'latex']]);
139 check_output_area.apply(this, ['display_data', ['text', 'latex']]);
141 });
140 });
142
141
143 this.then(function() {
142 this.then(function() {
144 clear_and_execute(this,
143 clear_and_execute(this,
145 "from IPython.display import HTML; HTML('<b>it works!</b>')");
144 "from IPython.display import HTML; HTML('<b>it works!</b>')");
146 });
145 });
147
146
148 this.then(function ( ) {
147 this.then(function ( ) {
149 check_output_area.apply(this, ['pyout', ['text', 'html']]);
148 check_output_area.apply(this, ['pyout', ['text', 'html']]);
150 });
149 });
151
150
152 this.then(function() {
151 this.then(function() {
153 clear_and_execute(this,
152 clear_and_execute(this,
154 "from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))");
153 "from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))");
155 });
154 });
156
155
157 this.then(function ( ) {
156 this.then(function ( ) {
158 check_output_area.apply(this, ['display_data', ['text', 'html']]);
157 check_output_area.apply(this, ['display_data', ['text', 'html']]);
159 });
158 });
160
159
161
160
162 this.then(function() {
161 this.then(function() {
163 clear_and_execute(this,
162 clear_and_execute(this,
164 "from IPython.display import Image; Image(" + black_dot_png + ")");
163 "from IPython.display import Image; Image(" + black_dot_png + ")");
165 });
164 });
166 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
165 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
167
166
168 this.then(function ( ) {
167 this.then(function ( ) {
169 check_output_area.apply(this, ['pyout', ['text', 'png']]);
168 check_output_area.apply(this, ['pyout', ['text', 'png']]);
170 });
169 });
171
170
172 this.then(function() {
171 this.then(function() {
173 clear_and_execute(this,
172 clear_and_execute(this,
174 "from IPython.display import Image, display; display(Image(" + black_dot_png + "))");
173 "from IPython.display import Image, display; display(Image(" + black_dot_png + "))");
175 });
174 });
176
175
177 this.then(function ( ) {
176 this.then(function ( ) {
178 check_output_area.apply(this, ['display_data', ['text', 'png']]);
177 check_output_area.apply(this, ['display_data', ['text', 'png']]);
179 });
178 });
180
179
181
180
182 this.then(function() {
181 this.then(function() {
183 clear_and_execute(this,
182 clear_and_execute(this,
184 "from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')");
183 "from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')");
185 });
184 });
186
185
187 this.then(function ( ) {
186 this.then(function ( ) {
188 check_output_area.apply(this, ['pyout', ['text', 'jpeg']]);
187 check_output_area.apply(this, ['pyout', ['text', 'jpeg']]);
189 });
188 });
190
189
191 this.then(function() {
190 this.then(function() {
192 clear_and_execute(this,
191 clear_and_execute(this,
193 "from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))");
192 "from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))");
194 });
193 });
195
194
196 this.then(function ( ) {
195 this.then(function ( ) {
197 check_output_area.apply(this, ['display_data', ['text', 'jpeg']]);
196 check_output_area.apply(this, ['display_data', ['text', 'jpeg']]);
198 });
197 });
199
198
200 this.then(function() {
199 this.then(function() {
201 clear_and_execute(this,
200 clear_and_execute(this,
202 "from IPython.core.display import SVG; SVG(" + svg + ")");
201 "from IPython.core.display import SVG; SVG(" + svg + ")");
203 });
202 });
204
203
205 this.then(function ( ) {
204 this.then(function ( ) {
206 check_output_area.apply(this, ['pyout', ['text', 'svg']]);
205 check_output_area.apply(this, ['pyout', ['text', 'svg']]);
207 });
206 });
208
207
209 this.then(function() {
208 this.then(function() {
210 clear_and_execute(this,
209 clear_and_execute(this,
211 "from IPython.core.display import SVG, display; display(SVG(" + svg + "))");
210 "from IPython.core.display import SVG, display; display(SVG(" + svg + "))");
212 });
211 });
213
212
214 this.then(function ( ) {
213 this.then(function ( ) {
215 check_output_area.apply(this, ['display_data', ['text', 'svg']]);
214 check_output_area.apply(this, ['display_data', ['text', 'svg']]);
216 });
215 });
217
216
218 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
217 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
219
218
220 this.then(function() {
219 this.then(function() {
221 clear_and_execute(this, [
220 clear_and_execute(this, [
222 "from IPython.core.formatters import HTMLFormatter",
221 "from IPython.core.formatters import HTMLFormatter",
223 "x = HTMLFormatter()",
222 "x = HTMLFormatter()",
224 "x.format_type = 'text/superfancymimetype'",
223 "x.format_type = 'text/superfancymimetype'",
225 "get_ipython().display_formatter.formatters['text/superfancymimetype'] = x",
224 "get_ipython().display_formatter.formatters['text/superfancymimetype'] = x",
226 "from IPython.display import HTML, display",
225 "from IPython.display import HTML, display",
227 'display(HTML("yo"))',
226 'display(HTML("yo"))',
228 "HTML('hello')"].join('\n')
227 "HTML('hello')"].join('\n')
229 );
228 );
230
229
231 });
230 });
232
231
233 this.wait_for_output(0, 1);
232 this.wait_for_output(0, 1);
234
233
235 this.then(function () {
234 this.then(function () {
236 var long_name = 'text/superfancymimetype';
235 var long_name = 'text/superfancymimetype';
237 var result = this.get_output_cell(0);
236 var result = this.get_output_cell(0);
238 this.test.assertTrue(result.hasOwnProperty(long_name),
237 this.test.assertTrue(result.hasOwnProperty(long_name),
239 'display_data custom mimetype ' + long_name);
238 'display_data custom mimetype ' + long_name);
240 var result = this.get_output_cell(0, 1);
239 var result = this.get_output_cell(0, 1);
241 this.test.assertTrue(result.hasOwnProperty(long_name),
240 this.test.assertTrue(result.hasOwnProperty(long_name),
242 'pyout custom mimetype ' + long_name);
241 'pyout custom mimetype ' + long_name);
243
242
244 });
243 });
245
244
246 });
245 });
General Comments 0
You need to be logged in to leave comments. Login now