jdk/test/javax/swing/JTabbedPane/8007563/Test8007563.java
changeset 25118 d237e069dd9c
child 31431 22615e4a6ab7
equal deleted inserted replaced
25117:e0d045874774 25118:d237e069dd9c
       
     1 /*
       
     2  * Copyright (c) 2014, 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.Color;
       
    25 import java.awt.Point;
       
    26 import java.awt.Robot;
       
    27 import java.util.ArrayList;
       
    28 import java.util.concurrent.CountDownLatch;
       
    29 import javax.swing.JFrame;
       
    30 import javax.swing.JLabel;
       
    31 import javax.swing.JTabbedPane;
       
    32 
       
    33 import static javax.swing.UIManager.*;
       
    34 import static javax.swing.SwingUtilities.*;
       
    35 
       
    36 /*
       
    37  * @test
       
    38  * @bug 8007563
       
    39  * @summary Tests JTabbedPane background
       
    40  * @author Sergey Malenkov
       
    41  */
       
    42 
       
    43 public class Test8007563 implements Runnable {
       
    44     private static final ArrayList<String> LIST = new ArrayList<>();
       
    45     private static final LookAndFeelInfo[] INFO = getInstalledLookAndFeels();
       
    46     private static final CountDownLatch LATCH = new CountDownLatch(INFO.length);
       
    47     private static Robot ROBOT;
       
    48 
       
    49     public static void main(String[] args) throws Exception {
       
    50         ROBOT = new Robot();
       
    51         invokeLater(new Test8007563());
       
    52         LATCH.await();
       
    53         if (!LIST.isEmpty()) {
       
    54             throw new Error(LIST.toString());
       
    55         }
       
    56     }
       
    57 
       
    58     private static void addOpaqueError(boolean opaque) {
       
    59         LIST.add(getLookAndFeel().getName() + " opaque=" + opaque);
       
    60     }
       
    61 
       
    62     private static boolean updateLookAndFeel() {
       
    63         int index = (int) LATCH.getCount() - 1;
       
    64         if (index >= 0) {
       
    65             try {
       
    66                 LookAndFeelInfo info = INFO[index];
       
    67                 System.err.println("L&F: " + info.getName());
       
    68                 setLookAndFeel(info.getClassName());
       
    69                 return true;
       
    70             } catch (Exception exception) {
       
    71                 exception.printStackTrace();
       
    72             }
       
    73         }
       
    74         return false;
       
    75     }
       
    76 
       
    77     private JFrame frame;
       
    78     private JTabbedPane pane;
       
    79 
       
    80     public void run() {
       
    81         if (this.frame == null) {
       
    82             if (!updateLookAndFeel()) {
       
    83                 return;
       
    84             }
       
    85             this.pane = new JTabbedPane();
       
    86             this.pane.setOpaque(false);
       
    87             this.pane.setBackground(Color.RED);
       
    88             for (int i = 0; i < 3; i++) {
       
    89                 this.pane.addTab("Tab " + i, new JLabel("Content area " + i));
       
    90             }
       
    91             this.frame = new JFrame(getClass().getSimpleName());
       
    92             this.frame.getContentPane().setBackground(Color.BLUE);
       
    93             this.frame.add(this.pane);
       
    94             this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       
    95             this.frame.setSize(400, 200);
       
    96             this.frame.setLocationRelativeTo(null);
       
    97             this.frame.setVisible(true);
       
    98         } else {
       
    99             Point point = new Point(this.pane.getWidth() - 2, 2);
       
   100             convertPointToScreen(point, this.pane);
       
   101             Color actual = ROBOT.getPixelColor(point.x, point.y);
       
   102 
       
   103             boolean opaque = this.pane.isOpaque();
       
   104             Color expected = opaque
       
   105                     ? this.pane.getBackground()
       
   106                     : this.frame.getContentPane().getBackground();
       
   107 
       
   108             if (!expected.equals(actual)){
       
   109                 addOpaqueError(opaque);
       
   110             }
       
   111             if (!opaque) {
       
   112                 this.pane.setOpaque(true);
       
   113                 this.pane.repaint();
       
   114             } else {
       
   115                 this.frame.dispose();
       
   116                 this.frame = null;
       
   117                 this.pane = null;
       
   118                 LATCH.countDown();
       
   119             }
       
   120 
       
   121         }
       
   122         invokeLater(this);
       
   123     }
       
   124 }