1 19 package org.netbeans.modules.xml.text.completion; 20 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import java.awt.event.InputEvent ; 24 import java.awt.event.KeyEvent ; 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 65 66 public class CompletionJTest extends JXTest { 67 private static int NO_WAIT = 0; 69 private static int EMPTY = 1; 70 private static int NO_EMPTY = 2; 71 72 73 int col; 74 75 EditorOperator editor; 76 77 81 public CompletionJTest(String testName) { 82 super(testName); 83 } 84 85 86 public void test() { 87 String folder; 88 String name = "Document"; 89 String 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 NewWizardOperator.create("XML" + DELIM + "OASIS XML Catalog", folder, name); 103 editor = new EditorOperator(name); 104 } catch (Exception 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 showCompl(NO_EMPTY); 117 enter(); 118 insert(">\n"); 119 insertTag("<", ">\n", 1); 121 123 126 insert("<t"); 127 showCompl(NO_EMPTY); 128 esc(); 129 insert("Test page"); 130 132 end(); 133 insert("\n"); 134 insertTag("</", "\n", -1); 136 insertTag("<", ">\n", 0); 138 insertTag("<h", " ", 0); 140 insertTag("t", "test\">Test", -1); 141 insertTag("</", "\n", -1); 142 insertTag("<t", " ", 0); 144 insertTag("b", "1\">\n", 1); 145 insertTag("<t", " ", 4); 147 insertTag("a", "center\">\n", -1); 148 150 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 168 end(); 169 insert("\n"); 170 insertTag("</", "\n", -1); 172 insertTag("</", "\n", -1); 174 insertTag("</", "\n", -1); 176 insertTag("</", "\n", -1); 178 save(); 179 ref(editor.getText()); 180 compareReferenceFiles(); 181 } 182 183 194 final protected void insertTag(String pref, String 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 sleepTest(500); 209 insert(suf); 210 } 211 212 216 protected final void insert(String txt) { 217 editor.txtEditorPane().typeText(txt); 218 } 219 220 225 protected final void move(int x, int y) { 226 col += y; 227 editor.setCaretPosition(editor.getLineNumber() + x, col); 228 } 229 230 231 protected final void save() { 232 editor.save(); 233 } 234 235 236 protected final void clearText() { 237 col = 0; 238 JTextComponentOperator text = new JTextComponentOperator(editor); 239 240 text.setText("X"); text.clearText(); 242 } 243 244 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 266 protected final void checkCompletion(int minSize) { 267 showCompl(NO_WAIT); 268 waitCompl(minSize); 269 esc(); 270 } 271 272 275 private void waitCompl(int minSize) { 276 CompletionChooser completionChoser = new CompletionChooser(minSize); 277 ListCompletionView completionView = (ListCompletionView) ComponentOperator 278 .waitComponent((Container ) editor.getWindowContainerOperator().getSource() 279 , completionChoser); 280 int size = completionView.getModel().getSize(); 281 } 282 283 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 component) { 296 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 getDescription() { 306 return("Instace of ScrollCompletionPane"); 307 } 308 } 309 310 312 316 protected final void delete(int len) { 317 editor.delete(len); 318 } 319 320 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 330 protected final void esc() { 331 editor.pressKey(KeyEvent.VK_ESCAPE); 332 } 333 334 335 protected final void enter() { 336 editor.pressKey(KeyEvent.VK_ENTER); 337 } 338 339 340 protected final void down() { 341 editor.pressKey(KeyEvent.VK_DOWN); 342 } 343 344 345 protected final void up() { 346 editor.pressKey(KeyEvent.VK_UP); 347 } 348 349 350 protected final void left() { 351 editor.pressKey(KeyEvent.VK_LEFT); 352 } 353 354 355 protected final void right() { 356 editor.pressKey(KeyEvent.VK_RIGHT); 357 } 358 359 360 protected final void end() { 361 editor.pressKey(KeyEvent.VK_END); 362 } 363 364 368 public static void main(String [] args) { 369 DEBUG = true; 371 TestRunner.run(CompletionJTest.class); 372 } 373 } 374 | Popular Tags |