jdk/test/java/awt/Window/setLocRelativeTo/SetLocationRelativeToTest.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 26604 7dd67ef43cce
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:
26604
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     1
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
26604
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     4
 *
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     8
 *
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    13
 * accompanied this code).
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    14
 *
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    18
 *
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    21
 * questions.
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    22
 */
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    23
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    24
import java.awt.*;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    25
import java.util.ArrayList;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    26
import javax.swing.*;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    27
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    28
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    29
 * @test
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    30
 * @key headful
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    31
 * @summary Toplevel should be correctly positioned as relative to a component:
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    32
 *          so that their centers coincide
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    33
 *          or, if the component is hidden, centered on the screen.
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    34
 * @bug 8036915
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    35
 * @library ../../../../lib/testlibrary
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    36
 * @build ExtendedRobot
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    37
 * @run main/timeout=1200 SetLocationRelativeToTest
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 26604
diff changeset
    38
 */
26604
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    39
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    40
public class SetLocationRelativeToTest {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    41
    private static int delay = 500;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    42
    private static boolean testEverything = false;// NB: change this to true to test everything
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    43
    java.util.List<Window> awtToplevels = new ArrayList<Window>();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    44
    java.util.List<Window> swingToplevels = new ArrayList<Window>();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    45
    java.util.List<Window> allToplevels = new ArrayList<Window>();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    46
    java.util.List<Component> awtComponents = new ArrayList<Component>();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    47
    java.util.List<Component> swingComponents = new ArrayList<Component>();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    48
    java.util.List<Component> allComponents = new ArrayList<Component>();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    49
    Label placeholder = new Label();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    50
    JLabel jplaceholder = new JLabel();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    51
    JFrame jcontainer;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    52
    public SetLocationRelativeToTest() {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    53
        Frame frame = new Frame("Frame");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    54
        frame.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    55
        Frame uframe = new Frame("U.Frame");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    56
        uframe.setUndecorated(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    57
        uframe.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    58
        Window window = new Window(frame);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    59
        window.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    60
        Dialog dialog = new Dialog(frame, "Dialog");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    61
        dialog.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    62
        awtToplevels.add(frame);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    63
        awtToplevels.add(uframe);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    64
        awtToplevels.add(window);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    65
        awtToplevels.add(dialog);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    66
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    67
        awtComponents.add(new TextArea("Am a TextArea"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    68
        awtComponents.add(new TextField("Am a TextField"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    69
        awtComponents.add(new Button("Press"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    70
        awtComponents.add(new Label("Label"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    71
        Choice aChoice = new Choice();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    72
        aChoice.add("One");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    73
        aChoice.add("Two");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    74
        awtComponents.add(aChoice);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    75
        awtComponents.add(new Canvas());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    76
        awtComponents.add(new List(4));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    77
        awtComponents.add(new Checkbox("Me CheckBox"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    78
        awtComponents.add(new Scrollbar());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    79
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    80
        swingComponents.add(new JTextArea("Am a JTextArea"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    81
        swingComponents.add(new JTextField("Am a JTextField"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    82
        swingComponents.add(new JButton("Press"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    83
        swingComponents.add(new JLabel("JLabel"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    84
        JComboBox jcombo = new JComboBox();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    85
        swingComponents.add(jcombo);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    86
        swingComponents.add(new JPanel());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    87
        swingComponents.add(new JList());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    88
        swingComponents.add(new JCheckBox("Me JCheckBox"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    89
        swingComponents.add(new JScrollBar());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    90
    }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    91
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    92
    public static void main(String args[]) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    93
        SetLocationRelativeToTest test = new SetLocationRelativeToTest();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    94
        test.doAWTTest(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    95
        test.doAWTTest(false);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    96
        try {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    97
            test.doSwingTest(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    98
            test.doSwingTest(false);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
    99
        }catch(InterruptedException ie) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   100
            ie.printStackTrace();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   101
        }catch(java.lang.reflect.InvocationTargetException ite) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   102
            ite.printStackTrace();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   103
            throw new RuntimeException("InvocationTarget?");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   104
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   105
        return;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   106
    }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   107
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   108
    // In regular testing, we select just few components to test
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   109
    // randomly. If full testing required, select many ("all").
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   110
    void selectObjectsToTest(boolean doSwing) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   111
        allToplevels.clear();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   112
        allComponents.clear();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   113
        if(testEverything) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   114
            allToplevels.addAll(0, awtToplevels);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   115
            allComponents.addAll(0, awtComponents);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   116
            if(doSwing) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   117
                allToplevels.addAll(allToplevels.size(), swingToplevels);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   118
                allComponents.addAll(allComponents.size(), swingComponents);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   119
            }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   120
        }else{
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   121
            //select a random of each
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   122
            int i = (int)(java.lang.Math.random()*awtToplevels.size());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   123
            allToplevels.add(awtToplevels.get(i));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   124
            i = (int)(java.lang.Math.random()*awtComponents.size());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   125
            allComponents.add(awtComponents.get(i));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   126
            if(doSwing) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   127
                i = (int)(java.lang.Math.random()*swingToplevels.size());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   128
                allToplevels.add(swingToplevels.get(i));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   129
                i = (int)(java.lang.Math.random()*swingComponents.size());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   130
                allComponents.add(swingComponents.get(i));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   131
            }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   132
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   133
    }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   134
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   135
    // create Frame, add an AWT component to it,
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   136
    // hide it (or not) and position a new toplevel
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   137
    // relativeTo
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   138
    void doAWTTest(boolean isHidden) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   139
        boolean res;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   140
        ExtendedRobot robot;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   141
        try {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   142
            robot = new ExtendedRobot();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   143
        }catch(Exception ex) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   144
            ex.printStackTrace();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   145
            throw new RuntimeException("Failed: "+ex.getMessage());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   146
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   147
        Frame container = new Frame("Frame");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   148
        container.setBounds(100,100,300,300);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   149
        container.setLayout(new GridLayout(3,1));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   150
        container.add(placeholder);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   151
        container.setVisible(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   152
        selectObjectsToTest(false);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   153
        for(Component c: allComponents) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   154
            placeholder.setText((isHidden ? "Hidden: " : "Below is ")+ c.getClass().getName());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   155
            c.setVisible(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   156
            container.add(c);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   157
            container.doLayout();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   158
            if(isHidden) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   159
                c.setVisible(false);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   160
            }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   161
            robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   162
            for(Window w: allToplevels) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   163
                w.setLocationRelativeTo(c);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   164
                w.setVisible(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   165
                robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   166
                res = compareLocations(w, c, robot);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   167
                System.out.println(c.getClass().getName()+"   \t: "+w.getClass().getName()+
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   168
                    ((w instanceof Frame) && (((Frame)w).isUndecorated()) ? " undec\t\t:" : "\t\t:")+" "+
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   169
                    (res ? "" : "Failed"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   170
                if(!res) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   171
                    throw new RuntimeException("Test failed.");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   172
                }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   173
                w.dispose();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   174
            }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   175
            container.remove(c);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   176
            robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   177
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   178
        container.dispose();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   179
    }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   180
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   181
    // Create JFrame, add an AWT or Swing component to it,
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   182
    // hide it (or not) and position a new toplevel
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   183
    // relativeTo
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   184
    void doSwingTest(boolean isHidden) throws InterruptedException,
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   185
                       java.lang.reflect.InvocationTargetException {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   186
        boolean res;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   187
        ExtendedRobot robot;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   188
        try {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   189
            robot = new ExtendedRobot();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   190
        }catch(Exception ex) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   191
            ex.printStackTrace();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   192
            throw new RuntimeException("Failed: "+ex.getMessage());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   193
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   194
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   195
        EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   196
            JFrame jframe = new JFrame("jframe");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   197
            jframe.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   198
            swingToplevels.add(jframe);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   199
            JFrame ujframe = new JFrame("ujframe");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   200
            ujframe.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   201
            ujframe.setUndecorated(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   202
            swingToplevels.add(ujframe);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   203
            JWindow jwin = new JWindow();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   204
            jwin.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   205
            swingToplevels.add(jwin);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   206
            JDialog jdia = new JDialog((Frame)null, "JDialog");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   207
            jdia.setSize(200,100);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   208
            swingToplevels.add(jdia);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   209
            jcontainer = new JFrame("JFrame");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   210
            jcontainer.setBounds(100,100,300,300);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   211
            jcontainer.setLayout(new GridLayout(3,1));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   212
            jcontainer.add(jplaceholder);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   213
            jcontainer.setVisible(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   214
            selectObjectsToTest(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   215
        });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   216
        robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   217
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   218
        for(Component c: allComponents) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   219
            EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   220
                jplaceholder.setText((isHidden ? "Hidden: " : "Below is: ")+ c.getClass().getName());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   221
                c.setVisible(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   222
                jcontainer.add(c);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   223
                jcontainer.doLayout();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   224
                if(isHidden) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   225
                    c.setVisible(false);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   226
                }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   227
            });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   228
            robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   229
            for(Window w: allToplevels) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   230
                EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   231
                    w.setLocationRelativeTo(c);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   232
                    w.setVisible(true);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   233
                });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   234
                robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   235
                res = compareLocations(w, c, robot);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   236
                System.out.println(c.getClass().getName()+"   \t: "+w.getClass().getName()+
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   237
                    ((w instanceof Frame) && (((Frame)w).isUndecorated()) ? " undec\t\t:" : "\t\t:")+" "+
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   238
                    (res ? "" : "Failed"));
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   239
                EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   240
                    w.dispose();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   241
                });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   242
                robot.waitForIdle();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   243
                if(!res) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   244
                    throw new RuntimeException("Test failed.");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   245
                }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   246
            }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   247
            EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   248
                jcontainer.remove(c);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   249
            });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   250
            robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   251
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   252
        EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   253
            jcontainer.dispose();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   254
        });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   255
    }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   256
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   257
    // Check, finally, if w  either is concentric with c
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   258
    // or sits in the center of the screen (if c is hidden)
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   259
    boolean compareLocations(final Window w, final Component c, ExtendedRobot robot) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   260
        final Point pc = new Point();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   261
        final Point pw = new Point();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   262
        try {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   263
            EventQueue.invokeAndWait( () -> {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   264
                pw.setLocation(w.getLocationOnScreen());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   265
                pw.translate(w.getWidth()/2, w.getHeight()/2);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   266
                if(!c.isVisible()) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   267
                    Rectangle screenRect = w.getGraphicsConfiguration().getBounds();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   268
                    pc.setLocation(screenRect.x+screenRect.width/2,
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   269
                                   screenRect.y+screenRect.height/2);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   270
                }else{
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   271
                    pc.setLocation(c.getLocationOnScreen());
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   272
                    pc.translate(c.getWidth()/2, c.getHeight()/2);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   273
                }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   274
            });
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   275
        } catch(InterruptedException ie) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   276
            throw new RuntimeException("Interrupted");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   277
        } catch(java.lang.reflect.InvocationTargetException ite) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   278
            ite.printStackTrace();
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   279
            throw new RuntimeException("InvocationTarget?");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   280
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   281
        robot.waitForIdle(delay);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   282
        // Compare with 1 tolerance to forgive possible rounding errors
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   283
        if(pc.x - pw.x > 1 ||
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   284
           pc.x - pw.x < -1 ||
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   285
           pc.y - pw.y > 1 ||
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   286
           pc.y - pw.y < -1 ) {
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   287
            System.out.println("Center of "+(c.isVisible() ? "Component:" : "screen:")+pc);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   288
            System.out.println("Center of Window:"+pw);
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   289
            System.out.println("Centers of "+w+" and "+c+" do not coincide");
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   290
            return false;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   291
        }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   292
        return true;
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   293
    }
7dd67ef43cce 8055664: move 14 tests about setLocationRelativeTo to jdk
yan
parents:
diff changeset
   294
}