Path: blob/master/test/jdk/java/awt/FileDialog/ModalFocus/FileDialogModalFocusTest.java
41153 views
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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @key headful27* @bug 802581528* @summary Child FileDialog of modal dialog does not get focus on Gnome29* @author Semyon Sadetsky30*/3132import javax.swing.*;33import java.awt.*;34import java.awt.event.*;35import java.awt.image.BufferedImage;36import java.lang.reflect.InvocationTargetException;37import java.util.concurrent.TimeUnit;38import java.util.concurrent.locks.Condition;39import java.util.concurrent.locks.ReentrantLock;4041public class FileDialogModalFocusTest {42public static void main(String[] args) throws Exception {43Frame frame = new Frame();44FileDialog fileDialog = new FileDialog((Frame) null);45test(frame, fileDialog);46frame = new Frame();47fileDialog = new FileDialog(frame);48test(frame, fileDialog);49System.out.println("ok");50}5152private static void test(final Frame frame, final FileDialog fileDialog)53throws InterruptedException, InvocationTargetException,54AWTException {55Button button = new Button();56button.setBackground(Color.RED);57button.addActionListener(new ActionListener() {58@Override59public void actionPerformed(ActionEvent e) {60fileDialog.setVisible(true);61}62});63frame.add(button);64frame.setSize(200, 200);65EventQueue.invokeAndWait(new Runnable() {66@Override67public void run() {68frame.setVisible(true);69}70});71Robot robot = new Robot();72robot.setAutoDelay(200);73robot.waitForIdle();74Point point = button.getLocationOnScreen();75point.translate(100, 100);76robot.mouseMove(point.x, point.y);77robot.mousePress(InputEvent.BUTTON1_MASK);78robot.mouseRelease(InputEvent.BUTTON1_MASK);79int delay = 0;80while (frame.isFocused() && delay < 2000) {81robot.delay(50);82delay += 50;83}84ReentrantLock lock = new ReentrantLock();85Condition condition = lock.newCondition();86button.addComponentListener(new ComponentAdapter() {87@Override88public void componentResized(ComponentEvent e) {89lock.lock();90condition.signal();91lock.unlock();92}93});94lock.lock();95EventQueue.invokeLater(new Runnable() {96@Override97public void run() {98frame.setExtendedState(JFrame.MAXIMIZED_BOTH);99}100});101condition.await(5, TimeUnit.SECONDS);102lock.unlock();103robot.delay(200);104robot.waitForIdle();105EventQueue.invokeAndWait(new Runnable() {106@Override107public void run() {108button.requestFocus();109Point p = new Point(button.getWidth() - 10, button.getHeight() - 10);110SwingUtilities.convertPointToScreen(p, button);111robot.mouseMove(p.x, p.y);112robot.mousePress(InputEvent.BUTTON1_MASK);113robot.mouseRelease(InputEvent.BUTTON1_MASK);114}115});116robot.waitForIdle();117Point p = new Point(100, 100);118SwingUtilities.convertPointToScreen(p, button);119BufferedImage image = robot.createScreenCapture(120new Rectangle(p,121new Dimension(button.getWidth() - 200,122button.getHeight() - 200)));123boolean found = false;124for (int x = 0; x < image.getWidth(); x+=50) {125for (int y = 0; y < image.getHeight(); y+=50) {126if( (image.getRGB(x, y) & 0x00FFFF) != 0 ) {127found = true;128break;129};130}131}132frame.dispose();133robot.waitForIdle();134fileDialog.dispose();135if(!found) {136throw new RuntimeException("file chooser is underneath");137}138}139}140141142