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