Skip to content

Commit

Permalink
8283624: Create an automated regression test for RFE-4390885
Browse files Browse the repository at this point in the history
Reviewed-by: serb
  • Loading branch information
Manukumar V S authored and Abdul Kolarkunnu committed Apr 29, 2022
1 parent 94b533a commit 99388ef
Showing 1 changed file with 281 additions and 0 deletions.
281 changes: 281 additions & 0 deletions test/jdk/javax/swing/JFileChooser/JFileChooserSetLocationTest.java
@@ -0,0 +1,281 @@
/*
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import static javax.swing.UIManager.getInstalledLookAndFeels;

/*
* @test
* @key headful
* @bug 4390885
* @summary This test checks CCC #4390885, which verifies that it should be
* possible to set the location of the JFileChooser.
* @run main JFileChooserSetLocationTest
*/
public class JFileChooserSetLocationTest {

public static final String SHOW_DIALOG_OUTSIDE_THE_PANEL =
"ShowFileChooser OUTSIDE the Panel";
public static final String SHOW_DIALOG_OVER_THE_PANEL =
"ShowFileChooser OVER the Panel";
public static final String SHOW_SAVE_DIALOG_OVER_THE_PANEL =
"ShowSaveDialog";
private static final int TOLERANCE_LEVEL = 6;
private static final int xOut = 75;
private static final int yOut = 75;
private static Robot robot;
private static JPanel panel;
private static MyFileChooser fileChooser;
private static int xIn;
private static int yIn;
private static JButton btn;
private static JButton btn1;
private static JButton btn2;
private static JFrame frame;

public static void main(String[] s) throws Exception {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(200);

List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
.map(LookAndFeelInfo::getClassName)
.collect(Collectors.toList());
for (final String laf : lafs) {
try {
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
SwingUtilities.invokeAndWait(() -> {
lafSetSuccess.set(setLookAndFeel(laf));
if (lafSetSuccess.get()) {
createUI();
}
});
if (!lafSetSuccess.get()) {
continue;
}
robot.waitForIdle();

AtomicReference<Point> pt = new AtomicReference<>();
AtomicReference<Dimension> dim = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> {
pt.set(panel.getLocationOnScreen());
dim.set(panel.getSize());
});
Point panelLoc = pt.get();
Dimension panelDim = dim.get();
xIn = (panelLoc.x + panelDim.width) / 2;
yIn = (panelLoc.y + panelDim.height) / 2;

Point dest = getCenterPointOf(btn);

robot.mouseMove(dest.x, dest.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

robot.waitForIdle();

Point actualPos = getActualLocation(fileChooser);

// Case 1 : Verifying that the location of JFileChooser
// 'Show Dialog' is correctly set outside the frame at (25,25)
verify(xOut, actualPos.x, yOut, actualPos.y);

hitKeys(KeyEvent.VK_ESCAPE);

dest = getCenterPointOf(btn1);
robot.mouseMove(dest.x, dest.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

actualPos = getActualLocation(fileChooser);

// Case 2 : Verifying that the location of JFileChooser
// 'Show Dialog' is correctly set inside the test frame
verify(xIn, actualPos.x, yIn, actualPos.y);

hitKeys(KeyEvent.VK_ESCAPE);

dest = getCenterPointOf(btn2);
robot.mouseMove(dest.x, dest.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

actualPos = getActualLocation(fileChooser);

// Case 3 : Verifying that the location of JFileChooser
// 'Save Dialog' is correctly set inside the test frame
verify(xIn, actualPos.x, yIn, actualPos.y);

hitKeys(KeyEvent.VK_ESCAPE);

System.out.println("Test Passed, All cases passed for " + laf);
} finally {
SwingUtilities.invokeAndWait(
JFileChooserSetLocationTest::disposeFrame);
}
}
}

private static Point getCenterPointOf(final Component comp)
throws Exception {

AtomicReference<Point> pt = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> pt.set(comp.getLocationOnScreen()));
Point loc = pt.get();
loc.translate(comp.getWidth() / 2, comp.getHeight() / 2);
return loc;
}

private static Point getActualLocation(final MyFileChooser fcoo)
throws Exception {
AtomicReference<Point> pt = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> pt.set(fcoo.getDialogLocation()));
return pt.get();
}

public static void verify(int x1, int x2, int y1, int y2) {
System.out.println("verify " + x1 + "==" + x2 + "; " + y1 + "==" + y2);
if ((Math.abs(x1 - x2) < TOLERANCE_LEVEL) &&
(Math.abs(y1 - y2) < TOLERANCE_LEVEL)) {
System.out.println("Test passed");
} else {
throw new RuntimeException(
"Test Failed, setLocation() is not working properly");
}
}

private static void hitKeys(int... keys) {
for (int key : keys) {
robot.keyPress(key);
}

for (int i = keys.length - 1; i >= 0; i--) {
robot.keyRelease(keys[i]);
}
}

public static void createUI() {
frame = new JFrame();
panel = new JPanel();
btn = new JButton(SHOW_DIALOG_OUTSIDE_THE_PANEL);
btn1 = new JButton(SHOW_DIALOG_OVER_THE_PANEL);
btn2 = new JButton(SHOW_SAVE_DIALOG_OVER_THE_PANEL);
ActionListener actionListener = actionEvent -> {
String btnAction = actionEvent.getActionCommand();
if (btnAction.equals(SHOW_DIALOG_OUTSIDE_THE_PANEL)) {
fileChooser = new MyFileChooser(xOut, yOut);
fileChooser.showOpenDialog(panel);
} else if (btnAction.equals(SHOW_DIALOG_OVER_THE_PANEL)) {
fileChooser = new MyFileChooser(xIn, yIn);
fileChooser.showOpenDialog(panel);
} else if (btnAction.equals(SHOW_SAVE_DIALOG_OVER_THE_PANEL)) {
fileChooser = new MyFileChooser(xIn, yIn);
fileChooser.showSaveDialog(panel);
}
};
btn.addActionListener(actionListener);
btn1.addActionListener(actionListener);
btn2.addActionListener(actionListener);
panel.add(btn);
panel.add(btn1);
panel.add(btn2);

frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

private static boolean setLookAndFeel(String lafName) {
try {
System.out.println("Testing " + lafName);
UIManager.setLookAndFeel(lafName);
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Ignoring Unsupported laf : " + lafName);
return false;
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
throw new RuntimeException(e);
}
return true;
}

private static void disposeFrame() {
if (frame != null) {
frame.dispose();
frame = null;
}
}

private static class MyFileChooser extends JFileChooser {
JDialog dialog;
int x, y;

public MyFileChooser(int x, int y) {
super();
this.x = x;
this.y = y;
}

protected JDialog createDialog(Component parent)
throws HeadlessException {

dialog = super.createDialog(parent);

System.out.println(
"createDialog and set location to (" + x + ", " + y + ")");
dialog.setLocation(x, y);

return dialog;
}

public Point getDialogLocation() {
return dialog.getLocation();
}

}

}

5 comments on commit 99388ef

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 99388ef Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 99388ef Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch GoeLin-backport-99388eff in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 99388eff from the openjdk/jdk repository.

The commit being backported was authored by Manukumar V S on 29 Apr 2022 and was reviewed by Sergey Bylokhov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-99388eff:GoeLin-backport-99388eff
$ git checkout GoeLin-backport-99388eff
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-99388eff

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 99388ef Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 99388ef Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch GoeLin-backport-99388eff in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 99388eff from the openjdk/jdk repository.

The commit being backported was authored by Manukumar V S on 29 Apr 2022 and was reviewed by Sergey Bylokhov.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev GoeLin-backport-99388eff:GoeLin-backport-99388eff
$ git checkout GoeLin-backport-99388eff
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev GoeLin-backport-99388eff

Please sign in to comment.