Skip to content
Permalink
Browse files
8253682: The AppletInitialFocusTest1.java is unstable
Backport-of: 1d88172
  • Loading branch information
GoeLin committed Sep 20, 2021
1 parent 48e5f41 commit 1bed6cda0f90e5ed6ce9997778029ca62e7dccfd
Showing with 31 additions and 45 deletions.
  1. +31 −45 test/jdk/java/awt/Focus/AppletInitialFocusTest/AppletInitialFocusTest1.java
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
@@ -21,65 +21,51 @@
* questions.
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

/*
@test
@key headful
@bug 4411534 4517274
@summary ensures that user's requestFocus() during applet initialization
is not ignored
@library ../../regtesthelpers
@build Util
@run main AppletInitialFocusTest1
/**
* @test
* @key headful
* @bug 4411534 4517274
* @summary ensures that user's requestFocus() during applet initialization
* is not ignored
*/
public class AppletInitialFocusTest1 extends Frame implements FocusListener {

Button button1 = new Button("Button1");
Button button2 = new Button("Button2");

Object lock = new Object();
private static volatile Object focused;

public static void main(final String[] args) throws Exception {
AppletInitialFocusTest1 app = new AppletInitialFocusTest1();
app.init();
Thread.sleep(10000);
}

public void init() {
setSize(200, 200);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
try {
app.setSize(200, 200);
app.setLocationRelativeTo(null);
app.setLayout(new FlowLayout());

Component parent = this;
while (parent != null && !(parent instanceof Window)) {
parent = parent.getParent();
}
/*
* 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);
app.button1.addFocusListener(app);
app.button2.addFocusListener(app);
app.add(app.button1);
app.add(app.button2);
app.setVisible(true);
app.button2.requestFocus();
// wait for the very very last focus event
Thread.sleep(10000);
if (app.button2 != focused) {
throw new RuntimeException("Wrong focus owner: " + focused);
}
} finally {
app.dispose();
}
button1.addFocusListener(this);
button2.addFocusListener(this);
add(button1);
add(button2);
setVisible(true);
button2.requestFocus();
}

public void focusGained(FocusEvent e) {
if (e.getSource() == button1) {
synchronized (lock) {
throw new RuntimeException("failed: focus on the wrong button");
}
}
focused = e.getSource();
System.out.println("focused = " + focused);
}

public void focusLost(FocusEvent e) {

1 comment on commit 1bed6cd

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 1bed6cd Sep 20, 2021

Please sign in to comment.