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