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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Toolkit;
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.text.FlowView_FlowStrategyTest.PartFactory;
027:
028: public class GlyphViewRTest extends BasicSwingTestCase {
029: private GlyphView view;
030:
031: private StyledDocument doc;
032:
033: private Element root;
034:
035: private Element leaf;
036:
037: private Font font;
038:
039: private FontMetrics metrics;
040:
041: private float width;
042:
043: private static final String FULL_TEXT = "this text to check how view breaks";
044:
045: // 0123456789012345678901234567890123
046: private static final int startOffset = 5;
047:
048: private static final int endOffset = 28;
049:
050: private static final int length = endOffset - startOffset;
051:
052: private static final String LEAF_TEXT = FULL_TEXT.substring(
053: startOffset, endOffset);
054:
055: private static final int X_AXIS = View.X_AXIS;
056:
057: @SuppressWarnings("deprecation")
058: @Override
059: protected void setUp() throws Exception {
060: super .setUp();
061: doc = new DefaultStyledDocument();
062: root = doc.getDefaultRootElement();
063: doc.insertString(0, FULL_TEXT, null);
064: doc.setCharacterAttributes(startOffset, length,
065: SimpleAttributeSet.EMPTY, false);
066: leaf = root.getElement(0).getElement(1);
067: view = new GlyphView(leaf);
068: font = view.getFont();
069: metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
070: }
071:
072: /**
073: * Tries to breakes the view when the view fits in the length.
074: */
075: public void testBreakView() {
076: View part;
077: width = metrics.stringWidth(LEAF_TEXT);
078: part = view.breakView(X_AXIS, startOffset, 0, width);
079: assertEquals(startOffset, part.getStartOffset());
080: assertEquals(endOffset, part.getEndOffset());
081: assertSame(view, part);
082: part = view.breakView(X_AXIS, startOffset + 1, 0, width);
083: assertEquals(startOffset + 1, part.getStartOffset());
084: assertEquals(endOffset, part.getEndOffset());
085: assertNotSame(view, part);
086: }
087:
088: public void testGetTabExpander() {
089: assertNull(view.getParent());
090: assertNull(view.getTabExpander());
091: ParagraphView paragraphView = new ParagraphView(root
092: .getElement(0));
093: paragraphView.loadChildren(null);
094: ((CompositeView) paragraphView.layoutPool)
095: .loadChildren(new PartFactory());
096: View child = paragraphView.layoutPool.getView(0);
097: assertTrue(child instanceof GlyphView);
098: assertSame(paragraphView, child.getParent().getParent());
099: if (isHarmony()) {
100: assertSame(paragraphView, ((GlyphView) child)
101: .getTabExpander());
102: } else {
103: assertNull(((GlyphView) child).getTabExpander());
104: }
105: }
106: }
|