Path: blob/master/test/jdk/javax/swing/JTabbedPane/TestBackgroundScrollPolicy.java
41149 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.util.ArrayList;25import java.util.concurrent.CountDownLatch;26import javax.swing.JFrame;27import javax.swing.JLabel;28import javax.swing.JTabbedPane;29import javax.swing.UIManager;30import javax.swing.SwingUtilities;31import javax.swing.UnsupportedLookAndFeelException;3233/*34* @test35* @key headful36* @bug 8172269 824455737* @summary Tests JTabbedPane background for SCROLL_TAB_LAYOUT38* @run main TestBackgroundScrollPolicy39*/4041public class TestBackgroundScrollPolicy {42private static Robot ROBOT;4344public static void main(String[] args) throws Exception {45ROBOT = new Robot();46ROBOT.setAutoWaitForIdle(true);47ROBOT.setAutoDelay(100);48for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {49System.out.println("Testing L&F: " + laf.getClassName());50SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));51try {52SwingUtilities.invokeAndWait(() -> createGUI());53ROBOT.waitForIdle();54ROBOT.delay(1000);55SwingUtilities.invokeAndWait(() -> test(laf));56ROBOT.delay(2000);57} finally {58if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());59}60ROBOT.delay(1000);61}62}6364private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {65try {66UIManager.setLookAndFeel(laf.getClassName());67} catch (UnsupportedLookAndFeelException ignored) {68System.out.println("Unsupported L&F: " + laf.getClassName());69} catch (ClassNotFoundException | InstantiationException70| IllegalAccessException e) {71throw new RuntimeException(e);72}73}7475private static void addOpaqueError(UIManager.LookAndFeelInfo laf, boolean opaque) {76throw new RuntimeException(laf.getClassName() + " background color wrong for opaque=" + opaque);77}7879private static JFrame frame;80private static JTabbedPane pane;8182public static void createGUI() {83pane = new JTabbedPane();84pane.setOpaque(true);85pane.setBackground(Color.RED);86for (int i = 0; i < 3; i++) {87pane.addTab("Tab " + i, new JLabel("Content area " + i));88}89pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);90pane.repaint();91frame = new JFrame();92frame.getContentPane().setBackground(Color.BLUE);93frame.add(pane);94frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);95frame.setSize(400, 200);96frame.setLocationRelativeTo(null);97frame.setVisible(true);98frame.toFront();99}100101public static void test(UIManager.LookAndFeelInfo laf) {102Point point = new Point(pane.getWidth() - 2, 2);103SwingUtilities.convertPointToScreen(point, pane);104Color actual = ROBOT.getPixelColor(point.x, point.y);105106boolean opaque = pane.isOpaque();107Color expected = opaque108? pane.getBackground()109: frame.getContentPane().getBackground();110111if (!expected.equals(actual)){112System.out.println("expected " + expected + " actual " + actual);113addOpaqueError(laf, opaque);114}115116}117}118119120121