##// END OF EJS Templates
Move code into is_cell_editor_focused
Jonathan Frederic -
Show More
@@ -1,231 +1,227 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 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 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 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 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 120 this.is_cell_editor_focus_valid = function(index) {
121 121 var cell = this.get_cell(index);
122 122 if (cell) {
123 123 if (cell.mode == 'edit') {
124 124 return this.is_cell_editor_focused(index);
125 125 } else {
126 126 return !this.is_cell_editor_focused(index);
127 127 }
128 128 }
129 129 return true;
130 130 };
131 131
132 132 this.is_cell_selected = function(index) {
133 133 return this.is_cell_on(index, 'selected', 'unselected');
134 134 };
135 135
136 136 this.is_cell_edit = function(index) {
137 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 164 this.is_cell_editor_focused = function(index) {
165 return this._is_cell_inputfield(index, '.CodeMirror-focused *');
165 return this.evaluate(function(i) {
166 var cell = IPython.notebook.get_cell(i);
167 if (cell) {
168 return $(cell.code_mirror.getInputField()).is('.CodeMirror-focused *');
169 }
170 return false;
171 }, {i : index});
166 172 };
167 173
168 174 this.is_cell_on = function(i, on_class, off_class) {
169 175 var cells = this.get_cells();
170 176 for (var j = 0; j < cells.length; j++) {
171 177 if (j === i) {
172 178 if (this._has_cell_class(j, off_class) || !this._has_cell_class(j, on_class)) {
173 179 return false;
174 180 }
175 181 } else {
176 182 if (!this._has_cell_class(j, off_class) || this._has_cell_class(j, on_class)) {
177 183 return false;
178 184 }
179 185 }
180 186 }
181 187 return true;
182 188 };
183 189
184 190 this.get_keyboard_mode = function() {
185 191 return this.evaluate(function() {
186 192 return IPython.keyboard_manager.mode;
187 193 }, {});
188 194 };
189 195
190 196 this.get_notebook_mode = function() {
191 197 return this.evaluate(function() {
192 198 return IPython.notebook.mode;
193 199 }, {});
194 200 };
195 201
196 202 this.get_cells = function() {
197 203 return this.evaluate(function() {
198 204 return IPython.notebook.get_cells();
199 205 }, {});
200 206 };
201 207
202 208 this.get_cell = function(index) {
203 209 return this.evaluate(function(i) {
204 210 var cell = IPython.notebook.get_cell(i);
205 211 if (cell) {
206 212 return cell;
207 213 }
208 214 return null;
209 215 }, {i : index});
210 216 };
211 217
212 this._is_cell_inputfield = function(index, selector) {
213 return this.evaluate(function(i, s) {
214 var cell = IPython.notebook.get_cell(i);
215 if (cell) {
216 return $(cell.code_mirror.getInputField()).is(s);
217 }
218 return false;
219 }, {i : index, s: selector});
220 };
221
222 218 this._has_cell_class = function(index, classes) {
223 219 return this.evaluate(function(i, c) {
224 220 var cell = IPython.notebook.get_cell(i);
225 221 if (cell) {
226 222 return cell.element.hasClass(c);
227 223 }
228 224 return false;
229 225 }, {i : index, c: classes});
230 226 };
231 227 });
General Comments 0
You need to be logged in to leave comments. Login now