1 19 20 25 26 package bookmarks; 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 import org.netbeans.junit.ide.ProjectSupport; 48 49 53 public class EditorTestCase extends JellyTestCase { 54 55 private static final int OPENED_PROJECT_ACCESS_TIMEOUT = 1000; 56 57 58 private String defaultProjectName = "editor_test"; 59 private String defaultSamplePackage = "dummy"; 60 private String defaultSampleName = "sample1"; 61 62 private static final char treeSeparator = '|'; 63 private final String defaultPackageNameTreePath = "Source packages"+treeSeparator+"dummy"; 64 private final String defaultFileName = "sample1"; 65 private String projectName = null; 66 private String treeSubPackagePathToFile = null; 67 private String fileName = null; 68 private final String dialogSaveTitle = "Save"; public static final int WAIT_MAX_MILIS_FOR_CLIPBOARD = 4000; 70 71 85 public EditorTestCase(String testMethodName) { 86 super(testMethodName); 87 88 defaultSamplePackage = getClass().getName(); 89 defaultSampleName = getName(); 90 } 91 92 95 public void openProject(String projectName) { 96 this.projectName = projectName; 97 File projectPath = new File (this.getDataDir() + "/projects", projectName); 98 log("data dir = "+this.getDataDir().toString()); 99 100 101 ProjectsTabOperator pto = new ProjectsTabOperator(); 102 pto.invoke(); 103 boolean isOpen = true; 104 try { 105 JemmyProperties.setCurrentTimeout("JTreeOperator.WaitNextNodeTimeout", OPENED_PROJECT_ACCESS_TIMEOUT); 106 ProjectRootNode prn = pto.getProjectRootNode(projectName); 107 } catch (TimeoutExpiredException ex) { 108 isOpen = false; 111 } 112 113 if ( isOpen ) { 114 log("Project is open!"); 115 return; 116 } 117 118 119 Object prj= ProjectSupport.openProject(projectPath); 120 } 121 122 130 protected String getDefaultProjectName() { 131 return defaultProjectName; 132 } 133 134 138 protected void openDefaultProject() { 139 openProject(getDefaultProjectName()); 140 } 141 142 145 protected void closeDefaultProject() { 146 closeProject(getDefaultProjectName()); 147 } 148 149 protected void closeProject(String projectName) { 150 ProjectSupport.closeProject(projectName); 151 } 152 153 154 156 public void openFile(String treeSubPackagePathToFile, String fileName) { 157 this.treeSubPackagePathToFile = treeSubPackagePathToFile; 159 ProjectsTabOperator pto = new ProjectsTabOperator(); 160 pto.invoke(); 161 ProjectRootNode prn = pto.getProjectRootNode(projectName); 162 prn.select(); 163 164 StringTokenizer st = new StringTokenizer (treeSubPackagePathToFile, 168 treeSeparator+""); 169 String token = ""; 170 String oldtoken = ""; 171 if (st.countTokens()>1) { 173 token = st.nextToken(); 174 String fullpath = token; 175 while (st.hasMoreTokens()) { 176 token = st.nextToken(); 177 waitForChildNode(fullpath, token); 178 fullpath += treeSeparator+token; 179 } 180 } 181 waitForChildNode(treeSubPackagePathToFile, fileName); 183 185 Node node = new Node(prn,treeSubPackagePathToFile+treeSeparator+fileName); 186 node.performPopupAction("Open"); 187 } 188 189 195 public void waitForChildNode(String parentPath, String childName) { 196 ProjectsTabOperator pto = new ProjectsTabOperator(); 197 ProjectRootNode prn = pto.getProjectRootNode(projectName); 198 prn.select(); 199 Node parent = new Node(prn, parentPath); 200 final String finalFileName = childName; 201 try { 202 JemmyProperties.setCurrentTimeout("Waiter.WaitingTime", 30000); 204 new Waiter(new Waitable() { 205 public Object actionProduced(Object parent) { 206 return ((Node)parent).isChildPresent(finalFileName) ? 207 Boolean.TRUE: null; 208 } 209 public String getDescription() { 210 return("Waiting for the tree to load."); 211 } 212 }).waitAction(parent); 213 } catch (InterruptedException e) { 214 throw new JemmyException("Interrupted.", e); 215 } 216 } 217 218 219 public void openFile() { 220 openFile(defaultPackageNameTreePath,defaultFileName); 221 } 222 223 225 public void closeFile() { 226 try { 227 new EditorOperator(fileName).close(); 228 } catch ( TimeoutExpiredException ex) { 229 log(ex.getMessage()); 230 log("Can't close the file"); 231 } 232 } 233 234 236 public void closeFileWithSave() { 237 try { 238 new EditorOperator(fileName).close(true); 239 } catch ( TimeoutExpiredException ex) { 240 log(ex.getMessage()); 241 log("Can't close the file"); 242 } 243 } 244 245 246 248 public void closeFileWithDiscard() { 249 try { 250 new EditorOperator(fileName).closeDiscard(); 251 } catch ( TimeoutExpiredException ex) { 252 log(ex.getMessage()); 253 log("Can't close the file"); 254 } 255 } 256 257 259 public void closeDialog(String title) { 260 NbDialogOperator dialog = new NbDialogOperator(title); 261 dialog.closeByButton(); 262 } 263 264 272 protected void compareReferenceFiles(Document testDoc) { 273 try { 274 ref(testDoc.getText(0, testDoc.getLength())); 275 compareReferenceFiles(); 276 } catch (BadLocationException e) { 277 e.printStackTrace(getLog()); 278 fail(); 279 } 280 } 281 282 288 protected void openSourceFile(String dir, String srcName) { 289 openFile(org.netbeans.jellytools.Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle", "NAME_src.dir")+treeSeparator+dir, srcName); 290 } 291 292 protected final String getDefaultSamplePackage() { 293 return defaultSamplePackage; 294 } 295 296 protected final String getDefaultSampleName() { 297 return defaultSampleName; 298 } 299 300 protected void openDefaultSampleFile() { 301 openSourceFile(defaultSamplePackage, defaultSampleName); 302 } 303 304 protected EditorOperator getDefaultSampleEditorOperator() { 305 return new EditorOperator(defaultSampleName); 306 } 307 308 317 protected boolean waitMaxMilisForValue(int maxMiliSeconds, ValueResolver resolver, Object requiredValue){ 318 int time = (int) maxMiliSeconds / 100; 319 while (time > 0) { 320 Object resolvedValue = resolver.getValue(); 321 if (requiredValue == null && resolvedValue == null){ 322 return true; 323 } 324 if (requiredValue != null && requiredValue.equals(resolvedValue)){ 325 return true; 326 } 327 try { 328 Thread.currentThread().sleep(100); 329 } catch (InterruptedException ex) { 330 time=0; 331 } 332 time--; 333 } 334 return false; 335 } 336 337 340 public static interface ValueResolver{ 341 342 Object getValue(); 343 } 344 345 } 346 | Popular Tags |