2 * Copyright 2000-2011 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.xdebugger;
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;
38 import java.lang.reflect.InvocationTargetException;
40 import java.util.concurrent.Semaphore;
41 import java.util.concurrent.TimeUnit;
43 public class XDebuggerTestUtil {
44 private static final int TIMEOUT = 25000;
46 private XDebuggerTestUtil() {
49 public static <B extends XBreakpoint<?>> void assertBreakpointValidity(Project project,
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());
63 public static void toggleBreakpoint(Project project, VirtualFile file, int line) {
64 XDebuggerUtil.getInstance().toggleLineBreakpoint(project, file, line);
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());
73 public static void assertCurrentPosition(XDebugSession session, VirtualFile file, int line) {
74 assertPosition(session.getCurrentPosition(), file, line);
77 public static XExecutionStack getActiveThread(@NotNull XDebugSession session) {
78 return session.getSuspendContext().getActiveExecutionStack();
81 public static List<XStackFrame> collectStacks(@NotNull XDebugSession session) throws InterruptedException {
82 return collectStacks(null, session);
85 public static List<XStackFrame> collectStacks(@Nullable XExecutionStack thread, @NotNull XDebugSession session) throws InterruptedException {
86 return collectStacks(thread == null ? getActiveThread(session) : thread);
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;
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);
102 public static void waitForSwing() throws InterruptedException, InvocationTargetException {
103 final com.intellij.util.concurrency.Semaphore s = new com.intellij.util.concurrency.Semaphore();
105 ApplicationManager.getApplication().invokeLater(new Runnable() {
111 UIUtil.invokeAndWaitIfNeeded(new Runnable() {
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;
124 throw new AssertionError("var '" + name + "' not found");
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();
132 value.computePresentation(node, XValuePlace.TREE);
133 Assert.assertTrue(node.waitFor(TIMEOUT));
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);
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);
150 public static void assertVariableValue(XValue var, @Nullable String name, @Nullable String value) throws InterruptedException {
151 assertVariable(var, name, null, value, null);
154 public static void assertVariableValue(Collection<XValue> vars, @Nullable String name, @Nullable String value) throws InterruptedException {
155 assertVariableValue(findVar(vars, name), name, value);
158 public static void assertVariableValueMatches(Collection<XValue> vars, @Nullable String name, String valuePattern) throws InterruptedException {
159 assertVariableValueMatches(findVar(vars, name), name, valuePattern);
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));
168 public static void assertVariables(List<XValue> vars, String... names) throws InterruptedException {
169 List<String> expectedNames = new ArrayList<String>(Arrays.asList(names));
171 List<String> actualNames = new ArrayList<String>();
172 for (XValue each : vars) {
173 actualNames.add(computePresentation(each).myName);
176 Collections.sort(actualNames);
177 Collections.sort(expectedNames);
178 UsefulTestCase.assertOrderedEquals(actualNames, expectedNames);
181 public static void assertSourcePosition(final XValue value, VirtualFile file, int offset) {
182 final XTestNavigatable n = new XTestNavigatable();
183 ApplicationManager.getApplication().runReadAction(new Runnable() {
186 value.computeSourcePosition(n);
189 Assert.assertNotNull(n.myPosition);
190 Assert.assertEquals(file, n.myPosition.getFile());
191 Assert.assertEquals(offset, n.myPosition.getOffset());
194 public static boolean waitFor(Semaphore semaphore, long timeoutInMillis) throws InterruptedException {
195 return semaphore.tryAcquire(timeoutInMillis, TimeUnit.MILLISECONDS);
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);
204 public static String getConsoleText(final @NotNull ConsoleViewImpl consoleView) {
206 protected void run(Result result) throws Throwable {
207 consoleView.flushDeferredText();
211 return consoleView.getEditor().getDocument().getText();
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);
225 protected void run(Result result) throws Throwable {
226 breakpoint.set(breakpointManager.addBreakpoint(breakpointType, properties));
232 return breakpoint.get();
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;
241 if (lineBreakpoint.getLine() == line) {
244 protected void run(Result result) throws Throwable {
245 lineBreakpoint.setCondition(condition);
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);
258 public void errorOccured(String errorMessage) {
259 setErrorMessage(errorMessage);
263 public static class XTestNavigatable implements XNavigatable {
264 private XSourcePosition myPosition;
267 public void setSourcePosition(@Nullable XSourcePosition sourcePosition) {
268 myPosition = sourcePosition;
271 public XSourcePosition getPosition() {