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