jdk/test/java/awt/Focus/RequestOnCompWithNullParent/RequestOnCompWithNullParent1.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 32664 dbe7dd1f80e3
child 42338 a60f280f803c
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:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32664
diff changeset
     2
 * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
  @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 32664
diff changeset
    26
  @key headful
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
  @bug 6418028
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
  @author oleg.sukhodolsky: area=awt.focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  @library ../../regtesthelpers
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    30
  @modules java.desktop/java.awt.peer
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    31
           java.desktop/sun.awt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  @build Util
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  @run main RequestOnCompWithNullParent1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.peer.ButtonPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    46
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    48
public class RequestOnCompWithNullParent1 {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    50
    public static void main(final String[] args) throws Exception {
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    51
        Frame frame = new Frame("test for 6418028");
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    52
        try {
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    53
            test(frame);
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    54
        } finally {
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    55
            frame.dispose();
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    56
        }
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    57
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    59
    private static void test(final Frame frame) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        frame.setLayout(new FlowLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Button btn1 = new Button("Button1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        frame.add(btn1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        TestButton btn2 = new TestButton("Button2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        frame.add(btn2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        frame.addWindowListener(new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            public void windowClosing(WindowEvent we) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                we.getWindow().dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        frame.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    74
        new Robot().waitForIdle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        btn2.instrumentPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        btn2.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        btn2.restorePeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
class TestButton extends Button {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    ButtonPeer origPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    ButtonPeer proxiedPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Creates a new instance of TestButton */
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    87
    TestButton(String text) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void instrumentPeer() {
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    92
        origPeer = AWTAccessor.getComponentAccessor().getPeer(this);
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
    93
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        InvocationHandler handler = new InvocationHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            public Object invoke(Object proxy, Method method, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (method.getName().equals("requestFocus")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    Container parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    parent.remove(TestButton.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    System.err.println("parent = " + parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    System.err.println("target = " + TestButton.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    System.err.println("new parent = " + TestButton.this.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                Object ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    ret = method.invoke(origPeer, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    throw new Error("Test error.", iae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                } catch (InvocationTargetException ita) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    throw new Error("Test error.", ita);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
32664
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
   115
        proxiedPeer = (ButtonPeer) Proxy.newProxyInstance(
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
   116
                                    ButtonPeer.class.getClassLoader(),
dbe7dd1f80e3 8080948: [TEST_BUG] Few test cases are failing due to use of getPeer()
serb
parents: 5506
diff changeset
   117
                                    new Class[] {ButtonPeer.class}, handler);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        setPeer(proxiedPeer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private void setPeer(final ComponentPeer newPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Field peer_field = Component.class.getDeclaredField("peer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            peer_field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            peer_field.set(this, newPeer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (IllegalArgumentException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new Error("Test error.", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (SecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new Error("Test error.", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new Error("Test error.", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (NoSuchFieldException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new Error("Test error.", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void restorePeer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (origPeer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            setPeer(origPeer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            proxiedPeer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}