Path: blob/master/test/jdk/javax/swing/JMenu/8178430/LabelDotTest.java
41153 views
/*1* Copyright (c) 2009, 2017, 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*/2223/**24* @test25* @bug 817843026* @summary JMenu in GridBagLayout flickers when label text shows "..." and27* is updated28* @key headful29* @run main LabelDotTest30*/3132import java.awt.Dimension;33import java.awt.GridBagConstraints;34import java.awt.GridBagLayout;35import java.awt.Robot;3637import java.util.stream.IntStream;3839import javax.swing.SwingUtilities;40import javax.swing.JLabel;41import javax.swing.JMenu;42import javax.swing.JFrame;43import javax.swing.JMenuBar;44import javax.swing.JPanel;45import javax.swing.SwingConstants;4647public class LabelDotTest48{49private final static String longText = "show a very long text to have it " +50"automatically shortened";51private final static String shortText = "show short text";5253private static JFrame frame;54private static JLabel label;55private static JMenu menu;56private static volatile boolean isException = false;5758private static void createUI() {59System.out.println("BEFORE CREATION");60frame = new JFrame();61frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);62frame.setSize(new Dimension(50, 150));63frame.setLocationRelativeTo(null);6465frame.setLayout(new GridBagLayout());66GridBagConstraints c = new GridBagConstraints();67c.fill = GridBagConstraints.BOTH;68c.weightx = 1.0;69c.weighty = 0.0;70c.gridwidth = GridBagConstraints.REMAINDER;7172JMenuBar menuBar = new JMenuBar();73menu = new JMenu("Menu");74menuBar.add(menu);75frame.add(menuBar, c);7677frame.add(new JLabel("Title", SwingConstants.CENTER), c);7879c.weighty = 1.0;80frame.add(new JPanel(new GridBagLayout()), c);81c.weighty = 0.0;8283label = new JLabel(shortText);84frame.add(label, c);8586frame.setVisible(true);87}8889private static void runTest(int iterations) throws Exception{90Robot robot = new Robot();9192IntStream.range(0, iterations).forEach((i) -> {93SwingUtilities.invokeLater(() -> {94if (label.getText().equals(shortText)) {95label.setText(longText);96} else {97label.setText(shortText);98}99/* For a top level menu item, minimum size and the100preferred size should be the same, and should not be101equal to 1. Save the exception state and throw later102once the iterations are completed.103*/104isException = (menu.getMinimumSize().height == 1 &&105!menu.getMinimumSize().equals(menu.getPreferredSize())) ||106isException;107});108robot.waitForIdle();109});110}111112public static void main(String[] args) throws Exception {113try {114SwingUtilities.invokeAndWait(() -> createUI());115runTest(50);116} finally {117if (frame != null) {118SwingUtilities.invokeAndWait(() -> frame.dispose());119}120if (isException)121throw new RuntimeException("Size of Menu bar is not correct.");122}123}124}125126127