test/jdk/javax/swing/JTextPane/bug8025082.java
changeset 47813 fedbf1b866a7
parent 47812 b140fe4ff916
parent 47811 d76a6042f5d7
child 47814 19fad4c04a15
equal deleted inserted replaced
47812:b140fe4ff916 47813:fedbf1b866a7
     1 /*
       
     2  * Copyright (c) 2015, 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 8025082
       
    28  * @summary The behaviour of the highlight will be lost after clicking the set
       
    29  * button.
       
    30  * @run main bug8025082
       
    31  */
       
    32 import java.awt.*;
       
    33 import java.awt.event.ActionEvent;
       
    34 import java.awt.event.ActionListener;
       
    35 import java.awt.event.InputEvent;
       
    36 import javax.swing.*;
       
    37 
       
    38 public class bug8025082 {
       
    39 
       
    40     private static JButton button;
       
    41     private static JFrame frame;
       
    42 
       
    43     public static void main(String[] args) throws Exception {
       
    44         Robot robo = new Robot();
       
    45         robo.delay(500);
       
    46 
       
    47         SwingUtilities.invokeAndWait(new Runnable() {
       
    48             public void run() {
       
    49                 createUI();
       
    50             }
       
    51         });
       
    52 
       
    53         robo.waitForIdle();
       
    54         Point point = getButtonLocationOnScreen();
       
    55         robo.mouseMove(point.x, point.y);
       
    56         robo.mousePress(InputEvent.BUTTON1_MASK);
       
    57         robo.mouseRelease(InputEvent.BUTTON1_MASK);
       
    58         robo.waitForIdle();
       
    59 
       
    60         SwingUtilities.invokeAndWait(new Runnable() {
       
    61             public void run() {
       
    62                 frame.dispose();
       
    63             }
       
    64         });
       
    65     }
       
    66 
       
    67     private static void createUI() {
       
    68         frame = new JFrame();
       
    69         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    70         frame.setSize(500, 500);
       
    71         JTextPane textpane = new JTextPane();
       
    72         textpane.setText("Select Me");
       
    73         textpane.selectAll();
       
    74 
       
    75         JPanel panel = new JPanel(new BorderLayout());
       
    76         panel.add(textpane, BorderLayout.CENTER);
       
    77         button = new JButton("Press Me");
       
    78         panel.add(button, BorderLayout.SOUTH);
       
    79 
       
    80         button.addActionListener(new ActionListener() {
       
    81             @Override
       
    82             public void actionPerformed(ActionEvent e) {
       
    83                 if (!textpane.getCaret().isSelectionVisible()) {
       
    84                     throw new RuntimeException("Highlight removed after "
       
    85                             + "button click");
       
    86                 }
       
    87             }
       
    88         });
       
    89 
       
    90         frame.getContentPane().add(panel);
       
    91         frame.setLocationRelativeTo(null);
       
    92         frame.setVisible(true);
       
    93     }
       
    94 
       
    95     private static Point getButtonLocationOnScreen() throws Exception {
       
    96         final Point[] result = new Point[1];
       
    97 
       
    98         SwingUtilities.invokeAndWait(new Runnable() {
       
    99             @Override
       
   100             public void run() {
       
   101                 Point point = button.getLocationOnScreen();
       
   102                 point.x += button.getWidth() / 2;
       
   103                 point.y += button.getHeight() / 2;
       
   104                 result[0] = point;
       
   105             }
       
   106         });
       
   107         return result[0];
       
   108     }
       
   109 }