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-2007 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: package org.netbeans.test.syntax;
042:
043: import java.awt.event.KeyEvent;
044: import java.io.File;
045: import javax.swing.JEditorPane;
046: import javax.swing.event.DocumentEvent;
047: import javax.swing.event.DocumentListener;
048: import javax.swing.text.Caret;
049: import junit.framework.Test;
050: import org.netbeans.editor.Utilities;
051: import org.netbeans.jellytools.EditorOperator;
052: import org.netbeans.editor.BaseDocument;
053: import org.netbeans.jellytools.modules.editor.CompletionJListOperator;
054: import org.netbeans.test.web.FileObjectFilter;
055: import org.netbeans.test.web.RecurrentSuiteFactory;
056: import org.netbeans.test.syntax.CompletionTest.TestStep;
057: import org.openide.actions.UndoAction;
058: import org.openide.filesystems.FileObject;
059: import org.openide.util.actions.SystemAction;
060:
061: /**
062: *
063: * @author jindra
064: */
065: public class AutoCompletionTest extends CompletionTest {
066:
067: /** Creates a new instance of AutoCompletionTest */
068: public AutoCompletionTest(String name, FileObject testFileObj) {
069: super (name, testFileObj);
070: debug = false;
071: }
072:
073: public static Test suite() {
074: // find folder with test projects and define file objects filter
075: File datadir = new AutoCompletionTest(null, null).getDataDir();
076: File projectsDir = new File(datadir,
077: "AutoCompletionTestProjects");
078: FileObjectFilter filter = new FileObjectFilter() {
079:
080: public boolean accept(FileObject fo) {
081: String ext = fo.getExt();
082: String name = fo.getName();
083: return (name.startsWith("test") || name
084: .startsWith("Test"))
085: && (XML_EXTS.contains(ext)
086: || JSP_EXTS.contains(ext) || ext
087: .equals("java"));
088: }
089: };
090: return RecurrentSuiteFactory.createSuite(
091: AutoCompletionTest.class, projectsDir, filter);
092: }
093:
094: @Override
095: public void setUp() {
096: super .setUp();
097: }
098:
099: @Override
100: protected void exec(JEditorPane editor, TestStep step)
101: throws Exception {
102: try {
103: final BaseDocument doc = (BaseDocument) editor
104: .getDocument();
105: ref(step.toString());
106: Caret caret = editor.getCaret();
107: caret.setDot(step.getOffset() + 1);
108: EditorOperator eo = new EditorOperator(testFileObj
109: .getNameExt());
110: eo.insert(step.getPrefix());
111: waitTypingFinished(doc);
112:
113: caret.setDot(step.getCursorPos());
114: eo.save();
115: final Object lock = new Object();
116: synchronized (lock) {
117: doc.addDocumentListener(new DocumentListener() {
118:
119: public void insertUpdate(DocumentEvent e) {
120: synchronized (lock) {
121: lock.notifyAll();
122: }
123: }
124:
125: public void removeUpdate(DocumentEvent e) {
126: }
127:
128: public void changedUpdate(DocumentEvent e) {
129: }
130: });
131: CompletionJListOperator.hideAll();
132: eo.txtEditorPane().pressKey(KeyEvent.VK_SPACE,
133: KeyEvent.CTRL_MASK);
134: lock.wait(10000);
135: }
136: doc.atomicLock();
137: int rowStart = Utilities.getRowStart(doc,
138: step.getOffset() + 1);
139: int rowEnd = Utilities.getRowEnd(doc, step.getOffset() + 1);
140: String result = doc.getText(new int[] { rowStart, rowEnd })
141: .trim();
142: doc.atomicUnlock();
143: if (!result.equals(step.getResult())) {
144: ref("EE: unexpected CC result:\n< " + result + "\n> "
145: + step.getResult());
146: }
147: } finally {
148: // undo all changes
149: final UndoAction ua = SystemAction.get(UndoAction.class);
150: assertNotNull("Cannot obtain UndoAction", ua);
151: while (ua.isEnabled()) {
152: runInAWT(new Runnable() {
153:
154: public void run() {
155: ua.performAction();
156: }
157: });
158: }
159: }
160: }
161: }
|