Path: blob/master/test/jdk/javax/accessibility/AccessibleName/GetAccessibleNameTest.java
41149 views
/*1* Copyright (c) 2019, 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*/2223import javax.accessibility.AccessibleContext;24import javax.swing.JButton;25import javax.swing.JCheckBox;26import javax.swing.JCheckBoxMenuItem;27import javax.swing.JComponent;28import javax.swing.JLabel;29import javax.swing.JMenu;30import javax.swing.JMenuItem;31import javax.swing.JRadioButton;32import javax.swing.JRadioButtonMenuItem;33import javax.swing.JToggleButton;34import javax.swing.SwingUtilities;35import java.lang.reflect.Constructor;36import java.lang.reflect.InvocationTargetException;3738/*39* @test40* @bug 494910541* @summary Access Bridge lacks html tags parsing42* @run main GetAccessibleNameTest43*/4445public class GetAccessibleNameTest {4647public static void main(String[] args) throws Exception {48testConstructor();49testSetText();50testAccessibleProperty();51}5253private static void testConstructor() {54Class[] testClass = new Class[] {55JLabel.class, JButton.class, JMenuItem.class,56JMenu.class, JCheckBoxMenuItem.class, JRadioButtonMenuItem.class,57JToggleButton.class, JRadioButton.class, JCheckBox.class };5859Class[] ctorArg = new Class[1];60ctorArg[0] = String.class;61String expectedText = "bold italic em mark del small big sup sub ins strong code strike";62String inputText = "<html><style>{color:#FF0000;}</style><body>" +63"<b>bold</b> <i>italic</i> <em>em</em> <mark>mark</mark> <del>del</del> " +64"<small>small</small> <big>big</big> <sup>sup</sup> <sub>sub</sub> <ins>ins</ins> " +65"<strong>strong</strong> <code>code</code> <strike>strike</strike>" +66"</body></html>";6768for (Class aClass : testClass) {69try {70Constructor constructor = aClass.getDeclaredConstructor(ctorArg);71JComponent comp = (JComponent) constructor.newInstance(inputText);72if (!expectedText.equals(comp.getAccessibleContext().getAccessibleName())) {73throw new RuntimeException("AccessibleName of " + aClass.getName() + " is incorrect." +74" Expected: " + expectedText +75" Actual: " + comp.getAccessibleContext().getAccessibleName());76}77} catch (NoSuchMethodException e) {78throw new RuntimeException(aClass.getName() + " does not have a constructor accepting" +79"String parameter.", e.getCause());80} catch (InstantiationException e) {81throw new RuntimeException(aClass.getName() + " could not be instantiated.",82e.getCause());83} catch (IllegalAccessException e) {84throw new RuntimeException(aClass.getName() + " constructor cannot be accessed.",85e.getCause());86} catch (InvocationTargetException e) {87throw new RuntimeException(aClass.getName() + " constructor cannot be invoked.",88e.getCause());89}90}91}9293private static void testSetText() {94String text = "html text";95JLabel testLabel = new JLabel("<html>" + text + "</html>");96if (!text.equals(testLabel.getAccessibleContext().getAccessibleName())) {97throw new RuntimeException("Incorrect AccessibleName," +98" Expected: " + text +99" Actual: " + testLabel.getAccessibleContext().getAccessibleName());100}101102text = "Non html text";103testLabel.setText(text);104if (!text.equals(testLabel.getAccessibleContext().getAccessibleName())) {105throw new RuntimeException("Incorrect AccessibleName," +106" Expected: " + text +107" Actual: " + testLabel.getAccessibleContext().getAccessibleName());108}109}110111private static void testAccessibleProperty() {112String text = "html text";113JLabel testLabel = new JLabel("<html>" + text + "</html>");114if (!text.equals(testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY))) {115throw new RuntimeException("Incorrect ACCESSIBLE_NAME_PROPERTY," +116" Expected: " + text +117" Actual: " + testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY));118}119120String namePropertyText = "name property";121testLabel.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, namePropertyText);122if (!namePropertyText.equals(testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY))) {123throw new RuntimeException("Incorrect ACCESSIBLE_NAME_PROPERTY," +124" Expected: " + namePropertyText +125" Actual: " + testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY));126}127128text = "different html text";129testLabel.setText("<html>" + text + "</html>");130if (!namePropertyText.equals(testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY))) {131throw new RuntimeException("Incorrect ACCESSIBLE_NAME_PROPERTY," +132" Expected: " + namePropertyText +133" Actual: " + testLabel.getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY));134}135}136}137138139