1 /*
   2  * Copyright (c) 2002, 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 /*
  25   test
  26   @bug 4092033 4529626
  27   @summary Tests that toFront makes window focused unless it is non-focusable
  28   @author  area=awt.focus
  29   @run applet ToFrontFocus.html
  30 */
  31 
  32 /**
  33  * ToFrontFocus.java
  34  *
  35  * summary:
  36  */
  37 
  38 import java.applet.Applet;
  39 import java.awt.*;
  40 import java.awt.event.*;
  41 import test.java.awt.regtesthelpers.Util;
  42 
  43 public class ToFrontFocus extends Applet
  44  {
  45    //Declare things used in the test, like buttons and labels here
  46 
  47      Frame cover, focus_frame, nonfocus_frame;
  48      Button focus_button, nonfocus_button;
  49      volatile boolean focus_gained = false, nonfocus_gained = false;
  50    public void init()
  51     {
  52       //Create instructions for the user here, as well as set up
  53       // the environment -- set the layout manager, add buttons,
  54       // etc.
  55 
  56       this.setLayout (new BorderLayout ());
  57 
  58       cover = new Frame("Cover frame");
  59       cover.setBounds(100, 100, 200, 200);
  60       focus_frame = new Frame("Focusable frame");
  61       focus_frame.setBounds(150, 100, 250, 150);
  62       nonfocus_frame = new Frame("Non-focusable frame");
  63       nonfocus_frame.setFocusableWindowState(false);
  64       nonfocus_frame.setBounds(150, 150, 250, 200);
  65       focus_button = new Button("Button in focusable frame");
  66       focus_button.addFocusListener(new FocusAdapter() {
  67               public void focusGained(FocusEvent e) {
  68                   focus_gained = true;
  69               }
  70           });
  71       nonfocus_button = new Button("Button in non-focusable frame");
  72       nonfocus_button.addFocusListener(new FocusAdapter() {
  73               public void focusGained(FocusEvent e) {
  74                   nonfocus_gained = true;
  75               }
  76           });
  77     }//End  init()
  78 
  79    public void start ()
  80     {
  81       //Get things going.  Request focus, set size, et cetera
  82       setSize (200,200);
  83       show();
  84       Util.waitForIdle(null);
  85 
  86       focus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
  87               public Component getInitialComponent(Window w) {
  88                   return null;
  89               }
  90           });
  91       focus_frame.setVisible(true);
  92       nonfocus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
  93               public Component getInitialComponent(Window w) {
  94                   return null;
  95               }
  96           });
  97       nonfocus_frame.setVisible(true);
  98       cover.setVisible(true);
  99 
 100       Util.waitForIdle(null);
 101 
 102       // So events are no generated at the creation add buttons here.
 103       focus_frame.add(focus_button);
 104       focus_frame.pack();
 105       focus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
 106               public Component getInitialComponent(Window w) {
 107                   return focus_button;
 108               }
 109           });
 110       nonfocus_frame.add(nonfocus_button);
 111       nonfocus_frame.pack();
 112       nonfocus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
 113               public Component getInitialComponent(Window w) {
 114                   return nonfocus_button;
 115               }
 116           });
 117 
 118       System.err.println("------------ Starting test ------------");
 119       // Make frame focused - focus_gained will be genereated for button.
 120       focus_frame.toFront();
 121       // focus_gained should not be generated
 122       nonfocus_frame.toFront();
 123 
 124       // Wait for events.
 125       Util.waitForIdle(null);
 126 
 127       if (!focus_gained || nonfocus_gained) {
 128           throw new RuntimeException("ToFront doesn't work as expected");
 129       }
 130     }// start()
 131 
 132  }// class ToFrontFocus