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