1 21 22 package gui.debuggercore; 23 24 import java.awt.Component ; 25 import java.awt.event.KeyEvent ; 26 import javax.swing.JComponent ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.JScrollPane ; 29 import javax.swing.KeyStroke ; 30 import org.netbeans.jellytools.*; 31 import org.netbeans.jellytools.actions.Action; 32 import org.netbeans.jellytools.actions.DebugProjectAction; 33 import org.netbeans.jellytools.modules.debugger.actions.FinishDebuggerAction; 34 import org.netbeans.jellytools.modules.debugger.actions.NewBreakpointAction; 35 import org.netbeans.jellytools.modules.debugger.actions.ToggleBreakpointAction; 36 import org.netbeans.jellytools.nodes.Node; 37 import org.netbeans.jemmy.ComponentChooser; 38 import org.netbeans.jemmy.EventTool; 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.ContainerOperator; 45 import org.netbeans.jemmy.operators.JPopupMenuOperator; 46 import org.netbeans.jemmy.operators.JTableOperator; 47 import org.openide.awt.StatusDisplayer; 48 49 50 51 52 public class Utilities { 53 54 public static String windowMenu = Bundle.getStringTrimmed("org.netbeans.core.Bundle", "Menu/Window"); 55 public static String runMenu = Bundle.getStringTrimmed("org.netbeans.modules.project.ui.Bundle", "Menu/RunProject"); 56 public static String debugMenu = Bundle.getStringTrimmed("org.netbeans.modules.debugger.resources.Bundle", "Menu/Window/Debug"); 57 public static String runFileMenu = Bundle.getString("org.netbeans.modules.java.project.Bundle", "LBL_RunFile_Action"); 58 public static String debugToolbarLabel = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.Bundle", "Toolbars/Debug"); 59 60 public static String toggleBreakpointItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Toggle_breakpoint"); 61 public static String newBreakpointItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_AddBreakpoint"); 62 public static String newWatchItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_New_Watch"); 63 public static String debugMainProjectItem = Bundle.getStringTrimmed("org.netbeans.modules.project.ui.actions.Bundle", "LBL_DebugMainProjectAction_Name"); 64 public static String stepIntoItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Step_into_action_name"); 65 public static String stepOverItem = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_Step_Over"); 66 public static String stepOutItem = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_Step_Out"); 67 public static String stepOverExpresItem = Bundle.getString("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Step_operation_action_name"); 68 public static String runIntoMethodItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Run_into_method_action_name"); 69 public static String continueItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Continue_action_name"); 70 public static String pauseItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Pause_action_name"); 71 public static String finishSessionsItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_KillAction_name"); 72 public static String runToCursorItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Run_to_cursor_action_name"); 73 public static String applyCodeChangesItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Fix_action_name"); 74 public static String evaluateExpressionItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.actions.Bundle", "CTL_Evaluate"); 75 76 public static String localVarsItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_LocalVariablesAction"); 77 public static String watchesItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_WatchesAction"); 78 public static String callStackItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_CallStackAction"); 79 public static String classesItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.actions.Bundle", "CTL_ClassesAction"); 80 public static String sourcesItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.actions.Bundle", "CTL_SourcesViewAction"); 81 public static String breakpointsItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_BreakpointsAction"); 82 public static String sessionsItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_SessionsAction"); 83 public static String threadsItem = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_ThreadsAction"); 84 85 public static String localVarsViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.views.Bundle", "CTL_Variables_view"); 86 public static String watchesViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.views.Bundle", "CTL_Watches_view"); 87 public static String callStackViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.views.Bundle", "CTL_Call_stack_view"); 88 public static String classesViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.views.Bundle", "CTL_Classes_view"); 89 public static String sourcesViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.views.Bundle", "CTL_Sourcess_view"); 90 public static String breakpointsViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.views.Bundle", "CTL_Breakpoints_view"); 91 public static String sessionsViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.views.Bundle", "CTL_Sessions_view"); 92 public static String threadsViewTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.views.Bundle", "CTL_Threads_view"); 93 94 public static String customizeBreakpointTitle = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.models.Bundle", "CTL_Breakpoint_Customizer_Title"); 95 public static String newBreakpointTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_Breakpoint_Title"); 96 public static String newWatchTitle = Bundle.getStringTrimmed("org.netbeans.modules.debugger.ui.actions.Bundle", "CTL_WatchDialog_Title"); 97 public static String debuggerConsoleTitle = Bundle.getString("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_DebuggerConsole_Title"); 98 99 100 public static String runningStatusBarText = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_Debugger_running"); 101 public static String stoppedStatusBarText = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_Debugger_stopped"); 102 public static String finishedStatusBarText = Bundle.getStringTrimmed("org.netbeans.modules.debugger.jpda.ui.Bundle", "CTL_Debugger_finished"); 103 public static String buildCompleteStatusBarText = "Finished building"; 104 public static String evaluatingPropertyText = Bundle.getString("org.netbeans.modules.viewmodel.Bundle", "EvaluatingProp"); 105 106 public static String openSourceAction = Bundle.getStringTrimmed("org.openide.actions.Bundle", "Open"); 107 public static String setMainProjectAction = Bundle.getStringTrimmed("org.netbeans.modules.project.ui.actions.Bundle", "LBL_SetAsMainProjectAction_Name"); 108 public static String projectPropertiesAction = Bundle.getStringTrimmed("org.netbeans.modules.project.ui.actions.Bundle", "LBL_CustomizeProjectAction_Popup_Name"); 109 public static String projectPropertiesTitle = Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.Bundle", "LBL_Customizer_Title"); 110 112 public static String testProjectName = "debugTestProject"; 113 114 public static KeyStroke toggleBreakpointShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F8, KeyEvent.CTRL_MASK); 115 public static KeyStroke newBreakpointShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F8, KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK); 116 public static KeyStroke newWatchShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F7, KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK); 117 public static KeyStroke debugProjectShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0); 118 public static KeyStroke debugFileShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F5, KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK); 119 public static KeyStroke runToCursorShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0); 120 public static KeyStroke stepIntoShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0); 121 public static KeyStroke stepOutShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F7, KeyEvent.CTRL_MASK); 122 public static KeyStroke stepOverShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0); 123 public static KeyStroke continueShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F5, KeyEvent.CTRL_MASK); 124 public static KeyStroke killSessionShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F5, KeyEvent.SHIFT_MASK); 125 public static KeyStroke buildProjectShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0); 126 public static KeyStroke openBreakpointsShortcut = KeyStroke.getKeyStroke(KeyEvent.VK_5, KeyEvent.ALT_MASK|KeyEvent.SHIFT_MASK); 127 128 129 130 public Utilities() {} 131 132 public static boolean verifyMainMenu(String actionPath, boolean expected) { 133 if (expected == MainWindowOperator.getDefault().menuBar().showMenuItem(actionPath).isEnabled()) 134 return MainWindowOperator.getDefault().menuBar().showMenuItem(actionPath).isEnabled(); 135 else { 136 for (int i = 0; i < 10; i++) { 137 if(MainWindowOperator.getDefault().menuBar().showMenuItem(actionPath).isEnabled() == expected) { 138 MainWindowOperator.getDefault().menuBar().closeSubmenus(); 139 return expected; 140 } 141 MainWindowOperator.getDefault().menuBar().closeSubmenus(); 142 new EventTool().waitNoEvent(500); 143 System.err.println("waiting on "+actionPath); 144 } 145 return MainWindowOperator.getDefault().menuBar().showMenuItem(actionPath).isEnabled(); 146 } 147 } 148 149 public static boolean verifyPopup(Node node, String [] menus) { 150 JPopupMenuOperator popup=node.callPopup(); 152 return verifyPopup(popup, menus); 153 } 154 155 public static boolean verifyPopup(final JPopupMenuOperator popup, String [] menus) { 156 for (int i=0;i < menus.length;i++) { 157 try { 158 popup.showMenuItem(menus[i]); 159 } catch (NullPointerException npe) { 160 throw new JemmyException("Popup path ["+menus[i]+"] not found."); 161 } 162 } 163 popup.waitState(new ComponentChooser() { 165 public boolean checkComponent(Component comp) { 166 popup.pushKey(KeyEvent.VK_ESCAPE); 167 return !popup.isVisible(); 168 } 169 public String getDescription() { 170 return "Popup menu closed"; 171 } 172 }); 173 return true; 174 } 175 176 public static boolean checkAnnotation(EditorOperator operator, int line, String annotationType) { 177 Object [] annotations = operator.getAnnotations(line); 178 boolean found = false; 179 JemmyProperties.getProperties().getOutput().print(">>>>> Annotations on line: "+line+"\n"); 180 for (int i=0;i < annotations.length;i++) { 181 JemmyProperties.getProperties().getOutput().print(" "+operator.getAnnotationType(annotations[i])+"\n"); 182 if (annotationType.equals(operator.getAnnotationType(annotations[i]))) found=true; 183 } 184 return found; 185 } 186 187 public static void deleteAllBreakpoints() { 188 showDebuggerView(breakpointsViewTitle); 189 JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(breakpointsViewTitle)); 190 if (jTableOperator.getRowCount() > 0) 191 new JPopupMenuOperator(jTableOperator.callPopupOnCell(0, 0)).pushMenu("Delete All"); 192 } 193 194 public static void deleteAllWatches() { 195 showDebuggerView(watchesViewTitle); 196 JTableOperator jTableOperator = new JTableOperator(new TopComponentOperator(watchesViewTitle)); 197 if (jTableOperator.getRowCount() > 0) 198 new JPopupMenuOperator(jTableOperator.callPopupOnCell(0, 0)).pushMenu("Delete All"); 199 } 200 201 public static void closeZombieSessions() { 202 MainWindowOperator mwo = MainWindowOperator.getDefault(); 203 showDebuggerView(sessionsViewTitle); 204 TopComponentOperator sessionsOper = new TopComponentOperator(sessionsViewTitle); 205 JTableOperator jTableOperator = new JTableOperator(sessionsOper); 206 207 for (int i = 0; i < jTableOperator.getRowCount(); i++) { 208 new Action(null, null, Utilities.killSessionShortcut).performShortcut(); 210 } 212 213 jTableOperator = new JTableOperator(sessionsOper); 214 if (jTableOperator.getRowCount() > 0) { 215 for (int i = 0; i < jTableOperator.getRowCount(); i++) { 216 jTableOperator.selectCell(i,0); 217 javax.swing.JPopupMenu jPopupMenu = jTableOperator.callPopupOnCell(jTableOperator.getRowCount() - 1,0); 218 new JPopupMenuOperator(jPopupMenu).pushMenu("Finish"); 219 mwo.waitStatusText("User program finished"); 220 } 221 } 222 sessionsOper.close(); 223 } 224 225 public static void showDebuggerView(String viewName) { 226 new Action(windowMenu + "|" + debugMenu + "|" + viewName, null).perform(); 227 new TopComponentOperator(viewName); 228 new EventTool().waitNoEvent(500); 229 } 230 231 public static String removeTags(String in) { 232 String out = ""; 233 in = in.trim(); 234 if (in.indexOf('<') == -1) { 235 out = in; 236 } else { 237 while (in.indexOf('<') >= 0) { 238 if (in.indexOf('<') == 0) { 239 in = in.substring(in.indexOf('>')+1, in.length()); 240 } else { 241 out += in.substring(0, in.indexOf('<')); 242 in = in.substring(in.indexOf('<'), in.length()); 243 } 244 } 245 } 246 return out; 247 } 248 249 public static void endAllSessions() { 250 String finishPath = runMenu+"|"+finishSessionsItem; 251 while (MainWindowOperator.getDefault().menuBar().showMenuItem(finishPath).isEnabled()) { 252 new FinishDebuggerAction().performShortcut(); 253 new EventTool().waitNoEvent(500); 254 } 255 MainWindowOperator.getDefault().menuBar().closeSubmenus(); 256 } 257 258 public static void startDebugger() { 259 String setAsMainProjectItem = Bundle.getStringTrimmed("org.netbeans.modules.project.ui.actions.Bundle", "LBL_SetAsMainProjectAction_Name"); 261 new Action(null, setAsMainProjectItem).perform(new ProjectsTabOperator().getProjectRootNode(testProjectName)); 262 new DebugProjectAction().performShortcut(); 263 getDebugToolbar().waitComponentVisible(true); 264 } 265 266 public static ContainerOperator getDebugToolbar() { 267 return MainWindowOperator.getDefault().getToolbar(debugToolbarLabel); 268 } 269 270 public static void setCaret(EditorOperator eo, final int line) { 271 eo.makeComponentVisible(); 272 eo.setCaretPositionToLine(line); 273 new EventTool().waitNoEvent(500); 274 275 try { 276 new Waiter(new Waitable() { 277 public Object actionProduced(Object editorOper) { 278 EditorOperator op = (EditorOperator)editorOper; 279 if (op.getLineNumber() == line) { 280 return Boolean.TRUE; 281 } 282 return null; 283 } 284 285 public String getDescription() { 286 return "Wait caret position on line " + line; 287 } 288 }).waitAction(eo); 289 } catch (InterruptedException ex) { 290 ex.printStackTrace(); 291 } 292 } 293 294 public static NbDialogOperator newBreakpoint(int line, int column) { 295 EditorOperator eo = new EditorOperator("MemoryView.java"); 296 eo.setCaretPosition(line, column); 297 new NewBreakpointAction().perform(); 298 NbDialogOperator dialog = new NbDialogOperator(newBreakpointTitle); 299 new EventTool().waitNoEvent(1000l); 300 return dialog; 301 } 302 303 public static NbDialogOperator newBreakpoint(int line) { 304 Node projectNode = ProjectsTabOperator.invoke().getProjectRootNode(testProjectName); 305 EditorOperator eo = new EditorOperator("MemoryView.java"); 306 setCaret(eo, line); 307 new NewBreakpointAction().perform(); 308 NbDialogOperator dialog = new NbDialogOperator(newBreakpointTitle); 309 new EventTool().waitNoEvent(1000l); 310 return dialog; 311 } 312 313 public static void toggleBreakpoint(EditorOperator eo, final int line) { 314 toggleBreakpoint(eo, line, true); 315 } 316 317 public static void toggleBreakpoint(EditorOperator eo, final int line, final boolean newBreakpoint) { 318 setCaret(eo, line); 319 320 new ToggleBreakpointAction().perform(); 321 try { 322 new Waiter(new Waitable() { 323 public Object actionProduced(Object editorOper) { 324 Object [] annotations = ((EditorOperator) editorOper).getAnnotations(line); 325 boolean found =false; 326 for (int i = 0; i < annotations.length; i++) { 327 if ("Breakpoint".equals(((EditorOperator) editorOper).getAnnotationType(annotations[i]))) { 328 found=true; 329 if (newBreakpoint) { 330 return Boolean.TRUE; 331 } 332 } 333 } 334 if (!found && !newBreakpoint) { 335 return Boolean.TRUE; 336 } 337 return null; 338 } 339 340 public String getDescription() { 341 return "Wait breakpoint established on line " + line; 342 } 343 }).waitAction(eo); 344 } catch (InterruptedException ex) { 345 ex.printStackTrace(); 346 } 347 } 348 349 public static void waitFinished(JellyTestCase test, String projectName, String target) { 350 long oldTimeout = MainWindowOperator.getDefault().getTimeouts().getTimeout("Waiter.WaitingTime"); 351 try { 352 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 240000); 354 MainWindowOperator.getDefault().waitStatusText("Finished building "+projectName+" ("+target+")"); 355 } finally { 356 MainWindowOperator.getDefault().getStatusTextTracer().start(); 358 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", oldTimeout); 360 test.getLog("RunOutput").print(new OutputTabOperator(projectName).getText()); } 363 } 364 365 public static void waitStatusText(String text) { 366 long oldTimeout = MainWindowOperator.getDefault().getTimeouts().getTimeout("Waiter.WaitingTime"); 367 try { 368 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", 240000); 370 MainWindowOperator.getDefault().waitStatusText(text); 371 } finally { 372 MainWindowOperator.getDefault().getStatusTextTracer().start(); 374 MainWindowOperator.getDefault().getTimeouts().setTimeout("Waiter.WaitingTime", oldTimeout); 376 } 377 } 378 379 public static void waitStatusTextPrefix(final String text) { 380 try { 381 new Waiter(new Waitable() { 382 public Object actionProduced(Object anObject) { 383 JemmyProperties.getProperties().getOutput().print(">>>>> status text: "+StatusDisplayer.getDefault().getStatusText()+" > "+anObject+"\n"); 384 if (StatusDisplayer.getDefault().getStatusText().startsWith(text)) 385 return Boolean.TRUE; 386 return null; 387 } 388 public String getDescription() { 389 return("Wait status text prefix: "+text); 390 } 391 }).waitAction(StatusDisplayer.getDefault()); 392 } catch (InterruptedException ex) { 393 ex.printStackTrace(); 394 } 395 } 396 397 public static int getDebuggerConsoleStatus() { 398 return new OutputTabOperator(debuggerConsoleTitle).getLineCount(); 399 } 400 401 public static int waitDebuggerConsole(final String text, final int status) { 402 OutputTabOperator op = new OutputTabOperator(debuggerConsoleTitle); 403 ConsoleChooser cch = new ConsoleChooser(op, text, status); 404 JemmyProperties.getCurrentOutput().printLine("Waiting on text in debugger console '"+text+"' from line "+status); 405 JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 30000l); 406 try { 407 op.waitState(cch); 408 } catch (TimeoutExpiredException ex) { 409 JemmyProperties.getCurrentOutput().printLine("Not found text in debugger console:"); 410 JemmyProperties.getCurrentOutput().printLine(op.getText()); 411 throw ex; 412 } 413 JemmyProperties.getCurrentOutput().printLine("Found text in debugger console '"+text+"' at line "+cch.getLastIndex()); 414 return cch.getLastIndex(); 415 } 416 417 static class ConsoleChooser implements ComponentChooser { 418 int lastIndex = 0; 419 OutputTabOperator op; 420 String text; 421 int status; 422 public ConsoleChooser(OutputTabOperator op, String text, int status) { 423 this.op = op; 424 this.text = text; 425 this.status = status; 426 } 427 public boolean checkComponent(Component comp) { 428 for(int i = status; i < op.getLineCount(); i++) { 429 if(op.getLine(i).startsWith(text)) { 430 lastIndex = i; 431 return true; 432 } 433 } 434 return false; 435 } 436 public String getDescription() { 437 return("\"" + text + "\" text"); 438 } 439 public int getLastIndex() { 440 return lastIndex; 441 } 442 } 443 } 444 445 | Popular Tags |