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