1 /*
   2  * Copyright (c) 2014, 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 8061954
  26    @summary Button does not get the focus on a mouse click, and NPE is thrown in KFM
  27    @author Anton Litvinov
  28 */
  29 
  30 import java.awt.Container;
  31 import java.awt.FlowLayout;
  32 import java.awt.Frame;
  33 import java.awt.Point;
  34 import java.awt.Robot;
  35 import java.awt.Toolkit;
  36 import java.awt.event.InputEvent;
  37 import javax.swing.JButton;
  38 import javax.swing.JDialog;
  39 import javax.swing.SwingUtilities;
  40 
  41 import sun.awt.OSInfo;
  42 import sun.awt.SunToolkit;
  43 
  44 public class NPEInKFMOnButtonClickInDialogTest {
  45     private static Frame frame = null;
  46     private static JDialog dialog = null;
  47     private static JButton cancelBtn = null;
  48     private static Point clickPoint = null;
  49     private static volatile Boolean cancelBtnIsFocused = null;
  50 
  51     public static void main(String[] args) {
  52         OSInfo.OSType osType = OSInfo.getOSType();
  53         if ((osType != OSInfo.OSType.LINUX) && (osType != OSInfo.OSType.SOLARIS)) {
  54             System.out.println("This test is only for Linux OS and Solaris OS.");
  55             return;
  56         }
  57 
  58         ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();
  59         Thread t = new Thread(new ThreadGroup(mainThreadGroup, "TestThreadGroup"), new Runnable() {
  60             public void run() {
  61                 try {
  62                     SunToolkit.createNewAppContext();
  63                     doTest();
  64                 } catch (Exception e) {
  65                     e.printStackTrace();
  66                 }
  67             }
  68         });
  69         t.start();
  70 
  71         try {
  72             t.join();
  73         } catch (InterruptedException ie) {
  74             ie.printStackTrace();
  75         }
  76 
  77         if (cancelBtnIsFocused == null) {
  78             throw new RuntimeException("Test failed for an unknown reason, look at error log.");
  79         } else if (cancelBtnIsFocused.booleanValue() == false) {
  80             throw new RuntimeException("'Cancel' button did not become a focus owner.");
  81         }
  82     }
  83 
  84     private static void doTest() throws Exception {
  85         final SunToolkit toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
  86         final Robot robot = new Robot();
  87         robot.setAutoDelay(50);
  88 
  89         try {
  90             frame = new Frame("Frame of NPEInKFMOnButtonClickInDialogTest");
  91             frame.setSize(100, 100);
  92             frame.setVisible(true);
  93             toolkit.realSync();
  94 
  95             SwingUtilities.invokeAndWait(new Runnable() {
  96                 public void run() {
  97                     dialog = new JDialog(frame,
  98                         "Dialog of NPEInKFMOnButtonClickInDialogTest", false);
  99                     Container content = dialog.getContentPane();
 100                     content.setLayout(new FlowLayout());
 101                     content.add(new JButton("Run"));
 102                     content.add(cancelBtn = new JButton("Cancel"));
 103                     dialog.pack();
 104                     dialog.setVisible(true);
 105                 }
 106             });
 107             toolkit.realSync();
 108 
 109             SwingUtilities.invokeAndWait(new Runnable() {
 110                 public void run() {
 111                     Point p = cancelBtn.getLocationOnScreen();
 112                     clickPoint = new Point(p.x + cancelBtn.getWidth() / 2,
 113                         p.y + cancelBtn.getHeight() / 2);
 114                 }
 115             });
 116             robot.mouseMove(clickPoint.x, clickPoint.y);
 117             robot.mousePress(InputEvent.BUTTON1_MASK);
 118             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 119             toolkit.realSync();
 120 
 121             SwingUtilities.invokeAndWait(new Runnable() {
 122                 public void run() {
 123                     cancelBtnIsFocused = cancelBtn.isFocusOwner();
 124                 }
 125             });
 126         } finally {
 127             if (dialog != null) {
 128                 dialog.dispose();
 129             }
 130             if (frame != null) {
 131                 frame.dispose();
 132             }
 133         }
 134     }
 135 }