KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > errorstripe > AnnotationViewTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.errorstripe;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.JScrollPane JavaDoc;
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 /**
43  *
44  * @author Jan Lahoda
45  */

46 public class AnnotationViewTest extends NbTestCase {
47     
48     public AnnotationViewTest(String JavaDoc testName) {
49         super(testName);
50     }
51     
52     protected void setUp() throws Exception JavaDoc {
53         UnitUtilities.prepareTest(new String JavaDoc[] {"/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 JavaDoc[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 JavaDoc {
64         performTest(new Action() {
65             public void test(AnnotationView aView, BaseDocument document) throws Exception JavaDoc {
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 JavaDoc {
76         performTest(new Action() {
77             public void test(AnnotationView aView, BaseDocument document) throws Exception JavaDoc {
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 JavaDoc {
96         performTest(new Action() {
97             public void test(AnnotationView aView, BaseDocument document) throws Exception JavaDoc {
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 JavaDoc {
124         performTest(new Action() {
125             public void test(AnnotationView aView, BaseDocument document) throws Exception JavaDoc {
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 JavaDoc {
148         performTest(new Action() {
149             public void test(AnnotationView aView, BaseDocument document) throws Exception JavaDoc {
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 JavaDoc[] getContents() {
172         StringBuffer JavaDoc largeBuffer = new StringBuffer JavaDoc(16384);
173         
174         for (int cntr = 0; cntr < 16300; cntr++) {
175             largeBuffer.append('\n');
176         }
177         
178         List JavaDoc contents = new ArrayList JavaDoc();
179         String JavaDoc 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 JavaDoc[] ) contents.toArray(new String JavaDoc[0]);
188     }
189     
190     private static void performTest(final Action action) throws Exception JavaDoc {
191         JFrame JavaDoc f = new JFrame JavaDoc();
192         JEditorPane JavaDoc editor = new JEditorPane JavaDoc();
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 JavaDoc 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 JavaDoc());
209         f.getContentPane().add(new JScrollPane JavaDoc(editor), BorderLayout.CENTER);
210         f.getContentPane().add(aView, BorderLayout.EAST);
211         
212         f.setSize(500, 500);
213         
214         f.setVisible(true);
215
216         String JavaDoc[] 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 JavaDoc;
237     }
238
239     
240     public void testAnnotationViewFactory() {
241         JEditorPane JavaDoc editor = new JEditorPane JavaDoc();
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