1 /*
   2  * Copyright (c) 2017, 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 import javax.swing.*;
  25 import java.awt.*;
  26 import java.awt.event.InputEvent;
  27 import java.awt.image.BufferedImage;
  28 
  29 /**
  30  * @test
  31  * @bug 8188081
  32  * @summary  Text selection does not clear after focus is lost
  33  * @run main HidingSelectionTest
  34  */
  35 
  36 public class HidingSelectionTest {
  37 
  38     private static JTextField field1;
  39     private static JTextField field2;
  40     private static JFrame frame;
  41     private static Rectangle bounds;
  42     private static JMenu menu;
  43     private static JTextField anotherWindow;
  44     private static Point menuLoc;
  45     private static JFrame frame2;
  46 
  47     public static void main(String[] args) throws Exception {
  48         SwingUtilities.invokeAndWait(() -> {
  49             frame = new JFrame();
  50             field1 = new JTextField("field1                       ");
  51             field2 = new JTextField("field2                       ");
  52             field1.setEditable(false);
  53             field2.setEditable(false);
  54             frame.getContentPane().setLayout(new FlowLayout());
  55             frame.getContentPane().add(field1);
  56             frame.getContentPane().add(field2);
  57             JMenuBar menuBar = new JMenuBar();
  58             menu = new JMenu("menu");
  59             menu.add(new JMenuItem("item"));
  60             menuBar.add(menu);
  61             frame.setJMenuBar(menuBar);
  62             frame.pack();
  63             frame.setVisible(true);
  64         });
  65 
  66         Robot robot = new Robot();
  67         robot.waitForIdle();
  68         robot.delay(200);
  69         SwingUtilities.invokeAndWait(() -> {
  70             bounds = field2.getBounds();
  71             bounds.setLocation(field2.getLocationOnScreen());
  72         });
  73         BufferedImage nosel = robot.createScreenCapture(bounds);
  74 
  75         SwingUtilities.invokeAndWait(field2::requestFocus);
  76         SwingUtilities.invokeAndWait(field2::selectAll);
  77         robot.waitForIdle();
  78         robot.delay(200);
  79         BufferedImage sel = robot.createScreenCapture(bounds);
  80 
  81         SwingUtilities.invokeAndWait(() -> {
  82             menuLoc = menu.getLocationOnScreen();
  83             menuLoc.translate(10, 10);
  84         });
  85         robot.mouseMove(menuLoc.x, menuLoc.y);
  86         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  87         robot.delay(50);
  88         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  89         robot.waitForIdle();
  90         robot.delay(200);
  91         if (!biEqual(robot.createScreenCapture(bounds), sel)) {
  92             throw new RuntimeException("Test fails: menu hides selection");
  93         }
  94 
  95         SwingUtilities.invokeAndWait(
  96                       MenuSelectionManager.defaultManager()::clearSelectedPath);
  97         SwingUtilities.invokeAndWait(field1::requestFocus);
  98         robot.waitForIdle();
  99         robot.delay(200);
 100         if (!biEqual(robot.createScreenCapture(bounds), nosel)) {
 101             throw new RuntimeException(
 102                     "Test fails: focus lost doesn't hide selection");
 103         }
 104 
 105         SwingUtilities.invokeAndWait(field2::requestFocus);
 106         robot.waitForIdle();
 107         SwingUtilities.invokeAndWait(() ->{
 108             frame2 = new JFrame();
 109             Point loc = frame.getLocationOnScreen();
 110             loc.translate(0, frame.getHeight());
 111             frame2.setLocation(loc);
 112             anotherWindow = new JTextField("textField3");
 113             frame2.add(anotherWindow);
 114             frame2.pack();
 115             frame2.setVisible(true);
 116         });
 117         robot.waitForIdle();
 118         SwingUtilities.invokeAndWait(anotherWindow::requestFocus);
 119         robot.waitForIdle();
 120         robot.delay(200);
 121         if (biEqual(robot.createScreenCapture(bounds), nosel)) {
 122             throw new RuntimeException(
 123                     "Test fails: switch window hides selection");
 124         }
 125 
 126         SwingUtilities.invokeAndWait(anotherWindow::selectAll);
 127         robot.waitForIdle();
 128         robot.delay(200);
 129         if (biEqual(robot.createScreenCapture(bounds), sel)) {
 130             throw new RuntimeException(
 131                 "Test fails: selection ownership is lost selection is shown");
 132         }
 133 
 134         SwingUtilities.invokeLater(frame2::dispose);
 135         SwingUtilities.invokeLater(frame::dispose);
 136     }
 137 
 138     static boolean biEqual(BufferedImage i1, BufferedImage i2) {
 139         if (i1.getWidth() == i2.getWidth() &&
 140                                          i1.getHeight() == i2.getHeight()) {
 141             for (int x = 0; x < i1.getWidth(); x++) {
 142                 for (int y = 0; y < i1.getHeight(); y++) {
 143                     if (i1.getRGB(x, y) != i2.getRGB(x, y)) {
 144                         return false;
 145                     }
 146                 }
 147             }
 148             return true;
 149         }
 150         return false;
 151     }
 152 }