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.plaf.basic;
021:
022: import java.awt.Color;
023: import java.awt.Font;
024: import javax.swing.JFrame;
025: import javax.swing.JTextArea;
026: import javax.swing.JTextField;
027: import javax.swing.JTextPane;
028: import javax.swing.SwingTestCase;
029: import javax.swing.plaf.ComponentUI;
030: import javax.swing.text.BadLocationException;
031: import javax.swing.text.DefaultStyledDocument;
032: import javax.swing.text.Style;
033: import javax.swing.text.StyleConstants;
034: import javax.swing.text.StyleContext;
035: import javax.swing.text.StyledDocument;
036:
037: public class BasicTextPaneUITest extends SwingTestCase {
038: JTextPane textPane;
039:
040: JFrame frame;
041:
042: BasicTextPaneUI ui;
043:
044: Font font = new Font("", Font.BOLD | Font.ITALIC, 25) {
045: private static final long serialVersionUID = 1L;
046:
047: @Override
048: public String getFamily() {
049: return "My FontFamily";
050: }
051:
052: @Override
053: public String getFontName() {
054: return "My FontName";
055: }
056:
057: @Override
058: public String getName() {
059: return "My Name";
060: }
061: };
062:
063: @Override
064: protected void setUp() throws Exception {
065: super .setUp();
066: setIgnoreNotImplemented(true);
067: frame = new JFrame();
068: textPane = new JTextPane();
069: ui = (BasicTextPaneUI) textPane.getUI();
070: frame.getContentPane().add(textPane);
071: frame.setSize(200, 300);
072: frame.pack();
073: }
074:
075: @Override
076: protected void tearDown() throws Exception {
077: frame.dispose();
078: super .tearDown();
079: }
080:
081: public void testCreateUI() {
082: ComponentUI ui1 = BasicTextPaneUI.createUI(textPane);
083: assertTrue(ui1 instanceof BasicTextPaneUI);
084: ui1 = BasicTextPaneUI.createUI(new JTextField());
085: ui1 = BasicTextPaneUI.createUI(new JTextArea());
086: assertTrue(ui1 instanceof BasicTextPaneUI);
087: assertTrue(ui1 instanceof BasicTextPaneUI);
088: ComponentUI ui2 = BasicTextPaneUI.createUI(textPane);
089: assertTrue(ui2 instanceof BasicTextPaneUI);
090: }
091:
092: public void testGetPropertyPrefix() {
093: assertEquals("TextPane", ui.getPropertyPrefix());
094: }
095:
096: public void testPropertyChange() throws BadLocationException {
097: Style style = textPane.getStyle(StyleContext.DEFAULT_STYLE);
098: //Font
099: assertFalse(25 == ((Integer) style
100: .getAttribute(StyleConstants.FontSize)).intValue());
101: textPane.setFont(font);
102: assertEquals(25, ((Integer) style
103: .getAttribute(StyleConstants.FontSize)).intValue());
104: assertEquals(font.getName(), style
105: .getAttribute(StyleConstants.FontFamily));
106: assertFalse(font.getFontName().equals(
107: style.getAttribute(StyleConstants.FontFamily)));
108: assertEquals(font.getName(), style
109: .getAttribute(StyleConstants.FontFamily));
110: // Foreground
111: assertFalse(Color.BLUE.equals(style
112: .getAttribute(StyleConstants.Foreground)));
113: textPane.setForeground(Color.BLUE);
114: assertEquals(Color.BLUE, style
115: .getAttribute(StyleConstants.Foreground));
116: // Document
117: style.addAttribute(StyleConstants.Subscript, Boolean.TRUE);
118: StyledDocument newDoc = new DefaultStyledDocument();
119: Style newStyle = newDoc.getStyle(StyleContext.DEFAULT_STYLE);
120: assertNull(newStyle.getAttribute(StyleConstants.FontSize));
121: assertNull(newStyle.getAttribute(StyleConstants.FontFamily));
122: newStyle.addAttribute(StyleConstants.FontFamily, "family2");
123: newStyle.addAttribute(StyleConstants.FontSize, new Integer(10));
124: newStyle.addAttribute(StyleConstants.Italic, Boolean.FALSE);
125: newStyle.addAttribute(StyleConstants.StrikeThrough,
126: Boolean.TRUE);
127: newStyle.addAttribute(StyleConstants.Subscript, Boolean.FALSE);
128: newStyle.addAttribute(StyleConstants.Foreground, Color.RED);
129: textPane.setDocument(newDoc);
130: assertNotSame(style, newStyle);
131: assertEquals(25, ((Integer) newStyle
132: .getAttribute(StyleConstants.FontSize)).intValue());
133: assertEquals(font.getName(), newStyle
134: .getAttribute(StyleConstants.FontFamily));
135: assertEquals(Boolean.TRUE, newStyle
136: .getAttribute(StyleConstants.Italic));
137: assertEquals(Boolean.TRUE, newStyle
138: .getAttribute(StyleConstants.Bold));
139: assertEquals(Boolean.TRUE, newStyle
140: .getAttribute(StyleConstants.StrikeThrough));
141: assertEquals(Boolean.FALSE, newStyle
142: .getAttribute(StyleConstants.Subscript));
143: assertEquals(Color.BLUE, newStyle
144: .getAttribute(StyleConstants.Foreground));
145: }
146:
147: public void testProPertyChange_FontFamilyName() {
148: Style style = textPane.getStyle(StyleContext.DEFAULT_STYLE);
149: textPane.setFont(font);
150: assertFalse(font.getFamily().equals(
151: style.getAttribute(StyleConstants.FontFamily)));
152: assertEquals(font.getName(), style
153: .getAttribute(StyleConstants.FontFamily));
154: StyledDocument newDoc = new DefaultStyledDocument();
155: Style newStyle = newDoc.getStyle(StyleContext.DEFAULT_STYLE);
156: textPane.setDocument(newDoc);
157: assertFalse(font.getFamily().equals(
158: newStyle.getAttribute(StyleConstants.FontFamily)));
159: assertEquals(font.getName(), newStyle
160: .getAttribute(StyleConstants.FontFamily));
161: }
162: }
|