1 19 20 package org.netbeans.modules.editor.fold; 21 22 import java.util.Collections ; 23 import javax.swing.text.AbstractDocument ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Position ; 26 import org.netbeans.api.editor.fold.Fold; 27 import org.netbeans.api.editor.fold.FoldType; 28 import org.netbeans.api.editor.fold.FoldHierarchy; 29 import org.netbeans.junit.MemoryFilter; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.spi.editor.fold.FoldHierarchyTransaction; 32 import org.netbeans.spi.editor.fold.FoldManager; 33 import org.netbeans.spi.editor.fold.FoldManagerFactory; 34 35 39 public class SimpleFoldManagerTest extends NbTestCase { 40 41 private static final int MAX_FOLD_MEMORY_SIZE = 64; 42 43 static final int FOLD_START_OFFSET_1 = 5; 44 static final int FOLD_END_OFFSET_1 = 10; 45 46 public SimpleFoldManagerTest(String testName) { 47 super(testName); 48 } 49 50 53 public void test() { 54 FoldHierarchyTestEnv env = new FoldHierarchyTestEnv(new SimpleFoldManagerFactory()); 55 56 FoldHierarchy hierarchy = env.getHierarchy(); 57 AbstractDocument doc = env.getDocument(); 58 doc.readLock(); 59 try { 60 hierarchy.lock(); 61 try { 62 63 Fold rootFold = hierarchy.getRootFold(); 64 int foldCount = rootFold.getFoldCount(); 65 int expectedFoldCount = 1; 66 assertTrue("Incorrect fold count " + foldCount, (foldCount == expectedFoldCount) 68 ); 69 70 Fold fold = rootFold.getFold(0); 71 FoldType foldType = fold.getType(); 72 int foldStartOffset = fold.getStartOffset(); 73 int foldEndOffset = fold.getEndOffset(); 74 assertTrue("Incorrect fold type " + foldType, (foldType == AbstractFoldManager.REGULAR_FOLD_TYPE)); 76 assertTrue("Incorrect fold start offset " + foldStartOffset, (foldStartOffset == FOLD_START_OFFSET_1)); 78 assertTrue("Incorrect fold end offset " + foldEndOffset, (foldEndOffset == FOLD_END_OFFSET_1)); 80 81 assertSize("Size of the fold " , Collections.singleton(fold), MAX_FOLD_MEMORY_SIZE, new FoldMemoryFilter(fold)); 84 85 } finally { 86 hierarchy.unlock(); 87 } 88 } finally { 89 doc.readUnlock(); 90 } 91 } 92 93 94 final class SimpleFoldManager extends AbstractFoldManager { 95 96 public void initFolds(FoldHierarchyTransaction transaction) { 97 try { 98 getOperation().addToHierarchy( 99 REGULAR_FOLD_TYPE, 100 "...", false, 102 FOLD_START_OFFSET_1, FOLD_END_OFFSET_1, 1, 1, 103 null, 104 transaction 105 ); 106 } catch (BadLocationException e) { 107 e.printStackTrace(); 108 fail(); 109 } 110 } 111 112 } 113 114 public final class SimpleFoldManagerFactory implements FoldManagerFactory { 115 116 public FoldManager createFoldManager() { 117 return new SimpleFoldManager(); 118 } 119 120 } 121 122 private final class FoldMemoryFilter implements MemoryFilter { 123 124 private Fold fold; 125 126 FoldMemoryFilter(Fold fold) { 127 this.fold = fold; 128 } 129 130 public boolean reject(Object o) { 131 return (o == fold.getType()) 132 || (o == fold.getDescription()) || (o == fold.getParent()) 134 || (o instanceof FoldOperationImpl) 135 || (o instanceof Position ); 136 137 } 139 140 } 141 142 } 143 | Popular Tags |