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.BasicSwingTestCase;
024:
025: public class BoxViewTest extends BasicSwingTestCase {
026: private Document doc;
027:
028: private Element root;
029:
030: private BoxView view;
031:
032: /*
033: * @see TestCase#setUp()
034: */
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: doc = new PlainDocument();
039: doc.insertString(0, "line1\nline\t2", null);
040: root = doc.getDefaultRootElement();
041: view = new BoxView(root, View.X_AXIS);
042: }
043:
044: public void testGetAlignment() {
045: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 0.00001f);
046: assertEquals(0.5f, view.getAlignment(View.Y_AXIS), 0.00001f);
047: try {
048: view.getAlignment(3);
049: fail("IllegalArgumentException must be thrown");
050: } catch (IllegalArgumentException e) {
051: }
052: }
053:
054: public void testPreferenceChanged() {
055: view.layout(width, height);
056: assertTrue(view.isLayoutValid(View.X_AXIS));
057: assertTrue(view.isLayoutValid(View.Y_AXIS));
058: view.preferenceChanged(null, true, false);
059: assertFalse(view.isLayoutValid(View.X_AXIS));
060: assertTrue(view.isLayoutValid(View.Y_AXIS));
061: view.layout(width, height);
062: view.preferenceChanged(null, false, true);
063: assertTrue(view.isLayoutValid(View.X_AXIS));
064: assertFalse(view.isLayoutValid(View.Y_AXIS));
065: }
066:
067: public void testReplace() {
068: // Check child cound and size of the cache of allocations
069: assertEquals(0, view.getViewCount());
070: try {
071: view.getOffset(view.getAxis(), 0);
072: fail("ArrayIndexOutOfBoundsException is expected");
073: } catch (ArrayIndexOutOfBoundsException e) {
074: }
075: try {
076: view.getSpan(view.getAxis(), 0);
077: fail("ArrayIndexOutOfBoundsException is expected");
078: } catch (ArrayIndexOutOfBoundsException e) {
079: }
080: // Do layout and check it became valid
081: view.layout(width, height);
082: assertTrue(view.isLayoutValid(View.X_AXIS));
083: assertTrue(view.isLayoutValid(View.Y_AXIS));
084: // Add two children into the view
085: view.replace(0, 0, new View[] { new PlainView(root),
086: new PlainView(root) });
087: // Check the side effects
088: assertFalse(view.isLayoutValid(View.X_AXIS));
089: assertFalse(view.isLayoutValid(View.Y_AXIS));
090: // Now we can get offset and span of child at index 1
091: view.getOffset(view.getAxis(), 1);
092: view.getSpan(view.getAxis(), 1);
093: // ...but can't get ones of child at index 2 as it doesn't exist
094: try {
095: view.getOffset(view.getAxis(), 2);
096: fail("ArrayIndexOutOfBoundsException is expected");
097: } catch (ArrayIndexOutOfBoundsException e) {
098: }
099: try {
100: view.getSpan(view.getAxis(), 2);
101: fail("ArrayIndexOutOfBoundsException is expected");
102: } catch (ArrayIndexOutOfBoundsException e) {
103: }
104: }
105:
106: private static void assertCalled(final boolean[] called,
107: final boolean layout, final boolean major,
108: final boolean minor) {
109: assertEquals(layout, called[0]);
110: assertEquals(major, called[1]);
111: assertEquals(minor, called[2]);
112: // Reset flags
113: called[0] = called[1] = called[2] = false;
114: }
115:
116: public void testSetSize() {
117: final boolean[] called = new boolean[] { false, false, false };
118: view = new BoxView(root, View.X_AXIS) {
119: @Override
120: public void layout(final int w, final int h) {
121: assertEquals(width - getLeftInset() - getRightInset(),
122: w);
123: assertEquals(height - getTopInset() - getBottomInset(),
124: h);
125: called[0] = true;
126: super .layout(w, h);
127: }
128:
129: @Override
130: protected void layoutMajorAxis(final int targetSpan,
131: final int axis, final int[] offsets,
132: final int[] spans) {
133: called[1] = true;
134: super .layoutMajorAxis(targetSpan, axis, offsets, spans);
135: }
136:
137: @Override
138: protected void layoutMinorAxis(final int targetSpan,
139: final int axis, final int[] offsets,
140: final int[] spans) {
141: called[2] = true;
142: super .layoutMinorAxis(targetSpan, axis, offsets, spans);
143: }
144: };
145: view.setSize(width, height);
146: assertCalled(called, true, true, true);
147: // Call with same sizes doesn't do the layout itself
148: view.setSize(width, height);
149: assertCalled(called, true, false, false);
150: // Set only left inset, layout of the major axis (X) must be updated
151: view.setInsets((short) 0, (short) 1, (short) 0, (short) 0);
152: view.setSize(width, height);
153: assertCalled(called, true, true, false);
154: // Set new insets and try again
155: view.setParagraphInsets(CompositeViewTest.getAttributeSet());
156: view.setSize(width, height);
157: assertCalled(called, true, true, true);
158: }
159:
160: /**
161: * Rectangle that is used in tests of is{After,Before}.
162: */
163: private static final Rectangle rect = new Rectangle(10, 15, 20, 30);
164:
165: /**
166: * Tests isAfter with default axis value of X.
167: *
168: * Differences in values are marked with asterisk in trailing comment.
169: */
170: public void testIsAfter01() {
171: assertEquals(View.X_AXIS, view.getAxis());
172: assertFalse(view.isAfter(0, 0, rect));
173: assertFalse(view.isAfter(9, 0, rect));
174: assertFalse(view.isAfter(0, 14, rect));
175: assertFalse(view.isAfter(9, 14, rect));
176: assertFalse(view.isAfter(9, 15, rect));
177: assertFalse(view.isAfter(9, 35, rect));
178: assertFalse(view.isAfter(10, 14, rect));
179: assertFalse(view.isAfter(10, 15, rect));
180: assertFalse(view.isAfter(20, 15, rect));
181: assertFalse(view.isAfter(20, 14, rect));
182: assertFalse(view.isAfter(20, 45, rect));
183: assertFalse(view.isAfter(20, 46, rect)); // *
184: assertFalse(view.isAfter(20, 50, rect)); // *
185: assertFalse(view.isAfter(30, 20, rect));
186: assertTrue(view.isAfter(31, 20, rect)); // *
187: assertTrue(view.isAfter(31, 15, rect)); // *
188: assertTrue(view.isAfter(31, 0, rect)); // *
189: }
190:
191: /**
192: * Tests isAfter with axis value set to Y.
193: *
194: * Differences in values are marked with asterisk in trailing comment.
195: */
196: public void testIsAfter02() {
197: view.setAxis(View.Y_AXIS);
198: assertFalse(view.isAfter(0, 0, rect));
199: assertFalse(view.isAfter(9, 0, rect));
200: assertFalse(view.isAfter(0, 14, rect));
201: assertFalse(view.isAfter(9, 14, rect));
202: assertFalse(view.isAfter(9, 15, rect));
203: assertFalse(view.isAfter(9, 35, rect));
204: assertFalse(view.isAfter(10, 14, rect));
205: assertFalse(view.isAfter(10, 15, rect));
206: assertFalse(view.isAfter(20, 15, rect));
207: assertFalse(view.isAfter(20, 14, rect));
208: assertFalse(view.isAfter(20, 45, rect));
209: assertTrue(view.isAfter(20, 46, rect)); // *
210: assertTrue(view.isAfter(20, 50, rect)); // *
211: assertFalse(view.isAfter(30, 20, rect));
212: assertFalse(view.isAfter(31, 20, rect)); // *
213: assertFalse(view.isAfter(31, 15, rect)); // *
214: assertFalse(view.isAfter(31, 0, rect)); // *
215: }
216:
217: /**
218: * Tests isBefore with default axis value of X.
219: *
220: * Differences in values are marked with asterisk in trailing comment.
221: */
222: public void testIsBefore01() {
223: assertTrue(view.isBefore(0, 0, rect));
224: assertTrue(view.isBefore(9, 0, rect));
225: assertTrue(view.isBefore(0, 14, rect));
226: assertTrue(view.isBefore(9, 14, rect));
227: assertTrue(view.isBefore(9, 15, rect)); // *
228: assertTrue(view.isBefore(9, 35, rect)); // *
229: assertFalse(view.isBefore(10, 14, rect)); // *
230: assertFalse(view.isBefore(10, 15, rect));
231: assertFalse(view.isBefore(20, 15, rect));
232: assertFalse(view.isBefore(20, 14, rect)); // *
233: }
234:
235: /**
236: * Tests isBefore with axis value set to Y.
237: *
238: * Differences in values are marked with asterisk in trailing comment.
239: */
240: public void testIsBefore02() {
241: view.setAxis(View.Y_AXIS);
242: assertTrue(view.isBefore(0, 0, rect));
243: assertTrue(view.isBefore(9, 0, rect));
244: assertTrue(view.isBefore(0, 14, rect));
245: assertTrue(view.isBefore(9, 14, rect));
246: assertFalse(view.isBefore(9, 15, rect)); // *
247: assertFalse(view.isBefore(9, 35, rect)); // *
248: assertTrue(view.isBefore(10, 14, rect)); // *
249: assertFalse(view.isBefore(10, 15, rect));
250: assertFalse(view.isBefore(20, 15, rect));
251: assertTrue(view.isBefore(20, 14, rect)); // *
252: }
253:
254: public void testBoxView() {
255: view = new BoxView(root, View.X_AXIS);
256: assertSame(root, view.getElement());
257: assertEquals(View.X_AXIS, view.getAxis());
258: view = new BoxView(root, View.Y_AXIS);
259: assertSame(root, view.getElement());
260: assertEquals(View.Y_AXIS, view.getAxis());
261: final boolean[] setAxisCalled = new boolean[1];
262: view = new BoxView(root, -1251) {
263: @Override
264: public void setAxis(int axis) {
265: setAxisCalled[0] = true;
266: super .setAxis(axis);
267: }
268: };
269: assertSame(root, view.getElement());
270: assertEquals(-1251, view.getAxis());
271: assertFalse(setAxisCalled[0]);
272: }
273:
274: public void testGetAxis() {
275: assertEquals(View.X_AXIS, view.getAxis());
276: }
277:
278: public void testGetHeight() {
279: assertEquals(0, view.getHeight());
280: assertFalse(view.isLayoutValid(View.X_AXIS));
281: view.layout(width, height);
282: assertEquals(height, view.getHeight());
283: view.setSize(width, height);
284: assertEquals(height, view.getHeight());
285: // Layout invalidation doesn't change the height
286: view.layoutChanged(View.Y_AXIS);
287: assertEquals(height, view.getHeight());
288: // Put some insets
289: view.setInsets((short) 10, (short) 0, (short) 0, (short) 0);
290: view.setSize(width, height);
291: if (isHarmony()) {
292: assertEquals(height - view.getTopInset(), view.getHeight());
293: } else {
294: // The width isn't changed
295: assertEquals(height, view.getHeight());
296: }
297: view.setInsets((short) 0, (short) 0, (short) 10, (short) 0);
298: view.setSize(width, height);
299: if (isHarmony()) {
300: assertEquals(height - view.getBottomInset(), view
301: .getHeight());
302: } else {
303: // The width is reduced by right inset twice
304: assertEquals(height - 2 * view.getBottomInset(), view
305: .getHeight());
306: }
307: // No matter if insets are set, layout width isn't changed
308: view.setInsets((short) 7, (short) 0, (short) 4, (short) 0);
309: view.layout(width, height);
310: if (isHarmony()) {
311: assertEquals(height, view.getHeight());
312: } else {
313: assertEquals(height
314: + (view.getTopInset() - view.getBottomInset()),
315: view.getHeight());
316: }
317: }
318:
319: public void testGetWidth() {
320: assertEquals(0, view.getWidth());
321: assertFalse(view.isLayoutValid(View.X_AXIS));
322: view.layout(width, height);
323: assertEquals(width, view.getWidth());
324: view.setSize(width, height);
325: assertEquals(width, view.getWidth());
326: // Layout invalidation doesn't change the width
327: view.layoutChanged(View.X_AXIS);
328: assertEquals(width, view.getWidth());
329: // Put some insets
330: view.setInsets((short) 0, (short) 10, (short) 0, (short) 0);
331: view.setSize(width, height);
332: if (isHarmony()) {
333: assertEquals(width - view.getLeftInset(), view.getWidth());
334: } else {
335: // The width isn't changed
336: assertEquals(width, view.getWidth());
337: }
338: view.setInsets((short) 0, (short) 0, (short) 0, (short) 10);
339: view.setSize(width, height);
340: if (isHarmony()) {
341: assertEquals(width - view.getRightInset(), view.getWidth());
342: } else {
343: // The width is reduced by right inset twice
344: assertEquals(width - 2 * view.getRightInset(), view
345: .getWidth());
346: }
347: // No matter if insets are set, layout width isn't changed
348: view.setInsets((short) 0, (short) 7, (short) 0, (short) 4);
349: view.layout(width, height);
350: if (isHarmony()) {
351: assertEquals(width, view.getWidth());
352: } else {
353: assertEquals(width
354: + (view.getLeftInset() - view.getRightInset()),
355: view.getWidth());
356: }
357: }
358:
359: public void testIsAllocationValid() {
360: assertFalse(view.isAllocationValid());
361: view.layout(width, height);
362: assertTrue(view.isAllocationValid());
363: view.preferenceChanged(null, true, false);
364: assertFalse(view.isAllocationValid());
365: }
366:
367: public void testIsLayoutValid() {
368: assertFalse(view.isLayoutValid(View.X_AXIS));
369: assertFalse(view.isLayoutValid(View.Y_AXIS));
370: view.layout(width, height);
371: assertTrue(view.isLayoutValid(View.X_AXIS));
372: assertTrue(view.isLayoutValid(View.Y_AXIS));
373: }
374:
375: private final int width = 150;
376:
377: private final int height = 325;
378:
379: public void testLayout() {
380: final boolean[] called = new boolean[] { false, false };
381: view = new BoxView(root, View.X_AXIS) {
382: @Override
383: protected void layoutMajorAxis(final int targetSpan,
384: final int axis, final int[] offsets,
385: final int[] spans) {
386: called[0] = true;
387: assertEquals(width, targetSpan);
388: assertEquals(X_AXIS, axis);
389: super .layoutMajorAxis(targetSpan, axis, offsets, spans);
390: }
391:
392: @Override
393: protected void layoutMinorAxis(final int targetSpan,
394: final int axis, final int[] offsets,
395: final int[] spans) {
396: called[1] = true;
397: assertEquals(height, targetSpan);
398: assertEquals(Y_AXIS, axis);
399: super .layoutMinorAxis(targetSpan, axis, offsets, spans);
400: }
401: };
402: assertFalse(view.isLayoutValid(View.X_AXIS));
403: assertFalse(view.isLayoutValid(View.Y_AXIS));
404: view.layout(width, height);
405: assertTrue(view.isLayoutValid(View.X_AXIS));
406: assertTrue(view.isLayoutValid(View.Y_AXIS));
407: assertTrue(called[0]);
408: called[0] = false;
409: assertTrue(called[1]);
410: called[1] = false;
411: view.preferenceChanged(null, true, false);
412: assertFalse(view.isLayoutValid(View.X_AXIS));
413: assertTrue(view.isLayoutValid(View.Y_AXIS));
414: view.layout(width, height);
415: assertTrue(called[0]);
416: called[0] = false;
417: assertFalse(called[1]);
418: called[1] = false;
419: }
420:
421: /**
422: * Tests that <code>layoutChanged</code> isn't called
423: * when width or height passed to
424: * <code>layout</code> is changed.
425: */
426: public void testLayout02() {
427: final boolean[] called = new boolean[] { false };
428: view = new BoxView(root, View.X_AXIS) {
429: @Override
430: public void layoutChanged(int axis) {
431: called[0] = false;
432: super .layoutChanged(axis);
433: }
434: };
435: assertFalse(view.isLayoutValid(View.X_AXIS));
436: assertFalse(view.isLayoutValid(View.Y_AXIS));
437: view.layout(width, height);
438: assertTrue(view.isLayoutValid(View.X_AXIS));
439: assertTrue(view.isLayoutValid(View.Y_AXIS));
440: assertFalse(called[0]);
441: view.layout(width, height);
442: assertFalse(called[0]);
443: view.layout(width - 10, height);
444: assertFalse(called[0]);
445: view.layout(width - 10, height - 20);
446: assertFalse(called[0]);
447: }
448:
449: public void testLayoutChanged() {
450: view.layout(width, height);
451: assertTrue(view.isLayoutValid(View.X_AXIS));
452: assertTrue(view.isLayoutValid(View.Y_AXIS));
453: view.layoutChanged(View.Y_AXIS);
454: assertTrue(view.isLayoutValid(View.X_AXIS));
455: assertFalse(view.isLayoutValid(View.Y_AXIS));
456: }
457:
458: public void testSetAxis() {
459: view.setAxis(View.Y_AXIS);
460: assertEquals(View.Y_AXIS, view.getAxis());
461: view.setAxis(View.X_AXIS);
462: assertEquals(View.X_AXIS, view.getAxis());
463: view.setAxis(-1251);
464: assertEquals(-1251, view.getAxis());
465: }
466: /*
467: public void testPaint() {
468: }
469:
470: public void testPaintChild() {
471: }
472: */
473: }
|