diff --git a/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java b/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java index 5846f97dad70..2ae92ce833ac 100644 --- a/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java +++ b/java/testFramework/src/com/intellij/testFramework/IdeaTestUtil.java @@ -15,40 +15,12 @@ */ package com.intellij.testFramework; -import com.intellij.ide.util.treeView.AbstractTreeNode; import com.intellij.openapi.module.Module; import com.intellij.openapi.roots.LanguageLevelModuleExtension; import com.intellij.openapi.roots.LanguageLevelProjectExtension; -import com.intellij.openapi.ui.Queryable; import com.intellij.pom.java.LanguageLevel; -import java.util.Comparator; - public class IdeaTestUtil extends PlatformTestUtil { - public static Comparator createComparator(final Queryable.PrintInfo printInfo) { - return new Comparator() { - @Override - public int compare(final AbstractTreeNode o1, final AbstractTreeNode o2) { - String displayText1 = o1.toTestString(printInfo); - String displayText2 = o2.toTestString(printInfo); - return displayText1.compareTo(displayText2); - } - }; - } - - /** - * Use {@link #createComparator(com.intellij.openapi.ui.Queryable.PrintInfo)} instead. - */ - @Deprecated - public static final Comparator DEFAULT_COMPARATOR = new Comparator() { - @Override - public int compare(AbstractTreeNode o1, AbstractTreeNode o2) { - String displayText1 = o1.getTestPresentation(); - String displayText2 = o2.getTestPresentation(); - return displayText1.compareTo(displayText2); - } - }; - public static void main(String[] args) { printDetectedPerformanceTimings(); } diff --git a/platform/platform-tests/platform-tests.iml b/platform/platform-tests/platform-tests.iml index 689106134d58..f739c73cdda3 100644 --- a/platform/platform-tests/platform-tests.iml +++ b/platform/platform-tests/platform-tests.iml @@ -14,6 +14,11 @@ + + + + + diff --git a/platform/platform-tests/testSrc/com/intellij/structureView/SmartTreeStructureTest.java b/platform/platform-tests/testSrc/com/intellij/structureView/SmartTreeStructureTest.java new file mode 100644 index 000000000000..6275b5fdd8d5 --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/structureView/SmartTreeStructureTest.java @@ -0,0 +1,238 @@ +package com.intellij.structureView; + +import com.intellij.ide.util.treeView.smartTree.*; +import com.intellij.testFramework.PlatformTestCase; +import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +import java.util.Comparator; + +public class SmartTreeStructureTest extends LightPlatformCodeInsightFixtureTestCase { + private TestTreeModel myModel; + + public SmartTreeStructureTest() { + PlatformTestCase.initPlatformLangPrefix(); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + TestTreeModel.StringTreeElement root = new TestTreeModel.StringTreeElement("root"); + myModel = new TestTreeModel(root); + root.addChild("abc").addChild("abcde"); + root.addChild("bcd").addChild("bhgyt"); + root.addChild("ade").addChild("aed"); + root.addChild("bft").addChild("ttt"); + root.addChild("xxx"); + root.addChild("eed").addChild("zzz"); + root.addChild("xxx").addChild("aaa").addChild("bbb"); + + } + + public void testGrouping() throws Exception { + assertStructureEqual("root\n" + + ".Group:a\n" + + "..Group:d\n" + + "...ade\n" + + "....Group:a\n" + + ".....Group:d\n" + + "......aed\n" + + "..abc\n" + + "...Group:a\n" + + "....Group:d\n" + + ".....abcde\n" + + ".Group:b\n" + + "..Group:d\n" + + "...bcd\n" + + "....Group:b\n" + + ".....bhgyt\n" + + "..Group:f\n" + + "...bft\n" + + "....ttt\n" + + ".Group:d\n" + + "..eed\n" + + "...zzz\n" + + ".xxx\n" + + ".xxx\n" + + "..Group:a\n" + + "...aaa\n" + + "....Group:b\n" + + ".....bbb\n", PlatformTestUtil.DEFAULT_COMPARATOR); + + + } + + public void testFiltering() throws Exception { + myModel.addFlter(new Filter(){ + @Override + @NotNull + public ActionPresentation getPresentation() { + throw new RuntimeException(); + } + + @Override + @NotNull + public String getName() { + throw new RuntimeException(); + } + + @Override + public boolean isVisible(TreeElement treeNode) { + return !treeNode.toString().contains("a"); + } + + @Override + public boolean isReverted() { + return false; + } + }); + + assertStructureEqual("root\n" + + ".Group:b\n" + + "..Group:d\n" + + "...bcd\n" + + "....Group:b\n" + + ".....bhgyt\n" + + "..Group:f\n" + + "...bft\n" + + "....ttt\n" + + ".Group:d\n" + + "..eed\n" + + "...zzz\n" + + ".xxx\n" + + ".xxx\n", PlatformTestUtil.DEFAULT_COMPARATOR); + + } + + public void testSorting() throws Exception { + + + myModel.addSorter(new Sorter() { + @Override + public Comparator getComparator() { + return new Comparator() { + @Override + public int compare(Object o1, Object o2) { + if (o1 instanceof Group && !(o2 instanceof Group)) return -1; + if (!(o1 instanceof Group) && o2 instanceof Group) return 1; + return 0; + } + }; + } + + @Override + public boolean isVisible() { + return true; + } + + @Override + @NotNull + public ActionPresentation getPresentation() { + throw new RuntimeException(); + } + + @Override + @NotNull + public String getName() { + throw new RuntimeException(); + } + }); + + myModel.addSorter(new Sorter() { + @Override + public Comparator getComparator() { + return new Comparator() { + @Override + public int compare(Object o1, Object o2) { + return o1.toString().compareTo(o2.toString()); + } + }; + } + + @Override + public boolean isVisible() { + return true; + } + + @Override + @NotNull + public ActionPresentation getPresentation() { + throw new RuntimeException(); + } + + @Override + @NotNull + public String getName() { + throw new RuntimeException(); + } + }); + + assertStructureEqual("root\n" + + ".Group:a\n" + + "..Group:d\n" + + "...ade\n" + + "....Group:a\n" + + ".....Group:d\n" + + "......aed\n" + + "..abc\n" + + "...Group:a\n" + + "....Group:d\n" + + ".....abcde\n" + + ".Group:b\n" + + "..Group:d\n" + + "...bcd\n" + + "....Group:b\n" + + ".....bhgyt\n" + + "..Group:f\n" + + "...bft\n" + + "....ttt\n" + + ".Group:d\n" + + "..eed\n" + + "...zzz\n" + + ".xxx\n" + + ".xxx\n" + + "..Group:a\n" + + "...aaa\n" + + "....Group:b\n" + + ".....bbb\n" + , null); + + } + + public void testUnsorted(){ + TestTreeModel.StringTreeElement root = new TestTreeModel.StringTreeElement("root"); + myModel = new TestTreeModel(root); + root.addChild("xxx"); + root.addChild("bbb"); + root.addChild("bbb"); + root.addChild("zzz").addChild("ttt"); + root.addChild("aaa"); + root.addChild("aaa").addChild("zzz"); + root.addChild("xxx"); + assertStructureEqual("root\n" + + ".xxx\n" + + ".Group:b\n" + + "..bbb\n" + + "..bbb\n" + + ".zzz\n" + + "..ttt\n" + + ".Group:a\n" + + "..aaa\n" + + "..aaa\n" + + "...zzz\n" + + ".xxx\n", null); + + } + + private void assertStructureEqual(@NonNls String expected, Comparator comparator) { + SmartTreeStructure structure = new SmartTreeStructure(myFixture.getProject(), myModel); + + String actual = PlatformTestUtil.print(structure, structure.getRootElement(), 0, comparator, -1, '.', null).toString(); + + + assertEquals(expected, actual); + } + +} diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java index 2604da35f53f..c155d4ebe703 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java @@ -323,17 +323,6 @@ public class PlatformTestUtil { return buffer; } - private static int doPrint(StringBuffer buffer, - int currentLevel, - Object node, - AbstractTreeStructure structure, - Comparator comparator, - int maxRowCount, - int currentLine, - char paddingChar) { - return doPrint(buffer, currentLevel, node, structure, comparator, maxRowCount, currentLine, paddingChar, null); - } - private static int doPrint(StringBuffer buffer, int currentLevel, Object node, @@ -740,4 +729,29 @@ public class PlatformTestUtil { } return homePath; } + + public static Comparator createComparator(final Queryable.PrintInfo printInfo) { + return new Comparator() { + @Override + public int compare(final AbstractTreeNode o1, final AbstractTreeNode o2) { + String displayText1 = o1.toTestString(printInfo); + String displayText2 = o2.toTestString(printInfo); + return displayText1.compareTo(displayText2); + } + }; + } + + /** + * Use {@link #createComparator(com.intellij.openapi.ui.Queryable.PrintInfo)} instead. + */ + @Deprecated + public static final Comparator DEFAULT_COMPARATOR = new Comparator() { + @Override + public int compare(AbstractTreeNode o1, AbstractTreeNode o2) { + String displayText1 = o1.getTestPresentation(); + String displayText2 = o2.getTestPresentation(); + return displayText1.compareTo(displayText2); + } + }; + }