|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2024, 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 | +/* @test |
| 25 | + * @bug 4325606 |
| 26 | + * @summary Tests getting row start |
| 27 | + * @key headful |
| 28 | + * @run main bug4325606 |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.AWTException; |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.Point; |
| 34 | +import java.awt.Robot; |
| 35 | +import java.awt.event.InputEvent; |
| 36 | +import java.awt.event.MouseAdapter; |
| 37 | +import java.awt.event.MouseEvent; |
| 38 | +import java.awt.event.WindowAdapter; |
| 39 | +import java.awt.event.WindowEvent; |
| 40 | +import java.lang.reflect.InvocationTargetException; |
| 41 | +import javax.swing.JEditorPane; |
| 42 | +import javax.swing.JFrame; |
| 43 | +import javax.swing.JScrollPane; |
| 44 | +import javax.swing.SwingUtilities; |
| 45 | +import javax.swing.text.BadLocationException; |
| 46 | +import javax.swing.text.Utilities; |
| 47 | + |
| 48 | +public class bug4325606 { |
| 49 | + |
| 50 | + public volatile boolean passed = true; |
| 51 | + private JFrame frame; |
| 52 | + private JEditorPane pane; |
| 53 | + |
| 54 | + public void setupGUI() { |
| 55 | + frame = new JFrame("Click Bug"); |
| 56 | + frame.setLayout(new BorderLayout()); |
| 57 | + |
| 58 | + pane = new JEditorPane(); |
| 59 | + pane.addMouseListener(new ClickListener()); |
| 60 | + pane.setContentType("text/html"); |
| 61 | + pane.setText("<html><body>" + |
| 62 | + "<p>Here is line one</p>" + |
| 63 | + "<p>Here is line two</p>" + |
| 64 | + "</body></html>"); |
| 65 | + |
| 66 | + frame.add(new JScrollPane(pane), BorderLayout.CENTER); |
| 67 | + |
| 68 | + frame.addWindowListener(new TestStateListener()); |
| 69 | + frame.setLocation(50, 50); |
| 70 | + frame.setSize(400, 300); |
| 71 | + frame.setVisible(true); |
| 72 | + } |
| 73 | + |
| 74 | + class TestStateListener extends WindowAdapter { |
| 75 | + public void windowOpened(WindowEvent ev) { |
| 76 | + Robot robo; |
| 77 | + try { |
| 78 | + robo = new Robot(); |
| 79 | + } catch (AWTException e) { |
| 80 | + throw new RuntimeException("Robot could not be created", e); |
| 81 | + } |
| 82 | + robo.setAutoDelay(100); |
| 83 | + robo.delay(1000); |
| 84 | + Point p = frame.getLocationOnScreen(); |
| 85 | + robo.mouseMove(p.x + 50, p.y + 50); |
| 86 | + robo.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 87 | + robo.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 88 | + robo.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 89 | + robo.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 90 | + robo.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 91 | + robo.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + class ClickListener extends MouseAdapter { |
| 96 | + public void mouseClicked(MouseEvent event) { |
| 97 | + try { |
| 98 | + Utilities.getRowStart(pane, pane.getCaretPosition()); |
| 99 | + } catch (BadLocationException blex) { |
| 100 | + throw new RuntimeException("Test failed.", blex); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public void cleanupGUI() { |
| 106 | + if (frame != null) { |
| 107 | + frame.setVisible(false); |
| 108 | + frame.dispose(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public static void main(String[] args) throws InterruptedException, |
| 113 | + InvocationTargetException, AWTException { |
| 114 | + bug4325606 b = new bug4325606(); |
| 115 | + try { |
| 116 | + Robot robot = new Robot(); |
| 117 | + SwingUtilities.invokeAndWait(b::setupGUI); |
| 118 | + robot.waitForIdle(); |
| 119 | + } finally { |
| 120 | + SwingUtilities.invokeAndWait(b::cleanupGUI); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments