test/jdk/javax/swing/JMenu/8178430/LabelDotTest.java
author goetz
Tue, 21 Nov 2017 17:39:04 +0100
changeset 48063 50d61f4b5d1a
parent 47963 ee3fd0a4120d
permissions -rw-r--r--
8191678: [TESTBUG] Add keyword headful in java/awt and javax tests. Summary: Also fix some NPE thrown if run headless. Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47963
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     1
/*
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     2
 * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     4
 *
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     8
 *
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    13
 * accompanied this code).
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    14
 *
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    18
 *
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    21
 * questions.
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    22
 */
48063
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
    23
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
    24
/**
47963
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    25
 * @test
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    26
 * @bug 8178430
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    27
 * @summary JMenu in GridBagLayout flickers when label text shows "..." and
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    28
 * is updated
48063
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
    29
 * @key headful
47963
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    30
 * @run main LabelDotTest
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    31
 */
48063
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
    32
47963
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    33
import java.awt.Dimension;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    34
import java.awt.GridBagConstraints;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    35
import java.awt.GridBagLayout;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    36
import java.awt.Robot;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    37
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    38
import java.util.stream.IntStream;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    39
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    40
import javax.swing.SwingUtilities;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    41
import javax.swing.JLabel;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    42
import javax.swing.JMenu;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    43
import javax.swing.JFrame;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    44
import javax.swing.JMenuBar;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    45
import javax.swing.JPanel;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    46
import javax.swing.SwingConstants;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    47
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    48
public class LabelDotTest
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    49
{
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    50
    private final static String longText = "show a very long text to have it " +
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    51
            "automatically shortened";
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    52
    private final static String shortText = "show short text";
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    53
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    54
    private static JFrame frame;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    55
    private static JLabel label;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    56
    private static JMenu menu;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    57
    private static volatile boolean isException = false;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    58
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    59
    private static void createUI() {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    60
       System.out.println("BEFORE CREATION");
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    61
       frame = new JFrame();
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    62
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    63
       frame.setSize(new Dimension(50, 150));
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    64
       frame.setLocationRelativeTo(null);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    65
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    66
       frame.setLayout(new GridBagLayout());
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    67
       GridBagConstraints c = new GridBagConstraints();
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    68
       c.fill = GridBagConstraints.BOTH;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    69
       c.weightx = 1.0;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    70
       c.weighty = 0.0;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    71
       c.gridwidth = GridBagConstraints.REMAINDER;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    72
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    73
       JMenuBar menuBar = new JMenuBar();
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    74
       menu = new JMenu("Menu");
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    75
       menuBar.add(menu);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    76
       frame.add(menuBar, c);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    77
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    78
       frame.add(new JLabel("Title", SwingConstants.CENTER), c);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    79
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    80
       c.weighty = 1.0;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    81
       frame.add(new JPanel(new GridBagLayout()), c);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    82
       c.weighty = 0.0;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    83
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    84
       label = new JLabel(shortText);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    85
       frame.add(label, c);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    86
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    87
       frame.setVisible(true);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    88
   }
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    89
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    90
   private static void runTest(int iterations) throws Exception{
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    91
        Robot robot = new Robot();
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    92
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    93
        IntStream.range(0, iterations).forEach((i) -> {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    94
                SwingUtilities.invokeLater(() -> {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    95
                    if (label.getText().equals(shortText)) {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    96
                        label.setText(longText);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    97
                    } else {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    98
                        label.setText(shortText);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
    99
                    }
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   100
                    /*  For a top level menu item, minimum size and the
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   101
                        preferred size should be the same, and should not be
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   102
                        equal to 1. Save the exception state and throw later
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   103
                        once the iterations are completed.
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   104
                    */
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   105
                    isException = (menu.getMinimumSize().height == 1 &&
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   106
                        !menu.getMinimumSize().equals(menu.getPreferredSize())) ||
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   107
                        isException;
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   108
                });
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   109
                robot.waitForIdle();
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   110
        });
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   111
   }
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   112
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   113
   public static void main(String[] args) throws Exception {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   114
        try {
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   115
            SwingUtilities.invokeAndWait(() -> createUI());
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   116
            runTest(50);
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   117
        } finally {
48063
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
   118
            if (frame != null) {
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
   119
                SwingUtilities.invokeAndWait(() -> frame.dispose());
50d61f4b5d1a 8191678: [TESTBUG] Add keyword headful in java/awt and javax tests.
goetz
parents: 47963
diff changeset
   120
            }
47963
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   121
            if (isException)
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   122
                throw new RuntimeException("Size of Menu bar is not correct.");
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   123
        }
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   124
   }
ee3fd0a4120d 8178430: JMenu in GridBagLayout flickers when label text shows "..." and is updated
kaddepalli
parents:
diff changeset
   125
}