001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import javax.swing.JFrame;
023: import javax.swing.JTextArea;
024: import javax.swing.SwingTestCase;
025:
026: public class DefaultHighlighterTest extends SwingTestCase {
027: JTextArea jta;
028:
029: JFrame jf;
030:
031: DefaultHighlighter dh;
032:
033: Highlighter.HighlightPainter lpnt;
034:
035: boolean bWasException;
036:
037: String s;
038:
039: Object obj1;
040:
041: Object obj2;
042:
043: Object obj3;
044:
045: Object obj4;
046:
047: Object obj5;
048:
049: @Override
050: protected void setUp() throws Exception {
051: super .setUp();
052: jta = new JTextArea("JTextArea for DefaultHighlight Tesing");
053: jf = new JFrame();
054: dh = new DefaultHighlighter();
055: lpnt = DefaultHighlighter.DefaultPainter;
056: bWasException = false;
057: s = null;
058: obj1 = null;
059: obj2 = null;
060: obj3 = null;
061: obj4 = null;
062: obj5 = null;
063: jta.setHighlighter(dh);
064: jf.getContentPane().add(jta);
065: jf.setLocation(200, 300);
066: jf.setSize(200, 300);
067: jf.pack();
068: }
069:
070: @Override
071: protected void tearDown() throws Exception {
072: jf.dispose();
073: super .tearDown();
074: }
075:
076: public void testDefaultHighlighter() {
077: DefaultHighlighter dh1 = new DefaultHighlighter();
078: assertNotNull(dh1);
079: }
080:
081: void assertEqualsHighlight(final Highlighter.Highlight h,
082: final int start, final int end,
083: final Highlighter.HighlightPainter pnt) {
084: assertEquals(start, h.getStartOffset());
085: assertEquals(end, h.getEndOffset());
086: assertEquals(pnt, h.getPainter());
087: }
088:
089: public void testAddHighlightBadLocationException() {
090: bWasException = false;
091: try {
092: dh.addHighlight(-8, -1, lpnt);
093: } catch (BadLocationException e) {
094: bWasException = true;
095: }
096: if (isHarmony()) {
097: assertTrue(bWasException);
098: }
099: bWasException = false;
100: try {
101: dh.addHighlight(8, 1, lpnt);
102: } catch (BadLocationException e) {
103: bWasException = true;
104: }
105: if (isHarmony()) {
106: assertTrue(bWasException);
107: }
108: }
109:
110: public void testAddHighlight() throws Exception {
111: try {
112: obj1 = dh.addHighlight(1, 5, lpnt);
113: } catch (BadLocationException e) {
114: bWasException = true;
115: s = e.getMessage();
116: }
117: assertNotNull(obj1);
118: dh.getHighlights();
119: assertEquals(1, dh.getHighlights().length);
120: assertEqualsHighlight(dh.getHighlights()[0], 1, 5, lpnt);
121: try {
122: obj2 = dh.addHighlight(3, 7, lpnt);
123: } catch (BadLocationException e) {
124: bWasException = true;
125: s = e.getMessage();
126: }
127: assertFalse("Unexpected exception: " + s, bWasException);
128: assertNotNull(obj1);
129: assertEquals(2, dh.getHighlights().length);
130: assertEqualsHighlight(dh.getHighlights()[0], 1, 5, lpnt);
131: assertEqualsHighlight(dh.getHighlights()[1], 3, 7, lpnt);
132: }
133:
134: public void testInstall() throws Exception {
135: try {
136: obj1 = dh.addHighlight(1, 5, lpnt);
137: } catch (BadLocationException e) {
138: bWasException = true;
139: s = e.getMessage();
140: }
141: assertFalse("Unexpected exception: " + s, bWasException);
142: try {
143: obj2 = dh.addHighlight(3, 7, lpnt);
144: } catch (BadLocationException e) {
145: bWasException = true;
146: s = e.getMessage();
147: }
148: assertFalse("Unexpected exception: " + s, bWasException);
149: dh.setDrawsLayeredHighlights(false);
150: dh.deinstall(jta);
151: dh.install(jta);
152: assertEquals(0, dh.getHighlights().length);
153: assertFalse(dh.getDrawsLayeredHighlights());
154: }
155:
156: public void testDeinstall() throws Exception {
157: try {
158: obj1 = dh.addHighlight(1, 5, lpnt);
159: } catch (BadLocationException e) {
160: bWasException = true;
161: s = e.getMessage();
162: }
163: assertFalse("Unexpected exception: " + s, bWasException);
164: try {
165: obj2 = dh.addHighlight(3, 7, lpnt);
166: } catch (BadLocationException e) {
167: bWasException = true;
168: s = e.getMessage();
169: }
170: assertFalse("Unexpected exception: " + s, bWasException);
171: dh.setDrawsLayeredHighlights(false);
172: dh.deinstall(new JTextArea());
173: assertEquals(2, dh.getHighlights().length);
174: assertFalse(dh.getDrawsLayeredHighlights());
175: }
176:
177: public void testChangeHighlight() throws Exception {
178: try {
179: obj1 = dh.addHighlight(1, 5, lpnt);
180: } catch (BadLocationException e) {
181: bWasException = true;
182: s = e.getMessage();
183: }
184: assertFalse("Unexpected exception: " + s, bWasException);
185: try {
186: obj2 = dh.addHighlight(3, 7, lpnt);
187: } catch (BadLocationException e) {
188: bWasException = true;
189: s = e.getMessage();
190: }
191: assertFalse("Unexpected exception: " + s, bWasException);
192: try {
193: dh.changeHighlight(obj2, 2, 6);
194: } catch (BadLocationException e) {
195: bWasException = true;
196: s = e.getMessage();
197: }
198: assertFalse("Unexpected exception: " + s, bWasException);
199: try {
200: dh.changeHighlight(obj1, 1, 8);
201: } catch (BadLocationException e) {
202: bWasException = true;
203: s = e.getMessage();
204: }
205: assertFalse("Unexpected exception: " + s, bWasException);
206: assertEquals(2, dh.getHighlights().length);
207: assertEqualsHighlight(dh.getHighlights()[0], 1, 8, lpnt);
208: assertEqualsHighlight(dh.getHighlights()[1], 2, 6, lpnt);
209: }
210:
211: public void testChangeHighlightBadLocationException() {
212: bWasException = false;
213: try {
214: obj1 = dh.addHighlight(1, 5, lpnt);
215: } catch (BadLocationException e) {
216: bWasException = true;
217: s = e.getMessage();
218: }
219: assertFalse("Unexpected exception: " + s, bWasException);
220: bWasException = false;
221: try {
222: dh.changeHighlight(obj1, -1, -8);
223: } catch (BadLocationException e) {
224: bWasException = true;
225: }
226: if (isHarmony()) {
227: assertTrue(bWasException);
228: }
229: bWasException = false;
230: try {
231: dh.changeHighlight(obj1, 8, 1);
232: } catch (BadLocationException e) {
233: bWasException = true;
234: }
235: if (isHarmony()) {
236: assertTrue(bWasException);
237: }
238: }
239:
240: public void testRemoveHighlight() throws Exception {
241: try {
242: obj1 = dh.addHighlight(1, 5, lpnt);
243: } catch (BadLocationException e) {
244: bWasException = true;
245: s = e.getMessage();
246: }
247: assertFalse("Unexpected exception: " + s, bWasException);
248: try {
249: obj2 = dh.addHighlight(3, 7, lpnt);
250: } catch (BadLocationException e) {
251: bWasException = true;
252: s = e.getMessage();
253: }
254: assertFalse("Unexpected exception: " + s, bWasException);
255: dh.removeHighlight(obj2);
256: assertEquals(1, dh.getHighlights().length);
257: assertEqualsHighlight(dh.getHighlights()[0], 1, 5, lpnt);
258: dh.removeHighlight(obj1);
259: assertEquals(0, dh.getHighlights().length);
260: }
261:
262: public void testSetGetDrawsLayeredHighlights() throws Exception {
263: assertTrue(dh.getDrawsLayeredHighlights());
264: dh.setDrawsLayeredHighlights(false);
265: assertFalse(dh.getDrawsLayeredHighlights());
266: }
267:
268: public void testRemoveAllHighlights() throws Exception {
269: try {
270: dh.addHighlight(0, 1, lpnt);
271: dh.addHighlight(0, 4, lpnt);
272: dh.addHighlight(5, 7, lpnt);
273: } catch (BadLocationException e) {
274: bWasException = true;
275: s = e.getMessage();
276: }
277: assertFalse("Unexpected exception: " + s, bWasException);
278: assertEquals(3, dh.getHighlights().length);
279: dh.removeAllHighlights();
280: assertEquals(0, dh.getHighlights().length);
281: }
282:
283: public void testGetHighlights() throws Exception {
284: final int N = 50;
285: final int M = 25;
286: Object[] obj = new Object[N];
287: for (int i = 0; i < N; i++) {
288: try {
289: obj[i] = dh.addHighlight(i, i + 1, lpnt);
290: } catch (BadLocationException e) {
291: bWasException = true;
292: s = e.getMessage();
293: }
294: assertFalse("Unexpected exception: " + s, bWasException);
295: }
296: for (int i = 0; i < N; i = i + 2) {
297: dh.removeHighlight(obj[i]);
298: }
299: for (int i = M; i < N; i++) {
300: try {
301: dh.addHighlight(i + 12000, i + 12100, lpnt);
302: } catch (BadLocationException e) {
303: bWasException = true;
304: s = e.getMessage();
305: }
306: assertFalse("Unexpected exception: " + s, bWasException);
307: }
308: Highlighter.Highlight highlights[] = dh.getHighlights();
309: for (int i = 0; i < M; i++) {
310: assertEqualsHighlight(highlights[i], 2 * i + 1,
311: 2 * (i + 1), lpnt);
312: }
313: for (int i = M; i < N; i++) {
314: assertEqualsHighlight(highlights[i], i + 12000, i + 12100,
315: lpnt);
316: }
317: }
318: }
|