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