8253682: The AppletInitialFocusTest1.java is unstable

Reviewed-by: jdv
This commit is contained in:
Sergey Bylokhov 2020-10-01 08:10:32 +00:00
parent 5dd9353b83
commit 1d88172c06

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -21,65 +21,51 @@
* questions. * questions.
*/ */
import java.awt.*; import java.awt.Button;
import java.awt.event.*; import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
/* /**
@test * @test
@key headful * @key headful
@bug 4411534 4517274 * @bug 4411534 4517274
@summary ensures that user's requestFocus() during applet initialization * @summary ensures that user's requestFocus() during applet initialization
is not ignored * is not ignored
@library ../../regtesthelpers
@build Util
@run main AppletInitialFocusTest1
*/ */
public class AppletInitialFocusTest1 extends Frame implements FocusListener { public class AppletInitialFocusTest1 extends Frame implements FocusListener {
Button button1 = new Button("Button1"); Button button1 = new Button("Button1");
Button button2 = new Button("Button2"); Button button2 = new Button("Button2");
private static volatile Object focused;
Object lock = new Object();
public static void main(final String[] args) throws Exception { public static void main(final String[] args) throws Exception {
AppletInitialFocusTest1 app = new AppletInitialFocusTest1(); AppletInitialFocusTest1 app = new AppletInitialFocusTest1();
app.init(); try {
Thread.sleep(10000); app.setSize(200, 200);
} app.setLocationRelativeTo(null);
app.setLayout(new FlowLayout());
public void init() { app.button1.addFocusListener(app);
setSize(200, 200); app.button2.addFocusListener(app);
setLocationRelativeTo(null); app.add(app.button1);
setLayout(new FlowLayout()); app.add(app.button2);
app.setVisible(true);
Component parent = this; app.button2.requestFocus();
while (parent != null && !(parent instanceof Window)) { // wait for the very very last focus event
parent = parent.getParent(); Thread.sleep(10000);
} if (app.button2 != focused) {
/* throw new RuntimeException("Wrong focus owner: " + focused);
* This applet is designed to be run only with appletviewer,
* so there always should be a toplevel frame.
*/
if (parent == null) {
synchronized (lock) {
System.err.println("appletviewer not running");
System.exit(3);
} }
} finally {
app.dispose();
} }
button1.addFocusListener(this);
button2.addFocusListener(this);
add(button1);
add(button2);
setVisible(true);
button2.requestFocus();
} }
public void focusGained(FocusEvent e) { public void focusGained(FocusEvent e) {
if (e.getSource() == button1) { focused = e.getSource();
synchronized (lock) { System.out.println("focused = " + focused);
throw new RuntimeException("failed: focus on the wrong button");
}
}
} }
public void focusLost(FocusEvent e) { public void focusLost(FocusEvent e) {