jdk/test/javax/swing/plaf/metal/MetalUtils/bug6190373.java
changeset 33854 e89a68072d5b
equal deleted inserted replaced
33853:a26aa530a23a 33854:e89a68072d5b
       
     1 /*
       
     2  * Copyright (c) 2005, 2015, 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 import java.awt.Graphics;
       
    25 import java.awt.image.BufferedImage;
       
    26 import java.util.concurrent.CyclicBarrier;
       
    27 
       
    28 import javax.swing.JButton;
       
    29 import javax.swing.SwingUtilities;
       
    30 import javax.swing.UIManager;
       
    31 import javax.swing.UnsupportedLookAndFeelException;
       
    32 
       
    33 import sun.awt.AppContext;
       
    34 import sun.awt.SunToolkit;
       
    35 
       
    36 import static javax.swing.UIManager.getInstalledLookAndFeels;
       
    37 
       
    38 /**
       
    39  * @test
       
    40  * @bug 6190373
       
    41  * @summary Tests 6190373
       
    42  * @author Scott Violet
       
    43  * @modules java.desktop/sun.awt
       
    44  */
       
    45 public final class bug6190373 {
       
    46 
       
    47     private static AppContext app1;
       
    48     private static AppContext app2;
       
    49     private static final int LOOP_COUNT = 10000;
       
    50     private static final CyclicBarrier barrier = new CyclicBarrier(2);
       
    51 
       
    52     public static void main(final String[] args) throws Exception {
       
    53         final Thread t1 = new Thread(new ThreadGroup("firstGroup"), () -> {
       
    54             app1 = SunToolkit.createNewAppContext();
       
    55             test(true);
       
    56         });
       
    57         final Thread t2 = new Thread(new ThreadGroup("secondGroup"), () -> {
       
    58             app2 = SunToolkit.createNewAppContext();
       
    59             test(false);
       
    60         });
       
    61 
       
    62         t1.start();
       
    63         t2.start();
       
    64         t1.join();
       
    65         t2.join();
       
    66         app1.dispose();
       
    67         app2.dispose();
       
    68     }
       
    69 
       
    70     private static void test(final boolean lock) {
       
    71         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
       
    72             try {
       
    73                 SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
       
    74                 barrier.await();
       
    75                 SwingUtilities.invokeAndWait(() -> slam(lock));
       
    76                 barrier.await();
       
    77             } catch (final Exception e) {
       
    78                 throw new RuntimeException(e);
       
    79             }
       
    80         }
       
    81     }
       
    82 
       
    83     private static void slam(final boolean lock) {
       
    84         JButton button = new JButton("HI");
       
    85         button.setSize(100, 100);
       
    86         BufferedImage image = new BufferedImage(100, 100,
       
    87                                                 BufferedImage.TYPE_INT_RGB);
       
    88         for (int i = 0; i < LOOP_COUNT; i++) {
       
    89             Graphics g = image.getGraphics();
       
    90             if (lock) {
       
    91                 synchronized (button.getTreeLock()) {
       
    92                     button.paint(g);
       
    93                 }
       
    94             } else {
       
    95                 button.paint(g);
       
    96             }
       
    97             g.dispose();
       
    98         }
       
    99     }
       
   100 
       
   101     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
       
   102         try {
       
   103             UIManager.setLookAndFeel(laf.getClassName());
       
   104             System.out.println("LookAndFeel: " + laf.getClassName());
       
   105         } catch (ClassNotFoundException | InstantiationException |
       
   106                 UnsupportedLookAndFeelException | IllegalAccessException e) {
       
   107             throw new RuntimeException(e);
       
   108         }
       
   109     }
       
   110 }