|
| 1 | +/* |
| 2 | + * Copyright (c) 2006, 2024, 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 | +* @test |
| 25 | +* @bug 6385277 |
| 26 | +* @key headful |
| 27 | +* @summary Tests that activation happens on correct AppContext. |
| 28 | +* @modules java.desktop/sun.awt |
| 29 | +* @run main ActivateOnProperAppContextTest |
| 30 | +*/ |
| 31 | + |
| 32 | +import sun.awt.AppContext; |
| 33 | +import sun.awt.SunToolkit; |
| 34 | + |
| 35 | +import java.awt.Button; |
| 36 | +import java.awt.Component; |
| 37 | +import java.awt.Container; |
| 38 | +import java.awt.Cursor; |
| 39 | +import java.awt.Dimension; |
| 40 | +import java.awt.FlowLayout; |
| 41 | +import java.awt.Frame; |
| 42 | +import java.awt.Label; |
| 43 | +import java.awt.Point; |
| 44 | +import java.awt.Robot; |
| 45 | +import java.awt.Toolkit; |
| 46 | +import java.awt.Window; |
| 47 | +import java.awt.event.InputEvent; |
| 48 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 49 | + |
| 50 | +public class ActivateOnProperAppContextTest { |
| 51 | + static Robot robot; |
| 52 | + SunToolkit toolkit; |
| 53 | + |
| 54 | + ThreadGroup threadGroup = new ThreadGroup("Test_Thread_Group"); |
| 55 | + AppContext appContext; |
| 56 | + Frame frame; |
| 57 | + volatile boolean passed = true; |
| 58 | + AtomicBoolean cond = new AtomicBoolean(false); |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + ActivateOnProperAppContextTest app = new ActivateOnProperAppContextTest(); |
| 62 | + robot = new Robot(); |
| 63 | + app.start(); |
| 64 | + } |
| 65 | + |
| 66 | + public void start() { |
| 67 | + toolkit = (SunToolkit)Toolkit.getDefaultToolkit(); |
| 68 | + |
| 69 | + Runnable runnable = new Runnable() { |
| 70 | + public void run() { |
| 71 | + test(); |
| 72 | + |
| 73 | + synchronized (cond) { |
| 74 | + cond.set(true); |
| 75 | + cond.notifyAll(); |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + Thread thread = new Thread(threadGroup, runnable, "Test Thread"); |
| 81 | + |
| 82 | + synchronized (cond) { |
| 83 | + |
| 84 | + thread.start(); |
| 85 | + |
| 86 | + while (!cond.get()) { |
| 87 | + try { |
| 88 | + cond.wait(); |
| 89 | + } catch (InterruptedException ie) { |
| 90 | + ie.printStackTrace(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (passed) { |
| 96 | + System.out.println("Test passed."); |
| 97 | + } else { |
| 98 | + throw new TestFailedException("Test failed!"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + void test() { |
| 103 | + appContext = SunToolkit.createNewAppContext(); |
| 104 | + System.out.println("Created new AppContext: " + appContext); |
| 105 | + |
| 106 | + frame = new Frame("ActivateOnProperAppContextTest Frame") { |
| 107 | + public boolean isActive() { |
| 108 | + verifyAppContext("Frame.isActive()"); |
| 109 | + return super.isActive(); |
| 110 | + } |
| 111 | + public boolean isFocused() { |
| 112 | + verifyAppContext("Frame.isFocused()"); |
| 113 | + return super.isFocused(); |
| 114 | + } |
| 115 | + public boolean isFocusable() { |
| 116 | + verifyAppContext("Frame.isFocusable()"); |
| 117 | + return super.isFocusable(); |
| 118 | + } |
| 119 | + public Window getOwner() { |
| 120 | + verifyAppContext("Frame.getOwner()"); |
| 121 | + return super.getOwner(); |
| 122 | + } |
| 123 | + public boolean isEnabled() { |
| 124 | + verifyAppContext("Frame.isEnabled()"); |
| 125 | + return super.isEnabled(); |
| 126 | + } |
| 127 | + public boolean isVisible() { |
| 128 | + verifyAppContext("Frame.isVisible()"); |
| 129 | + return super.isVisible(); |
| 130 | + } |
| 131 | + public Container getParent() { |
| 132 | + verifyAppContext("Frame.getParent()"); |
| 133 | + return super.getParent(); |
| 134 | + } |
| 135 | + public Cursor getCursor() { |
| 136 | + verifyAppContext("Frame.getCursor()"); |
| 137 | + return super.getCursor(); |
| 138 | + } |
| 139 | + public Point getLocation() { |
| 140 | + verifyAppContext("Frame.getLocation()"); |
| 141 | + return super.getLocation(); |
| 142 | + } |
| 143 | + public Point getLocationOnScreen() { |
| 144 | + verifyAppContext("Frame.getLocationOnScreen()"); |
| 145 | + return super.getLocationOnScreen(); |
| 146 | + } |
| 147 | + }; |
| 148 | + Window window = new Window(frame) { |
| 149 | + public boolean isFocused() { |
| 150 | + verifyAppContext("Window.isFocused()"); |
| 151 | + return super.isFocused(); |
| 152 | + } |
| 153 | + public boolean isFocusable() { |
| 154 | + verifyAppContext("Window.isFocusable()"); |
| 155 | + return super.isFocusable(); |
| 156 | + } |
| 157 | + public Window getOwner() { |
| 158 | + verifyAppContext("Window.getOwner()"); |
| 159 | + return super.getOwner(); |
| 160 | + } |
| 161 | + public boolean isEnabled() { |
| 162 | + verifyAppContext("Window.isEnabled()"); |
| 163 | + return super.isEnabled(); |
| 164 | + } |
| 165 | + public boolean isVisible() { |
| 166 | + verifyAppContext("Window.isVisible()"); |
| 167 | + return super.isVisible(); |
| 168 | + } |
| 169 | + public Container getParent() { |
| 170 | + verifyAppContext("Window.getParent()"); |
| 171 | + return super.getParent(); |
| 172 | + } |
| 173 | + public Cursor getCursor() { |
| 174 | + verifyAppContext("Window.getCursor()"); |
| 175 | + return super.getCursor(); |
| 176 | + } |
| 177 | + public Point getLocation() { |
| 178 | + verifyAppContext("Window.getLocation()"); |
| 179 | + return super.getLocation(); |
| 180 | + } |
| 181 | + public Point getLocationOnScreen() { |
| 182 | + verifyAppContext("Window.getLocationOnScreen()"); |
| 183 | + return super.getLocationOnScreen(); |
| 184 | + } |
| 185 | + }; |
| 186 | + Button button = new Button("button"); |
| 187 | + Label label = new Label("label"); |
| 188 | + |
| 189 | + window.setLayout(new FlowLayout()); |
| 190 | + window.add(button); |
| 191 | + window.add(label); |
| 192 | + window.setLocation(800, 0); |
| 193 | + window.pack(); |
| 194 | + window.setVisible(true); |
| 195 | + |
| 196 | + frame.setBounds(800, 100, 100, 50); |
| 197 | + frame.setVisible(true); |
| 198 | + |
| 199 | + toolkit.realSync(); |
| 200 | + |
| 201 | + /* |
| 202 | + * When the label is clicked in the window some of |
| 203 | + * the owner's public method get called. |
| 204 | + */ |
| 205 | + clickOn(label); |
| 206 | + } |
| 207 | + |
| 208 | + void verifyAppContext(String methodName) { |
| 209 | + AppContext ac = AppContext.getAppContext(); |
| 210 | + println(methodName + " called on AppContext: " + ac); |
| 211 | + |
| 212 | + if (ac != appContext) { |
| 213 | + passed = false; |
| 214 | + System.err.println("Test failed: " + methodName + " is called on wrong AppContext!"); |
| 215 | + Thread.dumpStack(); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + void clickOn(Component c) { |
| 220 | + Point p = c.getLocationOnScreen(); |
| 221 | + Dimension d = c.getSize(); |
| 222 | + |
| 223 | + robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2)); |
| 224 | + |
| 225 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 226 | + robot.delay(20); |
| 227 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 228 | + |
| 229 | + toolkit.realSync(); |
| 230 | + } |
| 231 | + |
| 232 | + void println(final String msg) { |
| 233 | + SunToolkit.executeOnEventHandlerThread(frame, new Runnable() { |
| 234 | + public void run() { |
| 235 | + System.out.println(msg); |
| 236 | + } |
| 237 | + }); |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +class TestFailedException extends RuntimeException { |
| 242 | + TestFailedException(String msg) { |
| 243 | + super(msg); |
| 244 | + } |
| 245 | +} |
0 commit comments