Skip to content

Commit

Permalink
8316149: Open source several Swing JTree JViewport KeyboardManager tests
Browse files Browse the repository at this point in the history
Reviewed-by: psadhukhan, tr
  • Loading branch information
Damon Nguyen committed Sep 20, 2023
1 parent 455cfae commit a021dbc
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/jdk/javax/swing/JTree/bug4696499.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2003, 2023, 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 4696499
* @summary new tree model asked about nodes of previous tree model
* @run main bug4696499
*/

import java.util.ArrayList;

import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class bug4696499 {
public static void main(String[] args) throws Exception {
JTree tree = new JTree();
TreeModel model = new MyModel();
tree.setModel(model);

tree.setSelectionRow(1);
model = new MyModel();
tree.setModel(model);
}
}

class MyModel implements TreeModel {
private Object root = "Root";
private ArrayList listeners = new ArrayList();
private TreeNode ONE;
static int next = 1;

MyModel() {
ONE = new DefaultMutableTreeNode(next);
next *= 2;
}

public void addTreeModelListener(TreeModelListener l) {
listeners.add(l);
}

public void removeTreeModelListener(TreeModelListener l) {
listeners.remove(l);
}

public void valueForPathChanged(TreePath tp, Object newValue) {
}

public Object getRoot() {
return root;
}

public boolean isLeaf(Object o) {
return o == ONE;
}

public int getIndexOfChild(Object parent, Object child) {
if (parent != root || child != ONE) {
throw new RuntimeException("This method is called with the child " +
"of the previous tree model");
}
return 0;
}

public int getChildCount(Object o) {
if (o == root) {
return 1;
}
if (o == ONE) {
return 0;
}
throw new IllegalArgumentException(o.toString());
}

public Object getChild(Object o, int index) {
if (o != root || index != 0) {
throw new IllegalArgumentException(o + ", " + index);
}
return ONE;
}
}
42 changes: 42 additions & 0 deletions test/jdk/javax/swing/JTree/bug5039542.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2004, 2023, 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 5039542
* @summary JTree's setToolTipText() doesn't work
* @run main bug5039542
*/

import javax.swing.JTree;

public class bug5039542 {
public static void main(String[] args) throws Exception {
final String exampleStr = "TEST";
JTree tree = new JTree();
tree.setToolTipText(exampleStr);
if (tree.getToolTipText(null) != exampleStr) {
throw new RuntimeException("The default JTree tooltip text " +
"have to be used if renderer doesn't provide it.");
}
}
}
73 changes: 73 additions & 0 deletions test/jdk/javax/swing/JViewport/bug4546474.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2003, 2023, 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 4546474
* @summary JScrollPane's always-visible scrollbars not updated when
* viewport is replaced
* @run main bug4546474
*/

import java.awt.Dimension;
import java.awt.Robot;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class bug4546474 {
static JScrollPane scrollpane;
static JScrollBar sbar;
static volatile boolean viewChanged;

public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500, 500));
scrollpane = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
sbar = scrollpane.getVerticalScrollBar();
});

Robot robot = new Robot();
robot.delay(500);
SwingUtilities.invokeAndWait(() -> {
sbar.addAdjustmentListener(e -> viewChanged = true);
scrollpane.setViewportView(null);
});
robot.delay(500);
if (!viewChanged) {
viewChanged = true;
}
robot.delay(500);

SwingUtilities.invokeAndWait(() -> {
if (sbar.getVisibleAmount() > 0) {
throw new RuntimeException("Vertical scrollbar is not " +
"updated when viewport is replaced");
}
});
}
}
55 changes: 55 additions & 0 deletions test/jdk/javax/swing/JViewport/bug4677611.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2003, 2023, 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 4677611
* @summary JViewport sets Opaque after UpdateUI (prevents UI delegates
* to determine look)
* @run main bug4677611
*/

import java.awt.Color;

import javax.swing.JScrollPane;
import javax.swing.JViewport;

public class bug4677611 {
public static void main(String[] args) throws Exception {
JScrollPane sp = new JScrollPane();
JViewport vp = new MyViewport();
vp.setBackground(Color.blue);
sp.setViewport(vp);

if (vp.isOpaque()) {
throw new RuntimeException("JViewport shouldn't set Opaque " +
"after update the UI");
}
}

static class MyViewport extends JViewport {
public void updateUI() {
setOpaque(false);
super.updateUI();
}
}
}
118 changes: 118 additions & 0 deletions test/jdk/javax/swing/KeyboardManager/bug4345798.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2000, 2023, 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 4345798
* @summary Tests if Pressing enter to dismiss menu works when a JRootPane
* has a default button.
* @key headful
* @run main bug4345798
*/

import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

public class bug4345798 {
private static JFrame f;
private static JButton b;
private static JMenu menu;
private static volatile boolean passed = true;
private static volatile Point p;

public static void main(String[] args) throws Exception {
try {
SwingUtilities.invokeAndWait(() -> {
f = new JFrame("bug4345798");
JMenuBar mbar = new JMenuBar();
JMenuItem item = new JMenuItem("Open...");
menu = new JMenu("File");
item.addActionListener(new TestActionListener());
menu.add(item);
mbar.add(menu);

f.setJMenuBar(mbar);

b = new JButton("Default");
b.addActionListener(new TestActionListener());
f.getContentPane().add(b);
JRootPane rp = f.getRootPane();
rp.setDefaultButton(b);

f.setSize(200, 200);
f.setLocationRelativeTo(null);
f.setVisible(true);
b.requestFocus();
});

Robot robot = new Robot();
robot.setAutoDelay(100);
robot.waitForIdle();
robot.delay(1000);

SwingUtilities.invokeAndWait(() -> p = menu.getLocationOnScreen());
robot.mouseMove(p.x, p.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

robot.keyPress(KeyEvent.VK_F10);
robot.keyRelease(KeyEvent.VK_F10);

robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);

robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} finally {
SwingUtilities.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}

if (!passed) {
throw new RuntimeException("Test failed.");
}
}

static class TestActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
passed = false;
}
}
}
}

5 comments on commit a021dbc

@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 a021dbc Dec 17, 2024

Choose a reason for hiding this comment

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

/backport jdk21u-dev

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on a021dbc Dec 17, 2024

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 a021dbc Dec 17, 2024

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 backport-GoeLin-a021dbcb-master 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 a021dbcb from the openjdk/jdk repository.

The commit being backported was authored by Damon Nguyen on 20 Sep 2023 and was reviewed by Prasanta Sadhukhan and Tejesh R.

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.git backport-GoeLin-a021dbcb-master:backport-GoeLin-a021dbcb-master
$ git checkout backport-GoeLin-a021dbcb-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-a021dbcb-master

@openjdk
Copy link

@openjdk openjdk bot commented on a021dbc Dec 17, 2024

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 backport-GoeLin-a021dbcb-master in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-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 a021dbcb from the openjdk/jdk repository.

The commit being backported was authored by Damon Nguyen on 20 Sep 2023 and was reviewed by Prasanta Sadhukhan and Tejesh R.

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/jdk21u-dev:

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

Please sign in to comment.