11/*
2- * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- import javax .swing .JFrame ;
25- import javax .swing .JMenu ;
26- import javax .swing .JMenuBar ;
27- import javax .swing .JMenuItem ;
28- import javax .swing .JTextField ;
29- import javax .swing .MenuSelectionManager ;
30- import javax .swing .SwingUtilities ;
31- import java .awt .FlowLayout ;
32- import java .awt .Point ;
33- import java .awt .Robot ;
24+ import javax .swing .*;
25+ import java .awt .*;
3426import java .awt .event .InputEvent ;
27+ import java .awt .image .BufferedImage ;
3528
3629/**
3730 * @test
@@ -46,6 +39,7 @@ public class HidingSelectionTest {
4639 private static JTextField field1 ;
4740 private static JTextField field2 ;
4841 private static JFrame frame ;
42+ private static Rectangle bounds ;
4943 private static JMenu menu ;
5044 private static JTextField anotherWindow ;
5145 private static Point menuLoc ;
@@ -73,9 +67,17 @@ public static void main(String[] args) throws Exception {
7367 Robot robot = new Robot ();
7468 robot .waitForIdle ();
7569 robot .delay (200 );
70+ SwingUtilities .invokeAndWait (() -> {
71+ bounds = field2 .getBounds ();
72+ bounds .setLocation (field2 .getLocationOnScreen ());
73+ });
74+ BufferedImage nosel = robot .createScreenCapture (bounds );
7675
7776 SwingUtilities .invokeAndWait (field2 ::requestFocus );
7877 SwingUtilities .invokeAndWait (field2 ::selectAll );
78+ robot .waitForIdle ();
79+ robot .delay (200 );
80+ BufferedImage sel = robot .createScreenCapture (bounds );
7981
8082 SwingUtilities .invokeAndWait (() -> {
8183 menuLoc = menu .getLocationOnScreen ();
@@ -87,7 +89,7 @@ public static void main(String[] args) throws Exception {
8789 robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
8890 robot .waitForIdle ();
8991 robot .delay (200 );
90- if (!field2 . getCaret (). isSelectionVisible ( )) {
92+ if (!biEqual ( robot . createScreenCapture ( bounds ), sel )) {
9193 throw new RuntimeException ("Test fails: menu hides selection" );
9294 }
9395
@@ -96,7 +98,7 @@ public static void main(String[] args) throws Exception {
9698 SwingUtilities .invokeAndWait (field1 ::requestFocus );
9799 robot .waitForIdle ();
98100 robot .delay (200 );
99- if (field2 . getCaret (). isSelectionVisible ( )) {
101+ if (! biEqual ( robot . createScreenCapture ( bounds ), nosel )) {
100102 throw new RuntimeException (
101103 "Test fails: focus lost doesn't hide selection" );
102104 }
@@ -117,12 +119,35 @@ public static void main(String[] args) throws Exception {
117119 SwingUtilities .invokeAndWait (anotherWindow ::requestFocus );
118120 robot .waitForIdle ();
119121 robot .delay (200 );
120- if (! field2 . getCaret (). isSelectionVisible ( )) {
122+ if (biEqual ( robot . createScreenCapture ( bounds ), nosel )) {
121123 throw new RuntimeException (
122124 "Test fails: switch window hides selection" );
123125 }
124126
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+
125135 SwingUtilities .invokeLater (frame2 ::dispose );
126136 SwingUtilities .invokeLater (frame ::dispose );
127137 }
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+ }
128153}
0 commit comments