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