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 javax.swing.JFrame;
023: import javax.swing.JTextArea;
024: import javax.swing.SwingTestCase;
025: import javax.swing.text.Position.Bias;
026:
027: /**
028: * Tests PlainViewI18N class, in particular how views are laid out.
029: *
030: */
031: public class PlainViewI18N_LayoutTest extends SwingTestCase {
032: private JTextArea area;
033:
034: private Document doc;
035:
036: private JFrame frame;
037:
038: private View rootView;
039:
040: private CompositeView view;
041:
042: /**
043: * Tests values returned by <code>flipEastAndWestAtEnds()</code>.
044: */
045: public void testFlipEastAndWest() throws Exception {
046: boolean[] forward = new boolean[] { false, false, false, false,
047: false, false, false, false, false, false, false, false };
048: boolean[] backward = new boolean[] { false, false, false,
049: false, false, false, false, false, false, false, false,
050: false };
051: getLineView();
052: final int length = doc.getLength() + 1;
053: for (int i = 0; i <= length; i++) {
054: assertEquals("Bias.Forward[" + i + "]", forward[i], view
055: .flipEastAndWestAtEnds(i, Bias.Forward));
056: assertEquals("Bias.Backward[" + i + "]", backward[i], view
057: .flipEastAndWestAtEnds(i, Bias.Backward));
058: }
059: }
060:
061: /**
062: * Tests how views are laid out: which parts of text they are
063: * resposible for.
064: */
065: public void testViewLayout() throws Exception {
066: int[] levels = new int[] { 0, 1, 2, 1, 0 };
067: Element bidiRoot = ((AbstractDocument) doc)
068: .getBidiRootElement();
069: assertEquals(5, bidiRoot.getElementCount());
070: for (int i = 0; i < levels.length; i++) {
071: Element child = bidiRoot.getElement(i);
072: assertEquals(levels[i], StyleConstants.getBidiLevel(child
073: .getAttributes()));
074: }
075: int[] viewPos = new int[] { 0, 2, 6, 8, 4, 6, 2, 4, 8, 11 };
076: getLineView();
077: assertEquals(5, view.getViewCount());
078: for (int i = 0, posIndex = 0; i < levels.length; i++, posIndex += 2) {
079: View child = view.getView(i);
080: assertEquals("Start", viewPos[posIndex], child
081: .getStartOffset());
082: assertEquals("End", viewPos[posIndex + 1], child
083: .getEndOffset());
084: }
085: }
086:
087: @Override
088: protected void setUp() throws Exception {
089: super .setUp();
090: frame = new JFrame("PlainViewI18N Layout Test");
091: area = new JTextArea("ab\u05DC\u05DD12\u05DE\u05DFcd");
092: // 012 3 456 7 89
093: frame.getContentPane().add(area);
094: frame.setSize(150, 100);
095: frame.pack();
096: doc = area.getDocument();
097: rootView = area.getUI().getRootView(area).getView(0);
098: }
099:
100: @Override
101: protected void tearDown() throws Exception {
102: super .tearDown();
103: frame.dispose();
104: }
105:
106: private void getLineView() {
107: if (isHarmony()) {
108: view = (CompositeView) rootView.getView(0);
109: } else {
110: view = (CompositeView) rootView.getView(0).getView(0);
111: }
112: }
113: }
|