1 /*
   2  * Copyright (c) 2011, 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  * @bug 7108598
  27  * @summary Container.paint/KeyboardFocusManager.clearMostRecentFocusOwner methods deadlock
  28  * @library ../../regtesthelpers
  29  * @author Oleg Pekhovskiy
  30  * @build Util
  31  * @run main/timeout=20 PaintSetEnabledDeadlock
  32  */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import test.java.awt.regtesthelpers.Util;
  37 
  38 public class PaintSetEnabledDeadlock extends Frame {
  39 
  40     final TestPanel panel;
  41     final Button button;
  42 
  43     public static void main(String[] args) {
  44         PaintSetEnabledDeadlock frame = new PaintSetEnabledDeadlock();
  45         frame.setSize(200, 200);
  46         frame.setVisible(true);
  47 
  48         Robot robot = Util.createRobot();
  49         robot.setAutoDelay(100);
  50         robot.setAutoWaitForIdle(true);
  51 
  52         for (int i = 0; i < 20; ++i) {
  53             Util.clickOnComp(frame.panel, robot);
  54             Util.clickOnComp(frame.button, robot);
  55         }
  56 
  57         boolean ret = frame.panel.stop();
  58         frame.dispose();
  59 
  60         if(!ret)
  61             throw new RuntimeException("Test failed!");
  62         System.out.println("Test passed.");
  63     }
  64 
  65     public PaintSetEnabledDeadlock() {
  66         super("7108598 test");
  67         setLayout(new GridLayout(1, 2));
  68         addWindowListener(new WindowAdapter() {
  69 
  70             @Override
  71             public void windowClosing(WindowEvent e) {
  72                 panel.stop();
  73                 System.exit(0);
  74             }
  75         });
  76         panel = new TestPanel();
  77         add(panel);
  78         button = new Button("Enable");
  79         button.addMouseListener(new MouseAdapter() {
  80 
  81             @Override
  82             public void mousePressed(MouseEvent e) {
  83                 panel.setEnabled(true);
  84                 panel.sync();
  85                 panel.repaint();
  86             }
  87         });
  88         add(button);
  89     }
  90 }
  91 
  92 class TestPanel extends Panel implements Runnable {
  93 
  94     Image image = null;
  95     Thread thread = null;
  96     volatile boolean active = true;
  97     final Object sync = new Object();
  98     Panel panel = this;
  99 
 100     public TestPanel() {
 101         addMouseListener(new MouseAdapter() {
 102 
 103             @Override
 104             public void mouseReleased(MouseEvent e) {
 105                 synchronized (panel) {
 106                     sync();
 107                     panel.setEnabled(false);
 108                 }
 109                 panel.repaint();
 110             }
 111         });
 112         thread = new Thread(this);
 113         thread.start();
 114     }
 115 
 116     @Override
 117     public void paint(Graphics paramGraphics) {
 118         synchronized (getTreeLock()) {
 119             Rectangle rect = getBounds();
 120             if (image == null) {
 121                 image = createImage(rect.width, rect.height);
 122             }
 123 
 124             if (image != null) {
 125                 paramGraphics.drawImage(image, 0, 0, this);
 126             }
 127         }
 128     }
 129 
 130     @Override
 131     public void run() {
 132         while (active) {
 133             try {
 134                 synchronized (sync) {
 135                     sync.wait();
 136                 }
 137             } catch (InterruptedException ex) {
 138             }
 139             if (active) {
 140                 draw();
 141             }
 142         }
 143     }
 144 
 145     public boolean stop() {
 146         active = false;
 147         try {
 148             sync();
 149             thread.join(1000);
 150             if (thread.isAlive()) {
 151                 thread.interrupt();
 152                 return false;
 153             }
 154         } catch (InterruptedException ex) {
 155             return false;
 156         }
 157         return true;
 158     }
 159 
 160     public void draw() {
 161         synchronized (getTreeLock()) {
 162             if (image != null) {
 163                 Graphics localGraphics = image.getGraphics();
 164                 Dimension size = getSize();
 165                 localGraphics.setColor(isEnabled() ? Color.green : Color.red);
 166                 localGraphics.fillRect(0, 0, size.width, size.height);
 167                 super.paint(localGraphics);
 168                 localGraphics.dispose();
 169                 getTreeLock().notifyAll();
 170             }
 171         }
 172     }
 173 
 174     public void sync() {
 175         synchronized (sync) {
 176             sync.notify();
 177         }
 178     }
 179 }