jdk/test/java/awt/FileDialog/ModalFocus/FileDialogModalFocusTest.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 32427 c22b7e41adf3
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
     1
32119
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     2
/*
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     3
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     5
 *
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     8
 * published by the Free Software Foundation.
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
     9
 *
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    14
 * accompanied this code).
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    15
 *
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    19
 *
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    22
 * questions.
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    23
 */
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    24
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    25
/*
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    26
 * @test
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    27
 * @key headful
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    28
 * @bug 8025815
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    29
 * @summary Child FileDialog of modal dialog does not get focus on Gnome
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    30
 * @author Semyon Sadetsky
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32427
diff changeset
    31
 */
32119
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    32
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    33
import javax.swing.*;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    34
import java.awt.*;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    35
import java.awt.event.*;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    36
import java.awt.image.BufferedImage;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    37
import java.lang.reflect.InvocationTargetException;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    39
import java.util.concurrent.locks.Condition;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    40
import java.util.concurrent.locks.ReentrantLock;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    41
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    42
public class FileDialogModalFocusTest {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    44
        Frame frame = new Frame();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    45
        FileDialog fileDialog = new FileDialog((Frame) null);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    46
        test(frame, fileDialog);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    47
        frame = new Frame();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    48
        fileDialog = new FileDialog(frame);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    49
        test(frame, fileDialog);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    50
        System.out.println("ok");
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    51
    }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    52
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    53
    private static void test(final Frame frame, final FileDialog fileDialog)
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    54
            throws InterruptedException, InvocationTargetException,
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    55
            AWTException {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    56
        Button button = new Button();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    57
        button.setBackground(Color.RED);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    58
        button.addActionListener(new ActionListener() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    59
            @Override
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    60
            public void actionPerformed(ActionEvent e) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    61
                fileDialog.setVisible(true);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    62
            }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    63
        });
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    64
        frame.add(button);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    65
        frame.setSize(200, 200);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    66
        EventQueue.invokeAndWait(new Runnable() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    67
            @Override
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    68
            public void run() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    69
                frame.setVisible(true);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    70
            }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    71
        });
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    72
        Robot robot = new Robot();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    73
        robot.setAutoDelay(200);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    74
        robot.waitForIdle();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    75
        Point point = button.getLocationOnScreen();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    76
        point.translate(100, 100);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    77
        robot.mouseMove(point.x, point.y);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    78
        robot.mousePress(InputEvent.BUTTON1_MASK);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    79
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    80
        int delay = 0;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    81
        while (frame.isFocused() && delay < 2000) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    82
            robot.delay(50);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    83
            delay += 50;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    84
        }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    85
        ReentrantLock lock = new ReentrantLock();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    86
        Condition condition = lock.newCondition();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    87
        button.addComponentListener(new ComponentAdapter() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    88
            @Override
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    89
            public void componentResized(ComponentEvent e) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    90
                lock.lock();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    91
                condition.signal();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    92
                lock.unlock();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    93
            }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    94
        });
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    95
        lock.lock();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    96
        EventQueue.invokeLater(new Runnable() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    97
            @Override
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    98
            public void run() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
    99
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   100
            }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   101
        });
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   102
        condition.await(5, TimeUnit.SECONDS);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   103
        lock.unlock();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   104
        robot.delay(200);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   105
        robot.waitForIdle();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   106
        EventQueue.invokeAndWait(new Runnable() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   107
            @Override
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   108
            public void run() {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   109
                button.requestFocus();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   110
                Point p = new Point(button.getWidth() - 10, button.getHeight() - 10);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   111
                SwingUtilities.convertPointToScreen(p, button);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   112
                robot.mouseMove(p.x, p.y);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   113
                robot.mousePress(InputEvent.BUTTON1_MASK);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   114
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   115
            }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   116
        });
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   117
        robot.waitForIdle();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   118
        Point p = new Point(100, 100);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   119
        SwingUtilities.convertPointToScreen(p, button);
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   120
        BufferedImage image = robot.createScreenCapture(
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   121
                new Rectangle(p,
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   122
                        new Dimension(button.getWidth() - 200,
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   123
                                button.getHeight() - 200)));
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   124
        boolean found = false;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   125
        for (int x = 0; x < image.getWidth(); x+=50) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   126
            for (int y = 0; y < image.getHeight(); y+=50) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   127
                if( (image.getRGB(x, y) & 0x00FFFF) != 0 ) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   128
                    found = true;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   129
                    break;
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   130
                };
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   131
            }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   132
        }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   133
        frame.dispose();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   134
        robot.waitForIdle();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   135
        fileDialog.dispose();
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   136
        if(!found) {
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   137
            throw new RuntimeException("file chooser is underneath");
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   138
        }
1778956d7bee 8025815: Child FileDialog of modal dialog does not get focus on Gnome
ssadetsky
parents:
diff changeset
   139
    }
32427
c22b7e41adf3 8134984: Text files should end in exactly one newline
martin
parents: 32119
diff changeset
   140
}