Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/8004298/bug8004298.java
38853 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*/2223/*24* @test25* @bug 800429826* @summary NPE in WindowsTreeUI.ensureRowsAreVisible27* @author Alexander Scherbatiy28* @library ../../regtesthelpers29* @build Util30* @run main bug800429831*/3233import java.awt.*;34import java.awt.event.InputEvent;35import javax.swing.*;36import javax.swing.tree.*;37import java.util.concurrent.Callable;38import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;39import com.sun.java.swing.plaf.windows.WindowsTreeUI;4041public class bug8004298 {4243private static JTree tree;4445public static void main(String[] args) throws Exception {46Robot robot = new Robot();47robot.setAutoDelay(50);48try {49UIManager.setLookAndFeel(new WindowsLookAndFeel());50} catch (javax.swing.UnsupportedLookAndFeelException ulafe) {51System.out.println(ulafe.getMessage());52System.out.println("The test is considered PASSED");53return;54}55SwingUtilities.invokeAndWait(new Runnable() {5657@Override58public void run() {59createAndShowGUI();60}61});6263robot.waitForIdle();6465Point point = Util.invokeOnEDT(new Callable<Point>() {6667@Override68public Point call() throws Exception {69Rectangle rect = tree.getRowBounds(2);70Point p = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);71SwingUtilities.convertPointToScreen(p, tree);72return p;73}74});7576robot.mouseMove(point.x, point.y);77robot.mousePress(InputEvent.BUTTON1_MASK);78robot.mouseRelease(InputEvent.BUTTON1_MASK);79robot.mousePress(InputEvent.BUTTON1_MASK);80robot.mouseRelease(InputEvent.BUTTON1_MASK);81robot.waitForIdle();8283}8485private static void createAndShowGUI() {86JFrame frame = new JFrame();87frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);8889DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");90root.add(new DefaultMutableTreeNode("colors"));91DefaultMutableTreeNode sports = new DefaultMutableTreeNode("sports");92sports.add(new DefaultMutableTreeNode("basketball"));93sports.add(new DefaultMutableTreeNode("football"));94root.add(sports);9596tree = new JTree(root);97tree.setUI(new NullReturningTreeUI());9899frame.getContentPane().add(tree);100frame.pack();101frame.setVisible(true);102103}104105private static final class NullReturningTreeUI extends WindowsTreeUI {106107@Override108public Rectangle getPathBounds(JTree tree, TreePath path) {109// the method can return null and callers have to be ready for110// that. Simulate the case by returning null for unknown reason.111if (path != null && path.toString().contains("football")) {112return null;113}114115return super.getPathBounds(tree, path);116}117}118}119120121