1 /* 2 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * 26 * @bug 6608456 27 * @author Igor Kushnirskiy 28 * @modules java.desktop/com.sun.java.swing 29 * @summary tests if delegate RepaintManager gets invoked. 30 */ 31 32 import java.awt.*; 33 import java.lang.reflect.Method; 34 import java.util.concurrent.Callable; 35 import java.util.concurrent.FutureTask; 36 import java.util.concurrent.TimeUnit; 37 38 import javax.swing.JComponent; 39 import javax.swing.JButton; 40 import javax.swing.JFrame; 41 import javax.swing.RepaintManager; 42 import javax.swing.SwingUtilities; 43 44 45 46 public class bug6608456 { 47 private static final TestFuture testFuture = new TestFuture(); 48 public static void main(String[] args) throws Exception { 49 final JComponent component = invokeAndWait( 50 new Callable<JComponent>() { 51 public JComponent call() throws Exception { 52 RepaintManager.setCurrentManager(new TestRepaintManager()); 53 JFrame frame = new JFrame("test"); 54 frame.setLayout(new FlowLayout()); 55 JButton button = new JButton("default"); 56 57 frame.add(button); 58 button = new JButton("delegate"); 59 if ( ! registerDelegate( 60 button, new TestRepaintManager())) { 61 return null; 62 } 63 frame.add(button); 64 frame.pack(); 65 frame.setVisible(true); 66 return button; 67 } 68 }); 69 if (component == null) { 70 throw new RuntimeException("failed. can not register delegate"); 71 } 72 blockTillDisplayed(component); 73 // trigger repaint for delegate RepaintManager 74 invokeAndWait( 75 new Callable<Void>() { 76 public Void call() { 77 component.repaint(); 78 return null; 79 } 80 }); 81 try { 82 if (testFuture.get(10, TimeUnit.SECONDS)) { 83 // passed 84 } 85 } catch (Exception e) { 86 throw new RuntimeException("failed", e); 87 } finally { 88 JFrame frame = (JFrame) SwingUtilities 89 .getAncestorOfClass(JFrame.class, component); 90 if (frame != null) { 91 frame.dispose(); 92 } 93 } 94 } 95 static class TestRepaintManager extends RepaintManager { 96 @Override 97 public void addDirtyRegion(JComponent c, int x, int y, int w, int h) { 98 if (RepaintManager.currentManager(c) == this) { 99 testFuture.defaultCalled(); 100 } else { 101 testFuture.delegateCalled(); 102 } 103 super.addDirtyRegion(c, x, y, w, h); 104 } 105 } 106 static class TestFuture extends FutureTask<Boolean> { 107 private volatile boolean defaultCalled = false; 108 private volatile boolean delegateCalled = false; 109 public TestFuture() { 110 super(new Callable<Boolean>() { 111 public Boolean call() { 112 return null; 113 } 114 }); 115 } 116 public void defaultCalled() { 117 defaultCalled = true; 118 updateState(); 119 } 120 public void delegateCalled() { 121 delegateCalled = true; 122 updateState(); 123 } 124 private void updateState() { 125 if (defaultCalled && delegateCalled) { 126 set(Boolean.TRUE); 127 } 128 } 129 } 130 131 private static boolean registerDelegate(JComponent c, 132 RepaintManager repaintManager) { 133 boolean rv = false; 134 try { 135 Class<?> clazz = Class.forName("com.sun.java.swing.SwingUtilities3"); 136 Method method = clazz.getMethod("setDelegateRepaintManager", 137 JComponent.class, RepaintManager.class); 138 method.invoke(clazz, c, repaintManager); 139 rv = true; 140 } catch (Exception ignore) { 141 } 142 return rv; 143 } 144 static <T> T invokeAndWait(Callable<T> callable) throws Exception { 145 FutureTask<T> future = new FutureTask<T>(callable); 146 SwingUtilities.invokeLater(future); 147 return future.get(); 148 } 149 150 public static void blockTillDisplayed(Component comp) { 151 Point p = null; 152 while (p == null) { 153 try { 154 p = comp.getLocationOnScreen(); 155 } catch (IllegalStateException e) { 156 try { 157 Thread.sleep(100); 158 } catch (InterruptedException ie) { 159 } 160 } 161 } 162 } 163 }