615N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
615N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
615N/A *
615N/A * This code is free software; you can redistribute it and/or modify it
615N/A * under the terms of the GNU General Public License version 2 only, as
615N/A * published by the Free Software Foundation.
615N/A *
615N/A * This code is distributed in the hope that it will be useful, but WITHOUT
615N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
615N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
615N/A * version 2 for more details (a copy is included in the LICENSE file that
615N/A * accompanied this code).
615N/A *
615N/A * You should have received a copy of the GNU General Public License version
615N/A * 2 along with this work; if not, write to the Free Software Foundation,
615N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
615N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
615N/A */
615N/A
615N/A/* @test
615N/A *
615N/A * @bug 6608456
615N/A * @author Igor Kushnirskiy
615N/A * @summary tests if delegate RepaintManager gets invoked.
615N/A */
615N/A
615N/Aimport java.awt.*;
615N/Aimport java.lang.reflect.Method;
615N/Aimport java.util.concurrent.Callable;
615N/Aimport java.util.concurrent.FutureTask;
615N/Aimport java.util.concurrent.TimeUnit;
615N/A
615N/Aimport javax.swing.JComponent;
615N/Aimport javax.swing.JButton;
615N/Aimport javax.swing.JFrame;
615N/Aimport javax.swing.RepaintManager;
615N/Aimport javax.swing.SwingUtilities;
615N/A
615N/A
615N/A
615N/Apublic class bug6608456 {
615N/A private static final TestFuture testFuture = new TestFuture();
615N/A public static void main(String[] args) throws Exception {
615N/A final JComponent component = invokeAndWait(
615N/A new Callable<JComponent>() {
615N/A public JComponent call() throws Exception {
615N/A RepaintManager.setCurrentManager(new TestRepaintManager());
615N/A JFrame frame = new JFrame("test");
615N/A frame.setLayout(new FlowLayout());
615N/A JButton button = new JButton("default");
615N/A
615N/A frame.add(button);
615N/A button = new JButton("delegate");
615N/A if ( ! registerDelegate(
615N/A button, new TestRepaintManager())) {
615N/A return null;
615N/A }
615N/A frame.add(button);
615N/A frame.pack();
615N/A frame.setVisible(true);
615N/A return button;
615N/A }
615N/A });
615N/A if (component == null) {
615N/A throw new RuntimeException("failed. can not register delegate");
615N/A }
615N/A blockTillDisplayed(component);
615N/A // trigger repaint for delegate RepaintManager
615N/A invokeAndWait(
615N/A new Callable<Void>() {
615N/A public Void call() {
615N/A component.repaint();
615N/A return null;
615N/A }
615N/A });
615N/A try {
615N/A if (testFuture.get(10, TimeUnit.SECONDS)) {
615N/A // passed
615N/A }
615N/A } catch (Exception e) {
615N/A throw new RuntimeException("failed", e);
615N/A } finally {
615N/A JFrame frame = (JFrame) SwingUtilities
615N/A .getAncestorOfClass(JFrame.class, component);
615N/A if (frame != null) {
615N/A frame.dispose();
615N/A }
615N/A }
615N/A }
615N/A static class TestRepaintManager extends RepaintManager {
615N/A @Override
615N/A public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
615N/A if (RepaintManager.currentManager(c) == this) {
615N/A testFuture.defaultCalled();
615N/A } else {
615N/A testFuture.delegateCalled();
615N/A }
615N/A super.addDirtyRegion(c, x, y, w, h);
615N/A }
615N/A }
615N/A static class TestFuture extends FutureTask<Boolean> {
615N/A private volatile boolean defaultCalled = false;
615N/A private volatile boolean delegateCalled = false;
615N/A public TestFuture() {
615N/A super(new Callable<Boolean>() {
615N/A public Boolean call() {
615N/A return null;
615N/A }
615N/A });
615N/A }
615N/A public void defaultCalled() {
615N/A defaultCalled = true;
615N/A updateState();
615N/A }
615N/A public void delegateCalled() {
615N/A delegateCalled = true;
615N/A updateState();
615N/A }
615N/A private void updateState() {
615N/A if (defaultCalled && delegateCalled) {
615N/A set(Boolean.TRUE);
615N/A }
615N/A }
615N/A }
615N/A
615N/A private static boolean registerDelegate(JComponent c,
615N/A RepaintManager repaintManager) {
615N/A boolean rv = false;
615N/A try {
615N/A Class<?> clazz = Class.forName("com.sun.java.swing.SwingUtilities3");
615N/A Method method = clazz.getMethod("setDelegateRepaintManager",
615N/A JComponent.class, RepaintManager.class);
615N/A method.invoke(clazz, c, repaintManager);
615N/A rv = true;
615N/A } catch (Exception ignore) {
615N/A }
615N/A return rv;
615N/A }
615N/A static <T> T invokeAndWait(Callable<T> callable) throws Exception {
615N/A FutureTask<T> future = new FutureTask<T>(callable);
615N/A SwingUtilities.invokeLater(future);
615N/A return future.get();
615N/A }
615N/A
615N/A public static void blockTillDisplayed(Component comp) {
615N/A Point p = null;
615N/A while (p == null) {
615N/A try {
615N/A p = comp.getLocationOnScreen();
615N/A } catch (IllegalStateException e) {
615N/A try {
615N/A Thread.sleep(100);
615N/A } catch (InterruptedException ie) {
615N/A }
615N/A }
615N/A }
615N/A }
615N/A}