KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > completion > CompletionJTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.text.completion;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.event.InputEvent JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import junit.textui.TestRunner;
26 import org.netbeans.editor.ext.ListCompletionView;
27 import org.netbeans.jellytools.EditorOperator;
28 import org.netbeans.jellytools.NewWizardOperator;
29 import org.netbeans.jemmy.ComponentChooser;
30 import org.netbeans.jemmy.operators.ComponentOperator;
31 import org.netbeans.jemmy.operators.JTextComponentOperator;
32 import org.netbeans.modules.xml.text.syntax.XMLOptions;
33 import org.netbeans.tests.xml.JXTest;
34 import org.openide.loaders.DataObject;
35 import org.openide.options.SystemOption;
36
37 /**
38  * <P>
39  * <P>
40  * <FONT COLOR="#CC3333" FACE="Courier New, Monospaced" SIZE="+1">
41  * <B>
42  * <BR> XML Module Jemmy Test: CompletionJTest
43  * </B>
44  * </FONT>
45  * <BR><BR><B>What it tests:</B><BR>
46  *
47  * - basic functionality of XML code completion<br>
48  *
49  * <BR><BR><B>How it works:</B><BR>
50  *
51  * Creates simple XML document by code completion.
52  *
53  * <BR><BR><B>Settings:</B><BR>
54  * none<BR>
55  *
56  * <BR><BR><B>Output (Golden file):</B><BR>
57  * XML documents<BR>
58  *
59  * <BR><B>To Do:</B><BR>
60  * none<BR>
61  *
62  * <P>Created on April 03, 2003, 12:33 PM
63  * <P>
64  */

