##// END OF EJS Templates
minor js test fix
Paul Ivanov -
Show More
@@ -1,235 +1,237 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 embeded JSON keeps ' + long_name);
30 'toJSON() original embeded JSON keeps ' + long_name);
31 this.test.assertTrue(result2.hasOwnProperty(long_name),
31 this.test.assertTrue(result2.hasOwnProperty(long_name),
32 'fromJSON() embeded ' + short_name + ' gets mime key ' + long_name);
32 'fromJSON() embeded ' + 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, lets add it some output
43 // appended cell will initially be empty, lets add it some output
44 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
44 var cell = IPython.notebook.get_cell(1).output_area.fromJSON(json);
45 return json;
45 return json;
46 });
46 });
47 // The evaluate call above happens asyncrhonously: wait for cell[1] to have output
48 this.wait_for_output(1);
47 var result = this.get_output_cell(0);
49 var result = this.get_output_cell(0);
48 var result2 = this.get_output_cell(1);
50 var result2 = this.get_output_cell(1);
49 this.test.assertEquals(result.output_type, output_type,
51 this.test.assertEquals(result.output_type, output_type,
50 'testing ' + output_type + ' for ' + keys.join(' and '));
52 'testing ' + output_type + ' for ' + keys.join(' and '));
51
53
52 for (var idx in keys) {
54 for (var idx in keys) {
53 assert_has.apply(this, [keys[idx], json, result, result2]);
55 assert_has.apply(this, [keys[idx], json, result, result2]);
54 }
56 }
55 }
57 }
56
58
57
59
58 // 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
59 // the first one
61 // the first one
60 function clear_and_execute(that, code) {
62 function clear_and_execute(that, code) {
61 that.evaluate(function() {
63 that.evaluate(function() {
62 IPython.notebook.get_cell(0).clear_output();
64 IPython.notebook.get_cell(0).clear_output();
63 IPython.notebook.get_cell(1).clear_output();
65 IPython.notebook.get_cell(1).clear_output();
64 });
66 });
65 that.set_cell_text(0, code);
67 that.set_cell_text(0, code);
66 that.execute_cell(0);
68 that.execute_cell(0);
67 }
69 }
68
70
69 casper.notebook_test(function () {
71 casper.notebook_test(function () {
70 this.evaluate(function () {
72 this.evaluate(function () {
71 var cell = IPython.notebook.get_cell(0);
73 var cell = IPython.notebook.get_cell(0);
72 // "we have to make messes to find out who we are"
74 // "we have to make messes to find out who we are"
73 cell.set_text([
75 cell.set_text([
74 "%%javascript",
76 "%%javascript",
75 "IPython.notebook.insert_cell_below('code')"
77 "IPython.notebook.insert_cell_below('code')"
76 ].join('\n')
78 ].join('\n')
77 );
79 );
78
80
79 cell.execute();
81 cell.execute();
80 });
82 });
81
83
82 this.wait_for_output(0);
84 this.wait_for_output(0);
83
85
84 this.then(function ( ) {
86 this.then(function ( ) {
85 var result = this.get_output_cell(0);
87 var result = this.get_output_cell(0);
86 var num_cells = this.get_cells_length();
88 var num_cells = this.get_cells_length();
87 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
89 this.test.assertEquals(num_cells, 2, '%%javascript magic works');
88 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
90 this.test.assertTrue(result.hasOwnProperty('application/javascript'),
89 'testing JS embeded with mime key');
91 'testing JS embeded with mime key');
90 });
92 });
91
93
92 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
94 //this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
93
95
94 this.then(function ( ) {
96 this.then(function ( ) {
95 check_output_area.apply(this, ['display_data', ['javascript']]);
97 check_output_area.apply(this, ['display_data', ['javascript']]);
96
98
97 });
99 });
98
100
99 this.then(function() {
101 this.then(function() {
100 clear_and_execute(this, '%lsmagic');
102 clear_and_execute(this, '%lsmagic');
101 });
103 });
102
104
103 this.then(function () {
105 this.then(function () {
104 check_output_area.apply(this, ['pyout', ['text', 'json']]);
106 check_output_area.apply(this, ['pyout', ['text', 'json']]);
105 });
107 });
106
108
107 this.then(function() {
109 this.then(function() {
108 clear_and_execute(this,
110 clear_and_execute(this,
109 "x = %lsmagic\nfrom IPython.display import display; display(x)");
111 "x = %lsmagic\nfrom IPython.display import display; display(x)");
110 });
112 });
111
113
112 this.then(function ( ) {
114 this.then(function ( ) {
113 check_output_area.apply(this, ['display_data', ['text', 'json']]);
115 check_output_area.apply(this, ['display_data', ['text', 'json']]);
114 });
116 });
115
117
116 this.then(function() {
118 this.then(function() {
117 clear_and_execute(this,
119 clear_and_execute(this,
118 "from IPython.display import Latex; Latex('$X^2$')");
120 "from IPython.display import Latex; Latex('$X^2$')");
119 });
121 });
120
122
121 this.then(function ( ) {
123 this.then(function ( ) {
122 check_output_area.apply(this, ['pyout', ['text', 'latex']]);
124 check_output_area.apply(this, ['pyout', ['text', 'latex']]);
123 });
125 });
124
126
125 this.then(function() {
127 this.then(function() {
126 clear_and_execute(this,
128 clear_and_execute(this,
127 "from IPython.display import Latex, display; display(Latex('$X^2$'))");
129 "from IPython.display import Latex, display; display(Latex('$X^2$'))");
128 });
130 });
129
131
130 this.then(function ( ) {
132 this.then(function ( ) {
131 check_output_area.apply(this, ['display_data', ['text', 'latex']]);
133 check_output_area.apply(this, ['display_data', ['text', 'latex']]);
132 });
134 });
133
135
134 this.then(function() {
136 this.then(function() {
135 clear_and_execute(this,
137 clear_and_execute(this,
136 "from IPython.display import HTML; HTML('<b>it works!</b>')");
138 "from IPython.display import HTML; HTML('<b>it works!</b>')");
137 });
139 });
138
140
139 this.then(function ( ) {
141 this.then(function ( ) {
140 check_output_area.apply(this, ['pyout', ['text', 'html']]);
142 check_output_area.apply(this, ['pyout', ['text', 'html']]);
141 });
143 });
142
144
143 this.then(function() {
145 this.then(function() {
144 clear_and_execute(this,
146 clear_and_execute(this,
145 "from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))");
147 "from IPython.display import HTML, display; display(HTML('<b>it works!</b>'))");
146 });
148 });
147
149
148 this.then(function ( ) {
150 this.then(function ( ) {
149 check_output_area.apply(this, ['display_data', ['text', 'html']]);
151 check_output_area.apply(this, ['display_data', ['text', 'html']]);
150 });
152 });
151
153
152
154
153 this.then(function() {
155 this.then(function() {
154 clear_and_execute(this,
156 clear_and_execute(this,
155 "from IPython.display import Image; Image(" + black_dot_png + ")");
157 "from IPython.display import Image; Image(" + black_dot_png + ")");
156 });
158 });
157 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
159 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
158
160
159 this.then(function ( ) {
161 this.then(function ( ) {
160 check_output_area.apply(this, ['pyout', ['text', 'png']]);
162 check_output_area.apply(this, ['pyout', ['text', 'png']]);
161 });
163 });
162
164
163 this.then(function() {
165 this.then(function() {
164 clear_and_execute(this,
166 clear_and_execute(this,
165 "from IPython.display import Image, display; display(Image(" + black_dot_png + "))");
167 "from IPython.display import Image, display; display(Image(" + black_dot_png + "))");
166 });
168 });
167
169
168 this.then(function ( ) {
170 this.then(function ( ) {
169 check_output_area.apply(this, ['display_data', ['text', 'png']]);
171 check_output_area.apply(this, ['display_data', ['text', 'png']]);
170 });
172 });
171
173
172
174
173 this.then(function() {
175 this.then(function() {
174 clear_and_execute(this,
176 clear_and_execute(this,
175 "from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')");
177 "from IPython.display import Image; Image(" + black_dot_jpeg + ", format='jpeg')");
176 });
178 });
177
179
178 this.then(function ( ) {
180 this.then(function ( ) {
179 check_output_area.apply(this, ['pyout', ['text', 'jpeg']]);
181 check_output_area.apply(this, ['pyout', ['text', 'jpeg']]);
180 });
182 });
181
183
182 this.then(function() {
184 this.then(function() {
183 clear_and_execute(this,
185 clear_and_execute(this,
184 "from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))");
186 "from IPython.display import Image, display; display(Image(" + black_dot_jpeg + ", format='jpeg'))");
185 });
187 });
186
188
187 this.then(function ( ) {
189 this.then(function ( ) {
188 check_output_area.apply(this, ['display_data', ['text', 'jpeg']]);
190 check_output_area.apply(this, ['display_data', ['text', 'jpeg']]);
189 });
191 });
190
192
191 this.then(function() {
193 this.then(function() {
192 clear_and_execute(this,
194 clear_and_execute(this,
193 "from IPython.core.display import SVG; SVG(" + svg + ")");
195 "from IPython.core.display import SVG; SVG(" + svg + ")");
194 });
196 });
195
197
196 this.then(function ( ) {
198 this.then(function ( ) {
197 check_output_area.apply(this, ['pyout', ['text', 'svg']]);
199 check_output_area.apply(this, ['pyout', ['text', 'svg']]);
198 });
200 });
199
201
200 this.then(function() {
202 this.then(function() {
201 clear_and_execute(this,
203 clear_and_execute(this,
202 "from IPython.core.display import SVG, display; display(SVG(" + svg + "))");
204 "from IPython.core.display import SVG, display; display(SVG(" + svg + "))");
203 });
205 });
204
206
205 this.then(function ( ) {
207 this.then(function ( ) {
206 check_output_area.apply(this, ['display_data', ['text', 'svg']]);
208 check_output_area.apply(this, ['display_data', ['text', 'svg']]);
207 });
209 });
208
210
209 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
211 this.thenEvaluate(function() { IPython.notebook.save_notebook(); });
210
212
211 this.then(function() {
213 this.then(function() {
212 clear_and_execute(this, [
214 clear_and_execute(this, [
213 "from IPython.core.formatters import HTMLFormatter",
215 "from IPython.core.formatters import HTMLFormatter",
214 "x = HTMLFormatter()",
216 "x = HTMLFormatter()",
215 "x.format_type = 'text/superfancymimetype'",
217 "x.format_type = 'text/superfancymimetype'",
216 "get_ipython().display_formatter.formatters['text/superfancymimetype'] = x",
218 "get_ipython().display_formatter.formatters['text/superfancymimetype'] = x",
217 "from IPython.display import HTML, display",
219 "from IPython.display import HTML, display",
218 'display(HTML("yo"))',
220 'display(HTML("yo"))',
219 "HTML('hello')"].join('\n')
221 "HTML('hello')"].join('\n')
220 );
222 );
221
223
222 });
224 });
223
225
224 this.then(function ( ) {
226 this.then(function ( ) {
225 var long_name = 'text/superfancymimetype';
227 var long_name = 'text/superfancymimetype';
226 var result = this.get_output_cell(0);
228 var result = this.get_output_cell(0);
227 this.test.assertTrue(result.hasOwnProperty(long_name),
229 this.test.assertTrue(result.hasOwnProperty(long_name),
228 'display_data custom mimetype ' + long_name);
230 'display_data custom mimetype ' + long_name);
229 var result = this.get_output_cell(0, 1);
231 var result = this.get_output_cell(0, 1);
230 this.test.assertTrue(result.hasOwnProperty(long_name),
232 this.test.assertTrue(result.hasOwnProperty(long_name),
231 'pyout custom mimetype ' + long_name);
233 'pyout custom mimetype ' + long_name);
232
234
233 });
235 });
234
236
235 });
237 });
General Comments 0
You need to be logged in to leave comments. Login now