bug6608456.java revision 14851:980da45565c8
1/* 2 * Copyright (c) 2007, 2016, 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/* 25 * @test 26 * @key headful 27 * 28 * @bug 6608456 29 * @author Igor Kushnirskiy 30 * @modules java.desktop/com.sun.java.swing 31 * @summary tests if delegate RepaintManager gets invoked. 32 */ 33 34import java.awt.*; 35import java.lang.reflect.Method; 36import java.util.concurrent.Callable; 37import java.util.concurrent.FutureTask; 38import java.util.concurrent.TimeUnit; 39 40import javax.swing.JComponent; 41import javax.swing.JButton; 42import javax.swing.JFrame; 43import javax.swing.RepaintManager; 44import javax.swing.SwingUtilities; 45 46 47 48public class bug6608456 { 49 private static final TestFuture testFuture = new TestFuture(); 50 public static void main(String[] args) throws Exception { 51 final JComponent component = invokeAndWait( 52 new Callable<JComponent>() { 53 public JComponent call() throws Exception { 54 RepaintManager.setCurrentManager(new TestRepaintManager()); 55 JFrame frame = new JFrame("test"); 56 frame.setLayout(new FlowLayout()); 57 JButton button = new JButton("default"); 58 59 frame.add(button); 60 button = new JButton("delegate"); 61 if ( ! registerDelegate( 62 button, new TestRepaintManager())) { 63 return null; 64 } 65 frame.add(button); 66 frame.pack(); 67 frame.setVisible(true); 68 return button; 69 } 70 }); 71 if (component == null) { 72 throw new RuntimeException("failed. can not register delegate"); 73 } 74 blockTillDisplayed(component); 75 // trigger repaint for delegate RepaintManager 76 invokeAndWait( 77 new Callable<Void>() { 78 public Void call() { 79 component.repaint(); 80 return null; 81 } 82 }); 83 try { 84 if (testFuture.get(10, TimeUnit.SECONDS)) { 85 // passed 86 } 87 } catch (Exception e) { 88 throw new RuntimeException("failed", e); 89 } finally { 90 JFrame frame = (JFrame) SwingUtilities 91 .getAncestorOfClass(JFrame.class, component); 92 if (frame != null) { 93 frame.dispose(); 94 } 95 } 96 } 97 static class TestRepaintManager extends RepaintManager { 98 @Override 99 public void addDirtyRegion(JComponent c, int x, int y, int w, int h) { 100 if (RepaintManager.currentManager(c) == this) { 101 testFuture.defaultCalled(); 102 } else { 103 testFuture.delegateCalled(); 104 } 105 super.addDirtyRegion(c, x, y, w, h); 106 } 107 } 108 static class TestFuture extends FutureTask<Boolean> { 109 private volatile boolean defaultCalled = false; 110 private volatile boolean delegateCalled = false; 111 public TestFuture() { 112 super(new Callable<Boolean>() { 113 public Boolean call() { 114 return null; 115 } 116 }); 117 } 118 public void defaultCalled() { 119 defaultCalled = true; 120 updateState(); 121 } 122 public void delegateCalled() { 123 delegateCalled = true; 124 updateState(); 125 } 126 private void updateState() { 127 if (defaultCalled && delegateCalled) { 128 set(Boolean.TRUE); 129 } 130 } 131 } 132 133 private static boolean registerDelegate(JComponent c, 134 RepaintManager repaintManager) { 135 boolean rv = false; 136 try { 137 Class<?> clazz = Class.forName("com.sun.java.swing.SwingUtilities3"); 138 Method method = clazz.getMethod("setDelegateRepaintManager", 139 JComponent.class, RepaintManager.class); 140 method.invoke(clazz, c, repaintManager); 141 rv = true; 142 } catch (Exception ignore) { 143 } 144 return rv; 145 } 146 static <T> T invokeAndWait(Callable<T> callable) throws Exception { 147 FutureTask<T> future = new FutureTask<T>(callable); 148 SwingUtilities.invokeLater(future); 149 return future.get(); 150 } 151 152 public static void blockTillDisplayed(Component comp) { 153 Point p = null; 154 while (p == null) { 155 try { 156 p = comp.getLocationOnScreen(); 157 } catch (IllegalStateException e) { 158 try { 159 Thread.sleep(100); 160 } catch (InterruptedException ie) { 161 } 162 } 163 } 164 } 165} 166