65
66 public class CompletionJTest extends JXTest {
67     // constants for showCompl() method
68
private static int NO_WAIT = 0;
69     private static int EMPTY = 1;
70     private static int NO_EMPTY = 2;
71     
72     /** Caret Column Position */
73     int col;
74     /** Editor Operator */
75     EditorOperator editor;
76     
77     /**
78      * Creates new CoreTemplatesTest
79      * @param testName
80      */

81     public CompletionJTest(String JavaDoc testName) {
82         super(testName);
83     }
84     
85     /** Main test method. */
86     public void test() {
87         String JavaDoc folder;
88         String JavaDoc name = "Document";
89         String JavaDoc ext = "xml";
90         XMLOptions options;
91         DataObject dao;
92         
93         try {
94             folder = getFilesystemName() + DELIM + getDataPackageName(DELIM);
95             
96             options = (XMLOptions) SystemOption.findObject(XMLOptions.class, true);
97             options.setCompletionAutoPopup(false);
98             
99             dao = TestUtil.THIS.findData(name + "." + ext);
100             if (dao != null) dao.delete();
101             // catalog is only real XML template in the module :-(
102
NewWizardOperator.create("XML" + DELIM + "OASIS XML Catalog", folder, name);
103             editor = new EditorOperator(name);
104         } catch (Exception JavaDoc ex) {
105             log("Cannot setup test.", ex);
106         }
107         
108         clearText();
109         editor.txtEditorPane().setText(""
110         + "<?xml version='1.0' encoding='UTF-8'?>\n"
111         + "<!DOCTYPE html PUBLIC '-//Test//DTD XHTML 1.0 Subset//EN' 'xhtml.dtd'>\n");
112
113         insert("<h");
114         save();
115         //tml>
116
showCompl(NO_EMPTY);
117         enter();
118         insert(">\n");
119         //<head>
120
insertTag("<", ">\n", 1);
121         //<title>Test page</title>
122

123         /*!!! #36306 hack
124         insertTag("<t", "Test page", -1);
125          */

126         insert("<t");
127         showCompl(NO_EMPTY);
128         esc();
129         insert("Test page");
130         //!!! end hack
131

132         end();
133         insert("\n");
134         //</head>
135
insertTag("</", "\n", -1);
136         //<body>
137
insertTag("<", ">\n", 0);
138         //<h1 title="test">Test</h1>
139
insertTag("<h", " ", 0);
140         insertTag("t", "test\">Test", -1);
141         insertTag("</", "\n", -1);
142         //<table border="1">
143
insertTag("<t", " ", 0);
144         insertTag("b", "1\">\n", 1);
145         //<tr align="center">
146
insertTag("<t", " ", 4);
147         insertTag("a", "center\">\n", -1);
148         //<td>1</td><td>2</td>
149

150         /*!!! #36306 hack
151         insertTag("<td", "1", -1);
152         end();
153         insertTag("<td", "2", -1);
154          */

155         
156         insert("<td");
157         showCompl(NO_EMPTY);
158         esc();
159         insert("1");
160         end();
161         
162         insert("<td");
163         showCompl(NO_EMPTY);
164         esc();
165         insert("2");
166         //!!! end hack
167

168         end();
169         insert("\n");
170         //</tr>
171
insertTag("</", "\n", -1);
172         //</table>
173
insertTag("</", "\n", -1);
174         //</body>
175
insertTag("</", "\n", -1);
176         //</html>
177
insertTag("</", "\n", -1);
178         save();
179         ref(editor.getText());
180         compareReferenceFiles();
181     }
182     
183     /** Inserts a tag using completion, i.e.:
184      * <ul>
185      * <li>types <i>pref</i> at caret position
186      * <li>triggers code completion
187      * <li>enters index-th element from completion list
188      * <li>types <i>suf</i> at caret position
189      * </ul>
190      * @param pref prefix
191      * @param suf sufix
192      * @param index index of inserting item, -1 =>
193      */

194     final protected void insertTag(String JavaDoc pref, String JavaDoc suf, int index) {
195         insert(pref);
196         if (index < 0) {
197             showCompl(NO_WAIT);
198         } else {
199             showCompl(NO_EMPTY);
200         }
201         for (int i = 0; i < index; i++) {
202             down();
203         }
204         if (index > -1) {
205             enter();
206         }
207         //!!! sometime completion doesn't finish on time
208
sleepTest(500);
209         insert(suf);
210     }
211     
212     /** Types a text at caret position
213      *
214      * @param txt String
215      */

216     protected final void insert(String JavaDoc txt) {
217         editor.txtEditorPane().typeText(txt);
218     }
219     
220     /** Moves caret about <i>x</i> lines and <i>y</i> rows
221      *
222      * @param x delta X
223      * @param y delta Y
224      */

225     protected final void move(int x, int y) {
226         col += y;
227         editor.setCaretPosition(editor.getLineNumber() + x, col);
228     }
229     
230     /** Saves the document */
231     protected final void save() {
232         editor.save();
233     }
234     
235     /** Clears the document */
236     protected final void clearText() {
237         col = 0;
238         JTextComponentOperator text = new JTextComponentOperator(editor);
239         
240         text.setText("X"); //!!! because clearText is to slow.
241
text.clearText();
242     }
243     
244     /** Triggers code completion in given mode.
245      * <ul>
246      * <li>NO_WAIT - only trigger completion and continue
247      * <li>EMPTY - trigger completion and wait for completion list
248      * <li>NO_EMPTY - trigger completion and wait for non-empty completion list
249      * </ul>
250      */

251     protected final void showCompl(int mode) {
252         editor.pressKey(KeyEvent.VK_SPACE, InputEvent.CTRL_MASK);
253         if (mode == NO_WAIT) {
254             return;
255         } else if (mode == NO_EMPTY) {
256             waitCompl(1);
257         } else if (mode == EMPTY) {
258             waitCompl(0);
259         }
260     }
261     
262     /** Triggers code completion and checks if completion list contains at least
263      * <i>minSize</i> items.
264      * @param minSize
265      */

266     protected final void checkCompletion(int minSize) {
267         showCompl(NO_WAIT);
268         waitCompl(minSize);
269         esc();
270     }
271     
272     /** Waits completion list with at least <i>minSize</i> items.
273      * @param minSize - nuber of items
274      */

275     private void waitCompl(int minSize) {
276         CompletionChooser completionChoser = new CompletionChooser(minSize);
277         ListCompletionView completionView = (ListCompletionView) ComponentOperator
278         .waitComponent((Container JavaDoc) editor.getWindowContainerOperator().getSource()
279         , completionChoser);
280         int size = completionView.getModel().getSize();
281     }
282     
283     /** Searches for a completion listh with at least <i>minSize</i> items */
284     private class CompletionChooser implements ComponentChooser {
285         int minSize;
286         
287         public CompletionChooser() {
288             this(0);
289         }
290         
291         public CompletionChooser(int minSize) {
292             this.minSize = minSize;
293         }
294         
295         public boolean checkComponent(Component JavaDoc component) {
296             //System.out.println("> " + component);
297

298             if (component instanceof ListCompletionView) {
299                 ListCompletionView cmpl = (ListCompletionView) component;
300                 if (cmpl.getModel().getSize() >= minSize) return true;
301             }
302             return false;
303         }
304         
305         public String JavaDoc getDescription() {
306             return("Instace of ScrollCompletionPane");
307         }
308     }
309     
310     // KEYS
311

312     /** Deletes given number of characters from current caret possition.
313      * Position of caret will not change.
314      * @param length number of characters to be deleted
315      */

316     protected final void delete(int len) {
317         editor.delete(len);
318     }
319     
320     /** Deletes given number of characters before current caret possition.
321      * @param length number of characters to be deleted
322      */

323     protected final void backSp(int len) {
324         for (int i = 0; i < len; i++) {
325             editor.pushKey(KeyEvent.VK_BACK_SPACE);
326         }
327     }
328     
329     /** Presses key [ESC] */
330     protected final void esc() {
331         editor.pressKey(KeyEvent.VK_ESCAPE);
332     }
333     
334     /** Presses key [ENTER] */
335     protected final void enter() {
336         editor.pressKey(KeyEvent.VK_ENTER);
337     }
338     
339     /** Presses key [DOWN] */
340     protected final void down() {
341         editor.pressKey(KeyEvent.VK_DOWN);
342     }
343     
344     /** Presses key [UP] */
345     protected final void up() {
346         editor.pressKey(KeyEvent.VK_UP);
347     }
348     
349     /** Presses key [LEFT] */
350     protected final void left() {
351         editor.pressKey(KeyEvent.VK_LEFT);
352     }
353     
354     /** Presses key [RIGHT] */
355     protected final void right() {
356         editor.pressKey(KeyEvent.VK_RIGHT);
357     }
358     
359     /** Presses key [END] */
360     protected final void end() {
361         editor.pressKey(KeyEvent.VK_END);
362     }
363     
364     /** Main method for debuging purpose
365      *
366      * @param args
367      */

368     public static void main(String JavaDoc[] args) {
369         //JamController.setFast(false);
370
DEBUG = true;
371         TestRunner.run(CompletionJTest.class);
372     }
373 }
374
Popular Tags