author | goetz |
Tue, 14 Jun 2016 10:44:59 +0200 | |
changeset 39056 | d99e63b6d962 |
parent 32664 | dbe7dd1f80e3 |
child 42338 | a60f280f803c |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
@test |
|
39056
d99e63b6d962
8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents:
32664
diff
changeset
|
26 |
@key headful |
2 | 27 |
@bug 6418028 |
28 |
@author oleg.sukhodolsky: area=awt.focus |
|
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 | 32 |
@build Util |
33 |
@run main RequestOnCompWithNullParent1 |
|
34 |
*/ |
|
35 |
||
36 |
import java.awt.*; |
|
37 |
import java.awt.event.*; |
|
38 |
import java.awt.peer.ButtonPeer; |
|
39 |
import java.awt.peer.ComponentPeer; |
|
40 |
import java.lang.reflect.Field; |
|
41 |
import java.lang.reflect.InvocationHandler; |
|
42 |
import java.lang.reflect.InvocationTargetException; |
|
43 |
import java.lang.reflect.Method; |
|
44 |
import java.lang.reflect.Proxy; |
|
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 | 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 | 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 | 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 | 60 |
frame.setLayout(new FlowLayout()); |
61 |
Button btn1 = new Button("Button1"); |
|
62 |
frame.add(btn1); |
|
63 |
TestButton btn2 = new TestButton("Button2"); |
|
64 |
frame.add(btn2); |
|
65 |
frame.pack(); |
|
66 |
frame.addWindowListener(new WindowAdapter() { |
|
67 |
@Override |
|
68 |
public void windowClosing(WindowEvent we) { |
|
69 |
we.getWindow().dispose(); |
|
70 |
} |
|
71 |
}); |
|
72 |
frame.setVisible(true); |
|
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 | 75 |
|
76 |
btn2.instrumentPeer(); |
|
77 |
btn2.requestFocusInWindow(); |
|
78 |
btn2.restorePeer(); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
class TestButton extends Button { |
|
83 |
ButtonPeer origPeer; |
|
84 |
ButtonPeer proxiedPeer; |
|
85 |
||
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 | 88 |
super(text); |
89 |
} |
|
90 |
||
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 | 94 |
InvocationHandler handler = new InvocationHandler() { |
95 |
public Object invoke(Object proxy, Method method, Object[] args) { |
|
96 |
if (method.getName().equals("requestFocus")) { |
|
97 |
Container parent = getParent(); |
|
98 |
parent.remove(TestButton.this); |
|
99 |
System.err.println("parent = " + parent); |
|
100 |
System.err.println("target = " + TestButton.this); |
|
101 |
System.err.println("new parent = " + TestButton.this.getParent()); |
|
102 |
} |
|
103 |
Object ret = null; |
|
104 |
try { |
|
105 |
ret = method.invoke(origPeer, args); |
|
106 |
} catch (IllegalAccessException iae) { |
|
107 |
throw new Error("Test error.", iae); |
|
108 |
} catch (InvocationTargetException ita) { |
|
109 |
throw new Error("Test error.", ita); |
|
110 |
} |
|
111 |
return ret; |
|
112 |
} |
|
113 |
}; |
|
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 | 118 |
setPeer(proxiedPeer); |
119 |
} |
|
120 |
||
121 |
private void setPeer(final ComponentPeer newPeer) { |
|
122 |
try { |
|
123 |
Field peer_field = Component.class.getDeclaredField("peer"); |
|
124 |
peer_field.setAccessible(true); |
|
125 |
peer_field.set(this, newPeer); |
|
126 |
} catch (IllegalArgumentException ex) { |
|
127 |
throw new Error("Test error.", ex); |
|
128 |
} catch (SecurityException ex) { |
|
129 |
throw new Error("Test error.", ex); |
|
130 |
} catch (IllegalAccessException ex) { |
|
131 |
throw new Error("Test error.", ex); |
|
132 |
} catch (NoSuchFieldException ex) { |
|
133 |
throw new Error("Test error.", ex); |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
public void restorePeer() { |
|
138 |
if (origPeer != null) { |
|
139 |
setPeer(origPeer); |
|
140 |
proxiedPeer = null; |
|
141 |
} |
|
142 |
} |
|
143 |
} |