1 19 20 25 26 package org.netbeans.test.editor.suites.abbrevs; 27 28 import java.awt.event.InputEvent ; 29 import java.awt.event.KeyEvent ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import org.netbeans.jellytools.EditorOperator; 36 import org.netbeans.jellytools.JellyTestCase; 37 import org.netbeans.jellytools.modules.editor.Abbreviations; 38 import org.netbeans.modules.editor.options.BaseOptions; 39 import org.netbeans.test.editor.LineDiff; 40 import org.openide.options.SystemOption; 41 42 46 public abstract class AbbreviationsTest extends JellyTestCase { 47 48 49 public AbbreviationsTest(String name) { 50 super(name); 51 } 52 53 public static class Abbreviation { 54 private String name; 55 private String expansion; 56 private String oldName; 57 private String oldExpansion; 58 59 public Abbreviation(String name, String expansion, String oldName, String oldExpansion) { 60 this.name = name; 61 this.expansion = expansion; 62 this.oldName = oldName; 63 this.oldExpansion = oldExpansion; 64 } 65 66 public String getName() { 67 return name; 68 } 69 70 public String getExpansion() { 71 return expansion; 72 } 73 74 public String getOldName() { 75 return oldName; 76 } 77 78 public String getOldExpansion() { 79 return expansion; 80 } 81 } 82 83 public abstract Abbreviation[] getDefaultAbbreviations(); 84 public abstract EditorOperator getTestEditor(); 85 public abstract void prepareEditor(); 86 public abstract void moveCaretIntoComment(); 87 public abstract void moveCaretIntoCode(); 88 public abstract Abbreviation[] getAbbreviationsToAdd(); 89 public abstract Abbreviation[] getAbbreviationsToRemove(); 90 public abstract Abbreviation[] getAbbreviationsToModify(); 91 public abstract String getEditorName(); 92 public abstract void finishEditor(); 93 94 protected void flushResult() { 95 getRef().print(getTestEditor().getText()); 96 getRef().flush(); 97 try { 98 assertFile("Output does not match golden file.", getGoldenFile(), new File (getWorkDir(), this.getName() + ".ref"), 99 new File (getWorkDir(), this.getName() + ".diff"), new LineDiff(false)); 100 } catch (IOException e) { 101 assertTrue("IOException: " + e.getMessage(), false); 102 } 103 } 104 105 private void useAbbreviation(String abbreviation, boolean expand) { 106 EditorOperator editor = getTestEditor(); 107 108 112 114 editor.txtEditorPane().typeText(abbreviation); 115 if (expand) 116 editor.typeKey(' '); 117 else 118 editor.typeKey(' ', KeyEvent.SHIFT_MASK); 119 try { 120 Thread.currentThread().sleep(100); } catch (InterruptedException ex) { 122 } 123 editor.txtEditorPane().typeText("CARET_POSITION"); 124 editor.pushKey(KeyEvent.VK_END); 125 editor.pushKey(KeyEvent.VK_ENTER); 126 } 127 128 public void testAbbreviationTest() { 129 log("testAbbreviationTest start"); 130 try { 131 Abbreviation[] abbs = getDefaultAbbreviations(); 132 133 prepareEditor(); 134 135 for (int cntr = 0; cntr < abbs.length; cntr++) { 136 moveCaretIntoCode(); 137 useAbbreviation(abbs[cntr].getName(), true); 138 } 139 140 log("testAbbreviationTest flush results:"); 141 142 flushResult(); 143 } finally { 144 145 log("testAbbreviationTest closing editor:"); 146 147 finishEditor(); 148 149 log("testAbbreviationTest finished"); 150 } 151 } 152 153 public void testAbbreviationInsideComment() { 154 log("testAbbreviationInsideComment start"); 155 try { 156 Abbreviation[] abbs = getDefaultAbbreviations(); 157 158 prepareEditor(); 159 160 for (int cntr = 0; cntr < abbs.length; cntr++) { 161 moveCaretIntoComment(); 162 useAbbreviation(abbs[cntr].getName(), true); 163 } 164 165 log("testAbbreviationInsideComment flush results:"); 166 167 flushResult(); 168 } finally { 169 log("testAbbreviationInsideComment closing editor:"); 170 171 finishEditor(); 172 173 log("testAbbreviationInsideComment finished"); 174 } 175 } 176 177 public void testAbbreviationWithoutExpansion() { 178 log("testAbbreviationWithoutExpansion start"); 179 try { 180 Abbreviation[] abbs = getDefaultAbbreviations(); 181 182 prepareEditor(); 183 184 for (int cntr = 0; cntr < abbs.length; cntr++) { 185 moveCaretIntoCode(); 186 useAbbreviation(abbs[cntr].getName(), false); 187 } 188 189 log("testAbbreviationWithoutExpansion flush results:"); 190 191 flushResult(); 192 } finally { 193 log("testAbbreviationWithoutExpansion closing editor:"); 194 195 finishEditor(); 196 197 log("testAbbreviationWithoutExpansion finished"); 198 } 199 } 200 201 public void testAbbreviationAdd() { 202 log("testAbbreviationAdd start"); 203 Object backup = Utilities.saveAbbreviationsState(); 204 205 try { 206 Abbreviation[] toAdd = getAbbreviationsToAdd(); 207 208 prepareEditor(); 209 210 for (int cntr = 0; cntr < toAdd.length; cntr++) { 211 Abbreviations.addAbbreviation(getEditorName(), toAdd[cntr].getName(), toAdd[cntr].getExpansion()); 212 } 213 214 for (int cntr = 0; cntr < toAdd.length; cntr++) { 215 moveCaretIntoCode(); 216 useAbbreviation(toAdd[cntr].getName(), true); 217 } 218 219 log("testAbbreviationAdd flush results:"); 220 221 flushResult(); 222 } finally { 223 log("testAbbreviationAdd closing editor:"); 224 225 finishEditor(); 226 227 log("testAbbreviationAdd restoring abbreviations map:"); 228 229 Utilities.restoreAbbreviationsState(backup); 230 231 log("testAbbreviationAdd finished"); 232 } 233 } 234 235 public void testAbbreviationChange() { 236 log("testAbbreviationChange start"); 237 Object backup = Utilities.saveAbbreviationsState(); 238 239 try { 240 Abbreviation[] toChange = getAbbreviationsToModify(); 241 242 prepareEditor(); 243 244 for (int cntr = 0; cntr < toChange.length; cntr++) { 245 assertTrue("Editing of abbreviation with original name=\"" + toChange[cntr].getOldName() + "\" failed.", 246 Abbreviations.editAbbreviation(getEditorName(), 247 toChange[cntr].getOldName(), 248 toChange[cntr].getName(), 249 toChange[cntr].getExpansion())); 250 } 251 252 for (int cntr = 0; cntr < toChange.length; cntr++) { 253 moveCaretIntoCode(); 254 useAbbreviation(toChange[cntr].getOldName(), true); 256 useAbbreviation(toChange[cntr].getName(), true); 258 } 259 260 log("testAbbreviationChange flush results:"); 261 262 flushResult(); 263 } finally { 264 log("testAbbreviationChange closing editor:"); 265 266 finishEditor(); 267 268 log("testAbbreviationChange results abbreviations map:"); 269 270 Utilities.restoreAbbreviationsState(backup); 271 272 log("testAbbreviationChange finished"); 273 } 274 } 275 276 public void testAbbreviationRemoveCancel() { 277 log("testAbbreviationRemoveCancel start"); 278 Object backup = Utilities.saveAbbreviationsState(); 279 280 prepareEditor(); 281 282 try { 283 Abbreviations dialog = Abbreviations.invoke(getEditorName()); 284 285 Object [] keys=dialog.listAbbreviations().keySet().toArray(); 286 287 for (int cntr = 0; cntr < keys.length; cntr++) { 288 if (!dialog.removeAbbreviation((String )keys[cntr])) 289 log("Couldn't remove abbreviation: "+keys[cntr]); 290 } 291 292 if (dialog.listAbbreviations().size() > 0) { 293 Object [] lst=dialog.listAbbreviations().values().toArray(); 294 for (int i=0;i < lst.length;i++) { 295 log("Remained abbreviation: "+lst[i]); 296 } 297 assertTrue("After removing all known abbreviations, some of them remained. Probably bug of test.", false); 298 } 299 dialog.cancel(); 300 301 for (int cntr = 0; cntr < keys.length; cntr++) { 302 moveCaretIntoCode(); 303 useAbbreviation((String )keys[cntr], true); 305 } 306 307 log("testAbbreviationRemoveCancel flush results:"); 308 309 flushResult(); 310 } catch (Exception ex) { 311 ex.printStackTrace(getLog()); 312 } finally { 313 log("testAbbreviationRemoveCancel closing editor:"); 314 315 finishEditor(); 316 317 log("testAbbreviationRemoveCancel restoring abbreviations map:"); 318 319 Utilities.restoreAbbreviationsState(backup); 320 321 log("testAbbreviationRemoveCancel finished"); 322 } 323 } 324 325 public void testAbbreviationRemove() { 326 log("testAbbreviationRemove start"); 327 Object backup = Utilities.saveAbbreviationsState(); 328 329 prepareEditor(); 330 331 try { 332 Abbreviations dialog = null; 333 dialog = Abbreviations.invoke(getEditorName()); 334 335 Object [] keysold; 336 Object [] keys=dialog.listAbbreviations().keySet().toArray(); 337 338 for (int cntr = 0; cntr < keys.length; cntr++) { 339 if (!dialog.removeAbbreviation((String )keys[cntr])) 340 log("Couldn't remove abbreviation: "+keys[cntr]); 341 } 342 343 if (dialog.listAbbreviations().size() > 0) { 344 keysold=keys; 345 keys=dialog.listAbbreviations().keySet().toArray(); 346 for (int i=0;i < keys.length;i++) { 347 log("Remained abbreviation: "+keys[i]); 348 for (int j=0;j < keysold.length;j++) { 349 if (((String )keysold[j]).compareTo((String )keys[i]) == 0) { 350 log("is in default list."); 351 break; 352 } 353 } 354 } 355 } 357 dialog.oK(); 358 log("Abbreviations removing confirmed."); 359 for (int cntr = 0; cntr < keys.length; cntr++) { 360 moveCaretIntoCode(); 361 useAbbreviation((String )keys[cntr], true); 363 } 364 365 log("testAbbreviationRemove flush results:"); 366 367 flushResult(); 368 } catch (Exception ex) { 369 ex.printStackTrace(getLog()); 370 } finally { 371 log("testAbbreviationRemove closing editor:"); 372 373 finishEditor(); 374 375 log("testAbbreviationRemove restoring abbreviations map:"); 376 377 Utilities.restoreAbbreviationsState(backup); 378 379 log("testAbbreviationRemove finished"); 380 } 381 } 382 383 public void setUp() { 384 log("Starting abbreviations test. Test class=" + getClass()); 385 log("Test name=" + getName()); 386 System.setOut(getLog()); 387 } 388 389 public void tearDown() throws Exception { 390 log("Finishing abbreviations test. Test class=" + getClass()); 391 } 392 } 393 | Popular Tags |