1 /*
   2  * Copyright (c) 2011, 2012, 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         }
  63         System.out.println("Test passed.");
  64     }
  65 
  66     public PaintSetEnabledDeadlock() {
  67         super("7108598 test");
  68         setLayout(new GridLayout(1, 2));
  69         addWindowListener(new WindowAdapter() {
  70 
  71             @Override
  72             public void windowClosing(WindowEvent e) {
  73                 panel.stop();
  74                 System.exit(0);
  75             }
  76         });
  77         panel = new TestPanel();
  78         add(panel);
  79         button = new Button("Enable");
  80         button.addMouseListener(new MouseAdapter() {
  81 
  82             @Override
  83             public void mousePressed(MouseEvent e) {
  84                 panel.setEnabled(true);
  85                 panel.sync();
  86                 panel.repaint();
  87             }
  88         });
  89         add(button);
  90     }
  91 }
  92 
  93 class TestPanel extends Panel implements Runnable {
  94 
  95     Image image = null;
  96     Thread thread = null;
  97     volatile boolean active = true;
  98     final Object sync = new Object();
  99     Panel panel = this;
 100 
 101     public TestPanel() {
 102         addMouseListener(new MouseAdapter() {
 103 
 104             @Override
 105             public void mouseReleased(MouseEvent e) {
 106                 synchronized (panel) {
 107                     sync();
 108                     panel.setEnabled(false);
 109                 }
 110                 panel.repaint();
 111             }
 112         });
 113         thread = new Thread(this);
 114         thread.start();
 115     }
 116 
 117     @Override
 118     public void paint(Graphics paramGraphics) {
 119         synchronized (getTreeLock()) {
 120             Rectangle rect = getBounds();
 121             if (image == null) {
 122                 image = createImage(rect.width, rect.height);
 123             }
 124 
 125             if (image != null) {
 126                 paramGraphics.drawImage(image, 0, 0, this);
 127             }
 128         }
 129     }
 130 
 131     @Override
 132     public void run() {
 133         while (active) {
 134             try {
 135                 synchronized (sync) {
 136                     sync.wait();
 137                 }
 138             } catch (InterruptedException ex) {
 139             }
 140             if (active) {
 141                 draw();
 142             }
 143         }
 144     }
 145 
 146     public boolean stop() {
 147         active = false;
 148         try {
 149             sync();
 150             thread.join(1000);
 151             if (thread.isAlive()) {
 152                 thread.interrupt();
 153                 return false;
 154             }
 155         } catch (InterruptedException ex) {
 156             return false;
 157         }
 158         return true;
 159     }
 160 
 161     public void draw() {
 162         synchronized (getTreeLock()) {
 163             if (image != null) {
 164                 Graphics localGraphics = image.getGraphics();
 165                 Dimension size = getSize();
 166                 localGraphics.setColor(isEnabled() ? Color.green : Color.red);
 167                 localGraphics.fillRect(0, 0, size.width, size.height);
 168                 super.paint(localGraphics);
 169                 localGraphics.dispose();
 170                 getTreeLock().notifyAll();
 171             }
 172         }
 173     }
 174 
 175     public void sync() {
 176         synchronized (sync) {
 177             sync.notify();
 178         }
 179     }
 180 }