Path: blob/master/test/jdk/javax/swing/JFileChooser/JFileChooserSetLocationTest.java
66644 views
/*1* Copyright (c) 2015, 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*/2223import java.awt.Component;24import java.awt.Dimension;25import java.awt.HeadlessException;26import java.awt.Point;27import java.awt.Robot;28import java.awt.event.ActionListener;29import java.awt.event.InputEvent;30import java.awt.event.KeyEvent;31import java.util.Arrays;32import java.util.List;33import java.util.concurrent.atomic.AtomicBoolean;34import java.util.concurrent.atomic.AtomicReference;35import java.util.stream.Collectors;36import javax.swing.JButton;37import javax.swing.JDialog;38import javax.swing.JFileChooser;39import javax.swing.JFrame;40import javax.swing.JPanel;41import javax.swing.SwingUtilities;42import javax.swing.UIManager;43import javax.swing.UIManager.LookAndFeelInfo;44import javax.swing.UnsupportedLookAndFeelException;4546import static javax.swing.UIManager.getInstalledLookAndFeels;4748/*49* @test50* @key headful51* @bug 439088552* @summary This test checks CCC #4390885, which verifies that it should be53* possible to set the location of the JFileChooser.54* @run main JFileChooserSetLocationTest55*/56public class JFileChooserSetLocationTest {5758public static final String SHOW_DIALOG_OUTSIDE_THE_PANEL =59"ShowFileChooser OUTSIDE the Panel";60public static final String SHOW_DIALOG_OVER_THE_PANEL =61"ShowFileChooser OVER the Panel";62public static final String SHOW_SAVE_DIALOG_OVER_THE_PANEL =63"ShowSaveDialog";64private static final int TOLERANCE_LEVEL = 6;65private static final int xOut = 75;66private static final int yOut = 75;67private static Robot robot;68private static JPanel panel;69private static MyFileChooser fileChooser;70private static int xIn;71private static int yIn;72private static JButton btn;73private static JButton btn1;74private static JButton btn2;75private static JFrame frame;7677public static void main(String[] s) throws Exception {78robot = new Robot();79robot.setAutoWaitForIdle(true);80robot.setAutoDelay(200);8182List<String> lafs = Arrays.stream(getInstalledLookAndFeels())83.map(LookAndFeelInfo::getClassName)84.collect(Collectors.toList());85for (final String laf : lafs) {86try {87AtomicBoolean lafSetSuccess = new AtomicBoolean(false);88SwingUtilities.invokeAndWait(() -> {89lafSetSuccess.set(setLookAndFeel(laf));90if (lafSetSuccess.get()) {91createUI();92}93});94if (!lafSetSuccess.get()) {95continue;96}97robot.waitForIdle();9899AtomicReference<Point> pt = new AtomicReference<>();100AtomicReference<Dimension> dim = new AtomicReference<>();101SwingUtilities.invokeAndWait(() -> {102pt.set(panel.getLocationOnScreen());103dim.set(panel.getSize());104});105Point panelLoc = pt.get();106Dimension panelDim = dim.get();107xIn = (panelLoc.x + panelDim.width) / 2;108yIn = (panelLoc.y + panelDim.height) / 2;109110Point dest = getCenterPointOf(btn);111112robot.mouseMove(dest.x, dest.y);113robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);114robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);115116robot.waitForIdle();117118Point actualPos = getActualLocation(fileChooser);119120// Case 1 : Verifying that the location of JFileChooser121// 'Show Dialog' is correctly set outside the frame at (25,25)122verify(xOut, actualPos.x, yOut, actualPos.y);123124hitKeys(KeyEvent.VK_ESCAPE);125126dest = getCenterPointOf(btn1);127robot.mouseMove(dest.x, dest.y);128robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);129robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);130131actualPos = getActualLocation(fileChooser);132133// Case 2 : Verifying that the location of JFileChooser134// 'Show Dialog' is correctly set inside the test frame135verify(xIn, actualPos.x, yIn, actualPos.y);136137hitKeys(KeyEvent.VK_ESCAPE);138139dest = getCenterPointOf(btn2);140robot.mouseMove(dest.x, dest.y);141robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);142robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);143144actualPos = getActualLocation(fileChooser);145146// Case 3 : Verifying that the location of JFileChooser147// 'Save Dialog' is correctly set inside the test frame148verify(xIn, actualPos.x, yIn, actualPos.y);149150hitKeys(KeyEvent.VK_ESCAPE);151152System.out.println("Test Passed, All cases passed for " + laf);153} finally {154SwingUtilities.invokeAndWait(155JFileChooserSetLocationTest::disposeFrame);156}157}158}159160private static Point getCenterPointOf(final Component comp)161throws Exception {162163AtomicReference<Point> pt = new AtomicReference<>();164SwingUtilities.invokeAndWait(() -> pt.set(comp.getLocationOnScreen()));165Point loc = pt.get();166loc.translate(comp.getWidth() / 2, comp.getHeight() / 2);167return loc;168}169170private static Point getActualLocation(final MyFileChooser fcoo)171throws Exception {172AtomicReference<Point> pt = new AtomicReference<>();173SwingUtilities.invokeAndWait(() -> pt.set(fcoo.getDialogLocation()));174return pt.get();175}176177public static void verify(int x1, int x2, int y1, int y2) {178System.out.println("verify " + x1 + "==" + x2 + "; " + y1 + "==" + y2);179if ((Math.abs(x1 - x2) < TOLERANCE_LEVEL) &&180(Math.abs(y1 - y2) < TOLERANCE_LEVEL)) {181System.out.println("Test passed");182} else {183throw new RuntimeException(184"Test Failed, setLocation() is not working properly");185}186}187188private static void hitKeys(int... keys) {189for (int key : keys) {190robot.keyPress(key);191}192193for (int i = keys.length - 1; i >= 0; i--) {194robot.keyRelease(keys[i]);195}196}197198public static void createUI() {199frame = new JFrame();200panel = new JPanel();201btn = new JButton(SHOW_DIALOG_OUTSIDE_THE_PANEL);202btn1 = new JButton(SHOW_DIALOG_OVER_THE_PANEL);203btn2 = new JButton(SHOW_SAVE_DIALOG_OVER_THE_PANEL);204ActionListener actionListener = actionEvent -> {205String btnAction = actionEvent.getActionCommand();206if (btnAction.equals(SHOW_DIALOG_OUTSIDE_THE_PANEL)) {207fileChooser = new MyFileChooser(xOut, yOut);208fileChooser.showOpenDialog(panel);209} else if (btnAction.equals(SHOW_DIALOG_OVER_THE_PANEL)) {210fileChooser = new MyFileChooser(xIn, yIn);211fileChooser.showOpenDialog(panel);212} else if (btnAction.equals(SHOW_SAVE_DIALOG_OVER_THE_PANEL)) {213fileChooser = new MyFileChooser(xIn, yIn);214fileChooser.showSaveDialog(panel);215}216};217btn.addActionListener(actionListener);218btn1.addActionListener(actionListener);219btn2.addActionListener(actionListener);220panel.add(btn);221panel.add(btn1);222panel.add(btn2);223224frame.add(panel);225frame.setLocationRelativeTo(null);226frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);227frame.pack();228frame.setVisible(true);229}230231private static boolean setLookAndFeel(String lafName) {232try {233System.out.println("Testing " + lafName);234UIManager.setLookAndFeel(lafName);235} catch (UnsupportedLookAndFeelException ignored) {236System.out.println("Ignoring Unsupported laf : " + lafName);237return false;238} catch (ClassNotFoundException | InstantiationException239| IllegalAccessException e) {240throw new RuntimeException(e);241}242return true;243}244245private static void disposeFrame() {246if (frame != null) {247frame.dispose();248frame = null;249}250}251252private static class MyFileChooser extends JFileChooser {253JDialog dialog;254int x, y;255256public MyFileChooser(int x, int y) {257super();258this.x = x;259this.y = y;260}261262protected JDialog createDialog(Component parent)263throws HeadlessException {264265dialog = super.createDialog(parent);266267System.out.println(268"createDialog and set location to (" + x + ", " + y + ")");269dialog.setLocation(x, y);270271return dialog;272}273274public Point getDialogLocation() {275return dialog.getLocation();276}277278}279280}281282283