1 19 20 25 26 package lib; 27 28 import java.awt.datatransfer.Transferable ; 29 import java.io.File ; 30 import java.util.StringTokenizer ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.Document ; 33 import org.netbeans.jellytools.EditorOperator; 34 import org.netbeans.jellytools.JellyTestCase; 35 import org.netbeans.jellytools.NbDialogOperator; 36 import org.netbeans.jellytools.ProjectsTabOperator; 37 import org.netbeans.jellytools.nodes.Node; 38 import org.netbeans.jellytools.nodes.ProjectRootNode; 39 import org.netbeans.jemmy.JemmyException; 40 import org.netbeans.jemmy.JemmyProperties; 41 import org.netbeans.jemmy.TimeoutExpiredException; 42 import org.netbeans.jemmy.Waitable; 43 import org.netbeans.jemmy.Waiter; 44 import org.netbeans.jemmy.operators.ComponentOperator; 45 import org.netbeans.jemmy.operators.JEditorPaneOperator; 46 import org.netbeans.jemmy.operators.JTextFieldOperator; 47 50 54 public class EditorTestCase extends JellyTestCase { 55 56 private static final int OPENED_PROJECT_ACCESS_TIMEOUT = 1000; 57 58 59 private String defaultProjectName = "editor_test"; 60 private String defaultSamplePackage = "dummy"; 61 private String defaultSampleName = "sample1"; 62 63 private static final char treeSeparator = '|'; 64 private final String defaultPackageNameTreePath = "Source packages"+treeSeparator+"dummy"; 65 private final String defaultFileName = "sample1"; 66 private String projectName = null; 67 private String treeSubPackagePathToFile = null; 68 private String fileName = null; 69 private final String dialogSaveTitle = "Save"; public static final int WAIT_MAX_MILIS_FOR_CLIPBOARD = 4000; 71 72 86 public EditorTestCase(String testMethodName) { 87 super(testMethodName); 88 89 defaultSamplePackage = getClass().getName(); 90 defaultSampleName = getName(); 91 } 92 93 99 107 108 111 public void openProject(String projectName) { 112 this.projectName = projectName; 113 File projectPath = new File (this.getDataDir() + "/projects", projectName); 114 log("data dir = "+this.getDataDir().toString()); 115 116 117 ProjectsTabOperator pto = new ProjectsTabOperator(); 118 pto.invoke(); 119 boolean isOpen = true; 120 try { 121 JemmyProperties.setCurrentTimeout("JTreeOperator.WaitNextNodeTimeout", OPENED_PROJECT_ACCESS_TIMEOUT); 122 ProjectRootNode prn = pto.getProjectRootNode(projectName); 123 } catch (TimeoutExpiredException ex) { 124 isOpen = false; 127 } 128 129 if ( isOpen ) { 130 log("Project is open!"); 131 return; 132 } 133 134 135 136 139 } 140 141 149 protected String getDefaultProjectName() { 150 return defaultProjectName; 151 } 152 153 154 155 161 166 167 171 protected void openDefaultProject() { 172 openProject(getDefaultProjectName()); 173 } 174 175 178 protected void closeDefaultProject() { 179 closeProject(getDefaultProjectName()); 180 } 181 182 protected void closeProject(String projectName) { 183 } 186 187 188 190 public void openFile(String treeSubPackagePathToFile, String fileName) { 191 this.treeSubPackagePathToFile = treeSubPackagePathToFile; 193 ProjectsTabOperator pto = new ProjectsTabOperator(); 194 pto.invoke(); 195 ProjectRootNode prn = pto.getProjectRootNode(projectName); 196 prn.select(); 197 198 StringTokenizer st = new StringTokenizer (treeSubPackagePathToFile, 202 treeSeparator+""); 203 String token = ""; 204 String oldtoken = ""; 205 if (st.countTokens()>1) { 207 token = st.nextToken(); 208 String fullpath = token; 209 while (st.hasMoreTokens()) { 210 token = st.nextToken(); 211 waitForChildNode(fullpath, token); 212 fullpath += treeSeparator+token; 213 } 214 } 215 waitForChildNode(treeSubPackagePathToFile, fileName); 217 219 Node node = new Node(prn,treeSubPackagePathToFile+treeSeparator+fileName); 220 node.performPopupAction("Open"); 221 } 222 223 229 public void waitForChildNode(String parentPath, String childName) { 230 ProjectsTabOperator pto = new ProjectsTabOperator(); 231 ProjectRootNode prn = pto.getProjectRootNode(projectName); 232 prn.select(); 233 Node parent = new Node(prn, parentPath); 234 final String finalFileName = childName; 235 try { 236 JemmyProperties.setCurrentTimeout("Waiter.WaitingTime", 30000); 238 new Waiter(new Waitable() { 239 public Object actionProduced(Object parent) { 240 return ((Node)parent).isChildPresent(finalFileName) ? 241 Boolean.TRUE: null; 242 } 243 public String getDescription() { 244 return("Waiting for the tree to load."); 245 } 246 }).waitAction(parent); 247 } catch (InterruptedException e) { 248 throw new JemmyException("Interrupted.", e); 249 } 250 } 251 252 253 public void openFile() { 254 openFile(defaultPackageNameTreePath,defaultFileName); 255 } 256 257 259 public void closeFile() { 260 try { 261 new EditorOperator(fileName).close(); 262 } catch ( TimeoutExpiredException ex) { 263 log(ex.getMessage()); 264 log("Can't close the file"); 265 } 266 } 267 268 270 public void closeFileWithSave() { 271 try { 272 new EditorOperator(fileName).close(true); 273 } catch ( TimeoutExpiredException ex) { 274 log(ex.getMessage()); 275 log("Can't close the file"); 276 } 277 } 278 279 280 282 public void closeFileWithDiscard() { 283 try { 284 new EditorOperator(fileName).closeDiscard(); 285 } catch ( TimeoutExpiredException ex) { 286 log(ex.getMessage()); 287 log("Can't close the file"); 288 } 289 } 290 291 293 public void closeDialog(String title) { 294 NbDialogOperator dialog = new NbDialogOperator(title); 295 dialog.closeByButton(); 296 } 297 298 306 protected void compareReferenceFiles(Document testDoc) { 307 try { 308 ref(testDoc.getText(0, testDoc.getLength())); 309 compareReferenceFiles(); 310 } catch (BadLocationException e) { 311 e.printStackTrace(getLog()); 312 fail(); 313 } 314 } 315 316 322 protected void openSourceFile(String dir, String srcName) { 323 openFile(org.netbeans.jellytools.Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle", "NAME_src.dir")+treeSeparator+dir, srcName); 324 } 325 326 protected final String getDefaultSamplePackage() { 327 return defaultSamplePackage; 328 } 329 330 protected final String getDefaultSampleName() { 331 return defaultSampleName; 332 } 333 334 protected void openDefaultSampleFile() { 335 openSourceFile(defaultSamplePackage, defaultSampleName); 336 } 337 338 protected EditorOperator getDefaultSampleEditorOperator() { 339 return new EditorOperator(defaultSampleName); 340 } 341 342 351 protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object requiredValue){ 352 int time = (int) maxMiliSeconds / 100; 353 while (time > 0) { 354 Object resolvedValue = resolver.getValue(); 355 if (requiredValue == null && resolvedValue == null){ 356 return true; 357 } 358 if (requiredValue != null && requiredValue.equals(resolvedValue)){ 359 return true; 360 } 361 try { 362 Thread.currentThread().sleep(100); 363 } catch (InterruptedException ex) { 364 time=0; 365 } 366 time--; 367 } 368 return false; 369 } 370 371 374 public static interface ValueResolver{ 375 376 Object getValue(); 377 } 378 379 protected ValueResolver getClipboardResolver(final JEditorPaneOperator txtOper, final Transferable oldClipValue){ 380 381 ValueResolver clipboardValueResolver = new ValueResolver(){ 382 public Object getValue(){ 383 Transferable newClipValue = txtOper.getToolkit().getSystemClipboard().getContents(txtOper); 384 log("newClipValue:"+newClipValue); 385 return (newClipValue == oldClipValue) ? Boolean.TRUE : Boolean.FALSE; 386 } 387 }; 388 389 return clipboardValueResolver; 390 } 391 392 protected ValueResolver getTextFieldResolver(final JTextFieldOperator oper,final String newValue) { 393 ValueResolver textFieldResolver = new ValueResolver(){ 394 public Object getValue(){ 395 String actVal = oper.getText(); 396 log("actual value:"+actVal); 397 return (newValue.equals(actVal)) ? Boolean.TRUE : Boolean.FALSE; 398 } 399 }; 400 401 return textFieldResolver; 402 } 403 404 protected void cutCopyViaStrokes(JEditorPaneOperator txtOper, int key, int mod){ 405 Transferable oldClipValue = txtOper.getToolkit().getSystemClipboard().getContents(txtOper); 406 log(""); 407 log("oldClipValue:"+oldClipValue); 408 txtOper.pushKey(key, mod); 409 boolean success = waitMaxMilisForValue(WAIT_MAX_MILIS_FOR_CLIPBOARD, getClipboardResolver(txtOper, oldClipValue), Boolean.FALSE); 411 if (success == false){ 412 log("!!!! ONCE AGAIN"); 415 txtOper.pushKey(key, mod); 416 waitMaxMilisForValue(WAIT_MAX_MILIS_FOR_CLIPBOARD, getClipboardResolver(txtOper, oldClipValue), Boolean.FALSE); 418 } 419 } 420 421 protected void pasteViaStrokes(ComponentOperator compOper, int key, int mod, ValueResolver resolver){ 422 compOper.pushKey(key, mod); 423 if (resolver !=null){ 425 boolean success = waitMaxMilisForValue(WAIT_MAX_MILIS_FOR_CLIPBOARD, resolver, Boolean.TRUE); 426 if (success == false){ 427 compOper.pushKey(key, mod); 429 waitMaxMilisForValue(WAIT_MAX_MILIS_FOR_CLIPBOARD, resolver, Boolean.TRUE); 431 } 432 } 433 } 434 435 } 436 | Popular Tags |