1 /*
   2  * Copyright (c) 2007, 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   @key headful
  27   @bug       6492970
  28   @summary   Tests that showing a toplvel in a not foreground Java process activates it.
  29   @library   ../../regtesthelpers
  30   @build     Util
  31   @author    Anton Tarasov: area=awt-focus
  32   @run       main ShowFrameCheckForegroundTest
  33  */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import java.applet.Applet;
  38 import java.util.concurrent.atomic.AtomicBoolean;
  39 import java.lang.reflect.InvocationTargetException;
  40 import test.java.awt.regtesthelpers.Util;
  41 
  42 public class ShowFrameCheckForegroundTest extends Applet {
  43     Robot robot;
  44     Frame nofocusFrame = new Frame("Non-focusable");
  45     Frame frame = new Frame("Frame");
  46     Dialog dialog1 = new Dialog(nofocusFrame, "Owned Dialog", false);
  47     Dialog dialog2 = new Dialog((Frame)null, "Owned Dialog", false);
  48     Window testToplevel = null;
  49     Button testButton = new Button("button");
  50     Button showButton = new Button("show");
  51     Runnable action = new Runnable() {
  52         public void run() {
  53             robot.keyPress(KeyEvent.VK_SPACE);
  54             robot.delay(50);
  55             robot.keyRelease(KeyEvent.VK_SPACE);
  56         }
  57     };
  58 
  59 
  60     public static void main(String[] args) {
  61         ShowFrameCheckForegroundTest app = new ShowFrameCheckForegroundTest();
  62         app.init();
  63         app.start();
  64     }
  65 
  66     public void init() {
  67         robot = Util.createRobot();
  68     }
  69 
  70     public void start() {
  71         showButton.addActionListener(new ActionListener() {
  72             public void actionPerformed(ActionEvent e) {
  73                 testToplevel.setVisible(true);
  74             }
  75         });
  76         nofocusFrame.add(showButton);
  77         nofocusFrame.pack();
  78         nofocusFrame.setFocusableWindowState(false);
  79         nofocusFrame.setVisible(true);
  80         Util.waitForIdle(robot);
  81 
  82         robot.delay(3000);
  83 
  84         // 1. Show the toplvel without clicking into the non-focusable frame.
  85         test(frame, 1);
  86         test(dialog1, 1);
  87         test(dialog2, 1);
  88 
  89         // 2. Showing the toplvel via clicking into the non-focusable frame.
  90         test(frame, 2);
  91         test(dialog1, 2);
  92         test(dialog2, 2);
  93 
  94         System.out.println("Test passed.");
  95     }
  96 
  97     private void test(Window toplevel, int stage) {
  98         toplevel.add(testButton);
  99         toplevel.pack();
 100         toplevel.setLocation(200, 0);
 101 
 102         switch (stage) {
 103             case 1:
 104                 toplevel.setVisible(true);
 105                 break;
 106             case 2:
 107                 testToplevel = toplevel;
 108                 Util.clickOnComp(showButton, robot);
 109                 break;
 110         }
 111         Util.waitForIdle(robot);
 112 
 113         if (!Util.trackActionPerformed(testButton, action, 2000, false)) {
 114             throw new TestFailedException("Stage " + stage + ". The toplevel " + toplevel + " wasn't made foreground on showing");
 115         }
 116         System.out.println("Stage " + stage + ". Toplevel " + toplevel + " - passed");
 117         toplevel.dispose();
 118     }
 119 }
 120 
 121 class TestFailedException extends RuntimeException {
 122     TestFailedException(String msg) {
 123         super("Test failed: " + msg);
 124     }
 125 }