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_editor_actions;
043:
044: import java.awt.event.KeyEvent;
045: import javax.swing.KeyStroke;
046: import org.netbeans.jellytools.EditorOperator;
047: import org.netbeans.jemmy.operators.JEditorPaneOperator;
048: import editor_actions.EditorActionsTest;
049:
050: /**
051: * Basic Navigation Actions Test class.
052: * The base navigation actions can be found at:
053: * http://editor.netbeans.org/doc/UserView/apdx_a_nshortcuts.html
054: *
055: * Test covers following actions:
056: *
057: * StandardNavigationActions:
058: * -------------------------
059: * caret-forward [RIGHT]
060: * caret-backward [LEFT]
061: * caret-down [DOWN]
062: * caret-up [UP]
063: * selection-forward [SHIFT-RIGHT]
064: * selection-backward [SHIFT-LEFT]
065: * selection-down [SHIFT-DOWN]
066: * selection-up [SHIFT-UP]
067: * caret-next-word [CTRL-RIGHT]
068: * caret-previous-word [CTRL-LEFT]
069: * selection-next-word [CTRL-SHIFT-RIGHT]
070: * selection-previous-word [CTRL-SHIFT-LEFT]
071: * page-down [PAGE_DOWN]
072: * page-up [PAGE_UP]
073: * selection-page-down [SHIFT-PAGE_DOWN]
074: * selection-page-up [SHIFT-PAGE_UP]
075: * caret-begin-line [HOME]
076: * caret-end-line [END]
077: * selection-begin-line [SHIFT-HOME]
078: * selection-end-line [SHIFT-END]
079: * caret-begin [CTRL-HOME]
080: * caret-end [CTRL-END]
081: * selection-begin [CTRL-SHIFT-HOME]
082: * selection-end [CTRL-SHIFT-END]
083: * caret-end-word [ALT-U E]
084: *
085: * @author Martin Roskanin
086: */
087: public class JavaNavigationActionsTest extends JavaEditorActionsTest {
088:
089: private JEditorPaneOperator txtOper;
090: private EditorOperator editor;
091:
092: /** Creates a new instance of Main */
093: public JavaNavigationActionsTest(String testMethodName) {
094: super (testMethodName);
095: }
096:
097: private ValueResolver getResolver(
098: final JEditorPaneOperator txtOper, final int etalon) {
099: ValueResolver resolver = new ValueResolver() {
100: public Object getValue() {
101: int newCaretPos = txtOper.getCaretPosition();
102: return (newCaretPos == etalon) ? Boolean.TRUE
103: : Boolean.FALSE;
104: }
105: };
106:
107: return resolver;
108: }
109:
110: private void checkActionByKeyStroke(int key, int mod,
111: int caretPosToSet, int etalon, boolean checkSelection) {
112: editor.setCaretPosition(caretPosToSet);
113: txtOper.pushKey(key, mod);
114: waitMaxMilisForValue(1500, getResolver(txtOper, etalon),
115: Boolean.TRUE);
116: int newCaretOffset = txtOper.getCaretPosition();
117: if (checkSelection) {
118: int selectionStart = txtOper.getSelectionStart();
119: int selectionEnd = txtOper.getSelectionEnd();
120: if (selectionStart != Math.min(caretPosToSet, etalon)
121: || selectionEnd != Math.max(caretPosToSet, etalon)) {
122: String keyString = KeyStroke.getKeyStroke(key, mod)
123: .toString();
124: fail(keyString
125: + ": Action failed: [etalon/newCaretOffset/selectionStart/selectionEnd]: ["
126: + etalon + "/" + newCaretOffset + "/"
127: + selectionStart + "/" + selectionEnd + "]");
128: }
129: } else {
130: if (etalon != newCaretOffset) {
131: String keyString = KeyStroke.getKeyStroke(key, mod)
132: .toString();
133: fail(keyString
134: + ": Action failed: [etalon/newCaretOffset]: ["
135: + etalon + "/" + newCaretOffset + "]");
136: }
137: }
138: }
139:
140: public void testStandardNavigationActions() {
141: openDefaultProject();
142: openDefaultSampleFile();
143: try {
144:
145: editor = getDefaultSampleEditorOperator();
146: txtOper = editor.txtEditorPane();
147:
148: // -------- test RIGHT action ---
149: checkActionByKeyStroke(KeyEvent.VK_RIGHT, 0, 20, 21, false);
150:
151: // -------- test LEFT action ---
152: checkActionByKeyStroke(KeyEvent.VK_LEFT, 0, 20, 19, false);
153:
154: // -------- test DOWN action ---
155: // set caret at 10,14
156: checkActionByKeyStroke(KeyEvent.VK_DOWN, 0, 200, 231, false);
157:
158: // -------- test UP action ---
159: // set caret at 10,14
160: checkActionByKeyStroke(KeyEvent.VK_UP, 0, 200, 170, false);
161:
162: // -------- test select RIGHT action ---
163: checkActionByKeyStroke(KeyEvent.VK_RIGHT,
164: KeyEvent.SHIFT_DOWN_MASK, 20, 21, true);
165:
166: // -------- test select LEFT action ---
167: checkActionByKeyStroke(KeyEvent.VK_LEFT,
168: KeyEvent.SHIFT_DOWN_MASK, 20, 19, true);
169:
170: // -------- test select DOWN action ---
171: // set caret at 10,14
172: checkActionByKeyStroke(KeyEvent.VK_DOWN,
173: KeyEvent.SHIFT_DOWN_MASK, 200, 231, true);
174:
175: // -------- test select UP action ---
176: // set caret at 10,14
177: checkActionByKeyStroke(KeyEvent.VK_UP,
178: KeyEvent.SHIFT_DOWN_MASK, 200, 170, true);
179:
180: // -------- test caret-next-word action ---
181: // set caret at 1,12
182: checkActionByKeyStroke(KeyEvent.VK_RIGHT,
183: KeyEvent.CTRL_DOWN_MASK, 11, 22, false);
184:
185: // -------- test caret-previous-word action -----
186: checkActionByKeyStroke(KeyEvent.VK_LEFT,
187: KeyEvent.CTRL_DOWN_MASK, 34, 33, false);
188:
189: // -------- test selection-next-word action ---
190: // set caret at 1,12
191: checkActionByKeyStroke(KeyEvent.VK_RIGHT,
192: KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK,
193: 11, 22, true);
194:
195: // -------- test selection-previous-word action -----
196: checkActionByKeyStroke(KeyEvent.VK_LEFT,
197: KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK,
198: 34, 33, true);
199:
200: // -------- test page-down action -------
201: editor.setCaretPosition(5, 1);
202: int caretDown = txtOper.getCaretPosition();
203: editor.setCaretPosition(1, 1);
204: int pageDownStart = txtOper.getCaretPosition();
205: txtOper.pushKey(KeyEvent.VK_PAGE_DOWN);
206: int pageDownEnd = txtOper.getCaretPosition();
207: if (pageDownEnd < caretDown) {
208: fail("PAGE_DOWN failed");
209: }
210:
211: // -------- test page-up action -------
212: editor.setCaretPosition(32, 1);
213: int caretUp = txtOper.getCaretPosition();
214: editor.setCaretPosition(38, 1);
215: int pageUpStart = txtOper.getCaretPosition();
216: txtOper.pushKey(KeyEvent.VK_PAGE_UP);
217: int pageUpEnd = txtOper.getCaretPosition();
218: if (pageUpEnd > caretUp) {
219: fail("PAGE_UP failed");
220: }
221:
222: // -------- test page-down action -------
223: checkActionByKeyStroke(KeyEvent.VK_PAGE_DOWN,
224: KeyEvent.SHIFT_DOWN_MASK, pageDownStart,
225: pageDownEnd, true);
226:
227: // -------- test page-up action -------
228: checkActionByKeyStroke(KeyEvent.VK_PAGE_UP,
229: KeyEvent.SHIFT_DOWN_MASK, pageUpStart, pageUpEnd,
230: true);
231:
232: // -------- test caret-begin-line action -------
233: checkActionByKeyStroke(KeyEvent.VK_HOME, 0, 18, 0, false);
234:
235: // -------- test caret-end-line action -------
236: checkActionByKeyStroke(KeyEvent.VK_END, 0, 18, 45, false);
237:
238: // -------- test selection-begin-line action -------
239: checkActionByKeyStroke(KeyEvent.VK_HOME,
240: KeyEvent.SHIFT_DOWN_MASK, 18, 0, true);
241:
242: // -------- test selection-end-line action -------
243: checkActionByKeyStroke(KeyEvent.VK_END,
244: KeyEvent.SHIFT_DOWN_MASK, 18, 45, true);
245:
246: // -------- test caret-begin action -------
247: checkActionByKeyStroke(KeyEvent.VK_HOME,
248: KeyEvent.CTRL_DOWN_MASK, 18, 0, false);
249:
250: // -------- test caret-end action -------
251: checkActionByKeyStroke(KeyEvent.VK_END,
252: KeyEvent.CTRL_DOWN_MASK, 18, txtOper.getDocument()
253: .getLength(), false);
254:
255: // -------- test selection-begin action -------
256: checkActionByKeyStroke(KeyEvent.VK_HOME,
257: KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK,
258: 18, 0, true);
259:
260: // -------- test selection-end action -------
261: checkActionByKeyStroke(KeyEvent.VK_END,
262: KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK,
263: 18, txtOper.getDocument().getLength(), true);
264:
265: // ---- caret-end-word ----
266: // testStandar|dNavigationActions
267: // editor.setCaretPosition(71);
268: // int etalon = 89; // testStandardNavigationActions|
269: // txtOper.pushKey(KeyEvent.VK_U, KeyEvent.ALT_DOWN_MASK);
270: // txtOper.pushKey(KeyEvent.VK_E);
271: // waitMaxMilisForValue(1500, getResolver(txtOper, etalon), Boolean.TRUE);
272: // int newCaretOffset = txtOper.getCaretPosition();
273: // if (etalon != newCaretOffset){
274: // fail("Alt+U E: Action failed: [etalon/newCaretOffset]: ["+etalon+"/"+
275: // newCaretOffset+"]");
276: // }
277:
278: } finally {
279: closeFileWithDiscard();
280: }
281:
282: }
283:
284: }
|