Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/4927934/bug4927934.java
38854 views
/*1* Copyright (c) 2013, 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*/22/* @test23@bug 492793424@summary JTree traversal is unlike Native windows tree traversal25@author Andrey Pikalev26@run main bug492793427*/2829import javax.swing.*;30import javax.swing.event.*;31import javax.swing.tree.*;32import java.awt.*;33import java.awt.event.*;34import java.lang.reflect.InvocationTargetException;3536public class bug4927934 implements TreeSelectionListener, TreeExpansionListener, FocusListener {3738final static Object listener = new bug4927934();3940static boolean focusGained = false;41public static boolean selectionChanged = false;42public static boolean treeExpanded = false;43public static boolean treeCollapsed = false;4445static JFrame frame;46static JTree tree;47static Robot robot;4849public static void main(String args[]) throws Exception {50UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());5152robot = new Robot();53robot.setAutoDelay(50);5455SwingUtilities.invokeAndWait(new Runnable() {56public void run() {57frame = new JFrame();5859DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");60createNodes(root);61tree = new JTree(root);62JScrollPane scrollPane = new JScrollPane(tree);63frame.getContentPane().add(scrollPane);6465tree.addFocusListener((FocusListener)listener);66tree.addTreeSelectionListener((TreeSelectionListener)listener);67tree.addTreeExpansionListener((TreeExpansionListener)listener);6869frame.setSize(300, 300);70frame.setVisible(true);71}72});7374robot.waitForIdle();75Thread.sleep(1000);7677SwingUtilities.invokeLater(new Runnable() {78public void run() {79tree.requestFocus();80}81});8283synchronized(listener) {84if (!focusGained) {85System.out.println("waiting focusGained...");86try {87listener.wait(10000);88} catch (InterruptedException e) {89e.printStackTrace();90}91}92}9394// GO TO RIGHT95selectionChanged = false;96hitKey(KeyEvent.VK_RIGHT);97robot.waitForIdle();98if (!checkSelectionChanged(tree, 0)) {99throw new RuntimeException("Root should be selected");100}101102selectionChanged = false;103hitKey(KeyEvent.VK_RIGHT);104robot.waitForIdle();105if (!checkSelectionChanged(tree, 1)) {106throw new RuntimeException("Node should be selected");107}108109treeExpanded = false;110hitKey(KeyEvent.VK_RIGHT);111robot.waitForIdle();112if (!isTreeExpanded()) {113throw new RuntimeException("Node should be expanded");114}115116selectionChanged = false;117hitKey(KeyEvent.VK_RIGHT);118robot.waitForIdle();119if (!checkSelectionChanged(tree, 2)) {120throw new RuntimeException("Leaf1 should be selected");121}122123selectionChanged = false;124hitKey(KeyEvent.VK_RIGHT);125robot.waitForIdle();126if (!checkSelectionChanged(tree, 2)) {127throw new RuntimeException("Leaf1 should be selected");128}129130// GO TO LEFT131selectionChanged = false;132hitKey(KeyEvent.VK_LEFT);133robot.waitForIdle();134if (!checkSelectionChanged(tree, 1)) {135throw new RuntimeException("Node should be selected");136}137138treeCollapsed = false;139hitKey(KeyEvent.VK_LEFT);140if (!isTreeCollapsed()) {141throw new RuntimeException("Node should be collapsed");142}143144selectionChanged = false;145hitKey(KeyEvent.VK_LEFT);146robot.waitForIdle();147if (!checkSelectionChanged(tree, 0)) {148throw new RuntimeException("Root should be selected");149}150151treeCollapsed = false;152hitKey(KeyEvent.VK_LEFT);153robot.waitForIdle();154if (!isTreeCollapsed()) {155throw new RuntimeException("Root should be collapsed");156}157}158159160synchronized public void focusLost(FocusEvent e) {161}162163synchronized public void focusGained(FocusEvent e) {164focusGained = true;165System.out.println("focusGained");166listener.notifyAll();167}168169private static void createNodes(DefaultMutableTreeNode root) {170DefaultMutableTreeNode node = new DefaultMutableTreeNode("Node");171node.add(new DefaultMutableTreeNode("Leaf1"));172node.add(new DefaultMutableTreeNode("Leaf2"));173root.add(node);174root.add(new DefaultMutableTreeNode("Leaf3"));175}176177synchronized public void valueChanged(TreeSelectionEvent e) {178selectionChanged = true;179System.out.println("selectionChanged");180notifyAll();181}182183synchronized public void treeCollapsed(TreeExpansionEvent e) {184System.out.println("treeCollapsed");185treeCollapsed = true;186notifyAll();187}188189synchronized public void treeExpanded(TreeExpansionEvent e) {190System.out.println("treeExpanded");191treeExpanded = true;192notifyAll();193}194195private static void hitKey(int key) {196System.out.println("key " + key + " pressed");197robot.keyPress(key);198robot.keyRelease(key);199}200201private static boolean checkSelectionChanged(JTree tree, int shouldBeSel) {202synchronized(listener) {203if (!selectionChanged) {204System.out.println("waiting for selectionChanged...");205try {206listener.wait(5000);207} catch (InterruptedException e) {208e.printStackTrace();209}210}211}212int selRow = tree.getLeadSelectionRow();213System.out.println("Selected row: " + selRow);214return selRow == shouldBeSel;215}216217private static boolean isTreeExpanded() {218synchronized(listener) {219if (!treeExpanded) {220System.out.println("waiting for treeExpanded...");221try {222listener.wait(5000);223} catch (InterruptedException e) {224e.printStackTrace();225}226}227}228return treeExpanded;229}230231private static boolean isTreeCollapsed() {232synchronized(listener) {233if (!treeCollapsed) {234System.out.println("waiting for treeCollapsed...");235try {236listener.wait(5000);237} catch (InterruptedException e) {238e.printStackTrace();239}240}241}242return treeCollapsed;243}244}245246247