Path: blob/master/test/jdk/javax/swing/JLabel/TestTranslucentLabelText.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*/22/*23* @test24* @bug 825601925* @summary Verifies if JLabel HTML text support translucent text colors26* @run main/manual TestTranslucentLabelText27*/2829import java.awt.BorderLayout;30import java.awt.Color;31import java.awt.Dimension;32import java.awt.Font;33import java.awt.FlowLayout;34import java.awt.event.WindowAdapter;35import java.awt.event.WindowEvent;36import java.util.concurrent.CountDownLatch;37import javax.imageio.ImageIO;38import javax.swing.JFrame;39import javax.swing.JDialog;40import javax.swing.JTextArea;41import javax.swing.JButton;42import javax.swing.JPanel;43import javax.swing.JLabel;44import javax.swing.SwingUtilities;4546public class TestTranslucentLabelText {47private static Color background = new Color(0, 150, 0);48private static Color foreground = new Color(255, 255, 255, 120);49private static Font font = new Font("Sans Serif", Font.PLAIN, 24);50private static JFrame frame;51static boolean testResult;52static CountDownLatch latch;53private static Thread mainThread;54private static boolean testPassed;55private static boolean testGeneratedInterrupt;5657private static void doTest(Runnable action) {58String description59= " A frame with 2 labels will be shown in middle of screen.\n"60+ " Left side label text should be opaque.\n "61+ " Right side label text should be translucent.\n"62+ " If Right side label text is translucent, press PASS else press FAIL";6364final JDialog dialog = new JDialog();65dialog.setTitle("JLabelTranslucentTest");66JTextArea textArea = new JTextArea(description);67textArea.setEditable(false);68final JButton testButton = new JButton("Start Test");69final JButton passButton = new JButton("PASS");70passButton.setEnabled(false);71passButton.addActionListener((e) -> {72dialog.dispose();73frame.dispose();74pass();75});76final JButton failButton = new JButton("FAIL");77failButton.setEnabled(false);78failButton.addActionListener((e) -> {79dialog.dispose();80frame.dispose();81fail();82});83testButton.addActionListener((e) -> {84testButton.setEnabled(false);85action.run();86passButton.setEnabled(true);87failButton.setEnabled(true);88});89JPanel mainPanel = new JPanel(new BorderLayout());90mainPanel.add(textArea, BorderLayout.CENTER);91JPanel buttonPanel = new JPanel(new FlowLayout());92buttonPanel.add(testButton);93buttonPanel.add(passButton);94buttonPanel.add(failButton);95mainPanel.add(buttonPanel, BorderLayout.SOUTH);96dialog.add(mainPanel);97dialog.pack();98dialog.setVisible(true);99dialog.addWindowListener(new WindowAdapter() {100@Override101public void windowClosing(WindowEvent e) {102System.out.println("main dialog closing");103testGeneratedInterrupt = false;104frame.dispose();105mainThread.interrupt();106}107});108}109110public static synchronized void pass() {111testPassed = true;112testGeneratedInterrupt = true;113mainThread.interrupt();114}115116public static synchronized void fail() {117testPassed = false;118testGeneratedInterrupt = true;119mainThread.interrupt();120}121122123private static JLabel create(String text)124{125JLabel label = new JLabel(text);126label.setOpaque(true);127label.setBackground(background);128label.setForeground(foreground);129label.setFont(font);130label.setPreferredSize(new Dimension(200, 40));131frame.add(label);132133return label;134}135136private static void runTest() {137frame = new JFrame();138frame.setUndecorated(true);139frame.setLayout(new FlowLayout());140141//JLabel l1 = create("Test1");142//JLabel opqLabel = create("<html>Test2</html>");143JLabel opqLabel = create("<html><p style=\"color:rgba(255, 0, 0, 1.00)\">TestLabel</p></html>");144JLabel tranLabel = create("<html><p style=\"color:rgba(255, 0, 0, 0.5)\">TestLabel</p></html>");145146frame.pack();147148frame.setLocationRelativeTo(null);149frame.setVisible(true);150frame.toFront();151}152153public static void main(String[] args) throws Exception {154SwingUtilities.invokeAndWait(() -> {155doTest(TestTranslucentLabelText::runTest);156});157mainThread = Thread.currentThread();158try {159Thread.sleep(180000);160} catch (InterruptedException e) {161if (!testPassed && testGeneratedInterrupt) {162throw new RuntimeException("" +163"Label HTML text does not support translucent text colors");164}165}166if (!testGeneratedInterrupt) {167throw new RuntimeException("user has not executed the test");168}169170}171172173}174175176177