Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/7199708/bug7199708.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*/2223import java.awt.Component;24import java.awt.Container;25import java.awt.Point;26import java.awt.Robot;27import java.awt.Toolkit;28import java.awt.event.InputEvent;29import java.awt.event.KeyEvent;30import java.io.File;31import java.io.IOException;32import javax.swing.JFileChooser;33import javax.swing.SwingUtilities;3435import java.nio.file.Files;36import javax.swing.AbstractButton;37import javax.swing.JTable;38import javax.swing.UIManager;3940/**41* @test42* @bug 719970843* @author Alexander Scherbatiy44* @summary FileChooser crashs when opening large folder45* @run main bug719970846*/47public class bug7199708 {4849private static int FILE_NUMBER = 30000;50private static volatile JFileChooser fileChooser;51private static volatile int locationX;52private static volatile int locationY;53private static volatile int width;5455public static void main(String[] args) throws Exception {5657Robot robot = new Robot();58robot.setAutoDelay(50);5960final File folder = createLargeFolder();61UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");6263SwingUtilities.invokeLater(new Runnable() {64public void run() {65fileChooser = new JFileChooser(folder);66fileChooser.showSaveDialog(null);67}68});6970robot.waitForIdle();7172SwingUtilities.invokeLater(new Runnable() {73public void run() {74final String detailsTooltip = UIManager.getString("FileChooser."75+ "detailsViewButtonToolTipText", fileChooser.getLocale());7677doAction(fileChooser, new ComponentAction() {78@Override79public boolean accept(Component component) {80return (component instanceof AbstractButton)81&& detailsTooltip.equals(82((AbstractButton) component).getToolTipText());83}8485@Override86public void perform(Component component) {87((AbstractButton) component).doClick();88}89});9091doAction(fileChooser, new ComponentAction() {92@Override93public boolean accept(Component component) {94return (component instanceof JTable);95}9697@Override98public void perform(Component component) {99Point tableLocation = component.getLocationOnScreen();100locationX = (int) tableLocation.getX();101locationY = (int) tableLocation.getY();102width = (int) fileChooser.getBounds().getWidth();103}104});105}106});107108robot.waitForIdle();109110int d = 25;111for (int i = 0; i < width / d; i++) {112robot.mouseMove(locationX + i * d, locationY + 5);113robot.mousePress(InputEvent.BUTTON1_MASK);114robot.mouseRelease(InputEvent.BUTTON1_MASK);115robot.waitForIdle();116}117118robot.keyPress(KeyEvent.VK_ESCAPE);119robot.keyRelease(KeyEvent.VK_ESCAPE);120}121122static void doAction(Component component, ComponentAction action) {123if (action.accept(component)) {124action.perform(component);125} else if (component instanceof Container) {126for (Component comp : ((Container) component).getComponents()) {127doAction(comp, action);128}129}130}131132private static File createLargeFolder() {133try {134135File largeFolder = Files.createTempDirectory("large_folder").toFile();136largeFolder.deleteOnExit();137138for (int i = 0; i < FILE_NUMBER; i++) {139File file = new File(largeFolder, "File_" + i + ".txt");140file.createNewFile();141file.deleteOnExit();142}143return largeFolder;144} catch (IOException ex) {145throw new RuntimeException(ex);146}147}148149interface ComponentAction {150151boolean accept(Component component);152153void perform(Component component);154}155}156157158