1 /*
2  * Copyright (c) 2020, 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 8236953
28  * @summary JavaFX SwingNode is not rendered on macOS
29  * @modules java.desktop/sun.swing
30  */
31 
32 import java.awt.EventQueue;
33 import java.awt.event.ComponentAdapter;
34 import java.awt.event.ComponentEvent;
35 import java.util.concurrent.atomic.AtomicInteger;
36 
37 import javax.swing.JComponent;
38 import javax.swing.JPanel;
39 
40 import sun.swing.JLightweightFrame;
41 import sun.swing.LightweightContent;
42 
43 public final class ResizedMovedEvents {
44 
45     private static final AtomicInteger resized = new AtomicInteger();
46     private static final AtomicInteger moved = new AtomicInteger();
47     private static JLightweightFrame jLightweightFrame;
48 
main(String[] args)49     public static void main(String[] args) throws Exception {
50         emulateSwingNode(true);  // emulate visible node
51         emulateSwingNode(false); // emulate invisible node
52     }
53 
emulateSwingNode(boolean visible)54     private static void emulateSwingNode(boolean visible) throws Exception {
55         try {
56             EventQueue.invokeAndWait(() -> {
57                 jLightweightFrame = new JLightweightFrame();
58                 jLightweightFrame.setContent(new XLightweightContent());
59                 jLightweightFrame.addComponentListener(new ComponentAdapter() {
60                     @Override
61                     public void componentResized(ComponentEvent e) {
62                         resized.incrementAndGet();
63                     }
64 
65                     @Override
66                     public void componentMoved(ComponentEvent e) {
67                         moved.incrementAndGet();
68                     }
69                 });
70                 jLightweightFrame.setVisible(visible);
71             });
72 
73             // Some dummy initial location
74             setBounds(10, 10, 10, 10);
75 
76             // resize and move
77             resetFlags();
78             setBounds(100, 100, 100, 100);
79             checkFlags(1, 1);
80 
81             // resize only
82             resetFlags();
83             setBounds(100, 100, 200, 200);
84             checkFlags(1, 0);
85 
86             // move only
87             resetFlags();
88             setBounds(200, 200, 200, 200);
89             checkFlags(0, 1);
90         } finally {
91             if (jLightweightFrame != null) jLightweightFrame.dispose();
92         }
93     }
94 
setBounds(int x, int y, int w, int h)95     private static void setBounds(int x, int y, int w, int h) throws Exception {
96         EventQueue.invokeAndWait(() -> {
97             jLightweightFrame.setBounds(x, y, w, h);
98         });
99         EventQueue.invokeAndWait(() -> {
100             // dummy event to flush the EventQueue
101         });
102     }
103 
resetFlags()104     private static void resetFlags() {
105         resized.set(0);
106         moved.set(0);
107     }
108 
checkFlags(int expectedR, int expectedM)109     private static void checkFlags(int expectedR, int expectedM) {
110         int actualR = resized.get();
111         int actualM = moved.get();
112         //
113         if (actualR < expectedR) {
114             System.err.println("Expected: " + expectedR);
115             System.err.println("Actual: " + actualR);
116             throw new RuntimeException("Wrong number of COMPONENT_RESIZED");
117         }
118         if (actualM < expectedM) {
119             System.err.println("Expected: " + expectedM);
120             System.err.println("Actual: " + actualM);
121             throw new RuntimeException("Wrong number of COMPONENT_MOVED");
122         }
123     }
124 
125     static final class XLightweightContent implements LightweightContent {
126         @Override
getComponent()127         public JComponent getComponent() {
128             return new JPanel();
129         }
130 
131         @Override
paintLock()132         public void paintLock() {
133         }
134 
135         @Override
paintUnlock()136         public void paintUnlock() {
137         }
138 
139         @Override
imageBufferReset(int[] data, int x, int y, int width, int height, int linestride, double scaleX, double scaleY)140         public void imageBufferReset(int[] data, int x, int y, int width,
141                                      int height, int linestride,
142                                      double scaleX,
143                                      double scaleY) {
144         }
145 
146         @Override
imageReshaped(int x, int y, int width, int height)147         public void imageReshaped(int x, int y, int width, int height) {
148         }
149 
150         @Override
imageUpdated(int dirtyX, int dirtyY, int dirtyWidth, int dirtyHeight)151         public void imageUpdated(int dirtyX, int dirtyY, int dirtyWidth,
152                                  int dirtyHeight) {
153         }
154 
155         @Override
focusGrabbed()156         public void focusGrabbed() {
157         }
158 
159         @Override
focusUngrabbed()160         public void focusUngrabbed() {
161         }
162 
163         @Override
preferredSizeChanged(int width, int height)164         public void preferredSizeChanged(int width, int height) {
165         }
166 
167         @Override
maximumSizeChanged(int width, int height)168         public void maximumSizeChanged(int width, int height) {
169         }
170 
171         @Override
minimumSizeChanged(int width, int height)172         public void minimumSizeChanged(int width, int height) {
173         }
174     }
175 }
176