1 19 20 package org.netbeans.modules.editor.errorstripe; 21 22 import java.awt.BorderLayout ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.JFrame ; 29 import javax.swing.JScrollPane ; 30 import junit.framework.*; 31 import org.netbeans.editor.BaseDocument; 32 import org.netbeans.editor.BaseKit; 33 import org.netbeans.editor.Utilities; 34 import org.netbeans.junit.NbTestCase; 35 import org.netbeans.modules.editor.errorstripe.caret.CaretMarkProviderCreator; 36 import org.netbeans.modules.editor.errorstripe.privatespi.Mark; 37 import org.netbeans.spi.editor.errorstripe.UpToDateStatus; 38 import org.netbeans.modules.editor.options.BaseOptions; 39 import org.netbeans.modules.editor.plain.PlainKit; 40 import org.netbeans.modules.editor.errorstripe.privatespi.Status; 41 42 46 public class AnnotationViewTest extends NbTestCase { 47 48 public AnnotationViewTest(String testName) { 49 super(testName); 50 } 51 52 protected void setUp() throws Exception { 53 UnitUtilities.prepareTest(new String [] {"/org/netbeans/modules/editor/resources/annotations-test-layer.xml", 54 "/org/netbeans/modules/editor/plain/resources/layer.xml", 55 "/org/netbeans/modules/editor/errorstripe/test-layer.xml"}, 56 new Object [0]); 57 BaseKit.getKit(PlainKit.class); 58 BaseOptions.findObject(BaseOptions.class, true); 59 60 CaretMarkProviderCreator.switchOff = true; 61 } 62 63 public void testModelToView() throws Exception { 64 performTest(new Action() { 65 public void test(AnnotationView aView, BaseDocument document) throws Exception { 66 double pos = aView.modelToView(2); 67 68 assertEquals(aView.viewToModel(pos)[0], aView.viewToModel(aView.modelToView(aView.viewToModel(pos)[0]))[0]); 69 70 assertEquals((-1.0), aView.modelToView(Utilities.getLineOffset(document, document.getLength()) + 1), 0.0001d); 71 } 72 }); 73 } 74 75 public void testViewToModelIsContinuous() throws Exception { 76 performTest(new Action() { 77 public void test(AnnotationView aView, BaseDocument document) throws Exception { 78 int[] last = new int[] {-1, -1}; 79 int topOffset = aView.topOffset(); 80 81 for (double pos = 0; pos < aView.getUsableHeight(); pos = pos + 1) { 82 int[] current = aView.viewToModel(pos + topOffset); 83 84 if (current == null) 85 continue; 86 assertTrue(last[0] <= current[0]); 87 assertTrue(last[1] <= current[1]); 88 89 last = current; 90 } 91 } 92 }); 93 } 94 95 public void testGetAnnotationIsContinuous() throws Exception { 96 performTest(new Action() { 97 public void test(AnnotationView aView, BaseDocument document) throws Exception { 98 Mark mark = null; 99 boolean wasMark = false; 100 int topOffset = aView.topOffset(); 101 102 for (double pos = 0; pos < aView.getUsableHeight(); pos = pos + 1) { 103 Mark newMark = aView.getMarkForPoint(pos + topOffset); 104 105 if (newMark != null && mark!= null) { 106 assertTrue(newMark == mark); 107 } 108 109 if (wasMark) { 110 assertNull("pos=" + pos + ", mark=" + mark + ", newMark=" + newMark, newMark); 111 } 112 113 if (mark != null && newMark == null) { 114 wasMark = true; 115 } 116 117 mark = newMark; 118 } 119 } 120 }); 121 } 122 123 public void testGetLinesSpanIsContinuous() throws Exception { 124 performTest(new Action() { 125 public void test(AnnotationView aView, BaseDocument document) throws Exception { 126 int startLine = 1; 127 int linesCount = Utilities.getRowCount(document); 128 129 while (startLine < linesCount) { 130 int[] span = aView.getLinesSpan(startLine); 131 132 assertTrue(startLine >= span[0]); 133 assertTrue(startLine <= span[1]); 134 135 if (span[1] < linesCount) { 136 int[] newSpan = aView.getLinesSpan(span[1] + 1); 137 138 assertEquals(newSpan[0], span[1] + 1); 139 } 140 141 startLine = span[1] + 1; 142 } 143 } 144 }); 145 } 146 147 public void testMarkSensitiveStripe1() throws Exception { 148 performTest(new Action() { 149 public void test(AnnotationView aView, BaseDocument document) throws Exception { 150 double position = aView.modelToView(6); 151 double start = position - AnnotationView.UPPER_HANDLE; 152 double end = position + AnnotationView.PIXELS_FOR_LINE + AnnotationView.LOWER_HANDLE - 1; 153 154 for (double pos = start; pos <= end; pos++) { 155 Mark m = aView.getMarkForPoint(pos); 156 157 assertNotNull("pos=" + pos + ", start=" + start + ", end=" + end + ", position=" + position, m); 158 } 159 160 Mark m1 = aView.getMarkForPoint(start - 1); 161 162 assertNull("There is a mark at position: " + (start - 1), m1); 163 164 Mark m2 = aView.getMarkForPoint(end + 1); 165 166 assertNull("There is a mark at position: " + (end + 1), m2); 167 } 168 }); 169 } 170 171 private static String [] getContents() { 172 StringBuffer largeBuffer = new StringBuffer (16384); 173 174 for (int cntr = 0; cntr < 16300; cntr++) { 175 largeBuffer.append('\n'); 176 } 177 178 List contents = new ArrayList (); 179 String large = largeBuffer.toString(); 180 181 for (int lines = 7; lines < 300; lines++) { 182 contents.add(large.substring(0, lines + 1)); 183 } 184 185 contents.add(large); 186 187 return (String [] ) contents.toArray(new String [0]); 188 } 189 190 private static void performTest(final Action action) throws Exception { 191 JFrame f = new JFrame (); 192 JEditorPane editor = new JEditorPane (); 193 194 editor.setEditorKit(BaseKit.getKit(PlainKit.class)); 195 196 TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {6, 6}); 197 TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {6, 6}); 198 199 List marks = Arrays.asList(new Mark[]{mark1, mark2}); 200 201 TestMarkProvider provider = new TestMarkProvider(Collections.EMPTY_LIST, UpToDateStatus.UP_TO_DATE_OK); 202 TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault(); 203 204 creator.setProvider(provider); 205 206 AnnotationView aView = new AnnotationView(editor); 207 208 f.getContentPane().setLayout(new BorderLayout ()); 209 f.getContentPane().add(new JScrollPane (editor), BorderLayout.CENTER); 210 f.getContentPane().add(aView, BorderLayout.EAST); 211 212 f.setSize(500, 500); 213 214 f.setVisible(true); 215 216 String [] contents = getContents(); 217 218 for (int index = 0; index < contents.length; index++) { 219 BaseDocument bd = (BaseDocument) editor.getDocument(); 220 221 bd.insertString(0, contents[index], null); 222 223 provider.setMarks(marks); 224 225 action.test(aView, bd); 226 227 provider.setMarks(Collections.EMPTY_LIST); 228 229 bd.remove(0, bd.getLength()); 230 } 231 232 f.setVisible(false); 233 } 234 235 private static abstract class Action { 236 public abstract void test(AnnotationView aView, BaseDocument document) throws Exception ; 237 } 238 239 240 public void testAnnotationViewFactory() { 241 JEditorPane editor = new JEditorPane (); 242 243 editor.setEditorKit(BaseKit.getKit(PlainKit.class)); 244 245 assertNotNull(new AnnotationViewFactory().createSideBar(editor)); 246 } 247 248 protected boolean runInEQ() { 249 return true; 250 } 251 252 } 253 | Popular Tags |