1 /*
   2  * Copyright (c) 2008, 2018, 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 import java.awt.*;
  25 import java.awt.event.*;
  26 
  27 /*
  28   @test
  29   @key headful
  30   @bug 4411534 4517274
  31   @summary ensures that user's requestFocus() during applet initialization
  32            is not ignored
  33   @library ../../regtesthelpers
  34   @build   Util
  35   @run main AppletInitialFocusTest1
  36  */
  37 public class AppletInitialFocusTest1 extends Frame implements FocusListener {
  38 
  39     Button button1 = new Button("Button1");
  40     Button button2 = new Button("Button2");
  41 
  42     Object lock = new Object();
  43 
  44     public static void main(final String[] args) throws Exception {
  45         AppletInitialFocusTest1 app = new AppletInitialFocusTest1();
  46         app.init();
  47         Thread.sleep(10000);
  48     }
  49 
  50     public void init() {
  51         setSize(200, 200);
  52         setLocationRelativeTo(null);
  53         setLayout(new FlowLayout());
  54 
  55         Component parent = this;
  56         while (parent != null && !(parent instanceof Window)) {
  57             parent = parent.getParent();
  58         }
  59         /*
  60          * This applet is designed to be run only with appletviewer,
  61          * so there always should be a toplevel frame.
  62          */
  63         if (parent == null) {
  64             synchronized (lock) {
  65                 System.err.println("appletviewer not running");
  66                 System.exit(3);
  67             }
  68         }
  69         button1.addFocusListener(this);
  70         button2.addFocusListener(this);
  71         add(button1);
  72         add(button2);
  73         setVisible(true);
  74         button2.requestFocus();
  75     }
  76 
  77     public void focusGained(FocusEvent e) {
  78         if (e.getSource() == button1) {
  79             synchronized (lock) {
  80                 throw new RuntimeException("failed: focus on the wrong button");
  81             }
  82         }
  83     }
  84 
  85     public void focusLost(FocusEvent e) {
  86     }
  87 }