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