|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @key headful |
| 27 | + * @bug 4217252 |
| 28 | + * @summary Verify that scrolling beyond the visible region and scrolling |
| 29 | + * a component smaller than the viewport is not allowed. |
| 30 | + * @library /javax/swing/regtesthelpers |
| 31 | + * @build Util |
| 32 | + * @run main/othervm -Dsun.java2d.uiScale=1 ScrollRectToVisibleTest3 |
| 33 | + */ |
| 34 | + |
| 35 | +import java.awt.BorderLayout; |
| 36 | +import java.awt.Component; |
| 37 | +import java.awt.Dimension; |
| 38 | +import java.awt.Point; |
| 39 | +import java.awt.Rectangle; |
| 40 | +import java.awt.Robot; |
| 41 | +import java.awt.event.MouseAdapter; |
| 42 | +import java.awt.event.MouseEvent; |
| 43 | +import javax.swing.JButton; |
| 44 | +import javax.swing.JFrame; |
| 45 | +import javax.swing.JScrollPane; |
| 46 | +import javax.swing.JTable; |
| 47 | +import javax.swing.SwingUtilities; |
| 48 | +import javax.swing.table.AbstractTableModel; |
| 49 | + |
| 50 | +public class ScrollRectToVisibleTest3 { |
| 51 | + private static JFrame frame; |
| 52 | + private static JTable table; |
| 53 | + private static JButton scrollButton; |
| 54 | + private static volatile int clickCount = 0; |
| 55 | + private static final String[] EXPECTED_TEXT = {"99 x 0", "98 x 0", |
| 56 | + "97 x 0", "96 x 0"}; |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + Robot robot = new Robot(); |
| 59 | + robot.setAutoWaitForIdle(true); |
| 60 | + |
| 61 | + SwingUtilities.invokeAndWait(ScrollRectToVisibleTest3::createTestUI); |
| 62 | + robot.waitForIdle(); |
| 63 | + robot.delay(1000); |
| 64 | + |
| 65 | + Rectangle frameBounds = Util.invokeOnEDT(() -> getComponentBounds(frame)); |
| 66 | + robot.delay(100); |
| 67 | + Point scrollBtnLoc = Util.getCenterPoint(scrollButton); |
| 68 | + |
| 69 | + robot.mouseMove(scrollBtnLoc.x, scrollBtnLoc.y); |
| 70 | + robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); |
| 71 | + robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); |
| 72 | + robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); |
| 73 | + robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); |
| 74 | + robot.delay(50); |
| 75 | + |
| 76 | + int rowHeight = Util.invokeOnEDT(() -> table.getRowHeight()); |
| 77 | + for (int i = 1; i <= 4; i++) { |
| 78 | + robot.mouseMove(frameBounds.x + 50, |
| 79 | + frameBounds.y + frameBounds.height - (rowHeight * i + 2)); |
| 80 | + robot.delay(300); |
| 81 | + robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK); |
| 82 | + robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK); |
| 83 | + // 500 ms delay added so that current mouseClicked event |
| 84 | + // is processed successfully before proceeding to the next |
| 85 | + robot.delay(500); |
| 86 | + } |
| 87 | + if (clickCount != 4) { |
| 88 | + throw new RuntimeException("Test Failed! Expected 4 mouse clicks" |
| 89 | + + " but got " + clickCount); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void createTestUI() { |
| 94 | + frame = new JFrame("ScrollRectToVisibleTest3"); |
| 95 | + table = new JTable(new TestModel()); |
| 96 | + table.addMouseListener(new MouseAdapter() { |
| 97 | + @Override |
| 98 | + public void mouseClicked(MouseEvent e) { |
| 99 | + JTable testTable = (JTable) e.getComponent(); |
| 100 | + int row = testTable.getSelectedRow(); |
| 101 | + int column = testTable.getSelectedColumn(); |
| 102 | + String cellContent = testTable.getValueAt(row, column).toString(); |
| 103 | + if (!EXPECTED_TEXT[clickCount].equals(cellContent)) { |
| 104 | + throw new RuntimeException(("Test failed! Table Cell Content" |
| 105 | + + " at (row %d , col %d)\n Expected: %s vs Actual: %s") |
| 106 | + .formatted(row, column, |
| 107 | + EXPECTED_TEXT[clickCount], cellContent)); |
| 108 | + } |
| 109 | + clickCount++; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + scrollButton = new JButton("Scroll"); |
| 114 | + scrollButton.addActionListener(ae -> { |
| 115 | + Rectangle bounds = table.getBounds(); |
| 116 | + bounds.y = bounds.height + table.getRowHeight(); |
| 117 | + bounds.height = table.getRowHeight(); |
| 118 | + System.out.println("scrolling: " + bounds); |
| 119 | + table.scrollRectToVisible(bounds); |
| 120 | + System.out.println("bounds: " + table.getVisibleRect()); |
| 121 | + }); |
| 122 | + |
| 123 | + frame.add(scrollButton, BorderLayout.NORTH); |
| 124 | + frame.add(new JScrollPane(table), BorderLayout.CENTER); |
| 125 | + frame.setSize(400, 300); |
| 126 | + frame.setLocationRelativeTo(null); |
| 127 | + frame.setVisible(true); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + private static class TestModel extends AbstractTableModel { |
| 132 | + @Override |
| 133 | + public String getColumnName(int column) { |
| 134 | + return Integer.toString(column); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public int getRowCount() { |
| 139 | + return 100; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public int getColumnCount() { |
| 144 | + return 5; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Object getValueAt(int row, int col) { |
| 149 | + return row + " x " + col; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean isCellEditable(int row, int column) { return false; } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void setValueAt(Object value, int row, int col) { |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private static Rectangle getComponentBounds(Component c) { |
| 161 | + Point locationOnScreen = c.getLocationOnScreen(); |
| 162 | + Dimension size = c.getSize(); |
| 163 | + return new Rectangle(locationOnScreen, size); |
| 164 | + } |
| 165 | +} |
0 commit comments