|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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.BorderLayout; |
| 25 | +import java.awt.Component; |
| 26 | +import java.awt.Container; |
| 27 | +import java.awt.Rectangle; |
| 28 | +import java.awt.Robot; |
| 29 | +import java.awt.Point; |
| 30 | +import java.awt.event.InputEvent; |
| 31 | +import java.awt.event.KeyEvent; |
| 32 | + |
| 33 | +import javax.swing.AbstractButton; |
| 34 | +import javax.swing.JFileChooser; |
| 35 | +import javax.swing.JFrame; |
| 36 | +import javax.swing.JTable; |
| 37 | +import javax.swing.JToggleButton; |
| 38 | +import javax.swing.SwingUtilities; |
| 39 | +import javax.swing.WindowConstants; |
| 40 | +import javax.swing.UIManager; |
| 41 | +import javax.swing.table.DefaultTableModel; |
| 42 | + |
| 43 | +import java.util.function.Predicate; |
| 44 | + |
| 45 | +/* |
| 46 | + * @test |
| 47 | + * @bug 6967482 |
| 48 | + * @key headful |
| 49 | + * @summary Test to check if TAB is working on JTable after JFileChooser is |
| 50 | + * closed |
| 51 | + * @run main TABTestONFCExit |
| 52 | + */ |
| 53 | + |
| 54 | +public class TABTestONFCExit { |
| 55 | + private static JTable table; |
| 56 | + private static JFileChooser fc; |
| 57 | + private static JFrame frame; |
| 58 | + private static Robot robot; |
| 59 | + private static volatile Point loc; |
| 60 | + private static volatile Rectangle rect; |
| 61 | + private static volatile int selectedColumnBeforeTabPress; |
| 62 | + private static volatile int selectedColumnAfterTabPress; |
| 63 | + |
| 64 | + public static void main(String[] args) throws Exception { |
| 65 | + robot = new Robot(); |
| 66 | + robot.setAutoDelay(50); |
| 67 | + UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); |
| 68 | + try { |
| 69 | + SwingUtilities.invokeAndWait(TABTestONFCExit::initialize); |
| 70 | + robot.waitForIdle(); |
| 71 | + robot.delay(100); |
| 72 | + |
| 73 | + SwingUtilities.invokeAndWait(TABTestONFCExit::clickDetails); |
| 74 | + robot.waitForIdle(); |
| 75 | + robot.delay(100); |
| 76 | + |
| 77 | + SwingUtilities.invokeAndWait(() -> { |
| 78 | + loc = table.getLocationOnScreen(); |
| 79 | + rect = table.getCellRect(0, 0, true); |
| 80 | + }); |
| 81 | + |
| 82 | + onClick(loc, rect); |
| 83 | + |
| 84 | + SwingUtilities.invokeAndWait(() -> |
| 85 | + selectedColumnBeforeTabPress = table.getSelectedColumn()); |
| 86 | + |
| 87 | + robot.keyPress(KeyEvent.VK_TAB); |
| 88 | + robot.keyRelease(KeyEvent.VK_TAB); |
| 89 | + robot.waitForIdle(); |
| 90 | + robot.delay(100); |
| 91 | + |
| 92 | + SwingUtilities.invokeAndWait(() -> |
| 93 | + selectedColumnAfterTabPress = table.getSelectedColumn()); |
| 94 | + robot.waitForIdle(); |
| 95 | + robot.delay(100); |
| 96 | + |
| 97 | + if (selectedColumnAfterTabPress == selectedColumnBeforeTabPress) { |
| 98 | + throw new RuntimeException("TAB failed to move cell!"); |
| 99 | + } |
| 100 | + System.out.println("Test Passed" ); |
| 101 | + |
| 102 | + } finally { |
| 103 | + SwingUtilities.invokeAndWait(() -> { |
| 104 | + if (frame != null) { |
| 105 | + frame.dispose(); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static void onClick(Point loc, Rectangle cellRect) { |
| 112 | + robot.mouseMove(loc.x + cellRect.x + cellRect.width / 2, |
| 113 | + loc.y + cellRect.y + cellRect.height / 2); |
| 114 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 115 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 116 | + robot.waitForIdle(); |
| 117 | + robot.delay(100); |
| 118 | + } |
| 119 | + |
| 120 | + private static void initialize() { |
| 121 | + frame = new JFrame("Tab Test"); |
| 122 | + fc = new JFileChooser(); |
| 123 | + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| 124 | + frame.add(getJTable(), BorderLayout.NORTH); |
| 125 | + frame.add(fc, BorderLayout.SOUTH); |
| 126 | + frame.pack(); |
| 127 | + frame.setVisible(true); |
| 128 | + } |
| 129 | + |
| 130 | + private static JTable getJTable() { |
| 131 | + if (table == null) { |
| 132 | + table = new JTable(); |
| 133 | + table.setModel(new DefaultTableModel(5, 5)); |
| 134 | + } |
| 135 | + return table; |
| 136 | + } |
| 137 | + private static void clickDetails() { |
| 138 | + AbstractButton details = findDetailsButton(fc); |
| 139 | + if (details == null) { |
| 140 | + throw new Error("Couldn't find 'Details' button in JFileChooser"); |
| 141 | + } |
| 142 | + details.doClick(); |
| 143 | + } |
| 144 | + |
| 145 | + private static AbstractButton findDetailsButton(final Container container) { |
| 146 | + Component result = findComponent(container, |
| 147 | + c -> c instanceof JToggleButton button |
| 148 | + && "Details".equals(button.getToolTipText())); |
| 149 | + return (AbstractButton) result; |
| 150 | + } |
| 151 | + |
| 152 | + private static Component findComponent(final Container container, |
| 153 | + final Predicate<Component> predicate) { |
| 154 | + for (Component child : container.getComponents()) { |
| 155 | + if (predicate.test(child)) { |
| 156 | + return child; |
| 157 | + } |
| 158 | + if (child instanceof Container cont && cont.getComponentCount() > 0) { |
| 159 | + Component result = findComponent(cont, predicate); |
| 160 | + if (result != null) { |
| 161 | + return result; |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return null; |
| 166 | + } |
| 167 | +} |
0 commit comments