001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package java_code_folding;
043:
044: import java.awt.event.KeyEvent;
045: import javax.swing.KeyStroke;
046: import javax.swing.text.JTextComponent;
047: import junit.textui.TestRunner;
048: import org.netbeans.jellytools.EditorOperator;
049: import org.netbeans.jemmy.operators.JEditorPaneOperator;
050: import org.netbeans.jemmy.operators.JTextComponentOperator;
051:
052: /**
053: * Test behavior of navigation through java code folds.
054: *
055: * Test covers following actions:
056: * caret-forward [RIGHT]
057: * caret-backward [LEFT]
058: * caret-down [DOWN]
059: * caret-up [UP]
060: * selection-forward [SHIFT-RIGHT]
061: * selection-backward [SHIFT-LEFT]
062: * selection-down [SHIFT-DOWN]
063: * selection-up [SHIFT-UP]
064: * caret-begin-line [HOME]
065: * caret-end-line [END]
066: * selection-begin-line [SHIFT-HOME]
067: * selection-end-line [SHIFT-END]
068: *
069: * Actions:
070: * caret-next-word [CTRL-RIGHT]
071: * caret-previous-word [CTRL-LEFT]
072: * selection-next-word [CTRL-SHIFT-RIGHT]
073: * selection-previous-word [CTRL-SHIFT-LEFT]
074: * should be added to testcase after issue #47454 will be fixed
075: *
076: * @author Martin Roskanin
077: */
078: public class JavaFoldsNavigationTest extends JavaCodeFoldingTest {
079:
080: private JEditorPaneOperator txtOper;
081: private EditorOperator editor;
082:
083: /** Creates a new instance of Main */
084: public JavaFoldsNavigationTest(String testMethodName) {
085: super (testMethodName);
086: }
087:
088: private ValueResolver getResolver(
089: final JEditorPaneOperator txtOper, final int etalon) {
090: ValueResolver resolver = new ValueResolver() {
091: public Object getValue() {
092: int newCaretPos = txtOper.getCaretPosition();
093: return (newCaretPos == etalon) ? Boolean.TRUE
094: : Boolean.FALSE;
095: }
096: };
097:
098: return resolver;
099: }
100:
101: private void checkActionByKeyStroke(int key, int mod,
102: int caretPosToSet, int etalon, boolean checkSelection) {
103: if (caretPosToSet == -1) {
104: caretPosToSet = txtOper.getCaretPosition();
105: } else {
106: editor.setCaretPosition(caretPosToSet);
107: txtOper.getCaret().setMagicCaretPosition(null);
108: }
109: txtOper.pushKey(key, mod);
110: waitMaxMilisForValue(3500, getResolver(txtOper, etalon),
111: Boolean.TRUE);
112: int newCaretOffset = txtOper.getCaretPosition();
113: if (checkSelection) {
114: int selectionStart = txtOper.getSelectionStart();
115: int selectionEnd = txtOper.getSelectionEnd();
116: if (selectionStart != Math.min(caretPosToSet, etalon)
117: || selectionEnd != Math.max(caretPosToSet, etalon)) {
118: String keyString = KeyStroke.getKeyStroke(key, mod)
119: .toString();
120: //fail(keyString+": Action failed: [etalon/newCaretOffset/selectionStart/selectionEnd]: ["+etalon+"/"+
121: // newCaretOffset+"/"+selectionStart+"/"+selectionEnd+"]");
122: System.out
123: .println(keyString
124: + ": Action failed: [etalon/newCaretOffset/selectionStart/selectionEnd]: ["
125: + etalon + "/" + newCaretOffset + "/"
126: + selectionStart + "/" + selectionEnd
127: + "]");
128: }
129: } else {
130: if (etalon != newCaretOffset) {
131: String keyString = KeyStroke.getKeyStroke(key, mod)
132: .toString();
133: //fail(keyString+": Action failed: [etalon/newCaretOffset]: ["+etalon+"/"+
134: // newCaretOffset+"]");
135: System.out.println(keyString
136: + ": Action failed: [etalon/newCaretOffset]: ["
137: + etalon + "/" + newCaretOffset + "]");
138: }
139: }
140: }
141:
142: public void testJavaFoldsNavigation() {
143: openDefaultProject();
144: openDefaultSampleFile();
145: try {
146: editor = getDefaultSampleEditorOperator();
147: JTextComponentOperator txtCompOper = new JTextComponentOperator(
148: editor);
149: JTextComponent target = (JTextComponent) txtCompOper
150: .getSource();
151: txtOper = editor.txtEditorPane();
152:
153: // wait max. 6 second for code folding initialization
154: waitForFolding(target, 6000);
155:
156: //01 collapse initial comment fold. [ */|]
157: // check caret left action
158: collapseFoldAtCaretPosition(editor, 4, 4); // 4,4 -caret offset 70
159:
160: // check left
161: checkActionByKeyStroke(KeyEvent.VK_LEFT, 0, 70, 0, false);
162:
163: //check selectin
164: checkActionByKeyStroke(KeyEvent.VK_LEFT,
165: KeyEvent.SHIFT_DOWN_MASK, 70, 0, true);
166:
167: // check caret right action
168: checkActionByKeyStroke(KeyEvent.VK_RIGHT, 0, 0, 70, false);
169:
170: // check caret right action, selection
171: checkActionByKeyStroke(KeyEvent.VK_RIGHT,
172: KeyEvent.SHIFT_DOWN_MASK, 0, 70, true);
173:
174: // check home action
175: checkActionByKeyStroke(KeyEvent.VK_HOME, 0, 70, 0, false);
176:
177: // check home action, selection
178: checkActionByKeyStroke(KeyEvent.VK_HOME,
179: KeyEvent.SHIFT_DOWN_MASK, 70, 0, true);
180:
181: // check end action
182: checkActionByKeyStroke(KeyEvent.VK_END, 0, 0, 70, false);
183:
184: // check end action, selection
185: checkActionByKeyStroke(KeyEvent.VK_END,
186: KeyEvent.SHIFT_DOWN_MASK, 0, 70, true);
187:
188: // check up action
189: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 71, 0, false);
190:
191: // check up action, selection
192: checkActionByKeyStroke(KeyEvent.VK_UP,
193: KeyEvent.SHIFT_DOWN_MASK, 71, 0, true);
194:
195: // check down action
196: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 0, 71, false);
197:
198: // check down action, selection
199: checkActionByKeyStroke(KeyEvent.VK_DOWN,
200: KeyEvent.SHIFT_DOWN_MASK, 0, 71, true);
201:
202: // checking end of fold
203: // check up action
204: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 78, 70, false);
205:
206: // check up action, selection
207: checkActionByKeyStroke(KeyEvent.VK_UP,
208: KeyEvent.SHIFT_DOWN_MASK, 78, 70, true);
209:
210: // check down action
211: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 70, 78, false);
212:
213: // check down action, selection
214: checkActionByKeyStroke(KeyEvent.VK_DOWN,
215: KeyEvent.SHIFT_DOWN_MASK, 70, 78, true);
216:
217: // check magic position
218: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 80, 70, false);
219: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, -1, 80, false);
220:
221: // ------------------------------------------------------------------------
222:
223: // check actions on one-line fold
224: collapseFoldAtCaretPosition(editor, 25, 13); // 25,13 - caret offset 422
225:
226: // check left
227: checkActionByKeyStroke(KeyEvent.VK_LEFT, 0, 454, 414, false);
228:
229: //check selectin
230: checkActionByKeyStroke(KeyEvent.VK_LEFT,
231: KeyEvent.SHIFT_DOWN_MASK, 454, 414, true);
232:
233: // check caret right action
234: checkActionByKeyStroke(KeyEvent.VK_RIGHT, 0, 414, 454,
235: false);
236:
237: // check caret right action, selection
238: checkActionByKeyStroke(KeyEvent.VK_RIGHT,
239: KeyEvent.SHIFT_DOWN_MASK, 414, 454, true);
240:
241: // check home action
242: checkActionByKeyStroke(KeyEvent.VK_HOME, 0, 454, 414, false);
243:
244: // check home action, selection
245: checkActionByKeyStroke(KeyEvent.VK_HOME,
246: KeyEvent.SHIFT_DOWN_MASK, 454, 414, true);
247:
248: // check end action
249: checkActionByKeyStroke(KeyEvent.VK_END, 0, 414, 454, false);
250:
251: // check end action, selection
252: checkActionByKeyStroke(KeyEvent.VK_END,
253: KeyEvent.SHIFT_DOWN_MASK, 414, 454, true);
254:
255: // check up action
256: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 459, 414, false);
257:
258: // check up action, selection
259: checkActionByKeyStroke(KeyEvent.VK_UP,
260: KeyEvent.SHIFT_DOWN_MASK, 459, 414, true);
261:
262: // check down action
263: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 414, 459, false);
264:
265: // check down action, selection
266: checkActionByKeyStroke(KeyEvent.VK_DOWN,
267: KeyEvent.SHIFT_DOWN_MASK, 414, 459, true);
268:
269: // checking end of fold
270: // check up action
271: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 467, 454, false);
272:
273: // check up action, selection
274: checkActionByKeyStroke(KeyEvent.VK_UP,
275: KeyEvent.SHIFT_DOWN_MASK, 467, 454, true);
276:
277: // check down action
278: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 454, 467, false);
279:
280: // check down action, selection
281: checkActionByKeyStroke(KeyEvent.VK_DOWN,
282: KeyEvent.SHIFT_DOWN_MASK, 454, 467, true);
283:
284: // check magic position
285: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 469, 454, false);
286: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, -1, 469, false);
287:
288: //----------------------------------------------------------------
289: //check multi fold on line
290:
291: collapseFoldAtCaretPosition(editor, 36, 86); // 36,84 -caret offset 920
292:
293: // check left
294: checkActionByKeyStroke(KeyEvent.VK_LEFT, 0, 920, 917, false);
295:
296: //check selectin
297: checkActionByKeyStroke(KeyEvent.VK_LEFT,
298: KeyEvent.SHIFT_DOWN_MASK, 920, 917, true);
299:
300: // check caret right action
301: checkActionByKeyStroke(KeyEvent.VK_RIGHT, 0, 917, 920,
302: false);
303:
304: // check caret right action, selection
305: checkActionByKeyStroke(KeyEvent.VK_RIGHT,
306: KeyEvent.SHIFT_DOWN_MASK, 917, 920, true);
307:
308: // check home action
309: checkActionByKeyStroke(KeyEvent.VK_HOME, 0, 920, 839, false);
310:
311: // check home action, selection
312: checkActionByKeyStroke(KeyEvent.VK_HOME,
313: KeyEvent.SHIFT_DOWN_MASK, 920, 839, true);
314:
315: // check end action
316: checkActionByKeyStroke(KeyEvent.VK_END, 0, 917, 949, false);
317:
318: // check end action, selection
319: checkActionByKeyStroke(KeyEvent.VK_END,
320: KeyEvent.SHIFT_DOWN_MASK, 917, 949, true);
321:
322: // check up action
323: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 1032, 917, false);
324:
325: // check up action, selection
326: checkActionByKeyStroke(KeyEvent.VK_UP,
327: KeyEvent.SHIFT_DOWN_MASK, 1032, 917, true);
328:
329: // check down action
330: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 917, 1032,
331: false);
332:
333: // check down action, selection
334: checkActionByKeyStroke(KeyEvent.VK_DOWN,
335: KeyEvent.SHIFT_DOWN_MASK, 917, 1032, true);
336:
337: // checking end of fold
338: // check up action
339: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 1035, 920, false);
340:
341: // check up action, selection
342: checkActionByKeyStroke(KeyEvent.VK_UP,
343: KeyEvent.SHIFT_DOWN_MASK, 1035, 920, true);
344:
345: // check down action
346: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 920, 1037,
347: false);
348:
349: // check down action, selection
350: checkActionByKeyStroke(KeyEvent.VK_DOWN,
351: KeyEvent.SHIFT_DOWN_MASK, 920, 1037, true);
352:
353: // check magic position
354: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 1033, 917, false);
355: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, -1, 1033, false);
356:
357: } finally {
358: closeFileWithDiscard();
359: }
360: }
361:
362: public static void main(String[] args) {
363: TestRunner.run(JavaFoldsNavigationTest.class);
364: }
365:
366: }
|