|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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 | +/* @test |
| 25 | + @bug 8277922 |
| 26 | + @key headful |
| 27 | + @summary TableCellRenderer of JTable cell with Boolean data should not |
| 28 | + support any AccessibleAction. |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.AWTException; |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.Container; |
| 34 | +import java.awt.Dimension; |
| 35 | +import java.awt.Robot; |
| 36 | +import java.lang.reflect.InvocationTargetException; |
| 37 | +import javax.accessibility.Accessible; |
| 38 | +import javax.accessibility.AccessibleAction; |
| 39 | +import javax.accessibility.AccessibleContext; |
| 40 | +import javax.accessibility.AccessibleTable; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JScrollPane; |
| 43 | +import javax.swing.JTable; |
| 44 | +import javax.swing.SwingUtilities; |
| 45 | +import javax.swing.table.DefaultTableModel; |
| 46 | +import javax.swing.table.TableCellRenderer; |
| 47 | + |
| 48 | +public class BooleanRendererHasAccessibleActionTest { |
| 49 | + private volatile JFrame frame; |
| 50 | + private volatile JTable table; |
| 51 | + |
| 52 | + public static void main(String[] args) throws InterruptedException, |
| 53 | + InvocationTargetException, AWTException { |
| 54 | + final BooleanRendererHasAccessibleActionTest test = |
| 55 | + new BooleanRendererHasAccessibleActionTest(); |
| 56 | + |
| 57 | + try { |
| 58 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 59 | + @Override |
| 60 | + public void run() { |
| 61 | + test.createGUI(); |
| 62 | + } |
| 63 | + }); |
| 64 | + Robot robot = new Robot(); |
| 65 | + robot.waitForIdle(); |
| 66 | + |
| 67 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + test.runTest(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } finally { |
| 74 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 75 | + @Override |
| 76 | + public void run() { |
| 77 | + test.dispose(); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void createGUI() { |
| 84 | + frame = new JFrame("BooleanRendererHasAccessibleActionTest"); |
| 85 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 86 | + Container content = frame.getContentPane(); |
| 87 | + content.setLayout(new BorderLayout()); |
| 88 | + |
| 89 | + String[] tblColNames = {"Column 1", "Column 2", "Column 3"}; |
| 90 | + Object[][] tblData = { |
| 91 | + {Boolean.TRUE, "Text 1", Boolean.FALSE}, |
| 92 | + {Boolean.FALSE, "Text 2", Boolean.TRUE} |
| 93 | + }; |
| 94 | + final DefaultTableModel tblModel = new DefaultTableModel( |
| 95 | + tblData, tblColNames) { |
| 96 | + @Override |
| 97 | + public Class<?> getColumnClass(int column) { |
| 98 | + return getValueAt(0, column).getClass(); |
| 99 | + } |
| 100 | + }; |
| 101 | + table = new JTable(tblModel); |
| 102 | + table.setPreferredScrollableViewportSize(new Dimension(400, 100)); |
| 103 | + |
| 104 | + JScrollPane tblScroller = new JScrollPane(table); |
| 105 | + tblScroller.setHorizontalScrollBarPolicy( |
| 106 | + JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); |
| 107 | + tblScroller.setVerticalScrollBarPolicy( |
| 108 | + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS |
| 109 | + ); |
| 110 | + content.add(tblScroller, BorderLayout.CENTER); |
| 111 | + |
| 112 | + frame.pack(); |
| 113 | + frame.setVisible(true); |
| 114 | + } |
| 115 | + |
| 116 | + private void dispose() { |
| 117 | + if (frame != null) { |
| 118 | + frame.dispose(); |
| 119 | + frame = null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void runTest() { |
| 124 | + if (table == null) { |
| 125 | + throw new RuntimeException("'table' should not be null"); |
| 126 | + } |
| 127 | + |
| 128 | + testAccessibleActionInCellRenderer(0, 0, true); |
| 129 | + testAccessibleActionInCellRenderer(1, 0, true); |
| 130 | + testAccessibleActionInCellRenderer(0, 2, true); |
| 131 | + testAccessibleActionInCellRenderer(1, 2, true); |
| 132 | + |
| 133 | + testAccessibleActionInCell(0, 0, true); |
| 134 | + testAccessibleActionInCell(1, 0, true); |
| 135 | + testAccessibleActionInCell(0, 2, true); |
| 136 | + testAccessibleActionInCell(1, 2, true); |
| 137 | + |
| 138 | + System.out.println("Test passed."); |
| 139 | + } |
| 140 | + |
| 141 | + private void testAccessibleActionInCellRenderer(int row, int column, |
| 142 | + boolean shouldBeNull) { |
| 143 | + System.out.println(String.format( |
| 144 | + "testAccessibleActionInCellRenderer():" + |
| 145 | + " row='%d', column='%d', shouldBeNull='%b'", |
| 146 | + row, column, shouldBeNull)); |
| 147 | + |
| 148 | + TableCellRenderer cellRenderer = table.getCellRenderer(row, column); |
| 149 | + if (!(cellRenderer instanceof Accessible)) { |
| 150 | + throw new RuntimeException("'cellRenderer' is not Accessible"); |
| 151 | + } |
| 152 | + |
| 153 | + AccessibleContext cellRendererAc = |
| 154 | + ((Accessible) cellRenderer).getAccessibleContext(); |
| 155 | + if (cellRendererAc == null) { |
| 156 | + throw new RuntimeException("'cellRendererAc' should not be null"); |
| 157 | + } |
| 158 | + |
| 159 | + AccessibleAction cellRendererAa = cellRendererAc.getAccessibleAction(); |
| 160 | + if ((shouldBeNull && (cellRendererAa != null)) || |
| 161 | + (!shouldBeNull && (cellRendererAa == null))) { |
| 162 | + throw new RuntimeException( |
| 163 | + "Test failed. 'cellRendererAa' is not as should be"); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void testAccessibleActionInCell(int row, int column, |
| 168 | + boolean shouldBeNull) { |
| 169 | + System.out.println(String.format("testAccessibleActionInCell():" + |
| 170 | + " row='%d', column='%d', shouldBeNull='%b'", |
| 171 | + row, column, shouldBeNull)); |
| 172 | + |
| 173 | + AccessibleContext tblAc = table.getAccessibleContext(); |
| 174 | + AccessibleTable accessibleTbl = tblAc.getAccessibleTable(); |
| 175 | + if (accessibleTbl == null) { |
| 176 | + throw new RuntimeException("'accessibleTbl' should not be null"); |
| 177 | + } |
| 178 | + |
| 179 | + Accessible cellAccessible = accessibleTbl.getAccessibleAt(row, column); |
| 180 | + AccessibleContext cellAc = cellAccessible.getAccessibleContext(); |
| 181 | + if (cellAc == null) { |
| 182 | + throw new RuntimeException("'cellAc' should not be null"); |
| 183 | + } |
| 184 | + |
| 185 | + AccessibleAction cellAa = cellAc.getAccessibleAction(); |
| 186 | + if ((shouldBeNull && (cellAa != null)) || |
| 187 | + (!shouldBeNull && (cellAa == null))) { |
| 188 | + throw new RuntimeException( |
| 189 | + "Test failed. 'cellAa' is not as should be"); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments