##// END OF EJS Templates
Fixed typo in dd test
Jonathan Frederic -
Show More
@@ -1,309 +1,309 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 26 this.click_cell(0);
27 27 this.validate_state('click cell 0', 'edit', 0);
28 28 this.click_cell(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 48 this.click_cell(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 52 this.click_cell(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 56 this.click_cell(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 65 this.click_cell(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 81 this.click_cell(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 96 this.click_cell(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 101 this.trigger_keydown('esc');
102 102 this.trigger_keydown('k');
103 103 this.validate_state('k in comand mode', 'command', base_index);
104 104 this.trigger_keydown('alt+enter'); // Creates one cell
105 105 this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1);
106 106
107 107 // Notebook will now have 8 cells, the index of the last cell will be 7.
108 108 this.test.assertEquals(this.get_cells().length, 8, '*-enter commands added cells where needed.');
109 109 this.click_cell(7);
110 110 this.trigger_keydown('esc');
111 111 this.validate_state('click cell ' + 7 + ' and esc', 'command', 7);
112 112
113 113 this.trigger_keydown('r');
114 114 this.test.assertEquals(this.get_cell(7).cell_type, 'raw', 'r; cell is raw');
115 115 this.trigger_keydown('1');
116 116 this.test.assertEquals(this.get_cell(7).cell_type, 'heading', '1; cell is heading');
117 117 this.test.assertEquals(this.get_cell(7).level, 1, '1; cell is level 1 heading');
118 118 this.trigger_keydown('2');
119 119 this.test.assertEquals(this.get_cell(7).level, 2, '2; cell is level 2 heading');
120 120 this.trigger_keydown('3');
121 121 this.test.assertEquals(this.get_cell(7).level, 3, '3; cell is level 3 heading');
122 122 this.trigger_keydown('4');
123 123 this.test.assertEquals(this.get_cell(7).level, 4, '4; cell is level 4 heading');
124 124 this.trigger_keydown('5');
125 125 this.test.assertEquals(this.get_cell(7).level, 5, '5; cell is level 5 heading');
126 126 this.trigger_keydown('6');
127 127 this.test.assertEquals(this.get_cell(7).level, 6, '6; cell is level 6 heading');
128 128 this.trigger_keydown('m');
129 129 this.test.assertEquals(this.get_cell(7).cell_type, 'markdown', 'm; cell is markdown');
130 130 this.trigger_keydown('y');
131 131 this.test.assertEquals(this.get_cell(7).cell_type, 'code', 'y; cell is code');
132 132
133 133 this.trigger_keydown('d');
134 134 this.trigger_keydown('d');
135 135 this.test.assertEquals(this.get_cells().length, 7, 'dd actually deletes a cell');
136 136 this.validate_state('dd', 'command', 6);
137 137
138 138 // Make sure that if the time between d presses is too long
139 139 this.trigger_keydown('d');
140 140 });
141 141 this.wait(1000);
142 142 this.then(function () {
143 143 this.trigger_keydown('d');
144 144
145 this.test.assertEquals(this.get_cells().length, 6, "d, 1 second wait, d doesn't delete a cell");
145 this.test.assertEquals(this.get_cells().length, 7, "d, 1 second wait, d doesn't delete a cell");
146 146 this.validate_state('d, 1 second wait, d', 'command', 6);
147 147
148 148 });
149 149
150 150 // Utility functions.
151 151 this.validate_state = function(message, mode, cell_index) {
152 152 // General tests.
153 153 this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(),
154 154 message + '; keyboard and notebook modes match');
155 155 // Is codemirror focused appropriately?
156 156 this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately');
157 157 // Is the selected cell the only cell that is selected?
158 158 if (cell_index!==undefined) {
159 159 this.test.assert(this.is_only_cell_selected(cell_index),
160 160 message + '; cell ' + cell_index + ' is the only cell selected');
161 161 }
162 162
163 163 // Mode specific tests.
164 164 if (mode==='command') {
165 165 // Are the notebook and keyboard manager in command mode?
166 166 this.test.assertEquals(this.get_keyboard_mode(), 'command',
167 167 message + '; in command mode');
168 168 // Make sure there isn't a single cell in edit mode.
169 169 this.test.assert(this.is_only_cell_edit(null),
170 170 message + '; all cells in command mode');
171 171
172 172 } else if (mode==='edit') {
173 173 // Are the notebook and keyboard manager in edit mode?
174 174 this.test.assertEquals(this.get_keyboard_mode(), 'edit',
175 175 message + '; in edit mode');
176 176 // Is the specified cell the only cell in edit mode?
177 177 if (cell_index!==undefined) {
178 178 this.test.assert(this.is_only_cell_edit(cell_index),
179 179 message + '; cell ' + cell_index + ' is the only cell in edit mode');
180 180 }
181 181
182 182 } else {
183 183 this.test.assert(false, message + '; ' + mode + ' is an unknown mode');
184 184 }
185 185 };
186 186
187 187 this.is_editor_focus_valid = function() {
188 188 var cells = this.get_cells();
189 189 for (var i = 0; i < cells.length; i++) {
190 190 if (!this.is_cell_editor_focus_valid(i)) {
191 191 return false;
192 192 }
193 193 }
194 194 return true;
195 195 };
196 196
197 197 this.is_cell_editor_focus_valid = function(index) {
198 198 var cell = this.get_cell(index);
199 199 if (cell) {
200 200 if (cell.mode == 'edit') {
201 201 return this.is_cell_editor_focused(index);
202 202 } else {
203 203 return !this.is_cell_editor_focused(index);
204 204 }
205 205 }
206 206 return true;
207 207 };
208 208
209 209
210 210 /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */
211 211
212 212
213 213
214 214 this.click_cell = function(index) {
215 215 // Code Mirror does not play nicely with emulated brower events.
216 216 // Instead of trying to emulate a click, here we run code similar to
217 217 // the code used in Code Mirror that handles the mousedown event on a
218 218 // region of codemirror that the user can focus.
219 219 this.evaluate(function (i) {
220 220 cm = IPython.notebook.get_cell(i).code_mirror;
221 221 if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input))
222 222 cm.display.input.focus();
223 223 }, {i: index});
224 224 };
225 225
226 226 this.focus_notebook = function() {
227 227 this.evaluate(function (){
228 228 $('#notebook').focus();
229 229 }, {});
230 230 };
231 231
232 232 this.trigger_keydown = function(key) {
233 233 this.evaluate(function (k) {
234 234 IPython.keyboard.trigger_keydown(k);
235 235 }, {k: key});
236 236 };
237 237
238 238 this.get_keyboard_mode = function() {
239 239 return this.evaluate(function() {
240 240 return IPython.keyboard_manager.mode;
241 241 }, {});
242 242 };
243 243
244 244 this.get_notebook_mode = function() {
245 245 return this.evaluate(function() {
246 246 return IPython.notebook.mode;
247 247 }, {});
248 248 };
249 249
250 250 this.get_cells = function() {
251 251 return this.evaluate(function() {
252 252 return IPython.notebook.get_cells();
253 253 }, {});
254 254 };
255 255
256 256 this.get_cell = function(index) {
257 257 return this.evaluate(function(i) {
258 258 var cell = IPython.notebook.get_cell(i);
259 259 if (cell) {
260 260 return cell;
261 261 }
262 262 return null;
263 263 }, {i : index});
264 264 };
265 265
266 266 this.is_cell_editor_focused = function(index) {
267 267 return this.evaluate(function(i) {
268 268 var cell = IPython.notebook.get_cell(i);
269 269 if (cell) {
270 270 return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *');
271 271 }
272 272 return false;
273 273 }, {i : index});
274 274 };
275 275
276 276 this.is_only_cell_selected = function(index) {
277 277 return this.is_only_cell_on(index, 'selected', 'unselected');
278 278 };
279 279
280 280 this.is_only_cell_edit = function(index) {
281 281 return this.is_only_cell_on(index, 'edit_mode', 'command_mode');
282 282 };
283 283
284 284 this.is_only_cell_on = function(i, on_class, off_class) {
285 285 var cells = this.get_cells();
286 286 for (var j = 0; j < cells.length; j++) {
287 287 if (j === i) {
288 288 if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) {
289 289 return false;
290 290 }
291 291 } else {
292 292 if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) {
293 293 return false;
294 294 }
295 295 }
296 296 }
297 297 return true;
298 298 };
299 299
300 300 this._has_cell_class = function(index, classes) {
301 301 return this.evaluate(function(i, c) {
302 302 var cell = IPython.notebook.get_cell(i);
303 303 if (cell) {
304 304 return cell.element.hasClass(c);
305 305 }
306 306 return false;
307 307 }, {i : index, c: classes});
308 308 };
309 309 });
General Comments 0
You need to be logged in to leave comments. Login now