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.util.List;
023: import javax.swing.BasicSwingTestCase;
024:
025: /**
026: * Tests GapContent methods which were in GapVector in respect to updating
027: * positions.
028: *
029: */
030: public class GapContent_PositionTest extends BasicSwingTestCase {
031: private GapContent content;
032:
033: /**
034: * Offsets in the document when the document is not changed.
035: */
036: private static final int[] offsets = { 0, 5, 10, 15, 20 };
037:
038: private Position[] positions;
039:
040: @Override
041: protected void setUp() throws Exception {
042: super .setUp();
043: content = new GapContent(30);
044: content.insertString(0, "This is a test string.");
045: content.shiftGap(7);
046: // Add some positions to the content
047: int[] offsets = { 0, 10, 5, 20, 15 };
048: positions = new Position[offsets.length];
049: for (int i = 0; i < offsets.length; i++) {
050: positions[i] = content.createPosition(offsets[i]);
051: }
052: }
053:
054: @Override
055: protected void tearDown() throws Exception {
056: for (int i = 0; i < offsets.length; i++) {
057: positions[i] = null;
058: }
059: super .tearDown();
060: }
061:
062: public void testShiftGapLeft() {
063: if (!isHarmony()) {
064: return;
065: }
066: content.shiftGap(2);
067: checkPositions(new int[] { 0, 12, 17, 22, 27 });
068: checkPositionOffsets(offsets);
069: }
070:
071: public void testShiftGapRight() {
072: if (!isHarmony()) {
073: return;
074: }
075: content.shiftGap(15);
076: checkPositions(new int[] { 0, 5, 10, 22, 27 });
077: checkPositionOffsets(offsets);
078: }
079:
080: /**
081: * Checks that position indexes are the same as expected.
082: *
083: * @param pos an array of expected position indexes
084: */
085: private void checkPositions(final int[] pos) {
086: Position p;
087: final List<?> list = GapContentTest.getPositionList(content);
088: for (int i = 0; i < pos.length; i++) {
089: p = (Position) list.get(i);
090: assertEquals(pos[i], GapContentTest.getIndex(p));
091: }
092: }
093:
094: /**
095: * Checks that position offsets are the same as expected.
096: *
097: * @param pos an array of expected position indexes
098: */
099: private void checkPositionOffsets(final int[] offsets) {
100: Position p;
101: List<?> list = GapContentTest.getPositionList(content);
102: for (int i = 0; i < offsets.length; i++) {
103: p = (Position) list.get(i);
104: assertEquals(offsets[i], p.getOffset());
105: }
106: }
107:
108: public void testShiftEnd() {
109: if (!isHarmony()) {
110: return;
111: }
112: content.shiftEnd(20);
113: checkPositionOffsets(offsets);
114: checkPositions(new int[] { 0, 5, 29, 34, 39 });
115: }
116:
117: public void testShiftGapStartDown() {
118: if (!isHarmony()) {
119: return;
120: }
121: content.shiftGapStartDown(3);
122: checkPositionOffsets(new int[] { 0, 3, 6, 11, 16 });
123: checkPositions(new int[] { 0, 14, 17, 22, 27 });
124: }
125:
126: public void testShiftGapEndUp() {
127: if (!isHarmony()) {
128: return;
129: }
130: content.shiftGapEndUp(22);
131: checkPositionOffsets(new int[] { 0, 5, 7, 7, 12 });
132: checkPositions(new int[] { 0, 5, 22, 22, 27 });
133: }
134:
135: public void testResetMarksAtZero() {
136: if (!isHarmony()) {
137: return;
138: }
139: content.shiftGapStartDown(0);
140: // No explicit call to content.resetMarksAtZero() is made,
141: // as it is called by content.shiftGapStartDown method
142: checkPositions(new int[] { 0, 0, 17, 22, 27 });
143: checkPositionOffsets(new int[] { 0, 0, 3, 8, 13 });
144: }
145: }
|