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