Path: blob/master/test/jdk/javax/accessibility/JTable/BooleanRendererHasAccessibleActionTest.java
66644 views
/*1* Copyright (c) 2022, 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/* @test24@bug 827792225@key headful26@summary TableCellRenderer of JTable cell with Boolean data should not27support any AccessibleAction.28*/2930import java.awt.AWTException;31import java.awt.BorderLayout;32import java.awt.Container;33import java.awt.Dimension;34import java.awt.Robot;35import java.lang.reflect.InvocationTargetException;36import javax.accessibility.Accessible;37import javax.accessibility.AccessibleAction;38import javax.accessibility.AccessibleContext;39import javax.accessibility.AccessibleTable;40import javax.swing.JFrame;41import javax.swing.JScrollPane;42import javax.swing.JTable;43import javax.swing.SwingUtilities;44import javax.swing.table.DefaultTableModel;45import javax.swing.table.TableCellRenderer;4647public class BooleanRendererHasAccessibleActionTest {48private volatile JFrame frame;49private volatile JTable table;5051public static void main(String[] args) throws InterruptedException,52InvocationTargetException, AWTException {53final BooleanRendererHasAccessibleActionTest test =54new BooleanRendererHasAccessibleActionTest();5556try {57SwingUtilities.invokeAndWait(new Runnable() {58@Override59public void run() {60test.createGUI();61}62});63Robot robot = new Robot();64robot.waitForIdle();6566SwingUtilities.invokeAndWait(new Runnable() {67@Override68public void run() {69test.runTest();70}71});72} finally {73SwingUtilities.invokeAndWait(new Runnable() {74@Override75public void run() {76test.dispose();77}78});79}80}8182private void createGUI() {83frame = new JFrame("BooleanRendererHasAccessibleActionTest");84frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);85Container content = frame.getContentPane();86content.setLayout(new BorderLayout());8788String[] tblColNames = {"Column 1", "Column 2", "Column 3"};89Object[][] tblData = {90{Boolean.TRUE, "Text 1", Boolean.FALSE},91{Boolean.FALSE, "Text 2", Boolean.TRUE}92};93final DefaultTableModel tblModel = new DefaultTableModel(94tblData, tblColNames) {95@Override96public Class<?> getColumnClass(int column) {97return getValueAt(0, column).getClass();98}99};100table = new JTable(tblModel);101table.setPreferredScrollableViewportSize(new Dimension(400, 100));102103JScrollPane tblScroller = new JScrollPane(table);104tblScroller.setHorizontalScrollBarPolicy(105JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);106tblScroller.setVerticalScrollBarPolicy(107JScrollPane.VERTICAL_SCROLLBAR_ALWAYS108);109content.add(tblScroller, BorderLayout.CENTER);110111frame.pack();112frame.setVisible(true);113}114115private void dispose() {116if (frame != null) {117frame.dispose();118frame = null;119}120}121122private void runTest() {123if (table == null) {124throw new RuntimeException("'table' should not be null");125}126127testAccessibleActionInCellRenderer(0, 0, true);128testAccessibleActionInCellRenderer(1, 0, true);129testAccessibleActionInCellRenderer(0, 2, true);130testAccessibleActionInCellRenderer(1, 2, true);131132testAccessibleActionInCell(0, 0, true);133testAccessibleActionInCell(1, 0, true);134testAccessibleActionInCell(0, 2, true);135testAccessibleActionInCell(1, 2, true);136137System.out.println("Test passed.");138}139140private void testAccessibleActionInCellRenderer(int row, int column,141boolean shouldBeNull) {142System.out.println(String.format(143"testAccessibleActionInCellRenderer():" +144" row='%d', column='%d', shouldBeNull='%b'",145row, column, shouldBeNull));146147TableCellRenderer cellRenderer = table.getCellRenderer(row, column);148if (!(cellRenderer instanceof Accessible)) {149throw new RuntimeException("'cellRenderer' is not Accessible");150}151152AccessibleContext cellRendererAc =153((Accessible) cellRenderer).getAccessibleContext();154if (cellRendererAc == null) {155throw new RuntimeException("'cellRendererAc' should not be null");156}157158AccessibleAction cellRendererAa = cellRendererAc.getAccessibleAction();159if ((shouldBeNull && (cellRendererAa != null)) ||160(!shouldBeNull && (cellRendererAa == null))) {161throw new RuntimeException(162"Test failed. 'cellRendererAa' is not as should be");163}164}165166private void testAccessibleActionInCell(int row, int column,167boolean shouldBeNull) {168System.out.println(String.format("testAccessibleActionInCell():" +169" row='%d', column='%d', shouldBeNull='%b'",170row, column, shouldBeNull));171172AccessibleContext tblAc = table.getAccessibleContext();173AccessibleTable accessibleTbl = tblAc.getAccessibleTable();174if (accessibleTbl == null) {175throw new RuntimeException("'accessibleTbl' should not be null");176}177178Accessible cellAccessible = accessibleTbl.getAccessibleAt(row, column);179AccessibleContext cellAc = cellAccessible.getAccessibleContext();180if (cellAc == null) {181throw new RuntimeException("'cellAc' should not be null");182}183184AccessibleAction cellAa = cellAc.getAccessibleAction();185if ((shouldBeNull && (cellAa != null)) ||186(!shouldBeNull && (cellAa == null))) {187throw new RuntimeException(188"Test failed. 'cellAa' is not as should be");189}190}191}192193194