Show More
@@ -1,309 +1,370 b'' | |||
|
1 | 1 | // Test the notebook dual mode feature. |
|
2 | 2 | |
|
3 | 3 | // Test |
|
4 | 4 | casper.notebook_test(function () { |
|
5 | 5 | var index = this.append_cell('print("a")'); |
|
6 | 6 | this.execute_cell_then(index); |
|
7 | 7 | index = this.append_cell('print("b")'); |
|
8 | 8 | this.execute_cell_then(index); |
|
9 | 9 | index = this.append_cell('print("c")'); |
|
10 | 10 | this.execute_cell_then(index); |
|
11 | 11 | |
|
12 | 12 | this.then(function () { |
|
13 | 13 | this.validate_state('initial state', 'edit', 0); |
|
14 | 14 | this.trigger_keydown('esc'); |
|
15 | 15 | this.validate_state('esc', 'command', 0); |
|
16 | 16 | this.trigger_keydown('down'); |
|
17 | 17 | this.validate_state('down', 'command', 1); |
|
18 | 18 | this.trigger_keydown('enter'); |
|
19 | 19 | this.validate_state('enter', 'edit', 1); |
|
20 | 20 | this.trigger_keydown('j'); |
|
21 | 21 | this.validate_state('j in edit mode', 'edit', 1); |
|
22 | 22 | this.trigger_keydown('esc'); |
|
23 | 23 | this.validate_state('esc', 'command', 1); |
|
24 | 24 | this.trigger_keydown('j'); |
|
25 | 25 | this.validate_state('j in command mode', 'command', 2); |
|
26 | this.click_cell(0); | |
|
26 | this.click_cell_editor(0); | |
|
27 | 27 | this.validate_state('click cell 0', 'edit', 0); |
|
28 | this.click_cell(3); | |
|
28 | this.click_cell_editor(3); | |
|
29 | 29 | this.validate_state('click cell 3', 'edit', 3); |
|
30 | 30 | this.trigger_keydown('esc'); |
|
31 | 31 | this.validate_state('esc', 'command', 3); |
|
32 | 32 | |
|
33 | 33 | // Open keyboard help |
|
34 | 34 | this.evaluate(function(){ |
|
35 | 35 | $('#keyboard_shortcuts a').click(); |
|
36 | 36 | }, {}); |
|
37 | 37 | |
|
38 | 38 | this.trigger_keydown('k'); |
|
39 | 39 | this.validate_state('k in command mode while keyboard help is up', 'command', 3); |
|
40 | 40 | |
|
41 | 41 | // Close keyboard help |
|
42 | 42 | this.evaluate(function(){ |
|
43 | 43 | $('div.modal button.close').click(); |
|
44 | 44 | }, {}); |
|
45 | 45 | |
|
46 | 46 | this.trigger_keydown('k'); |
|
47 | 47 | this.validate_state('k in command mode', 'command', 2); |
|
48 | this.click_cell(0); | |
|
48 | this.click_cell_editor(0); | |
|
49 | 49 | this.validate_state('click cell 0', 'edit', 0); |
|
50 | 50 | this.focus_notebook(); |
|
51 | 51 | this.validate_state('focus #notebook', 'command', 0); |
|
52 | this.click_cell(0); | |
|
52 | this.click_cell_editor(0); | |
|
53 | 53 | this.validate_state('click cell 0', 'edit', 0); |
|
54 | 54 | this.focus_notebook(); |
|
55 | 55 | this.validate_state('focus #notebook', 'command', 0); |
|
56 | this.click_cell(3); | |
|
56 | this.click_cell_editor(3); | |
|
57 | 57 | this.validate_state('click cell 3', 'edit', 3); |
|
58 | 58 | |
|
59 | 59 | // shift+enter tests. |
|
60 | 60 | // last cell in notebook |
|
61 | 61 | var base_index = 3; |
|
62 | 62 | this.trigger_keydown('shift+enter'); // Creates one cell |
|
63 | 63 | this.validate_state('shift+enter (no cell below)', 'edit', base_index + 1); |
|
64 | 64 | // not last cell in notebook & starts in edit mode |
|
65 | this.click_cell(base_index); | |
|
65 | this.click_cell_editor(base_index); | |
|
66 | 66 | this.validate_state('click cell ' + base_index, 'edit', base_index); |
|
67 | 67 | this.trigger_keydown('shift+enter'); |
|
68 | 68 | this.validate_state('shift+enter (cell exists below)', 'command', base_index + 1); |
|
69 | 69 | // starts in command mode |
|
70 | 70 | this.trigger_keydown('k'); |
|
71 | 71 | this.validate_state('k in comand mode', 'command', base_index); |
|
72 | 72 | this.trigger_keydown('shift+enter'); |
|
73 | 73 | this.validate_state('shift+enter (start in command mode)', 'command', base_index + 1); |
|
74 | 74 | |
|
75 | 75 | // ctrl+enter tests. |
|
76 | 76 | // last cell in notebook |
|
77 | 77 | base_index++; |
|
78 | 78 | this.trigger_keydown('ctrl+enter'); |
|
79 | 79 | this.validate_state('ctrl+enter (no cell below)', 'command', base_index); |
|
80 | 80 | // not last cell in notebook & starts in edit mode |
|
81 | this.click_cell(base_index-1); | |
|
81 | this.click_cell_editor(base_index-1); | |
|
82 | 82 | this.validate_state('click cell ' + (base_index-1), 'edit', base_index-1); |
|
83 | 83 | this.trigger_keydown('ctrl+enter'); |
|
84 | 84 | this.validate_state('ctrl+enter (cell exists below)', 'command', base_index-1); |
|
85 | 85 | // starts in command mode |
|
86 | 86 | this.trigger_keydown('j'); |
|
87 | 87 | this.validate_state('j in comand mode', 'command', base_index); |
|
88 | 88 | this.trigger_keydown('ctrl+enter'); |
|
89 | 89 | this.validate_state('ctrl+enter (start in command mode)', 'command', base_index); |
|
90 | 90 | |
|
91 | 91 | // alt+enter tests. |
|
92 | 92 | // last cell in notebook |
|
93 | 93 | this.trigger_keydown('alt+enter'); // Creates one cell |
|
94 | 94 | this.validate_state('alt+enter (no cell below)', 'edit', base_index + 1); |
|
95 | 95 | // not last cell in notebook & starts in edit mode |
|
96 | this.click_cell(base_index); | |
|
96 | this.click_cell_editor(base_index); | |
|
97 | 97 | this.validate_state('click cell ' + base_index, 'edit', base_index); |
|
98 | 98 | this.trigger_keydown('alt+enter'); // Creates one cell |
|
99 | 99 | this.validate_state('alt+enter (cell exists below)', 'edit', base_index + 1); |
|
100 | 100 | // starts in command mode |
|
101 | this.trigger_keydown('esc'); | |
|
102 | this.trigger_keydown('k'); | |
|
101 | this.trigger_keydown('esc', 'k'); | |
|
103 | 102 | this.validate_state('k in comand mode', 'command', base_index); |
|
104 | 103 | this.trigger_keydown('alt+enter'); // Creates one cell |
|
105 | 104 | this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1); |
|
106 | 105 | |
|
107 | 106 | // Notebook will now have 8 cells, the index of the last cell will be 7. |
|
108 | 107 | this.test.assertEquals(this.get_cells().length, 8, '*-enter commands added cells where needed.'); |
|
109 |
this. |
|
|
110 | this.trigger_keydown('esc'); | |
|
108 | this.select_cell(7); | |
|
111 | 109 | this.validate_state('click cell ' + 7 + ' and esc', 'command', 7); |
|
112 | 110 | |
|
113 | 111 | this.trigger_keydown('r'); |
|
114 | 112 | this.test.assertEquals(this.get_cell(7).cell_type, 'raw', 'r; cell is raw'); |
|
115 | 113 | this.trigger_keydown('1'); |
|
116 | 114 | this.test.assertEquals(this.get_cell(7).cell_type, 'heading', '1; cell is heading'); |
|
117 | 115 | this.test.assertEquals(this.get_cell(7).level, 1, '1; cell is level 1 heading'); |
|
118 | 116 | this.trigger_keydown('2'); |
|
119 | 117 | this.test.assertEquals(this.get_cell(7).level, 2, '2; cell is level 2 heading'); |
|
120 | 118 | this.trigger_keydown('3'); |
|
121 | 119 | this.test.assertEquals(this.get_cell(7).level, 3, '3; cell is level 3 heading'); |
|
122 | 120 | this.trigger_keydown('4'); |
|
123 | 121 | this.test.assertEquals(this.get_cell(7).level, 4, '4; cell is level 4 heading'); |
|
124 | 122 | this.trigger_keydown('5'); |
|
125 | 123 | this.test.assertEquals(this.get_cell(7).level, 5, '5; cell is level 5 heading'); |
|
126 | 124 | this.trigger_keydown('6'); |
|
127 | 125 | this.test.assertEquals(this.get_cell(7).level, 6, '6; cell is level 6 heading'); |
|
128 | 126 | this.trigger_keydown('m'); |
|
129 | 127 | this.test.assertEquals(this.get_cell(7).cell_type, 'markdown', 'm; cell is markdown'); |
|
130 | 128 | this.trigger_keydown('y'); |
|
131 | 129 | this.test.assertEquals(this.get_cell(7).cell_type, 'code', 'y; cell is code'); |
|
132 | 130 | |
|
133 | this.trigger_keydown('d'); | |
|
134 | this.trigger_keydown('d'); | |
|
131 | this.trigger_keydown('d', 'd'); | |
|
135 | 132 | this.test.assertEquals(this.get_cells().length, 7, 'dd actually deletes a cell'); |
|
136 | 133 | this.validate_state('dd', 'command', 6); |
|
137 | 134 | |
|
138 | 135 | // Make sure that if the time between d presses is too long |
|
139 | 136 | this.trigger_keydown('d'); |
|
140 | 137 | }); |
|
141 | 138 | this.wait(1000); |
|
142 | 139 | this.then(function () { |
|
143 | 140 | this.trigger_keydown('d'); |
|
144 | 141 | |
|
145 | 142 | this.test.assertEquals(this.get_cells().length, 7, "d, 1 second wait, d doesn't delete a cell"); |
|
146 | 143 | this.validate_state('d, 1 second wait, d', 'command', 6); |
|
144 | this.trigger_keydown('j'); | |
|
145 | this.validate_state('j at end of notebook', 'command', 6); | |
|
146 | this.trigger_keydown('down'); | |
|
147 | this.validate_state('down at end of notebook', 'command', 6); | |
|
148 | this.trigger_keydown('up'); | |
|
149 | this.validate_state('up', 'command', 5); | |
|
150 | this.select_cell(0); | |
|
151 | this.validate_state('select 0', 'command', 0); | |
|
152 | this.trigger_keydown('k'); | |
|
153 | this.validate_state('k at top of notebook', 'command', 0); | |
|
154 | this.trigger_keydown('up'); | |
|
155 | this.validate_state('up at top of notebook', 'command', 0); | |
|
156 | this.trigger_keydown('down'); | |
|
157 | this.validate_state('down', 'command', 1); | |
|
158 | ||
|
159 | this.click_cell_editor(6); | |
|
160 | this.validate_state('click cell 6', 'edit', 6); | |
|
161 | this.trigger_keydown('down'); | |
|
162 | this.validate_state('down at end of notebook', 'edit', 6); | |
|
163 | this.trigger_keydown('up'); | |
|
164 | this.validate_state('up', 'edit', 5); | |
|
165 | this.click_cell_editor(0); | |
|
166 | this.validate_state('click 0', 'edit', 0); | |
|
167 | this.trigger_keydown('up'); | |
|
168 | this.validate_state('up at top of notebook', 'edit', 0); | |
|
169 | this.trigger_keydown('down'); | |
|
170 | this.validate_state('down', 'edit', 1); | |
|
171 | ||
|
172 | this.select_cell(6); | |
|
173 | this.validate_state('select 6', 'command', 6); | |
|
174 | this.trigger_keydown('m'); | |
|
175 | this.test.assertEquals(this.get_cell(6).cell_type, 'markdown', 'm; cell is markdown'); | |
|
176 | this.test.assertEquals(this.get_cell(6).rendered, true, 'm; cell is rendered'); | |
|
177 | this.trigger_keydown('enter'); | |
|
178 | this.test.assertEquals(this.get_cell(6).rendered, false, 'enter; cell is unrendered'); | |
|
179 | this.validate_state('enter', 'edit', 6); | |
|
180 | this.trigger_keydown('ctrl+enter'); | |
|
181 | this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered'); | |
|
182 | this.validate_state('enter', 'command', 6); | |
|
183 | this.trigger_keydown('enter'); | |
|
184 | this.test.assertEquals(this.get_cell(6).rendered, false, 'enter; cell is unrendered'); | |
|
185 | this.select_cell(5); | |
|
186 | this.test.assertEquals(this.get_cell(6).rendered, false, 'select 5; cell 6 is still unrendered'); | |
|
187 | this.validate_state('select 5', 'command', 5); | |
|
188 | this.select_cell(6); | |
|
189 | this.validate_state('select 6', 'command', 5); | |
|
190 | this.trigger_keydown('ctrl+enter'); | |
|
191 | this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered'); | |
|
192 | this.select_cell(5); | |
|
193 | this.validate_state('select 5', 'command', 5); | |
|
194 | this.trigger_keydown('shift+enter'); | |
|
195 | this.validate_state('shift+enter', 'command', 6); | |
|
196 | this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered'); | |
|
197 | this.trigger_keydown('shift+enter'); // Creates one cell | |
|
198 | this.validate_state('shift+enter', 'edit', 7); | |
|
199 | this.test.assertEquals(this.get_cell(6).rendered, true, 'enter; cell is rendered'); | |
|
200 | ||
|
147 | 201 | |
|
148 | 202 | }); |
|
149 | 203 | |
|
150 | 204 | // Utility functions. |
|
151 | 205 | this.validate_state = function(message, mode, cell_index) { |
|
152 | 206 | // General tests. |
|
153 | 207 | this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(), |
|
154 | 208 | message + '; keyboard and notebook modes match'); |
|
155 | 209 | // Is codemirror focused appropriately? |
|
156 | 210 | this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately'); |
|
157 | 211 | // Is the selected cell the only cell that is selected? |
|
158 | 212 | if (cell_index!==undefined) { |
|
159 | 213 | this.test.assert(this.is_only_cell_selected(cell_index), |
|
160 | 214 | message + '; cell ' + cell_index + ' is the only cell selected'); |
|
161 | 215 | } |
|
162 | 216 | |
|
163 | 217 | // Mode specific tests. |
|
164 | 218 | if (mode==='command') { |
|
165 | 219 | // Are the notebook and keyboard manager in command mode? |
|
166 | 220 | this.test.assertEquals(this.get_keyboard_mode(), 'command', |
|
167 | 221 | message + '; in command mode'); |
|
168 | 222 | // Make sure there isn't a single cell in edit mode. |
|
169 | 223 | this.test.assert(this.is_only_cell_edit(null), |
|
170 | 224 | message + '; all cells in command mode'); |
|
171 | 225 | |
|
172 | 226 | } else if (mode==='edit') { |
|
173 | 227 | // Are the notebook and keyboard manager in edit mode? |
|
174 | 228 | this.test.assertEquals(this.get_keyboard_mode(), 'edit', |
|
175 | 229 | message + '; in edit mode'); |
|
176 | 230 | // Is the specified cell the only cell in edit mode? |
|
177 | 231 | if (cell_index!==undefined) { |
|
178 | 232 | this.test.assert(this.is_only_cell_edit(cell_index), |
|
179 | 233 | message + '; cell ' + cell_index + ' is the only cell in edit mode'); |
|
180 | 234 | } |
|
181 | 235 | |
|
182 | 236 | } else { |
|
183 | 237 | this.test.assert(false, message + '; ' + mode + ' is an unknown mode'); |
|
184 | 238 | } |
|
185 | 239 | }; |
|
186 | 240 | |
|
187 | 241 | this.is_editor_focus_valid = function() { |
|
188 | 242 | var cells = this.get_cells(); |
|
189 | 243 | for (var i = 0; i < cells.length; i++) { |
|
190 | 244 | if (!this.is_cell_editor_focus_valid(i)) { |
|
191 | 245 | return false; |
|
192 | 246 | } |
|
193 | 247 | } |
|
194 | 248 | return true; |
|
195 | 249 | }; |
|
196 | 250 | |
|
197 | 251 | this.is_cell_editor_focus_valid = function(index) { |
|
198 | 252 | var cell = this.get_cell(index); |
|
199 | 253 | if (cell) { |
|
200 | 254 | if (cell.mode == 'edit') { |
|
201 | 255 | return this.is_cell_editor_focused(index); |
|
202 | 256 | } else { |
|
203 | 257 | return !this.is_cell_editor_focused(index); |
|
204 | 258 | } |
|
205 | 259 | } |
|
206 | 260 | return true; |
|
207 | 261 | }; |
|
208 | 262 | |
|
209 | 263 | |
|
210 | 264 | /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */ |
|
211 | 265 | |
|
212 | 266 | |
|
267 | this.select_cell = function (index) { | |
|
268 | this.evaluate(function (i) { | |
|
269 | IPython.notebook.select(i); | |
|
270 | }, {i: index}); | |
|
271 | }; | |
|
213 | 272 | |
|
214 | this.click_cell = function(index) { | |
|
273 | this.click_cell_editor = function(index) { | |
|
215 | 274 | // Code Mirror does not play nicely with emulated brower events. |
|
216 | 275 | // Instead of trying to emulate a click, here we run code similar to |
|
217 | 276 | // the code used in Code Mirror that handles the mousedown event on a |
|
218 | 277 | // region of codemirror that the user can focus. |
|
219 | 278 | this.evaluate(function (i) { |
|
220 | 279 | cm = IPython.notebook.get_cell(i).code_mirror; |
|
221 | 280 | if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input)) |
|
222 | 281 | cm.display.input.focus(); |
|
223 | 282 | }, {i: index}); |
|
224 | 283 | }; |
|
225 | 284 | |
|
226 | 285 | this.focus_notebook = function() { |
|
227 | 286 | this.evaluate(function (){ |
|
228 | 287 | $('#notebook').focus(); |
|
229 | 288 | }, {}); |
|
230 | 289 | }; |
|
231 | 290 | |
|
232 |
this.trigger_keydown = function( |
|
|
291 | this.trigger_keydown = function() { | |
|
292 | for (var i = 0; i < arguments.length; i++) { | |
|
233 | 293 | this.evaluate(function (k) { |
|
234 | 294 | IPython.keyboard.trigger_keydown(k); |
|
235 |
}, {k: |
|
|
295 | }, {k: arguments[i]}); | |
|
296 | } | |
|
236 | 297 | }; |
|
237 | 298 | |
|
238 | 299 | this.get_keyboard_mode = function() { |
|
239 | 300 | return this.evaluate(function() { |
|
240 | 301 | return IPython.keyboard_manager.mode; |
|
241 | 302 | }, {}); |
|
242 | 303 | }; |
|
243 | 304 | |
|
244 | 305 | this.get_notebook_mode = function() { |
|
245 | 306 | return this.evaluate(function() { |
|
246 | 307 | return IPython.notebook.mode; |
|
247 | 308 | }, {}); |
|
248 | 309 | }; |
|
249 | 310 | |
|
250 | 311 | this.get_cells = function() { |
|
251 | 312 | return this.evaluate(function() { |
|
252 | 313 | return IPython.notebook.get_cells(); |
|
253 | 314 | }, {}); |
|
254 | 315 | }; |
|
255 | 316 | |
|
256 | 317 | this.get_cell = function(index) { |
|
257 | 318 | return this.evaluate(function(i) { |
|
258 | 319 | var cell = IPython.notebook.get_cell(i); |
|
259 | 320 | if (cell) { |
|
260 | 321 | return cell; |
|
261 | 322 | } |
|
262 | 323 | return null; |
|
263 | 324 | }, {i : index}); |
|
264 | 325 | }; |
|
265 | 326 | |
|
266 | 327 | this.is_cell_editor_focused = function(index) { |
|
267 | 328 | return this.evaluate(function(i) { |
|
268 | 329 | var cell = IPython.notebook.get_cell(i); |
|
269 | 330 | if (cell) { |
|
270 | 331 | return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *'); |
|
271 | 332 | } |
|
272 | 333 | return false; |
|
273 | 334 | }, {i : index}); |
|
274 | 335 | }; |
|
275 | 336 | |
|
276 | 337 | this.is_only_cell_selected = function(index) { |
|
277 | 338 | return this.is_only_cell_on(index, 'selected', 'unselected'); |
|
278 | 339 | }; |
|
279 | 340 | |
|
280 | 341 | this.is_only_cell_edit = function(index) { |
|
281 | 342 | return this.is_only_cell_on(index, 'edit_mode', 'command_mode'); |
|
282 | 343 | }; |
|
283 | 344 | |
|
284 | 345 | this.is_only_cell_on = function(i, on_class, off_class) { |
|
285 | 346 | var cells = this.get_cells(); |
|
286 | 347 | for (var j = 0; j < cells.length; j++) { |
|
287 | 348 | if (j === i) { |
|
288 | 349 | if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) { |
|
289 | 350 | return false; |
|
290 | 351 | } |
|
291 | 352 | } else { |
|
292 | 353 | if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) { |
|
293 | 354 | return false; |
|
294 | 355 | } |
|
295 | 356 | } |
|
296 | 357 | } |
|
297 | 358 | return true; |
|
298 | 359 | }; |
|
299 | 360 | |
|
300 | 361 | this._has_cell_class = function(index, classes) { |
|
301 | 362 | return this.evaluate(function(i, c) { |
|
302 | 363 | var cell = IPython.notebook.get_cell(i); |
|
303 | 364 | if (cell) { |
|
304 | 365 | return cell.element.hasClass(c); |
|
305 | 366 | } |
|
306 | 367 | return false; |
|
307 | 368 | }, {i : index, c: classes}); |
|
308 | 369 | }; |
|
309 | 370 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now