Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/8003400/Test8003400.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 800340026* @summary Tests that JTree shows the last row27* @author Sergey Malenkov28* @library ../../../../lib/testlibrary29* @build ExtendedRobot30* @run main/othervm Test800340031* @run main/othervm Test8003400 reverse32* @run main/othervm Test8003400 system33* @run main/othervm Test8003400 system reverse34*/3536import java.awt.Rectangle;37import java.awt.Robot;38import java.awt.event.KeyEvent;39import java.util.Arrays;40import java.util.Collections;41import java.util.List;42import javax.swing.JFrame;43import javax.swing.JScrollPane;44import javax.swing.JTree;45import javax.swing.SwingUtilities;46import javax.swing.UIManager;47import javax.swing.tree.DefaultMutableTreeNode;4849public class Test8003400 {5051private static final String TITLE = "Test JTree with a large model";52private static List<String> OBJECTS = Arrays.asList(TITLE, "x", "y", "z");53private static JScrollPane pane;5455public static void main(String[] args) throws Exception {56for (String arg : args) {57if (arg.equals("reverse")) {58Collections.reverse(OBJECTS);59}60if (arg.equals("system")) {61UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());62}63}64SwingUtilities.invokeAndWait(new Runnable() {65public void run() {66DefaultMutableTreeNode root = new DefaultMutableTreeNode();6768JTree tree = new JTree(root);69tree.setLargeModel(true);70tree.setRowHeight(16);7172JFrame frame = new JFrame(TITLE);73frame.add(pane = new JScrollPane(tree));74frame.setSize(200, 100);75frame.setLocationRelativeTo(null);76frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);77frame.setVisible(true);7879for (String object : OBJECTS) {80root.add(new DefaultMutableTreeNode(object));81}82tree.expandRow(0);83}84});8586ExtendedRobot robot = new ExtendedRobot();87robot.waitForIdle(500);88robot.keyPress(KeyEvent.VK_END);89robot.waitForIdle(500);90robot.keyRelease(KeyEvent.VK_END);91robot.waitForIdle();9293SwingUtilities.invokeAndWait(new Runnable() {94public void run() {95JTree tree = (JTree) pane.getViewport().getView();96Rectangle inner = tree.getRowBounds(tree.getRowCount() - 1);97Rectangle outer = SwingUtilities.convertRectangle(tree, inner, pane);98outer.y += tree.getRowHeight() - 1 - pane.getVerticalScrollBar().getHeight();99// error reporting only for automatic testing100if (null != System.getProperty("test.src", null)) {101SwingUtilities.getWindowAncestor(pane).dispose();102if (outer.y != 0) {103throw new Error("TEST FAILED: " + outer.y);104}105}106}107});108}109}110111112