Show More
@@ -1,232 +1,272 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 | ||
|
59 | // shift+enter tests. | |
|
60 | // last cell in notebook | |
|
61 | var base_index = 3; | |
|
62 | this.trigger_keydown('shift+enter'); // Creates one cell | |
|
63 | this.validate_state('shift+enter (no cell below)', 'edit', base_index + 1); | |
|
64 | // not last cell in notebook & starts in edit mode | |
|
65 | this.click_cell(base_index); | |
|
66 | this.validate_state('click cell %d' % base_index, 'edit', base_index); | |
|
58 | 67 | this.trigger_keydown('shift+enter'); |
|
59 |
this.validate_state('shift+enter ( |
|
|
60 | this.click_cell(3); | |
|
61 | this.validate_state('click cell 3', 'edit', 3); | |
|
68 | this.validate_state('shift+enter (cell exists below)', 'command', base_index + 1); | |
|
69 | // starts in command mode | |
|
70 | this.trigger_keydown('k'); | |
|
71 | this.validate_state('k in comand mode', 'command', base_index); | |
|
62 | 72 | this.trigger_keydown('shift+enter'); |
|
63 |
this.validate_state('shift+enter ( |
|
|
64 | this.click_cell(3); | |
|
65 | this.validate_state('click cell 3', 'edit', 3); | |
|
66 | this.trigger_keydown('alt+enter'); | |
|
67 | this.validate_state('alt+enter', 'edit', 4); | |
|
73 | this.validate_state('shift+enter (start in command mode)', 'command', base_index + 1); | |
|
74 | ||
|
75 | // ctrl+enter tests. | |
|
76 | // last cell in notebook | |
|
77 | base_index++; | |
|
78 | this.trigger_keydown('ctrl+enter'); | |
|
79 | this.validate_state('ctrl+enter (no cell below)', 'command', base_index); | |
|
80 | // not last cell in notebook & starts in edit mode | |
|
81 | this.click_cell(base_index-1); | |
|
82 | this.validate_state('click cell %d' % (base_index-1), 'edit', base_index-1); | |
|
68 | 83 | this.trigger_keydown('ctrl+enter'); |
|
69 |
this.validate_state('ctrl+enter', 'command', |
|
|
84 | this.validate_state('ctrl+enter (cell exists below)', 'command', base_index-1); | |
|
85 | // starts in command mode | |
|
86 | this.trigger_keydown('j'); | |
|
87 | this.validate_state('j in comand mode', 'command', base_index); | |
|
88 | this.trigger_keydown('ctrl+enter'); | |
|
89 | this.validate_state('ctrl+enter (start in command mode)', 'command', base_index); | |
|
90 | ||
|
91 | // alt+enter tests. | |
|
92 | // last cell in notebook | |
|
93 | base_index++; | |
|
94 | this.trigger_keydown('alt+enter'); // Creates one cell | |
|
95 | this.validate_state('alt+enter (no cell below)', 'edit', base_index + 1); | |
|
96 | // not last cell in notebook & starts in edit mode | |
|
97 | this.click_cell(base_index); | |
|
98 | this.validate_state('click cell %d' % base_index, 'edit', base_index); | |
|
99 | this.trigger_keydown('alt+enter'); // Creates one cell | |
|
100 | this.validate_state('alt+enter (cell exists below)', 'edit', base_index + 1); | |
|
101 | // starts in command mode | |
|
102 | this.trigger_keydown('esc'); | |
|
103 | this.trigger_keydown('k'); | |
|
104 | this.validate_state('k in comand mode', 'command', base_index); | |
|
105 | this.trigger_keydown('alt+enter'); // Creates one cell | |
|
106 | this.validate_state('alt+enter (start in command mode)', 'edit', base_index + 1); | |
|
107 | ||
|
108 | // Notebook will now have 8 cells, the index of the last cell will be 7. | |
|
109 | ||
|
70 | 110 | }); |
|
71 | 111 | |
|
72 | 112 | |
|
73 | 113 | // Utility functions. |
|
74 | 114 | this.validate_state = function(message, mode, cell_index) { |
|
75 | 115 | // General tests. |
|
76 | 116 | this.test.assertEquals(this.get_keyboard_mode(), this.get_notebook_mode(), |
|
77 | 117 | message + '; keyboard and notebook modes match'); |
|
78 | 118 | // Is codemirror focused appropriately? |
|
79 | 119 | this.test.assert(this.is_editor_focus_valid(), message + '; cell editor focused appropriately'); |
|
80 | 120 | // Is the selected cell the only cell that is selected? |
|
81 | 121 | if (cell_index!==undefined) { |
|
82 | 122 | this.test.assert(this.is_only_cell_selected(cell_index), |
|
83 | 123 | message + '; cell ' + cell_index + ' is the only cell selected'); |
|
84 | 124 | } |
|
85 | 125 | |
|
86 | 126 | // Mode specific tests. |
|
87 | 127 | if (mode==='command') { |
|
88 | 128 | // Are the notebook and keyboard manager in command mode? |
|
89 | 129 | this.test.assertEquals(this.get_keyboard_mode(), 'command', |
|
90 | 130 | message + '; in command mode'); |
|
91 | 131 | // Make sure there isn't a single cell in edit mode. |
|
92 | 132 | this.test.assert(this.is_only_cell_edit(null), |
|
93 | 133 | message + '; all cells in command mode'); |
|
94 | 134 | |
|
95 | 135 | } else if (mode==='edit') { |
|
96 | 136 | // Are the notebook and keyboard manager in edit mode? |
|
97 | 137 | this.test.assertEquals(this.get_keyboard_mode(), 'edit', |
|
98 | 138 | message + '; in edit mode'); |
|
99 | 139 | // Is the specified cell the only cell in edit mode? |
|
100 | 140 | if (cell_index!==undefined) { |
|
101 | 141 | this.test.assert(this.is_only_cell_edit(cell_index), |
|
102 | 142 | message + '; cell ' + cell_index + ' is the only cell in edit mode'); |
|
103 | 143 | } |
|
104 | 144 | |
|
105 | 145 | } else { |
|
106 | 146 | this.test.assert(false, message + '; ' + mode + ' is an unknown mode'); |
|
107 | 147 | } |
|
108 | 148 | }; |
|
109 | 149 | |
|
110 | 150 | this.is_editor_focus_valid = function() { |
|
111 | 151 | var cells = this.get_cells(); |
|
112 | 152 | for (var i = 0; i < cells.length; i++) { |
|
113 | 153 | if (!this.is_cell_editor_focus_valid(i)) { |
|
114 | 154 | return false; |
|
115 | 155 | } |
|
116 | 156 | } |
|
117 | 157 | return true; |
|
118 | 158 | }; |
|
119 | 159 | |
|
120 | 160 | this.is_cell_editor_focus_valid = function(index) { |
|
121 | 161 | var cell = this.get_cell(index); |
|
122 | 162 | if (cell) { |
|
123 | 163 | if (cell.mode == 'edit') { |
|
124 | 164 | return this.is_cell_editor_focused(index); |
|
125 | 165 | } else { |
|
126 | 166 | return !this.is_cell_editor_focused(index); |
|
127 | 167 | } |
|
128 | 168 | } |
|
129 | 169 | return true; |
|
130 | 170 | }; |
|
131 | 171 | |
|
132 | 172 | |
|
133 | 173 | /* TODO: MOVE EVERYTHING BELOW THIS LINE INTO THE BASE (utils.js) */ |
|
134 | 174 | |
|
135 | 175 | |
|
136 | 176 | |
|
137 | 177 | this.click_cell = function(index) { |
|
138 | 178 | // Code Mirror does not play nicely with emulated brower events. |
|
139 | 179 | // Instead of trying to emulate a click, here we run code similar to |
|
140 | 180 | // the code used in Code Mirror that handles the mousedown event on a |
|
141 | 181 | // region of codemirror that the user can focus. |
|
142 | 182 | this.evaluate(function (i) { |
|
143 | 183 | cm = IPython.notebook.get_cell(i).code_mirror; |
|
144 | 184 | if (cm.options.readOnly != "nocursor" && (document.activeElement != cm.display.input)) |
|
145 | 185 | cm.display.input.focus(); |
|
146 | 186 | }, {i: index}); |
|
147 | 187 | }; |
|
148 | 188 | |
|
149 | 189 | this.focus_notebook = function() { |
|
150 | 190 | this.evaluate(function (){ |
|
151 | 191 | $('#notebook').focus(); |
|
152 | 192 | }, {}); |
|
153 | 193 | }; |
|
154 | 194 | |
|
155 | 195 | this.trigger_keydown = function(key) { |
|
156 | 196 | this.evaluate(function (k) { |
|
157 | 197 | IPython.keyboard.trigger_keydown(k); |
|
158 | 198 | }, {k: key}); |
|
159 | 199 | }; |
|
160 | 200 | |
|
161 | 201 | this.get_keyboard_mode = function() { |
|
162 | 202 | return this.evaluate(function() { |
|
163 | 203 | return IPython.keyboard_manager.mode; |
|
164 | 204 | }, {}); |
|
165 | 205 | }; |
|
166 | 206 | |
|
167 | 207 | this.get_notebook_mode = function() { |
|
168 | 208 | return this.evaluate(function() { |
|
169 | 209 | return IPython.notebook.mode; |
|
170 | 210 | }, {}); |
|
171 | 211 | }; |
|
172 | 212 | |
|
173 | 213 | this.get_cells = function() { |
|
174 | 214 | return this.evaluate(function() { |
|
175 | 215 | return IPython.notebook.get_cells(); |
|
176 | 216 | }, {}); |
|
177 | 217 | }; |
|
178 | 218 | |
|
179 | 219 | this.get_cell = function(index) { |
|
180 | 220 | return this.evaluate(function(i) { |
|
181 | 221 | var cell = IPython.notebook.get_cell(i); |
|
182 | 222 | if (cell) { |
|
183 | 223 | return cell; |
|
184 | 224 | } |
|
185 | 225 | return null; |
|
186 | 226 | }, {i : index}); |
|
187 | 227 | }; |
|
188 | 228 | |
|
189 | 229 | this.is_cell_editor_focused = function(index) { |
|
190 | 230 | return this.evaluate(function(i) { |
|
191 | 231 | var cell = IPython.notebook.get_cell(i); |
|
192 | 232 | if (cell) { |
|
193 | 233 | return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *'); |
|
194 | 234 | } |
|
195 | 235 | return false; |
|
196 | 236 | }, {i : index}); |
|
197 | 237 | }; |
|
198 | 238 | |
|
199 | 239 | this.is_only_cell_selected = function(index) { |
|
200 | 240 | return this.is_only_cell_on(index, 'selected', 'unselected'); |
|
201 | 241 | }; |
|
202 | 242 | |
|
203 | 243 | this.is_only_cell_edit = function(index) { |
|
204 | 244 | return this.is_only_cell_on(index, 'edit_mode', 'command_mode'); |
|
205 | 245 | }; |
|
206 | 246 | |
|
207 | 247 | this.is_only_cell_on = function(i, on_class, off_class) { |
|
208 | 248 | var cells = this.get_cells(); |
|
209 | 249 | for (var j = 0; j < cells.length; j++) { |
|
210 | 250 | if (j === i) { |
|
211 | 251 | if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) { |
|
212 | 252 | return false; |
|
213 | 253 | } |
|
214 | 254 | } else { |
|
215 | 255 | if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) { |
|
216 | 256 | return false; |
|
217 | 257 | } |
|
218 | 258 | } |
|
219 | 259 | } |
|
220 | 260 | return true; |
|
221 | 261 | }; |
|
222 | 262 | |
|
223 | 263 | this._has_cell_class = function(index, classes) { |
|
224 | 264 | return this.evaluate(function(i, c) { |
|
225 | 265 | var cell = IPython.notebook.get_cell(i); |
|
226 | 266 | if (cell) { |
|
227 | 267 | return cell.element.hasClass(c); |
|
228 | 268 | } |
|
229 | 269 | return false; |
|
230 | 270 | }, {i : index, c: classes}); |
|
231 | 271 | }; |
|
232 | 272 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now