From 8f8aed0ccd0a91c62aaab213c27cc04649af8400 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 22 Sep 2022 18:06:52 +0530 Subject: [PATCH 1/5] JFileChooser combobox and selection textarea displayed directory list fix --- .../java/swing/plaf/gtk/GTKFileChooserUI.java | 4 + .../TestFileChooserDirectorySelection.java | 109 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java diff --git a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java index 675b2f2368c71..29402806c654d 100644 --- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java +++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java @@ -432,6 +432,10 @@ public DoubleClickListener(JList list) { public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); + File currentDirectory = getFileChooser().getCurrentDirectory(); + if (currentDirectory.getParentFile() == null && index == 1) { + return; + } if (index >= 0) { File f = (File) list.getModel().getElementAt(index); try { diff --git a/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java b/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java new file mode 100644 index 0000000000000..43c29616e1564 --- /dev/null +++ b/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 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. + */ + +/* + * @test + * @bug 6777156 + * @key headful + * @requires (os.family == "linux") + * @summary Verifies if user is not able to select "../" beyond root file system + * @run main TestFileChooserDirectorySelection + */ + +import java.io.File; +import java.awt.event.InputEvent; +import java.awt.Point; +import java.awt.Robot; +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; + +public class TestFileChooserDirectorySelection { + private static JFrame frame; + private static JFileChooser fileChooser; + private static Robot robot; + + public static void main(String[] args) throws Exception { + UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); + robot = new Robot(); + robot.setAutoDelay(100); + try { + SwingUtilities.invokeAndWait(() -> { + createAndShowUI(); + }); + + robot.waitForIdle(); + robot.delay(1000); + Point pt = frame.getLocationOnScreen(); + int i = 1; + boolean passed = false; + File crntDir = fileChooser.getCurrentDirectory(); + File prevDir = null; + while (true) { + prevDir = crntDir; + doubleClickMouse(pt); + crntDir = fileChooser.getCurrentDirectory(); + robot.delay(1000); + if (prevDir == crntDir) { + passed = true; + break; + } else if (++i > 5) { + break; + } + } + robot.delay(1000); + if (!passed) + throw new RuntimeException("User is able to select ../ " + + "beyond root directory"); + else + System.out.println("passed"); + } finally { + SwingUtilities.invokeAndWait(() -> { + if (frame != null) { + frame.dispose(); + } + }); + } + } + + private static void createAndShowUI() { + frame = new JFrame("Test File Chooser Directory Selection"); + fileChooser = new JFileChooser(); + frame.add(fileChooser); + frame.setSize(500,500); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + } + + private static void doubleClickMouse(Point p) { + robot.mouseMove(p.x+75, p.y+frame.getHeight()/2 - 90); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + robot.delay(100); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + robot.delay(1000); + } +} From ff3bce2c3a9d158fa81788c72fd13f24d5780a56 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 26 Sep 2022 11:35:31 +0530 Subject: [PATCH 2/5] Comment added and test case condotion update --- .../com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java | 8 ++++++++ .../JFileChooser/TestFileChooserDirectorySelection.java | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java index 29402806c654d..d99930a38e5b9 100644 --- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java +++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java @@ -432,10 +432,18 @@ public DoubleClickListener(JList list) { public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); + + /* + * Check if current directory is at root and user clicks on + * '../' (index = 1) to move to prevoius directory then don't + * process the event. + * It is not possible to walk past root directory. + */ File currentDirectory = getFileChooser().getCurrentDirectory(); if (currentDirectory.getParentFile() == null && index == 1) { return; } + if (index >= 0) { File f = (File) list.getModel().getElementAt(index); try { diff --git a/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java b/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java index 43c29616e1564..59cb07938eda0 100644 --- a/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java +++ b/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java @@ -56,7 +56,7 @@ public static void main(String[] args) throws Exception { robot.waitForIdle(); robot.delay(1000); Point pt = frame.getLocationOnScreen(); - int i = 1; + int i = 0; boolean passed = false; File crntDir = fileChooser.getCurrentDirectory(); File prevDir = null; @@ -65,7 +65,8 @@ public static void main(String[] args) throws Exception { doubleClickMouse(pt); crntDir = fileChooser.getCurrentDirectory(); robot.delay(1000); - if (prevDir == crntDir) { + // getParentFile() returns null for root directory + if (prevDir == crntDir && crntDir.getParentFile() == null) { passed = true; break; } else if (++i > 5) { From 698a12021177369576e8ac44d66170e8faba5a23 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 11 Oct 2022 12:07:27 +0530 Subject: [PATCH 3/5] Review comment fix and test case changed to manual --- .../java/swing/plaf/gtk/GTKFileChooserUI.java | 33 +++++--- .../TestFileChooserDirectorySelection.java | 79 ++++++------------- 2 files changed, 43 insertions(+), 69 deletions(-) diff --git a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java index d99930a38e5b9..e49330613ae9f 100644 --- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java +++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 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 @@ -432,18 +432,6 @@ public DoubleClickListener(JList list) { public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); - - /* - * Check if current directory is at root and user clicks on - * '../' (index = 1) to move to prevoius directory then don't - * process the event. - * It is not possible to walk past root directory. - */ - File currentDirectory = getFileChooser().getCurrentDirectory(); - if (currentDirectory.getParentFile() == null && index == 1) { - return; - } - if (index >= 0) { File f = (File) list.getModel().getElementAt(index); try { @@ -992,6 +980,25 @@ protected void sort(Vector v) { v.sort(Comparator.comparing(fsv::getSystemDisplayName)); } } + + @Override + public Vector getDirectories() { + Vector files = super.getDirectories(); + + /* + * Delete the "/.." file entry from file chooser directory list in + * GTK LAF if current directory is root and files vector contains + * "/.." entry. + * + * It is not possible to go beyond root directory. + */ + File crntDir = getFileChooser().getCurrentDirectory(); + if (crntDir != null && crntDir.getParentFile() == null && + files.contains(new File("/.."))) { + files.removeElementAt(0); + } + return files; + } } @SuppressWarnings("serial") // Superclass is not serializable across versions diff --git a/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java b/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java index 59cb07938eda0..842db56349820 100644 --- a/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java +++ b/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java @@ -24,61 +24,36 @@ /* * @test * @bug 6777156 - * @key headful + * @library /java/awt/regtesthelpers + * @build PassFailJFrame * @requires (os.family == "linux") - * @summary Verifies if user is not able to select "../" beyond root file system - * @run main TestFileChooserDirectorySelection + * @summary Verifies if user is not able to select "../" beyond + * root file system. + * @run main/manual TestFileChooserDirectorySelection */ -import java.io.File; -import java.awt.event.InputEvent; -import java.awt.Point; -import java.awt.Robot; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.UIManager; -public class TestFileChooserDirectorySelection { +public class TestFileChooserDirectorySelection { private static JFrame frame; - private static JFileChooser fileChooser; - private static Robot robot; + private static final String INSTRUCTIONS = + "Double click on the \"../\" entry from directory list.\n\n" + + "Repeat the same process till the current directory is root " + + "i.e \" / \" .\n\n" + + "If \" ../ \" option is not available at root directory" + + ", press Pass else Fail."; public static void main(String[] args) throws Exception { UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); - robot = new Robot(); - robot.setAutoDelay(100); + PassFailJFrame passFailJFrame = new PassFailJFrame( + "JFileChooser Test Instructions", INSTRUCTIONS, 5, 8, 35); try { - SwingUtilities.invokeAndWait(() -> { - createAndShowUI(); - }); - - robot.waitForIdle(); - robot.delay(1000); - Point pt = frame.getLocationOnScreen(); - int i = 0; - boolean passed = false; - File crntDir = fileChooser.getCurrentDirectory(); - File prevDir = null; - while (true) { - prevDir = crntDir; - doubleClickMouse(pt); - crntDir = fileChooser.getCurrentDirectory(); - robot.delay(1000); - // getParentFile() returns null for root directory - if (prevDir == crntDir && crntDir.getParentFile() == null) { - passed = true; - break; - } else if (++i > 5) { - break; - } - } - robot.delay(1000); - if (!passed) - throw new RuntimeException("User is able to select ../ " + - "beyond root directory"); - else - System.out.println("passed"); + SwingUtilities.invokeAndWait( + TestFileChooserDirectorySelection::createAndShowUI); + passFailJFrame.awaitAndCheck(); } finally { SwingUtilities.invokeAndWait(() -> { if (frame != null) { @@ -87,24 +62,16 @@ public static void main(String[] args) throws Exception { }); } } - private static void createAndShowUI() { frame = new JFrame("Test File Chooser Directory Selection"); - fileChooser = new JFileChooser(); + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setControlButtonsAreShown(false); + PassFailJFrame.addTestWindow(frame); + PassFailJFrame.positionTestWindow( + frame, PassFailJFrame.Position.HORIZONTAL); frame.add(fileChooser); - frame.setSize(500,500); - frame.setLocationRelativeTo(null); + frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } - - private static void doubleClickMouse(Point p) { - robot.mouseMove(p.x+75, p.y+frame.getHeight()/2 - 90); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - robot.delay(100); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - robot.delay(1000); - } } From c7493bf1e351a04ab64ce4de6b712ce0ad6ab124 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 11 Oct 2022 14:35:20 +0530 Subject: [PATCH 4/5] Review comment fix --- .../classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java index e49330613ae9f..2c62680ea6358 100644 --- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java +++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKFileChooserUI.java @@ -993,7 +993,8 @@ public Vector getDirectories() { * It is not possible to go beyond root directory. */ File crntDir = getFileChooser().getCurrentDirectory(); - if (crntDir != null && crntDir.getParentFile() == null && + FileSystemView fsv = getFileChooser().getFileSystemView(); + if (crntDir != null && fsv.isFileSystemRoot(crntDir) && files.contains(new File("/.."))) { files.removeElementAt(0); } From 7b9b74f5f3e4c4e36877ebad919acd7ec34c9c83 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 11 Oct 2022 15:35:08 +0530 Subject: [PATCH 5/5] Test moved to plaf/gtk folder --- .../java/swing/plaf/gtk}/TestFileChooserDirectorySelection.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/jdk/{javax/swing/JFileChooser => com/sun/java/swing/plaf/gtk}/TestFileChooserDirectorySelection.java (100%) diff --git a/test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java b/test/jdk/com/sun/java/swing/plaf/gtk/TestFileChooserDirectorySelection.java similarity index 100% rename from test/jdk/javax/swing/JFileChooser/TestFileChooserDirectorySelection.java rename to test/jdk/com/sun/java/swing/plaf/gtk/TestFileChooserDirectorySelection.java