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.Rectangle;
023: import javax.swing.SwingTestCase;
024: import javax.swing.event.DocumentEvent;
025: import javax.swing.event.DocumentListener;
026: import javax.swing.event.DocumentEvent.ElementChange;
027: import javax.swing.text.PlainViewI18N_BidiTextViewTest.PreferenceChange;
028: import javax.swing.text.PlainViewI18N_LineViewTest.PlainViewI18NWithTextArea;
029:
030: /**
031: * Tests PlainViewI18N.LineView class, in particular how it reacts to changes
032: * in a document.
033: */
034: public class PlainViewI18N_LineView_UpdateTest extends SwingTestCase
035: implements DocumentListener {
036: private PlainDocument doc;
037:
038: private Element root;
039:
040: private Element bidi;
041:
042: private PlainViewI18N parent;
043:
044: private PlainViewI18N.LineView view;
045:
046: private PreferenceChange preferenceParams;
047:
048: private DocumentEvent insertEvent;
049:
050: private DocumentEvent removeEvent;
051:
052: private Element line;
053:
054: private int startOffset;
055:
056: private final Rectangle shape = new Rectangle(27, 74, 91, 41);
057:
058: @Override
059: protected void setUp() throws Exception {
060: super .setUp();
061: if (!isHarmony()) {
062: return;
063: }
064: doc = new PlainDocument();
065: doc.insertString(0, PlainViewI18N_LineViewTest.defText, null);
066: root = doc.getDefaultRootElement();
067: bidi = doc.getBidiRootElement();
068: parent = new PlainViewI18NWithTextArea(root, doc);
069: line = root.getElement(3);
070: startOffset = line.getStartOffset();
071: view = parent.new LineView(line) {
072: @Override
073: public void preferenceChanged(View child, boolean w,
074: boolean h) {
075: preferenceParams = new PreferenceChange(child, w, h);
076: }
077: };
078: view.setParent(parent);
079: doc.addDocumentListener(this );
080: }
081:
082: public void testInsertUpdateLTRNoChange() throws Exception {
083: if (!isHarmony()) {
084: return;
085: }
086: doc.insertString(startOffset
087: + PlainViewI18N_LineViewTest.RTLLength + 1,
088: PlainViewI18N_LineViewTest.LTR, null);
089: view.insertUpdate(insertEvent, shape, null);
090: assertNull(insertEvent.getChange(bidi));
091: preferenceParams.check(view.getView(1), true, false);
092: }
093:
094: public void testInsertUpdateRTLNoChange() throws Exception {
095: if (!isHarmony()) {
096: return;
097: }
098: doc.insertString(startOffset + 1,
099: PlainViewI18N_LineViewTest.RTL, null);
100: view.insertUpdate(insertEvent, shape, null);
101: assertNull(insertEvent.getChange(bidi));
102: preferenceParams.check(view.getView(0), true, false);
103: }
104:
105: public void testInsertUpdateLTRWithChange() throws Exception {
106: if (!isHarmony()) {
107: return;
108: }
109: doc.insertString(startOffset + 1,
110: PlainViewI18N_LineViewTest.LTR, null);
111: view.insertUpdate(insertEvent, shape, null);
112: ElementChange change = insertEvent.getChange(bidi);
113: assertNotNull(change);
114: assertEquals(3, change.getChildrenRemoved().length);
115: assertEquals(6, change.getChildrenAdded().length);
116: assertEquals(6, view.getViewCount());
117: }
118:
119: public void testInsertUpdateRTLWithChange() throws Exception {
120: if (!isHarmony()) {
121: return;
122: }
123: doc.insertString(startOffset
124: + PlainViewI18N_LineViewTest.RTLLength + 1,
125: PlainViewI18N_LineViewTest.RTL, null);
126: view.insertUpdate(insertEvent, shape, null);
127: ElementChange change = insertEvent.getChange(bidi);
128: assertNotNull(change);
129: assertEquals(3, change.getChildrenRemoved().length);
130: assertEquals(6, change.getChildrenAdded().length);
131: assertEquals(6, view.getViewCount());
132: }
133:
134: // The tests ...NoChange are invalid 'cause the structure is changed
135: // tho' it needn't have been modified. If document is optimized to
136: // handle this situation, these tests may become actual.
137: /*
138: public void testRemoveUpdateLTRNoChange() throws Exception {
139: doc.remove(startOffset + PlainViewI18N_LineViewTest.RTLLength + 1, 1);
140:
141: view.removeUpdate(removeEvent, shape, null);
142: assertNull(removeEvent.getChange(bidi));
143: preferenceParams.check(view.getView(1), true, false);
144: }
145:
146: public void testRemoveUpdateRTLNoChange() throws Exception {
147: doc.remove(startOffset + 1, 1);
148:
149: view.removeUpdate(removeEvent, shape, null);
150: assertNull(removeEvent.getChange(bidi));
151: preferenceParams.check(view.getView(0), true, false);
152: }
153: */
154: public void testRemoveUpdateWithChange() throws Exception {
155: if (!isHarmony()) {
156: return;
157: }
158: doc.remove(startOffset + 1, 2);
159: view.removeUpdate(removeEvent, shape, null);
160: ElementChange change = removeEvent.getChange(bidi);
161: assertNotNull(change);
162: assertEquals(3, change.getChildrenRemoved().length);
163: assertEquals(4, change.getChildrenAdded().length);
164: assertEquals(4, view.getViewCount());
165: }
166:
167: public void changedUpdate(DocumentEvent event) {
168: }
169:
170: public void insertUpdate(DocumentEvent event) {
171: insertEvent = event;
172: }
173:
174: public void removeUpdate(DocumentEvent event) {
175: removeEvent = event;
176: }
177: }
|