1 19 20 package org.netbeans.modules.editor.fold; 21 22 import javax.swing.text.AbstractDocument ; 23 import javax.swing.text.BadLocationException ; 24 import junit.framework.TestCase; 25 import org.netbeans.api.editor.fold.Fold; 26 import org.netbeans.api.editor.fold.FoldType; 27 import org.netbeans.api.editor.fold.FoldHierarchy; 28 import org.netbeans.spi.editor.fold.FoldHierarchyTransaction; 29 import org.netbeans.spi.editor.fold.FoldManager; 30 import org.netbeans.spi.editor.fold.FoldManagerFactory; 31 32 36 public class NestedFoldManagerTest extends TestCase { 37 38 static final int FOLD_START_OFFSET_OUTER = 5; 39 static final int FOLD_END_OFFSET_OUTER = 10; 40 static final int FOLD_START_OFFSET_INNER = 6; 41 static final int FOLD_END_OFFSET_INNER = 8; 42 43 public NestedFoldManagerTest(String testName) { 44 super(testName); 45 } 46 47 public void test() { 48 test(true); 49 test(false); 50 } 51 52 55 public void test(boolean outerFirst) { 56 FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new NestedFoldManagerFactory(outerFirst)); 57 58 FoldHierarchy hierarchy = env.getHierarchy(); 59 AbstractDocument doc = env.getDocument(); 60 doc.readLock(); 61 try { 62 hierarchy.lock(); 63 try { 64 Fold rootFold = hierarchy.getRootFold(); 65 int foldCount = rootFold.getFoldCount(); 66 int expectedFoldCount = 1; 67 assertTrue("Incorrect fold count " + foldCount, (foldCount == expectedFoldCount) 69 ); 70 71 Fold fold = rootFold.getFold(0); 72 FoldType foldType = fold.getType(); 73 int foldStartOffset = fold.getStartOffset(); 74 int foldEndOffset = fold.getEndOffset(); 75 assertTrue("Incorrect fold type " + foldType, (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE)); 77 assertTrue("Incorrect fold start offset " + foldStartOffset, (foldStartOffset == FOLD_START_OFFSET_OUTER)); 79 assertTrue("Incorrect fold end offset " + foldEndOffset, (foldEndOffset == FOLD_END_OFFSET_OUTER)); 81 82 Fold outerFold = fold; 84 foldCount = outerFold.getFoldCount(); 85 expectedFoldCount = 1; 86 assertTrue("Incorrect fold count " + foldCount, (foldCount == expectedFoldCount) 88 ); 89 90 fold = outerFold.getFold(0); 91 assertTrue("Folds must differ", (fold != outerFold)); foldType = fold.getType(); 93 foldStartOffset = fold.getStartOffset(); 94 foldEndOffset = fold.getEndOffset(); 95 assertTrue("Incorrect fold type " + foldType, (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE)); 97 assertTrue("Incorrect fold start offset " + foldStartOffset, (foldStartOffset == FOLD_START_OFFSET_INNER)); 99 assertTrue("Incorrect fold end offset " + foldEndOffset, (foldEndOffset == FOLD_END_OFFSET_INNER)); 101 102 } finally { 103 hierarchy.unlock(); 104 } 105 } finally { 106 doc.readUnlock(); 107 } 108 } 109 110 111 final class NestedFoldManager extends AbstractFoldManager { 112 113 private boolean outerFirst; 114 115 NestedFoldManager(boolean outerFirst) { 116 this.outerFirst = outerFirst; 117 } 118 119 private void addOuter(FoldHierarchyTransaction transaction) throws BadLocationException { 120 getOperation().addToHierarchy( 121 REGULAR_FOLD_TYPE, 122 null, 123 false, 124 FOLD_START_OFFSET_OUTER, FOLD_END_OFFSET_OUTER, 1, 1, 125 null, 126 transaction 127 ); 128 } 129 130 private void addInner(FoldHierarchyTransaction transaction) throws BadLocationException { 131 getOperation().addToHierarchy( 132 REGULAR_FOLD_TYPE, 133 null, 134 false, 135 FOLD_START_OFFSET_INNER, FOLD_END_OFFSET_INNER, 1, 1, 136 null, 137 transaction 138 ); 139 } 140 141 public void initFolds(FoldHierarchyTransaction transaction) { 142 try { 143 if (outerFirst) { 144 addOuter(transaction); 145 } 146 addInner(transaction); 147 if (!outerFirst) { 148 addOuter(transaction); 149 } 150 } catch (BadLocationException e) { 151 e.printStackTrace(); 152 fail(); 153 } 154 } 155 156 } 157 158 public final class NestedFoldManagerFactory implements FoldManagerFactory { 159 160 private boolean outerFirst; 161 162 NestedFoldManagerFactory(boolean outerFirst) { 163 this.outerFirst = outerFirst; 164 } 165 166 public FoldManager createFoldManager() { 167 return new NestedFoldManager(outerFirst); 168 } 169 170 } 171 172 } 173 | Popular Tags |