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 8025815
  26    @summary Child FileDialog of modal dialog does not get focus on Gnome
  27    @author Semyon Sadetsky
  28   */
  29 
  30 import javax.swing.*;
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import java.awt.image.BufferedImage;
  34 import java.lang.reflect.InvocationTargetException;
  35 import java.util.concurrent.TimeUnit;
  36 import java.util.concurrent.locks.Condition;
  37 import java.util.concurrent.locks.ReentrantLock;
  38 
  39 public class FileDialogModalFocusTest {
  40     public static void main(String[] args) throws Exception {
  41         Frame frame = new Frame();
  42         FileDialog fileDialog = new FileDialog((Frame) null);
  43         test(frame, fileDialog);
  44         frame = new Frame();
  45         fileDialog = new FileDialog(frame);
  46         test(frame, fileDialog);
  47         System.out.println("ok");
  48     }
  49 
  50     private static void test(final Frame frame, final FileDialog fileDialog)
  51             throws InterruptedException, InvocationTargetException,
  52             AWTException {
  53         Button button = new Button();
  54         button.setBackground(Color.RED);
  55         button.addActionListener(new ActionListener() {
  56             @Override
  57             public void actionPerformed(ActionEvent e) {
  58                 fileDialog.setVisible(true);
  59             }
  60         });
  61         frame.add(button);
  62         frame.setSize(200, 200);
  63         EventQueue.invokeAndWait(new Runnable() {
  64             @Override
  65             public void run() {
  66                 frame.setVisible(true);
  67             }
  68         });
  69         Robot robot = new Robot();
  70         robot.setAutoDelay(200);
  71         robot.waitForIdle();
  72         Point point = button.getLocationOnScreen();
  73         point.translate(100, 100);
  74         robot.mouseMove(point.x, point.y);
  75         robot.mousePress(InputEvent.BUTTON1_MASK);
  76         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  77         int delay = 0;
  78         while (frame.isFocused() && delay < 2000) {
  79             robot.delay(50);
  80             delay += 50;
  81         }
  82         ReentrantLock lock = new ReentrantLock();
  83         Condition condition = lock.newCondition();
  84         button.addComponentListener(new ComponentAdapter() {
  85             @Override
  86             public void componentResized(ComponentEvent e) {
  87                 lock.lock();
  88                 condition.signal();
  89                 lock.unlock();
  90             }
  91         });
  92         lock.lock();
  93         EventQueue.invokeLater(new Runnable() {
  94             @Override
  95             public void run() {
  96                 frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
  97             }
  98         });
  99         condition.await(5, TimeUnit.SECONDS);
 100         lock.unlock();
 101         robot.delay(200);
 102         robot.waitForIdle();
 103         EventQueue.invokeAndWait(new Runnable() {
 104             @Override
 105             public void run() {
 106                 button.requestFocus();
 107                 Point p = new Point(button.getWidth() - 10, button.getHeight() - 10);
 108                 SwingUtilities.convertPointToScreen(p, button);
 109                 robot.mouseMove(p.x, p.y);
 110                 robot.mousePress(InputEvent.BUTTON1_MASK);
 111                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 112             }
 113         });
 114         robot.waitForIdle();
 115         Point p = new Point(100, 100);
 116         SwingUtilities.convertPointToScreen(p, button);
 117         BufferedImage image = robot.createScreenCapture(
 118                 new Rectangle(p,
 119                         new Dimension(button.getWidth() - 200,
 120                                 button.getHeight() - 200)));
 121         boolean found = false;
 122         for (int x = 0; x < image.getWidth(); x+=50) {
 123             for (int y = 0; y < image.getHeight(); y+=50) {
 124                 if( (image.getRGB(x, y) & 0x00FFFF) != 0 ) {
 125                     found = true;
 126                     break;
 127                 };
 128             }
 129         }
 130         frame.dispose();
 131         robot.waitForIdle();
 132         fileDialog.dispose();
 133         if(!found) {
 134             throw new RuntimeException("file chooser is underneath");
 135         }
 136     }
 137 }