4773798a5a5b43ae6a4ea44eadea3f75ca096e46
[idea/community.git] / platform / xdebugger-impl / testSrc / com / intellij / xdebugger / XDebuggerTestUtil.java
1 /*
2  * Copyright 2000-2011 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.xdebugger;
17
18 import com.intellij.execution.impl.ConsoleViewImpl;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.application.Result;
21 import com.intellij.openapi.application.WriteAction;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.Pair;
24 import com.intellij.openapi.util.Ref;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.testFramework.UsefulTestCase;
27 import com.intellij.util.ui.UIUtil;
28 import com.intellij.xdebugger.breakpoints.*;
29 import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
30 import com.intellij.xdebugger.frame.*;
31 import com.intellij.xdebugger.impl.breakpoints.XBreakpointUtil;
32 import com.intellij.xdebugger.impl.breakpoints.XLineBreakpointImpl;
33 import com.intellij.xdebugger.ui.DebuggerIcons;
34 import junit.framework.Assert;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37
38 import java.lang.reflect.InvocationTargetException;
39 import java.util.*;
40 import java.util.concurrent.Semaphore;
41 import java.util.concurrent.TimeUnit;
42
43 public class XDebuggerTestUtil {
44   private static final int TIMEOUT = 25000;
45
46   private XDebuggerTestUtil() {
47   }
48
49   public static <B extends XBreakpoint<?>> void assertBreakpointValidity(Project project,
50                                                                          VirtualFile file,
51                                                                          int line,
52                                                                          boolean validity,
53                                                                          String errorMessage,
54                                                                          Class<? extends XBreakpointType<B, ?>> breakpointType) {
55     XLineBreakpointType type = (XLineBreakpointType)XDebuggerUtil.getInstance().findBreakpointType(breakpointType);
56     XBreakpointManager manager = XDebuggerManager.getInstance(project).getBreakpointManager();
57     XLineBreakpointImpl breakpoint = (XLineBreakpointImpl)manager.findBreakpointAtLine(type, file, line);
58     Assert.assertNotNull(breakpoint);
59     Assert.assertEquals(validity ? DebuggerIcons.VERIFIED_BREAKPOINT_ICON : DebuggerIcons.INVALID_BREAKPOINT_ICON, breakpoint.getIcon());
60     Assert.assertEquals(errorMessage, breakpoint.getErrorMessage());
61   }
62
63   public static void toggleBreakpoint(Project project, VirtualFile file, int line) {
64     XDebuggerUtil.getInstance().toggleLineBreakpoint(project, file, line);
65   }
66
67   public static void assertPosition(XSourcePosition pos, VirtualFile file, int line) {
68     Assert.assertNotNull(pos);
69     Assert.assertEquals(file, pos.getFile());
70     if (line != -1) Assert.assertEquals(line, pos.getLine());
71   }
72
73   public static void assertCurrentPosition(XDebugSession session, VirtualFile file, int line) {
74     assertPosition(session.getCurrentPosition(), file, line);
75   }
76
77   public static XExecutionStack getActiveThread(@NotNull XDebugSession session) {
78     return session.getSuspendContext().getActiveExecutionStack();
79   }
80
81   public static List<XStackFrame> collectStacks(@NotNull XDebugSession session) throws InterruptedException {
82     return collectStacks(null, session);
83   }
84
85   public static List<XStackFrame> collectStacks(@Nullable XExecutionStack thread, @NotNull XDebugSession session) throws InterruptedException {
86     return collectStacks(thread == null ? getActiveThread(session) : thread);
87   }
88
89   public static List<XStackFrame> collectStacks(@NotNull XExecutionStack thread) throws InterruptedException {
90     XTestStackFrameContainer container = new XTestStackFrameContainer();
91     thread.computeStackFrames(0, container);
92     return container.waitFor(TIMEOUT * 2).first;
93   }
94
95   public static Pair<XValue, String> evaluate(XDebugSession session, String expression) throws InterruptedException {
96     XDebuggerEvaluator evaluator = session.getCurrentStackFrame().getEvaluator();
97     XTestEvaluationCallback callback = new XTestEvaluationCallback();
98     evaluator.evaluate(expression, callback, session.getCurrentPosition());
99     return callback.waitFor(TIMEOUT);
100   }
101
102   public static void waitForSwing() throws InterruptedException, InvocationTargetException {
103     final com.intellij.util.concurrency.Semaphore s = new com.intellij.util.concurrency.Semaphore();
104     s.down();
105     ApplicationManager.getApplication().invokeLater(new Runnable() {
106       public void run() {
107         s.up();
108       }
109     });
110     s.waitForUnsafe();
111     UIUtil.invokeAndWaitIfNeeded(new Runnable() {
112       public void run() {
113       }
114     });
115   }
116
117   @NotNull
118   public static XValue findVar(Collection<XValue> vars, String name) {
119     for (XValue each : vars) {
120       if (each instanceof XNamedValue) {
121         if (((XNamedValue)each).getName().equals(name)) return each;
122       }
123     }
124     throw new AssertionError("var '" + name + "' not found");
125   }
126
127   public static XTestValueNode computePresentation(@NotNull XValue value) throws InterruptedException {
128     XTestValueNode node = new XTestValueNode();
129     if (value instanceof XNamedValue) {
130       node.myName = ((XNamedValue)value).getName();
131     }
132     value.computePresentation(node, XValuePlace.TREE);
133     Assert.assertTrue(node.waitFor(TIMEOUT));
134     return node;
135   }
136
137   public static void assertVariable(XValue var,
138                                     @Nullable String name,
139                                     @Nullable String type,
140                                     @Nullable String value,
141                                     @Nullable Boolean hasChildren) throws InterruptedException {
142     XTestValueNode node = computePresentation(var);
143
144     Assert.assertEquals(name, node.myName);
145     if (type != null) Assert.assertEquals(type, node.myType);
146     if (value != null) Assert.assertEquals(value, node.myValue);
147     if (hasChildren != null) Assert.assertEquals((boolean)hasChildren, node.myHasChildren);
148   }
149
150   public static void assertVariableValue(XValue var, @Nullable String name, @Nullable String value) throws InterruptedException {
151     assertVariable(var, name, null, value, null);
152   }
153
154   public static void assertVariableValue(Collection<XValue> vars, @Nullable String name, @Nullable String value) throws InterruptedException {
155     assertVariableValue(findVar(vars, name), name, value);
156   }
157
158   public static void assertVariableValueMatches(Collection<XValue> vars, @Nullable String name, String valuePattern) throws InterruptedException {
159     assertVariableValueMatches(findVar(vars, name), name, valuePattern);
160   }
161
162   public static void assertVariableValueMatches(XValue var, String name, String valuePattern) throws InterruptedException {
163     XTestValueNode node = computePresentation(var);
164     Assert.assertEquals(name, node.myName);
165     Assert.assertTrue(node.myValue, node.myValue.matches(valuePattern));
166   }
167
168   public static void assertVariables(List<XValue> vars, String... names) throws InterruptedException {
169     List<String> expectedNames = new ArrayList<String>(Arrays.asList(names));
170
171     List<String> actualNames = new ArrayList<String>();
172     for (XValue each : vars) {
173       actualNames.add(computePresentation(each).myName);
174     }
175
176     Collections.sort(actualNames);
177     Collections.sort(expectedNames);
178     UsefulTestCase.assertOrderedEquals(actualNames, expectedNames);
179   }
180
181   public static void assertSourcePosition(final XValue value, VirtualFile file, int offset) {
182     final XTestNavigatable n = new XTestNavigatable();
183     ApplicationManager.getApplication().runReadAction(new Runnable() {
184       @Override
185       public void run() {
186         value.computeSourcePosition(n);
187       }
188     });
189     Assert.assertNotNull(n.myPosition);
190     Assert.assertEquals(file, n.myPosition.getFile());
191     Assert.assertEquals(offset, n.myPosition.getOffset());
192   }
193
194   public static boolean waitFor(Semaphore semaphore, long timeoutInMillis) throws InterruptedException {
195     return semaphore.tryAcquire(timeoutInMillis, TimeUnit.MILLISECONDS);
196   }
197
198   public static void assertVariable(Collection<XValue> vars, String name, String type, String value, Boolean hasChildren)
199     throws InterruptedException {
200     assertVariable(findVar(vars, name), name, type, value, hasChildren);
201   }
202
203   @NotNull
204   public static String getConsoleText(final @NotNull ConsoleViewImpl consoleView) {
205     new WriteAction() {
206       protected void run(Result result) throws Throwable {
207         consoleView.flushDeferredText();
208       }
209     }.execute();
210
211     return consoleView.getEditor().getDocument().getText();
212   }
213
214   public static <T extends XBreakpointType> XBreakpoint addBreakpoint(@NotNull final Project project,
215                                                                @NotNull final Class<T> exceptionType,
216                                                                @NotNull final XBreakpointProperties properties) {
217     final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
218     XBreakpointType[] types = XBreakpointUtil.getBreakpointTypes();
219     final Ref<XBreakpoint> breakpoint = Ref.create(null);
220     for (XBreakpointType type : types) {
221       if (exceptionType.isInstance(type)) {
222         final T breakpointType = exceptionType.cast(type);
223         new WriteAction() {
224           @Override
225           protected void run(Result result) throws Throwable {
226             breakpoint.set(breakpointManager.addBreakpoint(breakpointType, properties));
227           }
228         }.execute();
229         break;
230       }
231     }
232     return breakpoint.get();
233   }
234
235   public static void setBreakpointCondition(Project project, int line, final String condition) {
236     XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
237     for (XBreakpoint breakpoint : breakpointManager.getAllBreakpoints()) {
238       if (breakpoint instanceof XLineBreakpoint) {
239         final XLineBreakpoint lineBreakpoint = (XLineBreakpoint)breakpoint;
240
241         if (lineBreakpoint.getLine() == line) {
242           new WriteAction() {
243             @Override
244             protected void run(Result result) throws Throwable {
245               lineBreakpoint.setCondition(condition);
246             }
247           }.execute();
248         }
249       }
250     }
251   }
252
253   public static class XTestStackFrameContainer extends XTestContainer<XStackFrame> implements XExecutionStack.XStackFrameContainer {
254     public void addStackFrames(@NotNull List<? extends XStackFrame> stackFrames, boolean last) {
255       addChildren(stackFrames, last);
256     }
257
258     public void errorOccured(String errorMessage) {
259       setErrorMessage(errorMessage);
260     }
261   }
262
263   public static class XTestNavigatable implements XNavigatable {
264     private XSourcePosition myPosition;
265
266     @Override
267     public void setSourcePosition(@Nullable XSourcePosition sourcePosition) {
268       myPosition = sourcePosition;
269     }
270
271     public XSourcePosition getPosition() {
272       return myPosition;
273     }
274   }
275 }