Path: blob/master/test/jdk/javax/accessibility/4670319/AccessibleJTreePCESourceTest.java
66644 views
/*1* Copyright (c) 2008, 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/*24* @test25* @key headful26* @bug 467031927* @summary AccessibleJTree should fire a PropertyChangeEvent28* using a AccessibleJTreeNode as source.29* @run main AccessibleJTreePCESourceTest30*/3132import java.awt.Robot;33import java.beans.PropertyChangeEvent;34import java.util.ArrayList;3536import javax.swing.JFrame;37import javax.swing.JTree;38import javax.swing.SwingUtilities;3940public class AccessibleJTreePCESourceTest {41private static JTree jTree;42private static JFrame jFrame;4344private static ArrayList<PropertyChangeEvent> eventsList =45new ArrayList<PropertyChangeEvent>();4647private static void doTest() throws Exception {48try {49SwingUtilities.invokeAndWait(() -> createGUI());50Robot robot = new Robot();51robot.waitForIdle();5253expand(1);54robot.waitForIdle();55collapse(1);56robot.waitForIdle();57expand(2);58robot.waitForIdle();59collapse(2);60robot.waitForIdle();61} finally {62SwingUtilities.invokeAndWait(() -> jFrame.dispose());63}64}6566public static void expand(int row) throws Exception {67SwingUtilities.invokeAndWait(() -> jTree.expandRow(row));68}6970public static void collapse(int row) throws Exception {71SwingUtilities.invokeAndWait(() -> jTree.collapseRow(row));72}7374private static void createGUI() {75jTree = new JTree();76jFrame = new JFrame();7778jFrame.add(jTree);7980jTree.getAccessibleContext().addPropertyChangeListener(event -> {81if (event.getNewValue() != null) {82eventsList.add(event);83}84});8586jFrame.setSize(200, 200);87jFrame.getContentPane().add(jTree);88jFrame.setVisible(true);89}9091public static void main(String args[]) throws Exception {92doTest();9394for (int i = 0; i < eventsList.size(); i++) {95PropertyChangeEvent obj = eventsList.get(i);96String state = obj.getNewValue().toString();9798if ((state.equals("expanded") || state.equals("collapsed"))99&& (obj.getPropertyName().toString())100.equals("AccessibleState")) {101if (!(obj.getSource().getClass().getName()).equals(102"javax.swing.JTree$AccessibleJTree$AccessibleJTreeNode")) {103throw new RuntimeException("Test Failed: When tree node is "104+ state + ", PropertyChangeEventSource is "105+ obj.getSource().getClass().getName());106}107}108}109System.out.println(110"Test Passed: When tree node is expanded/collapsed, "111+ "PropertyChangeEventSource is the Node");112}113}114115116117