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 Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Color;
023: import java.awt.Container;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.awt.Rectangle;
027: import javax.swing.BasicSwingTestCase;
028: import javax.swing.JTextArea;
029: import javax.swing.event.DocumentEvent;
030:
031: public class LabelViewTest extends BasicSwingTestCase {
032: private final Marker backgroundColorSetterMarker = new Marker();
033:
034: private final Marker strikeThroughSetterMarker = new Marker();
035:
036: private final Marker subscriptSetterMarker = new Marker();
037:
038: private final Marker super scriptSetterMarker = new Marker();
039:
040: private final Marker underlineSetterMarker = new Marker();
041:
042: private final Marker propertiesFromAttributesSetter = new Marker();
043:
044: private final Marker fontMetricsGetter = new Marker();
045:
046: private final Marker containerFontMetricsGetter = new Marker();
047:
048: private class TestLabelView extends LabelView {
049: public TestLabelView(Element element) {
050: super (element);
051: }
052:
053: @Override
054: protected void setBackground(Color bg) {
055: super .setBackground(bg);
056: backgroundColorSetterMarker.setOccurred();
057: }
058:
059: @Override
060: protected void setStrikeThrough(boolean strike) {
061: super .setStrikeThrough(strike);
062: strikeThroughSetterMarker.setOccurred();
063: }
064:
065: @Override
066: protected void setSubscript(boolean subscript) {
067: super .setSubscript(subscript);
068: subscriptSetterMarker.setOccurred();
069: }
070:
071: @Override
072: protected void setSuperscript(boolean super script) {
073: super .setSuperscript(super script);
074: super scriptSetterMarker.setOccurred();
075: }
076:
077: @Override
078: protected void setUnderline(boolean underline) {
079: super .setUnderline(underline);
080: underlineSetterMarker.setOccurred();
081: }
082:
083: @Override
084: protected FontMetrics getFontMetrics() {
085: fontMetricsGetter.setOccurred();
086: return super .getFontMetrics();
087: }
088:
089: @Override
090: protected void setPropertiesFromAttributes() {
091: propertiesFromAttributesSetter.setOccurred();
092: super .setPropertiesFromAttributes();
093: }
094:
095: @Override
096: public Container getContainer() {
097: return textArea;
098: }
099: }
100:
101: private DefaultStyledDocument styledDoc;
102:
103: private LabelView labelView;
104:
105: private MutableAttributeSet attrs;
106:
107: private final JTextArea textArea;
108:
109: public LabelViewTest() {
110: super ();
111: textArea = new JTextArea() {
112: private static final long serialVersionUID = 1L;
113:
114: @Override
115: public FontMetrics getFontMetrics(Font f) {
116: containerFontMetricsGetter.setOccurred();
117: return super .getFontMetrics(f);
118: }
119: };
120: }
121:
122: @Override
123: protected void setUp() throws Exception {
124: super .setUp();
125: styledDoc = new DefaultStyledDocument();
126: styledDoc.insertString(0, "Hello world", null);
127: attrs = new SimpleAttributeSet();
128: StyleConstants.setBold(attrs, true);
129: styledDoc.insertString(styledDoc.getLength(), "bold \n string",
130: attrs);
131: labelView = new LabelView(styledDoc.getDefaultRootElement()
132: .getElement(0).getElement(0));
133: }
134:
135: public void testChangedUpdate() {
136: StyleConstants.setUnderline(attrs, true);
137: DocumentEvent event = new DocumentEvent() {
138: public int getOffset() {
139: return labelView.getStartOffset();
140: }
141:
142: public int getLength() {
143: return labelView.getEndOffset()
144: - labelView.getStartOffset();
145: }
146:
147: public Document getDocument() {
148: return null;
149: }
150:
151: public EventType getType() {
152: return EventType.CHANGE;
153: }
154:
155: public ElementChange getChange(Element elem) {
156: return null;
157: }
158: };
159: Rectangle rectangle = new Rectangle();
160: StyleConstants.setUnderline(attrs, true);
161: StyleConstants.setBackground(attrs, Color.BLUE);
162: setLabelViewAttributeSet(attrs);
163: labelView.changedUpdate(event, rectangle, null);
164: assertTrue(labelView.isUnderline());
165: assertEquals(Color.BLUE, labelView.getBackground());
166: labelView.setUnderline(false);
167: labelView.changedUpdate(event, rectangle, null);
168: assertTrue(labelView.isUnderline());
169: }
170:
171: public void testIsSuperscript() {
172: StyleConstants.setSuperscript(attrs, false);
173: setLabelViewAttributeSet(attrs);
174: assertFalse(labelView.isSuperscript());
175: StyleConstants.setSuperscript(attrs, true);
176: setLabelViewAttributeSet(attrs);
177: assertFalse(labelView.isSuperscript());
178: labelView.setPropertiesFromAttributes();
179: assertTrue(labelView.isSuperscript());
180: StyleConstants.setSuperscript(attrs, false);
181: setLabelViewAttributeSet(attrs);
182: assertTrue(labelView.isSuperscript());
183: labelView.setPropertiesFromAttributes();
184: assertFalse(labelView.isSuperscript());
185: labelView = createTestLabelView();
186: labelView.isSuperscript();
187: assertTrue(propertiesFromAttributesSetter.isOccurred());
188: clearMarkers();
189: labelView.isSuperscript();
190: assertFalse(propertiesFromAttributesSetter.isOccurred());
191: }
192:
193: public void testSetSuperscript() {
194: boolean defaultSuperscript = false;
195: assertEquals(defaultSuperscript, labelView.isSuperscript());
196: StyleConstants.setSuperscript(attrs, !defaultSuperscript);
197: assertEquals(defaultSuperscript, labelView.isSuperscript());
198: labelView.setSuperscript(true);
199: assertTrue(labelView.isSuperscript());
200: labelView.setSuperscript(false);
201: assertFalse(labelView.isSuperscript());
202: StyleConstants.setSuperscript(attrs, true);
203: setLabelViewAttributeSet(attrs);
204: assertFalse(labelView.isSuperscript());
205: labelView.setPropertiesFromAttributes();
206: assertTrue(labelView.isSuperscript());
207: StyleConstants.setSuperscript(attrs, false);
208: setLabelViewAttributeSet(attrs);
209: assertTrue(labelView.isSuperscript());
210: labelView.setPropertiesFromAttributes();
211: assertFalse(labelView.isSuperscript());
212: labelView = createTestLabelView();
213: labelView.setSuperscript(true);
214: assertFalse(propertiesFromAttributesSetter.isOccurred());
215: }
216:
217: public void testIsSubscript() {
218: StyleConstants.setSubscript(attrs, false);
219: setLabelViewAttributeSet(attrs);
220: assertFalse(labelView.isSubscript());
221: StyleConstants.setSubscript(attrs, true);
222: setLabelViewAttributeSet(attrs);
223: assertFalse(labelView.isSubscript());
224: labelView.setPropertiesFromAttributes();
225: assertTrue(labelView.isSubscript());
226: StyleConstants.setSubscript(attrs, false);
227: setLabelViewAttributeSet(attrs);
228: assertTrue(labelView.isSubscript());
229: labelView.setPropertiesFromAttributes();
230: assertFalse(labelView.isSubscript());
231: labelView = createTestLabelView();
232: labelView.isSuperscript();
233: assertTrue(propertiesFromAttributesSetter.isOccurred());
234: clearMarkers();
235: labelView.isSuperscript();
236: assertFalse(propertiesFromAttributesSetter.isOccurred());
237: }
238:
239: public void testSetSubscript() {
240: boolean defaultSubscript = false;
241: assertEquals(defaultSubscript, labelView.isSubscript());
242: StyleConstants.setSubscript(attrs, !defaultSubscript);
243: assertEquals(defaultSubscript, labelView.isSubscript());
244: labelView.setSubscript(true);
245: assertTrue(labelView.isSubscript());
246: labelView.setSubscript(false);
247: assertFalse(labelView.isSubscript());
248: StyleConstants.setSubscript(attrs, true);
249: setLabelViewAttributeSet(attrs);
250: assertFalse(labelView.isSubscript());
251: labelView.setPropertiesFromAttributes();
252: assertTrue(labelView.isSubscript());
253: StyleConstants.setSubscript(attrs, false);
254: setLabelViewAttributeSet(attrs);
255: assertTrue(labelView.isSubscript());
256: labelView.setPropertiesFromAttributes();
257: assertFalse(labelView.isSubscript());
258: labelView = createTestLabelView();
259: labelView.setSubscript(true);
260: assertFalse(propertiesFromAttributesSetter.isOccurred());
261: }
262:
263: public void testIsStrikeThrough() {
264: StyleConstants.setStrikeThrough(attrs, false);
265: setLabelViewAttributeSet(attrs);
266: assertFalse(labelView.isStrikeThrough());
267: StyleConstants.setStrikeThrough(attrs, true);
268: setLabelViewAttributeSet(attrs);
269: assertFalse(labelView.isStrikeThrough());
270: labelView.setPropertiesFromAttributes();
271: assertTrue(labelView.isStrikeThrough());
272: StyleConstants.setStrikeThrough(attrs, false);
273: setLabelViewAttributeSet(attrs);
274: assertTrue(labelView.isStrikeThrough());
275: labelView.setPropertiesFromAttributes();
276: assertFalse(labelView.isStrikeThrough());
277: labelView = createTestLabelView();
278: labelView.isStrikeThrough();
279: assertTrue(propertiesFromAttributesSetter.isOccurred());
280: clearMarkers();
281: labelView.isStrikeThrough();
282: assertFalse(propertiesFromAttributesSetter.isOccurred());
283: }
284:
285: public void testSetStrikeThrough() {
286: boolean defaultStrikeThrough = false;
287: assertEquals(defaultStrikeThrough, labelView.isStrikeThrough());
288: StyleConstants.setStrikeThrough(attrs, !defaultStrikeThrough);
289: assertEquals(defaultStrikeThrough, labelView.isStrikeThrough());
290: labelView.setStrikeThrough(true);
291: assertTrue(labelView.isStrikeThrough());
292: labelView.setStrikeThrough(false);
293: assertFalse(labelView.isStrikeThrough());
294: StyleConstants.setStrikeThrough(attrs, true);
295: setLabelViewAttributeSet(attrs);
296: assertFalse(labelView.isStrikeThrough());
297: labelView.setPropertiesFromAttributes();
298: assertTrue(labelView.isStrikeThrough());
299: StyleConstants.setStrikeThrough(attrs, false);
300: setLabelViewAttributeSet(attrs);
301: assertTrue(labelView.isStrikeThrough());
302: labelView.setPropertiesFromAttributes();
303: assertFalse(labelView.isStrikeThrough());
304: labelView = createTestLabelView();
305: labelView.setStrikeThrough(true);
306: assertFalse(propertiesFromAttributesSetter.isOccurred());
307: }
308:
309: public void testIsUnderline() {
310: StyleConstants.setUnderline(attrs, false);
311: setLabelViewAttributeSet(attrs);
312: assertFalse(labelView.isUnderline());
313: StyleConstants.setUnderline(attrs, true);
314: setLabelViewAttributeSet(attrs);
315: assertFalse(labelView.isUnderline());
316: labelView.setPropertiesFromAttributes();
317: assertTrue(labelView.isUnderline());
318: StyleConstants.setUnderline(attrs, false);
319: setLabelViewAttributeSet(attrs);
320: assertTrue(labelView.isUnderline());
321: labelView.setPropertiesFromAttributes();
322: assertFalse(labelView.isUnderline());
323: labelView = createTestLabelView();
324: labelView.isUnderline();
325: assertTrue(propertiesFromAttributesSetter.isOccurred());
326: clearMarkers();
327: labelView.isUnderline();
328: assertFalse(propertiesFromAttributesSetter.isOccurred());
329: }
330:
331: public void testSetUnderline() {
332: boolean defaultUnderline = false;
333: assertEquals(defaultUnderline, labelView.isUnderline());
334: StyleConstants.setUnderline(attrs, !defaultUnderline);
335: assertEquals(defaultUnderline, labelView.isUnderline());
336: labelView.setUnderline(true);
337: assertTrue(labelView.isUnderline());
338: labelView.setUnderline(false);
339: assertFalse(labelView.isUnderline());
340: StyleConstants.setUnderline(attrs, true);
341: setLabelViewAttributeSet(attrs);
342: assertFalse(labelView.isUnderline());
343: labelView.setPropertiesFromAttributes();
344: assertTrue(labelView.isUnderline());
345: StyleConstants.setUnderline(attrs, false);
346: setLabelViewAttributeSet(attrs);
347: assertTrue(labelView.isUnderline());
348: labelView.setPropertiesFromAttributes();
349: assertFalse(labelView.isUnderline());
350: labelView = createTestLabelView();
351: labelView.setUnderline(true);
352: assertFalse(propertiesFromAttributesSetter.isOccurred());
353: }
354:
355: public void testGetFont() {
356: Font font = labelView.getFont();
357: assertSame(styledDoc.getFont(labelView.getAttributes()), font);
358: assertSame(styledDoc.getFont(labelView.getAttributes()), font);
359: StyleConstants.setFontSize(attrs, font.getSize() + 1);
360: setLabelViewAttributeSet(attrs);
361: assertSame(font, labelView.getFont());
362: assertSame(font, labelView.getFont());
363: labelView.setPropertiesFromAttributes();
364: font = labelView.getFont();
365: assertSame(styledDoc.getFont(labelView.getAttributes()), font);
366: assertSame(font, labelView.getFont());
367: StyleConstants.setFontSize(attrs, font.getSize() + 1);
368: labelView.setPropertiesFromAttributes();
369: assertSame(font, labelView.getFont());
370: assertSame(font, styledDoc.getFont(labelView.getAttributes()));
371: labelView = createTestLabelView();
372: labelView.getFont();
373: assertTrue(propertiesFromAttributesSetter.isOccurred());
374: clearMarkers();
375: labelView.getFont();
376: assertFalse(propertiesFromAttributesSetter.isOccurred());
377: }
378:
379: public void testGetFontMetrics() {
380: labelView = createTestLabelView();
381: FontMetrics fontMetrics = labelView.getFontMetrics();
382: assertTrue(propertiesFromAttributesSetter.isOccurred());
383: propertiesFromAttributesSetter.setOccurred(false);
384: fontMetrics = labelView.getFontMetrics();
385: assertFalse(propertiesFromAttributesSetter.isOccurred());
386: propertiesFromAttributesSetter.setOccurred(false);
387: assertEquals(labelView.getFont(), fontMetrics.getFont());
388: labelView = createTestLabelView();
389: labelView.getFont();
390: assertTrue(propertiesFromAttributesSetter.isOccurred());
391: assertFalse(fontMetricsGetter.isOccurred());
392: assertFalse(containerFontMetricsGetter.isOccurred());
393: clearMarkers();
394: labelView.getFontMetrics();
395: assertFalse(propertiesFromAttributesSetter.isOccurred());
396: assertTrue(containerFontMetricsGetter.isOccurred());
397: }
398:
399: public void testGetForeground() {
400: Color defaultColor;
401: defaultColor = labelView.getForeground();
402: assertNotNull(defaultColor);
403: StyleConstants.setForeground(attrs, Color.RED);
404: setLabelViewAttributeSet(attrs);
405: assertEquals(defaultColor, labelView.getForeground());
406: assertEquals(defaultColor, labelView.getForeground());
407: labelView.setPropertiesFromAttributes();
408: assertEquals(Color.RED, labelView.getForeground());
409: StyleConstants.setForeground(attrs, Color.BLUE);
410: setLabelViewAttributeSet(attrs);
411: assertEquals(Color.RED, labelView.getForeground());
412: assertEquals(Color.RED, labelView.getForeground());
413: labelView.setPropertiesFromAttributes();
414: assertEquals(Color.BLUE, labelView.getForeground());
415: labelView = createTestLabelView();
416: labelView.getForeground();
417: assertTrue(propertiesFromAttributesSetter.isOccurred());
418: clearMarkers();
419: labelView.getForeground();
420: assertFalse(propertiesFromAttributesSetter.isOccurred());
421: }
422:
423: public void testGetBackground() {
424: Color defaultColor = labelView.getBackground();
425: assertNull(defaultColor);
426: StyleConstants.setBackground(attrs, Color.RED);
427: setLabelViewAttributeSet(attrs);
428: assertEquals(defaultColor, labelView.getBackground());
429: assertEquals(defaultColor, labelView.getBackground());
430: labelView.setPropertiesFromAttributes();
431: assertEquals(Color.RED, labelView.getBackground());
432: StyleConstants.setBackground(attrs, Color.BLUE);
433: setLabelViewAttributeSet(attrs);
434: assertEquals(Color.RED, labelView.getBackground());
435: assertEquals(Color.RED, labelView.getBackground());
436: labelView.setPropertiesFromAttributes();
437: assertEquals(Color.BLUE, labelView.getBackground());
438: labelView = createTestLabelView();
439: labelView.getBackground();
440: assertTrue(propertiesFromAttributesSetter.isOccurred());
441: clearMarkers();
442: labelView.getBackground();
443: assertFalse(propertiesFromAttributesSetter.isOccurred());
444: }
445:
446: public void testSetBackground_WithNull() {
447: labelView.getBackground();
448: labelView.setBackground(null);
449: assertNull(labelView.getBackground());
450: }
451:
452: public void testSetBackground_WithValidValues() {
453: labelView.getBackground();
454: labelView.setBackground(Color.RED);
455: assertEquals(Color.RED, labelView.getBackground());
456: StyleConstants.setBackground(attrs, Color.RED);
457: assertEquals(Color.RED, labelView.getBackground());
458: labelView.setBackground(Color.BLUE);
459: assertEquals(Color.BLUE, labelView.getBackground());
460: labelView = createTestLabelView();
461: labelView.getBackground();
462: assertTrue(propertiesFromAttributesSetter.isOccurred());
463: }
464:
465: public void testSetPropertiesFromAttributes() {
466: // SuperscriptReSync
467: StyleConstants.setSuperscript(attrs, true);
468: assertFalse(labelView.isSuperscript());
469: setLabelViewAttributeSet(attrs);
470: labelView.setPropertiesFromAttributes();
471: assertTrue(labelView.isSuperscript());
472: // BackgroundReSync
473: StyleConstants.setBackground(attrs, Color.BLUE);
474: assertNull(labelView.getBackground());
475: setLabelViewAttributeSet(attrs);
476: assertNull(labelView.getBackground());
477: labelView.setPropertiesFromAttributes();
478: StyleConstants.setBackground(attrs, Color.RED);
479: assertSame(Color.BLUE, labelView.getBackground());
480: setLabelViewAttributeSet(attrs);
481: labelView.setPropertiesFromAttributes();
482: assertSame(Color.RED, labelView.getBackground());
483: labelView = createTestLabelView();
484: labelView.setPropertiesFromAttributes();
485: assertFalse(fontMetricsGetter.isOccurred());
486: if (isHarmony()) {
487: assertTrue(backgroundColorSetterMarker.isOccurred());
488: } else {
489: assertFalse(backgroundColorSetterMarker.isOccurred());
490: }
491: assertTrue(strikeThroughSetterMarker.isOccurred());
492: assertTrue(subscriptSetterMarker.isOccurred());
493: assertTrue(super scriptSetterMarker.isOccurred());
494: assertTrue(underlineSetterMarker.isOccurred());
495: }
496:
497: private void setLabelViewAttributeSet(MutableAttributeSet attrs) {
498: styledDoc.setCharacterAttributes(labelView.getStartOffset(),
499: labelView.getEndOffset() - labelView.getStartOffset(),
500: attrs, true);
501: assertSame(labelView.getElement(), styledDoc
502: .getDefaultRootElement().getElement(0).getElement(0));
503: }
504:
505: private void clearMarkers() {
506: backgroundColorSetterMarker.setOccurred(false);
507: strikeThroughSetterMarker.setOccurred(false);
508: subscriptSetterMarker.setOccurred(false);
509: super scriptSetterMarker.setOccurred(false);
510: underlineSetterMarker.setOccurred(false);
511: propertiesFromAttributesSetter.setOccurred(false);
512: fontMetricsGetter.setOccurred(false);
513: containerFontMetricsGetter.setOccurred(false);
514: }
515:
516: private TestLabelView createTestLabelView() {
517: clearMarkers();
518: return new TestLabelView(styledDoc.getDefaultRootElement()
519: .getElement(0).getElement(0));
520: }
521: }
|