tree tests moved to community
[fedora-idea.git] / platform / testFramework / src / com / intellij / testFramework / PlatformTestUtil.java
blobe0c0b24009cfacdc577a2a59f0f773135df75214
1 /*
2 * Copyright 2000-2009 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.testFramework;
18 import com.intellij.ide.util.treeView.AbstractTreeNode;
19 import com.intellij.ide.util.treeView.AbstractTreeStructure;
20 import com.intellij.idea.Bombed;
21 import com.intellij.openapi.Disposable;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.extensions.ExtensionPoint;
24 import com.intellij.openapi.extensions.ExtensionPointName;
25 import com.intellij.openapi.extensions.Extensions;
26 import com.intellij.openapi.extensions.ExtensionsArea;
27 import com.intellij.openapi.util.Comparing;
28 import com.intellij.openapi.util.Condition;
29 import com.intellij.openapi.util.Disposer;
30 import com.intellij.openapi.util.text.StringUtil;
31 import com.intellij.util.Alarm;
32 import com.intellij.util.SystemProperties;
33 import com.intellij.util.ui.UIUtil;
34 import junit.framework.Assert;
35 import org.jetbrains.annotations.NonNls;
36 import org.jetbrains.annotations.Nullable;
38 import javax.swing.*;
39 import javax.swing.tree.DefaultMutableTreeNode;
40 import javax.swing.tree.TreePath;
41 import java.util.*;
43 /**
44 * @author yole
46 public class PlatformTestUtil {
47 public static <T> void registerExtension(final ExtensionPointName<T> name, final T t, final Disposable parentDisposable) {
48 registerExtension(Extensions.getRootArea(), name, t, parentDisposable);
51 public static <T> void registerExtension(final ExtensionsArea area, final ExtensionPointName<T> name, final T t, final Disposable parentDisposable) {
52 final ExtensionPoint<T> extensionPoint = area.getExtensionPoint(name.getName());
53 extensionPoint.registerExtension(t);
54 Disposer.register(parentDisposable, new Disposable() {
55 public void dispose() {
56 extensionPoint.unregisterExtension(t);
58 });
61 protected static String toString(Object node) {
62 if (node instanceof AbstractTreeNode) {
63 return ((AbstractTreeNode)node).getTestPresentation();
65 else if (node == null) {
66 return "NULL";
68 else {
69 return node.toString();
73 public static String print(JTree tree, boolean withSelection) {
74 return print(tree, withSelection, null);
77 public static String print(JTree tree, boolean withSelection, Condition<String> nodePrintCondition) {
78 StringBuffer buffer = new StringBuffer();
79 Object root = tree.getModel().getRoot();
80 printImpl(tree, root, buffer, 0, withSelection, nodePrintCondition);
81 return buffer.toString();
85 private static void printImpl(JTree tree, Object root, StringBuffer buffer, int level, boolean withSelection, @Nullable Condition<String> nodePrintCondition) {
86 DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode)root;
89 final Object userObject = defaultMutableTreeNode.getUserObject();
90 String nodeText;
91 if (userObject != null) {
92 nodeText = toString(userObject);
94 else {
95 nodeText = defaultMutableTreeNode + "";
99 if (nodePrintCondition != null && !nodePrintCondition.value(nodeText)) return;
101 boolean expanded = tree.isExpanded(new TreePath(defaultMutableTreeNode.getPath()));
102 StringUtil.repeatSymbol(buffer, ' ', level);
103 if (expanded && !defaultMutableTreeNode.isLeaf()) {
104 buffer.append("-");
107 if (!expanded && !defaultMutableTreeNode.isLeaf()) {
108 buffer.append("+");
111 final boolean selected = tree.getSelectionModel().isPathSelected(new TreePath(defaultMutableTreeNode.getPath()));
113 if (withSelection && selected) {
114 buffer.append("[");
118 buffer.append(nodeText);
120 if (withSelection && selected) {
121 buffer.append("]");
124 buffer.append("\n");
125 int childCount = tree.getModel().getChildCount(root);
126 if (expanded) {
127 for (int i = 0; i < childCount; i++) {
128 printImpl(tree, tree.getModel().getChild(root, i), buffer, level + 1, withSelection, nodePrintCondition);
133 public static void assertTreeEqual(JTree tree, @NonNls String expected) {
134 assertTreeEqual(tree, expected, false);
137 public static void assertTreeEqual(JTree tree, String expected, boolean checkSelected) {
138 String treeStringPresentation = print(tree, checkSelected);
139 Assert.assertEquals(expected, treeStringPresentation);
142 public static void waitForAlarm(final int delay) throws InterruptedException {
143 final boolean[] invoked = new boolean[]{false};
144 final Alarm alarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
145 alarm.addRequest(new Runnable() {
146 public void run() {
147 ApplicationManager.getApplication().invokeLater(new Runnable() {
148 public void run() {
149 alarm.addRequest(new Runnable() {
150 public void run() {
151 invoked[0] = true;
153 }, delay);
157 }, delay);
159 UIUtil.dispatchAllInvocationEvents();
161 while (!invoked[0]) {
162 UIUtil.dispatchAllInvocationEvents();
163 Thread.sleep(delay);
167 private static Date raidDate(Bombed bombed) {
168 final Calendar instance = Calendar.getInstance();
169 instance.set(Calendar.YEAR, bombed.year());
170 instance.set(Calendar.MONTH, bombed.month());
171 instance.set(Calendar.DAY_OF_MONTH, bombed.day());
172 instance.set(Calendar.HOUR_OF_DAY, bombed.time());
173 instance.set(Calendar.MINUTE, 0);
174 Date time = instance.getTime();
176 return time;
179 public static boolean bombExplodes(Bombed bombedAnnotation) {
180 Date now = new Date();
181 return now.after(raidDate(bombedAnnotation));
184 public static boolean bombExplodes(int year, int month, int day, int h, int m, String who, String message) {
185 final Calendar instance = Calendar.getInstance();
186 instance.set(Calendar.YEAR, year);
187 instance.set(Calendar.MONTH, month);
188 instance.set(Calendar.DAY_OF_MONTH, day);
189 instance.set(Calendar.HOUR_OF_DAY, h);
190 instance.set(Calendar.MINUTE, m);
191 Date time = instance.getTime();
192 return isItMe(who) || new Date().after(time);
195 public static boolean isRotten(Bombed bomb) {
196 long bombRotPeriod = 30L * 24 * 60 * 60 * 1000; // month
197 return new Date().after(new Date(raidDate(bomb).getTime() + bombRotPeriod));
200 private static boolean isItMe(final String who) {
201 return Comparing.equal(who, SystemProperties.getUserName(), false);
204 public static StringBuffer print(AbstractTreeStructure structure,
205 Object node,
206 int currentLevel,
207 Comparator comparator,
208 int maxRowCount,
209 char paddingChar) {
210 StringBuffer buffer = new StringBuffer();
211 doPrint(buffer, currentLevel, node, structure, comparator, maxRowCount, 0, paddingChar);
212 return buffer;
215 private static int doPrint(StringBuffer buffer,
216 int currentLevel,
217 Object node,
218 AbstractTreeStructure structure,
219 Comparator comparator,
220 int maxRowCount,
221 int currentLine,
222 char paddingChar) {
223 if (currentLine >= maxRowCount && maxRowCount != -1) return currentLine;
225 StringUtil.repeatSymbol(buffer, paddingChar, currentLevel);
226 buffer.append(toString(node)).append("\n");
227 currentLine++;
228 Object[] children = structure.getChildElements(node);
230 if (comparator != null) {
231 ArrayList<?> list = new ArrayList<Object>(Arrays.asList(children));
232 Collections.sort(list, comparator);
233 children = list.toArray(new Object[list.size()]);
235 for (Object child : children) {
236 currentLine = doPrint(buffer, currentLevel + 1, child, structure, comparator, maxRowCount, currentLine, paddingChar);
239 return currentLine;
242 public static String print(Object[] objects) {
243 return print(Arrays.asList(objects));
246 public static String print(Collection c) {
247 StringBuilder result = new StringBuilder();
248 for (Iterator iterator = c.iterator(); iterator.hasNext();) {
249 Object each = iterator.next();
250 result.append(toString(each));
251 if (iterator.hasNext()) {
252 result.append("\n");
256 return result.toString();
259 public static String print(ListModel model) {
260 StringBuilder result = new StringBuilder();
261 for (int i = 0; i < model.getSize(); i++) {
262 result.append(toString(model.getElementAt(i)));
263 result.append("\n");
265 return result.toString();
268 public static String print(JTree tree) {
269 return print(tree, false);
272 public static void assertTreeStructureEquals(final AbstractTreeStructure treeStructure, final String expected) {
273 Assert.assertEquals(expected, print(treeStructure, treeStructure.getRootElement(), 0, null, -1, ' ').toString());