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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Button;
024: import java.awt.Dimension;
025: import java.awt.EventQueue;
026: import java.awt.Frame;
027: import java.awt.Graphics;
028: import java.awt.Image;
029: import java.awt.Rectangle;
030: import java.awt.Toolkit;
031: import java.awt.Window;
032: import java.awt.event.InvocationEvent;
033: import org.apache.harmony.awt.ComponentInternals;
034:
035: public class RepaintManagerTest extends BasicSwingTestCase {
036: private Dimension dbMaxSize;
037:
038: public RepaintManagerTest(final String name) {
039: super (name);
040: }
041:
042: @Override
043: protected void setUp() throws Exception {
044: super .setUp();
045: timeoutDelay = 10 * DEFAULT_TIMEOUT_DELAY;
046: dbMaxSize = RepaintManager.currentManager(null)
047: .getDoubleBufferMaximumSize();
048: }
049:
050: @Override
051: protected void tearDown() throws Exception {
052: RepaintManager.currentManager(null).setDoubleBufferMaximumSize(
053: dbMaxSize);
054: super .tearDown();
055: }
056:
057: public void testCurrentManager() throws Exception {
058: RepaintManager inst1 = RepaintManager
059: .currentManager(new JButton());
060: assertNotNull(inst1);
061: RepaintManager inst2 = RepaintManager
062: .currentManager(new Button());
063: assertNotNull(inst2);
064: RepaintManager inst3 = RepaintManager.currentManager(null);
065: assertNotNull(inst3);
066: assertTrue(inst1 == inst2);
067: assertTrue(inst2 == inst3);
068: }
069:
070: public void testSetCurrentManager() throws Exception {
071: RepaintManager newInst = new RepaintManager();
072: RepaintManager.setCurrentManager(newInst);
073: assertTrue(RepaintManager.currentManager(null) == newInst);
074: RepaintManager.setCurrentManager(null);
075: assertFalse(RepaintManager.currentManager(null) == newInst);
076: assertNotNull(RepaintManager.currentManager(null));
077: }
078:
079: public void testAddRemoveInvalidComponent() throws Exception {
080: Frame f = new Frame();
081: final JPanel rootPanel = new JPanel(new BorderLayout()) {
082: private static final long serialVersionUID = 1L;
083:
084: @Override
085: public boolean isValidateRoot() {
086: return true;
087: }
088: };
089: final JPanel controlled = new JPanel();
090: f.add(rootPanel);
091: rootPanel.add(controlled);
092:
093: assertFalse(controlled.isValid());
094: assertFalse(rootPanel.isValid());
095: SwingUtilities.invokeAndWait(new Runnable() {
096: public void run() {
097: RepaintManager.currentManager(null)
098: .addInvalidComponent(controlled);
099: }
100: });
101:
102: final Marker isValid = new Marker();
103: SwingUtilities.invokeAndWait(new Runnable() {
104: public void run() {
105: isValid.setOccurred(controlled.isValid());
106: }
107: });
108: assertFalse(isValid.isOccurred());
109:
110: f.setVisible(true);
111: waitForIdle();
112: assertTrue(controlled.isValid());
113: SwingUtilities.invokeAndWait(new Runnable() {
114: public void run() {
115: RepaintManager.currentManager(null)
116: .addInvalidComponent(controlled);
117: }
118: });
119: assertTrue(controlled.isValid());
120: assertTrue(rootPanel.isValid());
121:
122: isValid.reset();
123: controlled.invalidate();
124: waitForIdle();
125: assertFalse(controlled.isValid());
126: SwingUtilities.invokeAndWait(new Runnable() {
127: public void run() {
128: RepaintManager.currentManager(null)
129: .addInvalidComponent(controlled);
130: }
131: });
132: SwingUtilities.invokeAndWait(new Runnable() {
133: public void run() {
134: isValid.setOccurred(controlled.isValid());
135: }
136: });
137: assertTrue(isValid.isOccurred());
138:
139: isValid.reset();
140: controlled.invalidate();
141: assertFalse(controlled.isValid());
142: SwingUtilities.invokeAndWait(new Runnable() {
143: public void run() {
144: RepaintManager.currentManager(null)
145: .addInvalidComponent(controlled);
146: RepaintManager.currentManager(null)
147: .removeInvalidComponent(controlled);
148: }
149: });
150: SwingUtilities.invokeAndWait(new Runnable() {
151: public void run() {
152: isValid.setOccurred(controlled.isValid());
153: }
154: });
155: assertTrue(isValid.isOccurred());
156:
157: isValid.reset();
158: controlled.invalidate();
159: assertFalse(controlled.isValid());
160: SwingUtilities.invokeAndWait(new Runnable() {
161: public void run() {
162: RepaintManager.currentManager(null)
163: .addInvalidComponent(controlled);
164: RepaintManager.currentManager(null)
165: .removeInvalidComponent(rootPanel);
166: }
167: });
168: SwingUtilities.invokeAndWait(new Runnable() {
169: public void run() {
170: isValid.setOccurred(controlled.isValid());
171: }
172: });
173: assertFalse(isValid.isOccurred());
174:
175: try { // Regression test for HARMONY-1725
176: RepaintManager.currentManager(null).addInvalidComponent(
177: null);
178: } catch (NullPointerException e) {
179: fail("Unexpected NullPointerException is thrown");
180: }
181: }
182:
183: public void testValidateInvalidComponents() throws Exception {
184: Frame f = new Frame();
185: final JPanel rootPanel = new JPanel(new BorderLayout()) {
186: private static final long serialVersionUID = 1L;
187:
188: @Override
189: public boolean isValidateRoot() {
190: return true;
191: }
192: };
193: final JPanel controlled = new JPanel();
194: f.add(rootPanel);
195: rootPanel.add(controlled);
196:
197: f.setVisible(true);
198: waitForIdle();
199: assertTrue(controlled.isValid());
200: controlled.invalidate();
201: assertFalse(controlled.isValid());
202: final Marker isValid = new Marker();
203: SwingUtilities.invokeAndWait(new Runnable() {
204: public void run() {
205: RepaintManager.currentManager(null)
206: .addInvalidComponent(controlled);
207: RepaintManager.currentManager(null)
208: .validateInvalidComponents();
209: isValid.setOccurred(controlled.isValid());
210: }
211: });
212: assertTrue(isValid.isOccurred());
213:
214: f.dispose();
215: isValid.reset();
216: controlled.invalidate();
217: assertFalse(controlled.isValid());
218: SwingUtilities.invokeAndWait(new Runnable() {
219: public void run() {
220: RepaintManager.currentManager(null)
221: .addInvalidComponent(controlled);
222: RepaintManager.currentManager(null)
223: .removeInvalidComponent(rootPanel);
224: RepaintManager.currentManager(null)
225: .validateInvalidComponents();
226: isValid.setOccurred(controlled.isValid());
227: }
228: });
229: assertFalse(isValid.isOccurred());
230: }
231:
232: public void testAddDirtyRegion() throws Exception {
233: final JPanel root = new JPanel(new BorderLayout());
234: JFrame f = new JFrame();
235: f.getContentPane().add(root);
236: f.setSize(100, 100);
237: f.setVisible(true);
238: waitForNativePaint(f);
239: final Marker marker = new Marker();
240: SwingUtilities.invokeAndWait(new Runnable() {
241: public void run() {
242: RepaintManager.currentManager(null).addDirtyRegion(
243: root, 10, 10, 40, 40);
244: marker.setAuxiliary(RepaintManager.currentManager(null)
245: .getDirtyRegion(root));
246: }
247: });
248: assertEquals(new Rectangle(10, 10, 40, 40), marker
249: .getAuxiliary());
250: marker.setAuxiliary(null);
251: SwingUtilities.invokeAndWait(new Runnable() {
252: public void run() {
253: RepaintManager.currentManager(null).addDirtyRegion(
254: root, 10, 10, 200, 200);
255: marker.setAuxiliary(RepaintManager.currentManager(null)
256: .getDirtyRegion(root));
257: }
258: });
259: assertEquals(new Rectangle(10, 10, 200, 200), marker
260: .getAuxiliary());
261: marker.setAuxiliary(null);
262: SwingUtilities.invokeAndWait(new Runnable() {
263: public void run() {
264: RepaintManager.currentManager(null).addDirtyRegion(
265: root, 10, 10, 40, 40);
266: RepaintManager.currentManager(null).addDirtyRegion(
267: root, 20, 5, 40, 30);
268: marker.setAuxiliary(RepaintManager.currentManager(null)
269: .getDirtyRegion(root));
270: }
271: });
272: assertEquals(new Rectangle(10, 5, 50, 45), marker
273: .getAuxiliary());
274: f.dispose();
275: }
276:
277: public void testMarkCompletelyDirtyClean() throws Exception {
278: final JPanel root = new JPanel(new BorderLayout());
279: JFrame f = new JFrame();
280: f.getContentPane().add(root);
281: f.setSize(100, 100);
282: f.setVisible(true);
283: waitForNativePaint(f);
284: final Marker marker = new Marker();
285: SwingUtilities.invokeAndWait(new Runnable() {
286: public void run() {
287: RepaintManager.currentManager(null).addDirtyRegion(
288: root, 10, 10, 40, 40);
289: RepaintManager.currentManager(null)
290: .markCompletelyDirty(root);
291: marker.setAuxiliary(RepaintManager.currentManager(null)
292: .getDirtyRegion(root));
293: }
294: });
295: Rectangle dirtyRect = (Rectangle) marker.getAuxiliary();
296: assertEquals(new Rectangle(0, 0, Integer.MAX_VALUE,
297: Integer.MAX_VALUE), dirtyRect);
298: marker.setAuxiliary(null);
299: SwingUtilities.invokeAndWait(new Runnable() {
300: public void run() {
301: RepaintManager.currentManager(null)
302: .markCompletelyClean(root);
303: RepaintManager.currentManager(null)
304: .markCompletelyDirty(root);
305: marker.setAuxiliary(RepaintManager.currentManager(null)
306: .getDirtyRegion(root));
307: }
308: });
309: Rectangle dirtyRect2 = (Rectangle) marker.getAuxiliary();
310: assertEquals(new Rectangle(0, 0, Integer.MAX_VALUE,
311: Integer.MAX_VALUE), dirtyRect2);
312: assertNotSame(dirtyRect, dirtyRect2);
313: marker.setAuxiliary(null);
314: SwingUtilities.invokeAndWait(new Runnable() {
315: public void run() {
316: RepaintManager.currentManager(null).addDirtyRegion(
317: root, 10, 20, Integer.MAX_VALUE,
318: Integer.MAX_VALUE);
319: marker.setOccurred(RepaintManager.currentManager(null)
320: .isCompletelyDirty(root));
321: }
322: });
323: assertTrue(marker.isOccurred());
324: marker.setAuxiliary(null);
325: SwingUtilities.invokeAndWait(new Runnable() {
326: public void run() {
327: RepaintManager.currentManager(null).addDirtyRegion(
328: root, 10, 10, 40, 40);
329: RepaintManager.currentManager(null)
330: .markCompletelyClean(root);
331: marker.setAuxiliary(RepaintManager.currentManager(null)
332: .getDirtyRegion(root));
333: }
334: });
335: assertEquals(new Rectangle(), marker.getAuxiliary());
336: }
337:
338: public void testIsCompletelyDirty() throws Exception {
339: final JPanel root = new JPanel(new BorderLayout());
340: JFrame f = new JFrame();
341: f.getContentPane().add(root);
342: f.setSize(100, 100);
343: f.setVisible(true);
344: waitForNativePaint(f);
345: final Marker marker = new Marker();
346: SwingUtilities.invokeAndWait(new Runnable() {
347: public void run() {
348: RepaintManager.currentManager(null).addDirtyRegion(
349: root, 10, 10, 40, 40);
350: marker.setOccurred(RepaintManager.currentManager(null)
351: .isCompletelyDirty(root));
352: }
353: });
354: assertFalse(marker.isOccurred());
355: marker.setAuxiliary(null);
356: SwingUtilities.invokeAndWait(new Runnable() {
357: public void run() {
358: RepaintManager.currentManager(null).addDirtyRegion(
359: root, 0, 0, 200, 200);
360: marker.setOccurred(RepaintManager.currentManager(null)
361: .isCompletelyDirty(root));
362: }
363: });
364: assertFalse(marker.isOccurred());
365: marker.setAuxiliary(null);
366: SwingUtilities.invokeAndWait(new Runnable() {
367: public void run() {
368: RepaintManager.currentManager(null).addDirtyRegion(
369: root, 0, 0, Integer.MAX_VALUE,
370: Integer.MAX_VALUE);
371: marker.setOccurred(RepaintManager.currentManager(null)
372: .isCompletelyDirty(root));
373: }
374: });
375: assertTrue(marker.isOccurred());
376: marker.setAuxiliary(null);
377: SwingUtilities.invokeAndWait(new Runnable() {
378: public void run() {
379: RepaintManager.currentManager(null)
380: .markCompletelyDirty(root);
381: marker.setOccurred(RepaintManager.currentManager(null)
382: .isCompletelyDirty(root));
383: }
384: });
385: assertTrue(marker.isOccurred());
386: }
387:
388: public void testGetDirtyRegion() throws Exception {
389: final JPanel root = new JPanel(new BorderLayout());
390: JFrame f = new JFrame();
391: f.getContentPane().add(root);
392: f.setSize(100, 100);
393: f.setVisible(true);
394: waitForNativePaint(f);
395: final Marker marker = new Marker();
396: SwingUtilities.invokeAndWait(new Runnable() {
397: public void run() {
398: Rectangle r1 = RepaintManager.currentManager(null)
399: .getDirtyRegion(root);
400: Rectangle r2 = RepaintManager.currentManager(null)
401: .getDirtyRegion(root);
402: marker.setAuxiliary(new Rectangle[] { r1, r2 });
403: }
404: });
405: assertEquals(new Rectangle(), ((Rectangle[]) marker
406: .getAuxiliary())[0]);
407: assertEquals(new Rectangle(), ((Rectangle[]) marker
408: .getAuxiliary())[1]);
409: assertNotSame(((Rectangle[]) marker.getAuxiliary())[0],
410: ((Rectangle[]) marker.getAuxiliary())[1]);
411: marker.setAuxiliary(null);
412: SwingUtilities.invokeAndWait(new Runnable() {
413: public void run() {
414: RepaintManager.currentManager(null).addDirtyRegion(
415: root, 10, 10, 40, 40);
416: marker.setAuxiliary(RepaintManager.currentManager(null)
417: .getDirtyRegion(root));
418: }
419: });
420: assertEquals(new Rectangle(10, 10, 40, 40), marker
421: .getAuxiliary());
422: }
423:
424: public void testPaintDirtyRegions() throws Exception {
425: final Marker rootPaintMarker = new Marker();
426: final Marker rootPaintImmediatelyMarker = new Marker();
427: final JPanel root = new JPanel(null) {
428: private static final long serialVersionUID = 1L;
429:
430: @Override
431: public void paint(final Graphics g) {
432: if (checkRepaintEvent()) {
433: rootPaintMarker.setAuxiliary(g.getClipBounds());
434: }
435: super .paint(g);
436: }
437:
438: @Override
439: public void paintImmediately(final int x, final int y,
440: final int w, final int h) {
441: if (checkRepaintEvent()) {
442: rootPaintImmediatelyMarker
443: .setAuxiliary(new Rectangle(x, y, w, h));
444: }
445: super .paintImmediately(x, y, w, h);
446: }
447:
448: @Override
449: public void paintImmediately(final Rectangle r) {
450: if (checkRepaintEvent()) {
451: rootPaintImmediatelyMarker.setAuxiliary(r);
452: }
453: super .paintImmediately(r);
454: }
455: };
456: final Marker inner1PaintMarker = new Marker();
457: final Marker inner1PaintImmediatelyMarker = new Marker();
458: final JPanel inner1 = new JPanel() {
459: private static final long serialVersionUID = 1L;
460:
461: @Override
462: public void paint(final Graphics g) {
463: if (checkRepaintEvent()) {
464: inner1PaintMarker.setAuxiliary(g.getClipBounds());
465: }
466: super .paint(g);
467: }
468:
469: @Override
470: public void paintImmediately(final int x, final int y,
471: final int w, final int h) {
472: if (checkRepaintEvent()) {
473: inner1PaintImmediatelyMarker
474: .setAuxiliary(new Rectangle(x, y, w, h));
475: }
476: super .paintImmediately(x, y, w, h);
477: }
478:
479: @Override
480: public void paintImmediately(final Rectangle r) {
481: if (checkRepaintEvent()) {
482: inner1PaintImmediatelyMarker.setAuxiliary(r);
483: }
484: super .paintImmediately(r);
485: }
486: };
487: inner1.setBounds(20, 20, 40, 40);
488: final Marker inner2PaintMarker = new Marker();
489: final Marker inner2PaintImmediatelyMarker = new Marker();
490: final JPanel inner2 = new JPanel() {
491: private static final long serialVersionUID = 1L;
492:
493: @Override
494: public void paint(final Graphics g) {
495: if (checkRepaintEvent()) {
496: inner2PaintMarker.setAuxiliary(g.getClipBounds());
497: }
498: super .paint(g);
499: }
500:
501: @Override
502: public void paintImmediately(final int x, final int y,
503: final int w, final int h) {
504: if (checkRepaintEvent()) {
505: inner2PaintImmediatelyMarker
506: .setAuxiliary(new Rectangle(x, y, w, h));
507: }
508: super .paintImmediately(x, y, w, h);
509: }
510:
511: @Override
512: public void paintImmediately(final Rectangle r) {
513: if (checkRepaintEvent()) {
514: inner2PaintImmediatelyMarker.setAuxiliary(r);
515: }
516: super .paintImmediately(r);
517: }
518: };
519: inner2.setBounds(10, 70, 20, 20);
520: final JFrame f = new JFrame();
521: f.getContentPane().add(root);
522: root.add(inner1);
523: root.add(inner2);
524: f.setSize(150, 150);
525: f.setVisible(true);
526: waitForNativePaint(f);
527: final Marker marker = new Marker();
528: SwingUtilities.invokeAndWait(new Runnable() {
529: public void run() {
530: RepaintManager.currentManager(null).addDirtyRegion(
531: root, 10, 10, 40, 40);
532: RepaintManager.currentManager(null).paintDirtyRegions();
533: marker.setAuxiliary(RepaintManager.currentManager(null)
534: .getDirtyRegion(root));
535: }
536: });
537: assertEquals(new Rectangle(), marker.getAuxiliary());
538: f.setVisible(false);
539: f.setVisible(true);
540: waitForNativePaint(f);
541: rootPaintMarker.setAuxiliary(null);
542: inner1PaintMarker.setAuxiliary(null);
543: inner2PaintMarker.setAuxiliary(null);
544: rootPaintImmediatelyMarker.setAuxiliary(null);
545: inner1PaintImmediatelyMarker.setAuxiliary(null);
546: inner2PaintImmediatelyMarker.setAuxiliary(null);
547: SwingUtilities.invokeAndWait(new Runnable() {
548: public void run() {
549: RepaintManager.currentManager(null).addDirtyRegion(
550: root, 10, 10, 40, 40);
551: RepaintManager.currentManager(null).paintDirtyRegions();
552: }
553: });
554: assertEquals(new Rectangle(10, 10, 40, 40), rootPaintMarker
555: .getAuxiliary());
556: assertEquals(new Rectangle(0, 0, 30, 30), inner1PaintMarker
557: .getAuxiliary());
558: assertNull(inner2PaintMarker.getAuxiliary());
559: assertEquals(new Rectangle(10, 10, 40, 40),
560: rootPaintImmediatelyMarker.getAuxiliary());
561: assertNull(inner1PaintImmediatelyMarker.getAuxiliary());
562: assertNull(inner2PaintImmediatelyMarker.getAuxiliary());
563: f.setVisible(false);
564: f.setVisible(true);
565: waitForNativePaint(f);
566: rootPaintMarker.setAuxiliary(null);
567: inner1PaintMarker.setAuxiliary(null);
568: inner2PaintMarker.setAuxiliary(null);
569: rootPaintImmediatelyMarker.setAuxiliary(null);
570: inner1PaintImmediatelyMarker.setAuxiliary(null);
571: inner2PaintImmediatelyMarker.setAuxiliary(null);
572: SwingUtilities.invokeAndWait(new Runnable() {
573: public void run() {
574: RepaintManager.currentManager(null).addDirtyRegion(
575: root, 5, 10, 20, 70);
576: RepaintManager.currentManager(null).paintDirtyRegions();
577: }
578: });
579: assertEquals(new Rectangle(5, 10, 20, 70), rootPaintMarker
580: .getAuxiliary());
581: assertEquals(new Rectangle(0, 0, 5, 40), inner1PaintMarker
582: .getAuxiliary());
583: assertEquals(new Rectangle(0, 0, 15, 10), inner2PaintMarker
584: .getAuxiliary());
585: assertEquals(new Rectangle(5, 10, 20, 70),
586: rootPaintImmediatelyMarker.getAuxiliary());
587: assertNull(inner1PaintImmediatelyMarker.getAuxiliary());
588: assertNull(inner2PaintImmediatelyMarker.getAuxiliary());
589: f.setVisible(false);
590: f.setVisible(true);
591: waitForNativePaint(f);
592: rootPaintMarker.setAuxiliary(null);
593: inner1PaintMarker.setAuxiliary(null);
594: inner2PaintMarker.setAuxiliary(null);
595: rootPaintImmediatelyMarker.setAuxiliary(null);
596: inner1PaintImmediatelyMarker.setAuxiliary(null);
597: inner2PaintImmediatelyMarker.setAuxiliary(null);
598: SwingUtilities.invokeAndWait(new Runnable() {
599: public void run() {
600: RepaintManager.currentManager(null).addDirtyRegion(
601: root, 10, 10, 40, 40);
602: RepaintManager.currentManager(null).addDirtyRegion(
603: inner1, 20, 25, 15, 10);
604: RepaintManager.currentManager(null).paintDirtyRegions();
605: }
606: });
607: assertEquals(new Rectangle(10, 10, 45, 45), rootPaintMarker
608: .getAuxiliary());
609: assertEquals(new Rectangle(0, 0, 35, 35), inner1PaintMarker
610: .getAuxiliary());
611: assertNull(inner2PaintMarker.getAuxiliary());
612: assertEquals(new Rectangle(10, 10, 45, 45),
613: rootPaintImmediatelyMarker.getAuxiliary());
614: assertNull(inner1PaintImmediatelyMarker.getAuxiliary());
615: assertNull(inner2PaintImmediatelyMarker.getAuxiliary());
616: f.setVisible(false);
617: f.setVisible(true);
618: waitForNativePaint(f);
619: rootPaintMarker.setAuxiliary(null);
620: inner1PaintMarker.setAuxiliary(null);
621: inner2PaintMarker.setAuxiliary(null);
622: rootPaintImmediatelyMarker.setAuxiliary(null);
623: inner1PaintImmediatelyMarker.setAuxiliary(null);
624: inner2PaintImmediatelyMarker.setAuxiliary(null);
625: SwingUtilities.invokeAndWait(new Runnable() {
626: public void run() {
627: RepaintManager.currentManager(null).addDirtyRegion(
628: inner1, 10, 10, 20, 20);
629: RepaintManager.currentManager(null).paintDirtyRegions();
630: }
631: });
632: assertNull(rootPaintMarker.getAuxiliary());
633: assertEquals(new Rectangle(10, 10, 20, 20), inner1PaintMarker
634: .getAuxiliary());
635: assertNull(inner2PaintMarker.getAuxiliary());
636: assertNull(rootPaintImmediatelyMarker.getAuxiliary());
637: assertEquals(new Rectangle(10, 10, 20, 20),
638: inner1PaintImmediatelyMarker.getAuxiliary());
639: assertNull(inner2PaintImmediatelyMarker.getAuxiliary());
640: f.setVisible(false);
641: f.setVisible(true);
642: waitForNativePaint(f);
643: rootPaintMarker.setAuxiliary(null);
644: inner1PaintMarker.setAuxiliary(null);
645: inner2PaintMarker.setAuxiliary(null);
646: rootPaintImmediatelyMarker.setAuxiliary(null);
647: inner1PaintImmediatelyMarker.setAuxiliary(null);
648: inner2PaintImmediatelyMarker.setAuxiliary(null);
649: SwingUtilities.invokeAndWait(new Runnable() {
650: public void run() {
651: RepaintManager.currentManager(null).addDirtyRegion(
652: inner1, 10, 10, 20, 20);
653: RepaintManager.currentManager(null).addDirtyRegion(
654: inner2, 5, 5, 10, 10);
655: RepaintManager.currentManager(null).paintDirtyRegions();
656: }
657: });
658: assertNull(rootPaintMarker.getAuxiliary());
659: assertEquals(new Rectangle(10, 10, 20, 20), inner1PaintMarker
660: .getAuxiliary());
661: assertEquals(new Rectangle(5, 5, 10, 10), inner2PaintMarker
662: .getAuxiliary());
663: assertNull(rootPaintImmediatelyMarker.getAuxiliary());
664: assertEquals(new Rectangle(10, 10, 20, 20),
665: inner1PaintImmediatelyMarker.getAuxiliary());
666: assertEquals(new Rectangle(5, 5, 10, 10),
667: inner2PaintImmediatelyMarker.getAuxiliary());
668: f.dispose();
669: }
670:
671: public void testIsDoubleBufferingEnabled() throws Exception {
672: assertTrue(RepaintManager.currentManager(null)
673: .isDoubleBufferingEnabled());
674: RepaintManager.currentManager(null).setDoubleBufferingEnabled(
675: false);
676: assertFalse(RepaintManager.currentManager(null)
677: .isDoubleBufferingEnabled());
678: }
679:
680: public void testGetDoubleBufferMaximumSize() throws Exception {
681: assertEquals(Toolkit.getDefaultToolkit().getScreenSize(),
682: RepaintManager.currentManager(null)
683: .getDoubleBufferMaximumSize());
684: Dimension bufferSize = new Dimension(100, 100);
685: RepaintManager.currentManager(null).setDoubleBufferMaximumSize(
686: bufferSize);
687: assertEquals(bufferSize, RepaintManager.currentManager(null)
688: .getDoubleBufferMaximumSize());
689: }
690:
691: public void testGetOffscreenBuffer() throws Exception {
692: JPanel root = new JPanel();
693: JFrame f = new JFrame();
694: f.getContentPane().add(root);
695: assertNull(RepaintManager.currentManager(null)
696: .getOffscreenBuffer(root, 10, 10));
697: f.pack();
698: Image offscreenImage = RepaintManager.currentManager(null)
699: .getOffscreenBuffer(root, 10, 10);
700: assertNotNull(offscreenImage);
701: assertEquals(10, offscreenImage.getWidth(f));
702: assertEquals(10, offscreenImage.getHeight(f));
703: assertEquals(RepaintManager.currentManager(null)
704: .getOffscreenBuffer(root, 10, 10), RepaintManager
705: .currentManager(null).getOffscreenBuffer(root, 10, 10));
706: assertEquals(RepaintManager.currentManager(null)
707: .getOffscreenBuffer(f.getRootPane(), 10, 10),
708: RepaintManager.currentManager(null).getOffscreenBuffer(
709: root, 10, 10));
710: assertEquals(RepaintManager.currentManager(null)
711: .getOffscreenBuffer(f, 10, 10), RepaintManager
712: .currentManager(null).getOffscreenBuffer(root, 10, 10));
713: Image im10x10 = RepaintManager.currentManager(null)
714: .getOffscreenBuffer(root, 10, 10);
715: Image im10x20 = RepaintManager.currentManager(null)
716: .getOffscreenBuffer(root, 10, 20);
717: Image im20x10 = RepaintManager.currentManager(null)
718: .getOffscreenBuffer(root, 20, 10);
719: Image im20x20 = RepaintManager.currentManager(null)
720: .getOffscreenBuffer(root, 20, 20);
721: assertNotSame(im10x10, im10x20);
722: assertNotSame(im10x20, im20x10);
723: assertNotSame(im10x10, im20x10);
724: assertNotSame(im10x20, im20x20);
725: assertNotSame(im10x10, RepaintManager.currentManager(null)
726: .getOffscreenBuffer(root, 10, 10));
727: assertSame(im20x20, RepaintManager.currentManager(null)
728: .getOffscreenBuffer(root, 10, 10));
729: assertSame(im20x20, RepaintManager.currentManager(null)
730: .getOffscreenBuffer(root, 20, 20));
731: assertSame(im20x20, RepaintManager.currentManager(null)
732: .getOffscreenBuffer(f, 20, 20));
733: assertSame(im20x20, RepaintManager.currentManager(null)
734: .getOffscreenBuffer(new JButton(), 20, 20));
735: Image im30x20 = RepaintManager.currentManager(null)
736: .getOffscreenBuffer(root, 30, 20);
737: assertNotSame(im20x20, im30x20);
738: assertSame(im30x20, RepaintManager.currentManager(null)
739: .getOffscreenBuffer(root, 20, 20));
740: assertNull(RepaintManager.currentManager(null)
741: .getOffscreenBuffer(new JButton(), 50, 20));
742: assertNotSame(im30x20, RepaintManager.currentManager(null)
743: .getOffscreenBuffer(root, 20, 20));
744: offscreenImage = RepaintManager.currentManager(null)
745: .getOffscreenBuffer(root, 10000, 10000);
746: assertNotNull(offscreenImage);
747: assertEquals(RepaintManager.currentManager(null)
748: .getDoubleBufferMaximumSize().width, offscreenImage
749: .getWidth(f));
750: assertEquals(RepaintManager.currentManager(null)
751: .getDoubleBufferMaximumSize().height, offscreenImage
752: .getHeight(f));
753: offscreenImage = RepaintManager.currentManager(null)
754: .getOffscreenBuffer(root, 10000, 10000);
755: assertNotNull(offscreenImage);
756: assertEquals(RepaintManager.currentManager(null)
757: .getDoubleBufferMaximumSize().width, offscreenImage
758: .getWidth(f));
759: assertEquals(RepaintManager.currentManager(null)
760: .getDoubleBufferMaximumSize().height, offscreenImage
761: .getHeight(f));
762: f.dispose();
763: }
764:
765: public void testGetVolatileOffscreenBuffer() throws Exception {
766: JPanel root = new JPanel();
767: JFrame f = new JFrame();
768: f.getContentPane().add(root);
769: f.pack();
770: Image offscreenImage = RepaintManager.currentManager(null)
771: .getVolatileOffscreenBuffer(root, 400, 400);
772: assertNotNull(offscreenImage);
773: assertEquals(400, offscreenImage.getWidth(f));
774: assertEquals(400, offscreenImage.getHeight(f));
775: assertEquals(RepaintManager.currentManager(null)
776: .getVolatileOffscreenBuffer(root, 400, 400),
777: RepaintManager.currentManager(null)
778: .getVolatileOffscreenBuffer(root, 400, 400));
779: assertEquals(RepaintManager.currentManager(null)
780: .getVolatileOffscreenBuffer(f.getRootPane(), 400, 400),
781: RepaintManager.currentManager(null)
782: .getVolatileOffscreenBuffer(root, 400, 400));
783: assertEquals(RepaintManager.currentManager(null)
784: .getVolatileOffscreenBuffer(f, 400, 400),
785: RepaintManager.currentManager(null)
786: .getVolatileOffscreenBuffer(root, 400, 400));
787: Image im400x400 = RepaintManager.currentManager(null)
788: .getVolatileOffscreenBuffer(root, 400, 400);
789: Image im400x420 = RepaintManager.currentManager(null)
790: .getVolatileOffscreenBuffer(root, 400, 420);
791: Image im420x400 = RepaintManager.currentManager(null)
792: .getVolatileOffscreenBuffer(root, 420, 400);
793: Image im420x420 = RepaintManager.currentManager(null)
794: .getVolatileOffscreenBuffer(root, 420, 420);
795: assertNotSame(im400x400, im400x420);
796: assertNotSame(im400x420, im420x400);
797: assertNotSame(im400x420, im420x400);
798: assertNotSame(im400x420, im420x420);
799: assertNotSame(im400x400, RepaintManager.currentManager(null)
800: .getVolatileOffscreenBuffer(root, 400, 400));
801: assertSame(im420x420, RepaintManager.currentManager(null)
802: .getVolatileOffscreenBuffer(root, 400, 400));
803: assertSame(im420x420, RepaintManager.currentManager(null)
804: .getVolatileOffscreenBuffer(root, 420, 420));
805: assertSame(im420x420, RepaintManager.currentManager(null)
806: .getVolatileOffscreenBuffer(f, 420, 420));
807: assertSame(im420x420, RepaintManager.currentManager(null)
808: .getVolatileOffscreenBuffer(new JButton(), 420, 420));
809: Image im430x420 = RepaintManager.currentManager(null)
810: .getVolatileOffscreenBuffer(root, 430, 420);
811: assertNotSame(im420x420, im430x420);
812: assertSame(im430x420, RepaintManager.currentManager(null)
813: .getVolatileOffscreenBuffer(root, 420, 420));
814: assertSame(im430x420, RepaintManager.currentManager(null)
815: .getVolatileOffscreenBuffer(root, 420, 420));
816: offscreenImage = RepaintManager.currentManager(null)
817: .getVolatileOffscreenBuffer(root, 10000, 10000);
818: assertNotNull(offscreenImage);
819: assertEquals(RepaintManager.currentManager(null)
820: .getDoubleBufferMaximumSize().width, offscreenImage
821: .getWidth(f));
822: assertEquals(RepaintManager.currentManager(null)
823: .getDoubleBufferMaximumSize().height, offscreenImage
824: .getHeight(f));
825: f.dispose();
826: }
827:
828: private boolean checkRepaintEvent() {
829: return EventQueue.getCurrentEvent() instanceof InvocationEvent;
830: }
831:
832: private void waitForNativePaint(final Window w) throws Exception {
833: if (!isHarmony()) {
834: return;
835: }
836: int counter = 0;
837: while (!wasPainted(w) && counter++ < 50) {
838: try {
839: Thread.sleep(100);
840: } catch (InterruptedException e) {
841: }
842: }
843: Thread.sleep(1000);
844: }
845:
846: private boolean wasPainted(final Window w) throws Exception {
847: final Marker result = new Marker();
848: SwingUtilities.invokeAndWait(new Runnable() {
849: public void run() {
850: result.setOccurred(ComponentInternals
851: .getComponentInternals().wasPainted(w));
852: }
853: });
854: return result.isOccurred();
855: }
856: }
|