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 java.awt.Color;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026: import java.awt.Shape;
027: import javax.swing.JFrame;
028: import javax.swing.JPasswordField;
029: import javax.swing.SwingTestCase;
030: import javax.swing.plaf.basic.BasicTextUI;
031:
032: public class PasswordViewTest extends SwingTestCase {
033: PasswordView view;
034:
035: JPasswordField pf;
036:
037: String content = "abcd\tefg";
038:
039: JFrame jf;
040:
041: int x = 123;
042:
043: int y = 234;
044:
045: Segment text;
046:
047: FontMetrics fm;
048:
049: Segment echoChars;
050:
051: int textWidth;
052:
053: int echoCharsWidth;
054:
055: int height;
056:
057: char zero = '\0';
058:
059: int length;
060:
061: boolean bWasException;
062:
063: @Override
064: protected void setUp() throws Exception {
065: super .setUp();
066: bWasException = false;
067: length = content.length();
068: jf = new JFrame();
069: pf = new JPasswordField(content);
070: view = (PasswordView) ((BasicTextUI) pf.getUI())
071: .getRootView(pf).getView(0);
072: fm = pf.getFontMetrics(pf.getFont());
073: assertNotNull(fm);
074: text = new Segment();
075: pf.getDocument().getText(0, length, text);
076: echoChars = new Segment(new char[] { '*', '*', '*', '*', '*',
077: '*', '*', '*' }, 0, length);
078: textWidth = Utilities.getTabbedTextWidth(text, fm, 0, view, 0);
079: echoCharsWidth = Utilities.getTabbedTextWidth(echoChars, fm, 0,
080: view, 0);
081: height = fm.getHeight();
082: assertNotNull(text);
083: jf.getContentPane().add(pf);
084: pf.setForeground(Color.RED);
085: pf.setSelectedTextColor(Color.GREEN);
086: jf.setSize(200, 300);
087: jf.pack();
088: }
089:
090: @Override
091: protected void tearDown() throws Exception {
092: super .tearDown();
093: jf.dispose();
094: }
095:
096: public void testGetPreferredSpan() {
097: assertEquals(echoCharsWidth, (int) view
098: .getPreferredSpan(View.X_AXIS));
099: assertEquals(height, (int) view.getPreferredSpan(View.Y_AXIS));
100: pf.setColumns(3);
101: assertEquals(echoCharsWidth, (int) view
102: .getPreferredSpan(View.X_AXIS));
103: pf.setColumns(0);
104: pf.setEchoChar(zero);
105: assertEquals(textWidth, (int) view
106: .getPreferredSpan(View.X_AXIS));
107: assertEquals(height, (int) view.getPreferredSpan(View.Y_AXIS));
108: pf.setColumns(3);
109: assertEquals(textWidth, (int) view
110: .getPreferredSpan(View.X_AXIS));
111: }
112:
113: public void testDrawEchoCharacter() {
114: char c = '&';
115: int m_width = fm.charWidth(c);
116: Graphics g = pf.getGraphics();
117: Color color = g.getColor();
118: assertEquals(x + m_width, view.drawEchoCharacter(g, x, y, c));
119: assertEquals(color, g.getColor());
120: }
121:
122: public void testDrawSelectedText() {
123: Graphics g = pf.getGraphics();
124: Color color = g.getColor();
125: try {
126: assertEquals(echoCharsWidth + x, view.drawSelectedText(g,
127: x, y, 0, length));
128: pf.setEchoChar(zero);
129: assertEquals(textWidth, view.drawSelectedText(g, 0, y, 0,
130: length));
131: } catch (BadLocationException e) {
132: assertTrue("Unexpected exception: ", false);
133: }
134: if (isHarmony()) {
135: assertEquals(color, g.getColor());
136: }
137: }
138:
139: public void testDrawUnselectedText() {
140: Graphics g = pf.getGraphics();
141: Color old = g.getColor();
142: try {
143: assertEquals(echoCharsWidth + x, view.drawUnselectedText(g,
144: x, y, 0, length));
145: pf.setEchoChar(zero);
146: assertEquals(textWidth, view.drawSelectedText(g, 0, y, 0,
147: length));
148: } catch (BadLocationException e) {
149: assertTrue("Unexpected exception: ", false);
150: }
151: if (isHarmony()) {
152: assertEquals(old, g.getColor());
153: }
154: }
155:
156: public void testModelToView() throws Exception {
157: Shape shape = new Rectangle(2, 20, 30, 40);
158: Rectangle adjustRect = view.adjustAllocation(shape).getBounds();
159: Rectangle rect = (Rectangle) view.modelToView(1, shape,
160: Position.Bias.Forward);
161: assertEquals(adjustRect.x + fm.stringWidth("*"), rect.x);
162: assertEquals(adjustRect.y, rect.y);
163: assertEquals(1, rect.width);
164: assertEquals(height, rect.height);
165: rect = (Rectangle) view.modelToView(5, shape,
166: Position.Bias.Forward);
167: assertEquals(adjustRect.x + fm.stringWidth("*****"), rect.x);
168: assertEquals(adjustRect.y, rect.y);
169: assertEquals(1, rect.width);
170: assertEquals(height, rect.height);
171: try {
172: view.modelToView(-1000, shape, Position.Bias.Forward);
173: if (isHarmony()) {
174: fail("Should produce am exception");
175: }
176: } catch (BadLocationException ble) {
177: }
178: }
179:
180: public void testViewToModel() {
181: Shape shape = new Rectangle(10, 20, 30, 40);
182: int px = 10 + fm.stringWidth("***");
183: int py = 20 + fm.getAscent() - 1;
184: Position.Bias[] biasRet = new Position.Bias[1];
185: assertEquals(3, view.viewToModel(px, py, shape, biasRet));
186: px = 10 + fm.stringWidth("****");
187: py = 20 + fm.getAscent() - 1;
188: biasRet = new Position.Bias[1];
189: assertEquals(4, view.viewToModel(px, py, shape, biasRet));
190: }
191: }
|