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 org.netbeans.test.java.editor.completiongui;
043:
044: import java.awt.event.KeyEvent;
045: import java.util.regex.Pattern;
046: import junit.textui.TestRunner;
047: import lib.EditorTestCase;
048: import org.netbeans.jellytools.EditorOperator;
049: import org.netbeans.jemmy.EventTool;
050:
051: /**
052: *
053: * @author Jiri Prox Jiri.Prox@Sun.COM
054: */
055: public class GuiTest extends EditorTestCase {
056:
057: public final String defaultSamplePackage = "org.netbeans.test.java.editor.completiongui.GuiTest";
058:
059: public final String version;
060:
061: private static boolean firstRun = true;
062:
063: /** Creates a new instance of CreateConstructor */
064: public GuiTest(String name) {
065: super (name);
066: version = getJDKVersionCode();
067: }
068:
069: private String getJDKVersionCode() {
070: String specVersion = System.getProperty("java.version");
071:
072: if (specVersion.startsWith("1.4"))
073: return "jdk14";
074:
075: if (specVersion.startsWith("1.5"))
076: return "jdk15";
077:
078: if (specVersion.startsWith("1.6"))
079: return "jdk16";
080:
081: throw new IllegalStateException("Specification version: "
082: + specVersion + " not recognized.");
083: }
084:
085: @Override
086: protected void setUp() throws Exception {
087: super .setUp();
088: openProject("java_editor_test");
089: //openDefaultProject();
090: }
091:
092: @Override
093: protected void tearDown() throws Exception {
094: super .tearDown();
095: }
096:
097: private void performCodeCompletion(String testFile, String prefix,
098: int line, int itemNo, String pattern, boolean allsymbols) {
099: int delay;
100: openSourceFile(defaultSamplePackage, testFile);
101: if (firstRun) {
102: new EventTool().waitNoEvent(10000);
103: firstRun = false;
104: }
105: EditorOperator editor = new EditorOperator(testFile);
106: try {
107: editor.requestFocus();
108: editor.setCaretPosition(line, 1);
109: if (prefix != null) {
110: for (int i = 0; i < prefix.length(); i++) {
111: char c = prefix.charAt(i);
112: editor.typeKey(c);
113: }
114: }
115: if (allsymbols) {
116: editor.pushKey(KeyEvent.VK_SPACE,
117: KeyEvent.CTRL_DOWN_MASK
118: | KeyEvent.ALT_DOWN_MASK);
119: delay = 5000;
120: } else {
121: editor.pushKey(KeyEvent.VK_SPACE,
122: KeyEvent.CTRL_DOWN_MASK);
123:
124: delay = 2500;
125: }
126: new EventTool().waitNoEvent(delay);
127: while (itemNo > 1) {
128: editor.pushKey(KeyEvent.VK_DOWN);
129: new EventTool().waitNoEvent(200);
130: itemNo--;
131: }
132: editor.pushKey(KeyEvent.VK_ENTER);
133:
134: new EventTool().waitNoEvent(300);
135: String text = editor.getText();
136: Pattern p = Pattern.compile(pattern, Pattern.DOTALL);
137: boolean ok = p.matcher(text).matches();
138: if (!ok) {
139: System.out.println("Pattern: " + pattern);
140: System.out.println("-------");
141: System.out.println(text);
142:
143: log("Pattern: " + pattern);
144: log("-------");
145: log(text);
146:
147: }
148: assertTrue("Expected text not found in editor", ok);
149: } finally {
150: editor.close(false);
151: }
152: }
153:
154: public void testOverrideMethod() {
155: String pattern = "";
156: if (version.equals("1.6")) {
157: pattern = ".*@Override.*protected void finalize\\(\\) throws Throwable \\{.*super.finalize\\(\\);.*\\}.*";
158: } else {
159: pattern = ".*protected void finalize\\(\\) throws Throwable \\{.*super.finalize\\(\\);.*\\}.*";
160: }
161: performCodeCompletion("TestSimpleCase", null, 12, 3, pattern,
162: false);
163: }
164:
165: public void testKeyWord() {
166: String pattern = ".*protected.*";
167: performCodeCompletion("TestSimpleCase", "p", 12, 2, pattern,
168: false);
169: }
170:
171: public void testKeyWord2() {
172: String pattern = ".*extends.*";
173: performCodeCompletion("ContextAware", "ex", 11, 1, pattern,
174: false);
175: }
176:
177: public void testParameter() {
178: String pattern = ".*\\{.*x.*\\}.*";
179: performCodeCompletion("TestSimpleCase", null, 10, 1, pattern,
180: false);
181: }
182:
183: public void testFiled() {
184: String pattern = ".*String s; public void neco\\(\\) \\{s.*";
185: performCodeCompletion("TestSimpleCase",
186: "String s; public void neco() {", 12, 1, pattern, false);
187: }
188:
189: public void testAnonymousClass() {
190: String pattern = ".*new Runnable\\(\\) \\{.*public void run\\(\\) \\{.*throw new UnsupportedOperationException\\(\"Not supported yet.\"\\);.*\\}.*\\}.*";
191: performCodeCompletion("TestSimpleCase", "new Runnable", 10, 1,
192: pattern, false);
193: }
194:
195: public void testAddClassWithImport() {
196: String pattern = ".*import java.io.IOException;.*IOException.*";
197: performCodeCompletion("TestSimpleCase", "IOE", 10, 2, pattern,
198: true);
199: }
200:
201: public void testAddSuperClass() {
202: String pattern = ".*extends Byte.*";
203: performCodeCompletion("ContextAware", "extends B", 11, 2,
204: pattern, false);
205: }
206:
207: public void testAddInterface() {
208: String pattern = ".*implements Comparable.*";
209: performCodeCompletion("ContextAware", "implements ", 11, 4,
210: pattern, false);
211: }
212:
213: public void testAddInterface2() {
214: String pattern = ".*implements Comparable, Cloneable.*";
215: performCodeCompletion("ContextAware",
216: "implements Comparable, ", 11, 3, pattern, false);
217: }
218:
219: public void testAddThrows() {
220: String pattern = ".*throws AssertionError.*";
221: performCodeCompletion("ContextAware", "throws A", 15, 5,
222: pattern, false);
223: }
224:
225: public void testLoop() {
226: String pattern = ".*for\\(Thread t:.*threads.*";
227: performCodeCompletion("ContextAware", "", 23, 1, pattern, false);
228: }
229:
230: public void testCatch() {
231: String pattern = ".*catch \\(.*MalformedURLException.*";
232: performCodeCompletion("ContextAware", "", 31, 2, pattern, false);
233: }
234:
235: public void testReturn() {
236: String pattern = ".*return x.*";
237: performCodeCompletion("ContextAware", "return ", 35, 1,
238: pattern, false);
239: }
240:
241: public static void main(String[] args) {
242: new TestRunner().run(GuiTest.class);
243:
244: }
245:
246: }
|