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