1 /*
   2  * Copyright (c) 2015, 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 /* @test
  25    @bug 8139227
  26    @summary Text fields in JPopupMenu structure do not receive focus in hosted
  27             Applets
  28    @author Semyon Sadetsky
  29    @run applet FindOwnerTest.html
  30 */
  31 
  32 import java.applet.Applet;
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import javax.swing.*;
  36 
  37 public class FindOwnerTest extends Applet
  38 {
  39 
  40     private boolean gained;
  41 
  42     public void init() {
  43         super.init();
  44     }
  45 
  46     @Override
  47     public void start() {
  48         Window owner = SwingUtilities.windowForComponent(this);
  49 
  50         Window window1 = new Window(owner);
  51         window1.setVisible(true);
  52 
  53         Window window2 = new Window(window1);
  54         window2.setFocusable(true);
  55         JTextField field = new JTextField("JTextField");
  56         field.addFocusListener(new FocusListener() {
  57             @Override
  58             public void focusGained(FocusEvent e) {
  59                 gained = true;
  60             }
  61 
  62             @Override
  63             public void focusLost(FocusEvent e) {
  64             }
  65         });
  66         window2.setBounds(100, 100, 200, 200);
  67         window2.add(field);
  68         window2.setVisible(true);
  69 
  70         try {
  71             gained = false;
  72             Robot robot = new Robot();
  73             robot.setAutoDelay(50);
  74             robot.waitForIdle();
  75             robot.delay(200);
  76 
  77             Point p = field.getLocationOnScreen();
  78             System.out.println(p);
  79             robot.mouseMove(p.x + 1, p.y + 1);
  80             robot.mousePress(InputEvent.BUTTON1_MASK);
  81             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  82             robot.waitForIdle();
  83             robot.delay(200);
  84 
  85             if (!gained) {
  86                 throw new Exception("Focus is not gained upon mouse click");
  87             }
  88             System.out.println("ok");
  89         } catch (SecurityException e) {
  90 
  91             JOptionPane optionPane = new JOptionPane(
  92                     "You are in the browser so test is manual. Try to " +
  93                     "click \"JTextField\" in the opened window then press OK " +
  94                     "below",
  95                     JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
  96             JDialog dialog =
  97                     optionPane.createDialog(null,"FindOwnerTest instruction");
  98             dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  99             dialog.setVisible(true);
 100             if (!gained) {
 101                 throw new RuntimeException(
 102                         "Focus is not gained upon mouse click");
 103             }
 104             System.out.println("ok");
 105         } catch (Exception e) {
 106             throw new RuntimeException(e);
 107         } finally {
 108             window1.dispose();
 109             stop();
 110         }
 111     }
 112 }