1 /*
   2  * Copyright (c) 2007, 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       6516675
  27   @summary   Tests that EmbeddedFrame can be focused.
  28   @author    anton.tarasov: area=awt-focus
  29   @requires (os.family == "windows")
  30   @modules   java.desktop/java.awt.peer
  31              java.desktop/sun.awt
  32              java.desktop/sun.awt.windows
  33   @library /java/awt/patchlib    ../../regtesthelpers
  34   @build java.desktop/java.awt.Helper
  35   @build     Util UtilInternal
  36   @run       main FocusEmbeddedFrameTest
  37 */
  38 
  39 import java.awt.*;
  40 import java.awt.event.*;
  41 import java.applet.Applet;
  42 import java.util.concurrent.atomic.AtomicBoolean;
  43 import java.lang.reflect.InvocationTargetException;
  44 import test.java.awt.regtesthelpers.Util;
  45 import test.java.awt.regtesthelpers.UtilInternal;
  46 
  47 public class FocusEmbeddedFrameTest extends Applet {
  48     static Frame embedder = new Frame("Embedder");
  49     static Frame ef = null;
  50     static volatile boolean passed;
  51 
  52     Robot robot;
  53 
  54     public static void main(String[] args) {
  55         FocusEmbeddedFrameTest app = new FocusEmbeddedFrameTest();
  56         app.init();
  57         app.start();
  58     }
  59 
  60     public void init() {
  61         robot = Util.createRobot();
  62     }
  63 
  64     public void start() {
  65 
  66         if (!"sun.awt.windows.WToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  67             System.out.println("The test is for WToolkit only!");
  68             return;
  69         }
  70 
  71         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  72                 public void eventDispatched(AWTEvent e) {
  73                    System.err.println("--> " + e);
  74                 }
  75             }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
  76 
  77         embedder.addNotify();
  78 
  79         try {
  80             ef = UtilInternal.createEmbeddedFrame(embedder);
  81         } catch (Throwable t) {
  82             t.printStackTrace();
  83             throw new Error("Test error: couldn't create an EmbeddedFrame!");
  84         }
  85 
  86         ef.setSize(100, 100);
  87         ef.setBackground(Color.blue);
  88 
  89         embedder.addFocusListener(new FocusAdapter() {
  90                 public void focusGained(FocusEvent e) {
  91                     FocusEmbeddedFrameTest.ef.requestFocus();
  92                 }
  93             });
  94 
  95         ef.addFocusListener(new FocusAdapter() {
  96                 public void focusGained(FocusEvent e) {
  97                     passed = true;
  98                 }
  99             });
 100 
 101         embedder.setSize(150, 150);
 102         embedder.setVisible(true);
 103 
 104         Util.waitForIdle(robot);
 105 
 106         if (!passed) {
 107             throw new TestFailedException("the EmbeddedFrame didn't get focus on request!");
 108         }
 109 
 110         System.out.println("Test passed.");
 111     }
 112 }
 113 
 114 class TestFailedException extends RuntimeException {
 115     TestFailedException(String msg) {
 116         super("Test failed: " + msg);
 117     }
 118 }