Skip to content

Commit 1705362

Browse files
committed
Backport 358ff196336407484b1b892f08936e9378701959
1 parent 682c939 commit 1705362

File tree

5 files changed

+649
-0
lines changed

5 files changed

+649
-0
lines changed

test/jdk/ProblemList.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ java/awt/Focus/NoAutotransferToDisabledCompTest/NoAutotransferToDisabledCompTest
134134
java/awt/Focus/TypeAhead/TestFocusFreeze.java 8198622,6447537 macosx-all,windows-all,linux-all
135135
java/awt/Focus/ToFrontFocusTest/ToFrontFocus.java 7156130 linux-all
136136
java/awt/Focus/WrongKeyTypedConsumedTest/WrongKeyTypedConsumedTest.java 8169096 macosx-all
137+
java/awt/Focus/TestDisabledAutoTransfer.java 8159871 macosx-all,windows-all
138+
java/awt/Focus/TestDisabledAutoTransferSwing.java 6962362 windows-all
139+
java/awt/Focus/ActivateOnProperAppContextTest.java 8136516 macosx-all
137140
java/awt/event/KeyEvent/CorrectTime/CorrectTime.java 6626492 generic-all
138141
java/awt/EventQueue/6980209/bug6980209.java 8198615 macosx-all
139142
java/awt/grab/EmbeddedFrameTest1/EmbeddedFrameTest1.java 7080150 macosx-all
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
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+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2001, 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 4402942
26+
* @summary After deactivation and activation of frame, focus should be restored correctlty
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @run main/manual KillFocusTest
30+
*/
31+
32+
import java.awt.Frame;
33+
import java.awt.TextField;
34+
import java.awt.event.FocusEvent;
35+
import java.awt.event.FocusListener;
36+
37+
public class KillFocusTest {
38+
39+
private static final String INSTRUCTIONS = """
40+
After starting the test you should see \"Test Frame\"
41+
with the \"Click me\" text field.
42+
Click on this text field and try to type something in it.
43+
Make sure that the field receives focus and you can enter text in it.
44+
Click on any non-java window.
45+
Click on \"Click me\" text field to return focus to it
46+
If the caret is in the text field and you are able to type
47+
in it then press pass else press fail.""";
48+
49+
public static void main(String[] args) throws Exception {
50+
PassFailJFrame.builder()
51+
.title("KillFocusTest Instructions")
52+
.instructions(INSTRUCTIONS)
53+
.rows((int) INSTRUCTIONS.lines().count() + 2)
54+
.columns(35)
55+
.testUI(KillFocusTest::createTestUI)
56+
.logArea()
57+
.build()
58+
.awaitAndCheck();
59+
}
60+
61+
private static Frame createTestUI() {
62+
63+
Frame frame = new Frame("KillFocusTest Frame");
64+
TextField textField = new TextField("Click me", 10);
65+
textField.addFocusListener(new FocusListener() {
66+
public void focusGained(FocusEvent fe) {
67+
PassFailJFrame.log("Focus gained");
68+
}
69+
public void focusLost(FocusEvent fe) {
70+
PassFailJFrame.log("Focus lost");
71+
}
72+
});
73+
frame.add(textField);
74+
frame.setSize(200, 100);
75+
return frame;
76+
}
77+
78+
79+
}
80+

0 commit comments

Comments
 (0)