1/*
2 * Copyright (c) 2007, 2014, 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 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 *
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.
22 */
23
24import java.awt.*;
25import java.awt.event.*;
26
27/*
28 * @test
29 * @summary Check for AWTEventMulticaster working in headless mode
30 * @run main/othervm -Djava.awt.headless=true HeadlessAWTEventMulticaster
31 */
32
33public class HeadlessAWTEventMulticaster {
34    class ComponentListenerImpl implements ComponentListener {
35        public boolean hidden = false;
36        public boolean moved = false;
37        public boolean resized = false;
38        public boolean shown = false;
39
40        public void componentHidden(ComponentEvent e) {
41            hidden = true;
42        }
43
44        public void componentMoved(ComponentEvent e) {
45            moved = true;
46        }
47
48        public void componentResized(ComponentEvent e) {
49            resized = true;
50        }
51
52        public void componentShown(ComponentEvent e) {
53            shown = true;
54        }
55    }
56
57    class ContainerListenerImpl implements ContainerListener {
58        public boolean removed = false;
59        public boolean added = false;
60
61        public void componentAdded(ContainerEvent e) {
62            added = true;
63        }
64
65        public void componentRemoved(ContainerEvent e) {
66            removed = true;
67        }
68    }
69
70    class FocusListenerImpl implements FocusListener {
71        public boolean gained = false;
72        public boolean lost = false;
73
74        public void focusGained(FocusEvent e) {
75            gained = true;
76        }
77
78        public void focusLost(FocusEvent e) {
79            lost = true;
80        }
81    }
82
83    class KeyListenerImpl implements KeyListener {
84        public boolean pressed = false;
85        public boolean released = false;
86        public boolean typed = false;
87
88        public void keyPressed(KeyEvent e) {
89            pressed = true;
90        }
91
92        public void keyReleased(KeyEvent e) {
93            released = true;
94        }
95
96        public void keyTyped(KeyEvent e) {
97            typed = true;
98        }
99    }
100
101    public static void main(String args[]) {
102        new HeadlessAWTEventMulticaster().doTest();
103    }
104
105    void doTest() {
106        ComponentListener compList;
107        ComponentListenerImpl compListImpl;
108
109        ContainerListener contList;
110        ContainerListenerImpl contListImpl;
111
112        FocusListener focList;
113        FocusListenerImpl focListImpl;
114
115        KeyListener keyList;
116        KeyListenerImpl keyListImpl;
117
118        Component component = new Component(){};
119
120        // Component resized
121        compListImpl = new ComponentListenerImpl();
122        compList = AWTEventMulticaster.add(compListImpl, null);
123        compList.componentResized(new ComponentEvent(component,
124                ComponentEvent.COMPONENT_RESIZED));
125        if (compListImpl.hidden || compListImpl.moved || compListImpl.shown) {
126            throw new RuntimeException("Wrong id delivered: hidden || moved || shown");
127        }
128        if (!compListImpl.resized) {
129            throw new RuntimeException("Expected id, resized, not delivered");
130        }
131
132        // Component moved
133        compListImpl = new ComponentListenerImpl();
134        compList = AWTEventMulticaster.add(compListImpl, null);
135        compList.componentMoved(new ComponentEvent(component,
136                ComponentEvent.COMPONENT_MOVED));
137        if (compListImpl.hidden || compListImpl.resized || compListImpl.shown) {
138            throw new RuntimeException("Wrong id delivered: hidden || resized || shown");
139        }
140        if (!compListImpl.moved) {
141            throw new RuntimeException("Expected id, moved, not delivered");
142        }
143
144        // Component shown
145        compListImpl = new ComponentListenerImpl();
146        compList = AWTEventMulticaster.add(compListImpl, null);
147        compList.componentShown(new ComponentEvent(component,
148                ComponentEvent.COMPONENT_SHOWN));
149        if (compListImpl.hidden || compListImpl.resized || compListImpl.moved) {
150            throw new RuntimeException("Wrong id delivered: hidden || resized || moved");
151        }
152        if (!compListImpl.shown) {
153            throw new RuntimeException("Expected id, shown, not delivered");
154        }
155
156        // Component hidden
157        compListImpl = new ComponentListenerImpl();
158        compList = AWTEventMulticaster.add(compListImpl, null);
159        compList.componentHidden(new ComponentEvent(component,
160                ComponentEvent.COMPONENT_HIDDEN));
161        if (compListImpl.shown || compListImpl.resized || compListImpl.moved) {
162            throw new RuntimeException("Wrong id delivered: shown || resized || moved");
163        }
164        if (!compListImpl.hidden) {
165            throw new RuntimeException("Expected id, hidden, not delivered");
166        }
167
168        // Component added
169        contListImpl = new ContainerListenerImpl();
170        contList = AWTEventMulticaster.add(contListImpl, null);
171        contList.componentAdded(new ContainerEvent(component,
172                ContainerEvent.COMPONENT_ADDED, component));
173        if (contListImpl.removed) {
174            throw new RuntimeException("Wrong id delivered: removed");
175        }
176        if (!contListImpl.added) {
177            throw new RuntimeException("Expected id, added, not delivered");
178        }
179
180        // Component removed
181        contListImpl = new ContainerListenerImpl();
182        contList = AWTEventMulticaster.add(contListImpl, null);
183        contList.componentRemoved(new ContainerEvent(component,
184                ContainerEvent.COMPONENT_REMOVED, component));
185        if (contListImpl.added) {
186            throw new RuntimeException("Wrong id delivered: added");
187        }
188        if (!contListImpl.removed) {
189            throw new RuntimeException("Expected id, removed, not delivered");
190        }
191
192        // Focus gained
193        focListImpl = new FocusListenerImpl();
194        focList = AWTEventMulticaster.add(focListImpl, null);
195        focList.focusGained(new FocusEvent(component, FocusEvent.FOCUS_GAINED));
196        if (focListImpl.lost) {
197            throw new RuntimeException("Wrong id delivered: lost");
198        }
199        if (!focListImpl.gained) {
200            throw new RuntimeException("Expected id, gained, not delivered");
201        }
202
203        // Focus lost
204        focListImpl = new FocusListenerImpl();
205        focList = AWTEventMulticaster.add(focListImpl, null);
206        focList.focusLost(new FocusEvent(component, FocusEvent.FOCUS_LOST));
207        if (focListImpl.gained) {
208            throw new RuntimeException("Wrong id delivered: gained");
209        }
210        if (!focListImpl.lost) {
211            throw new RuntimeException("Expected id, lost, not delivered");
212        }
213
214        // Key typed
215        keyListImpl = new KeyListenerImpl();
216        keyList = AWTEventMulticaster.add(keyListImpl, null);
217        keyList.keyTyped(new KeyEvent(component,
218                KeyEvent.KEY_TYPED, 0L, 0, 0));
219        if (keyListImpl.pressed || keyListImpl.released)
220            throw new RuntimeException("Wrong id delivered: pressed || released");
221
222        if (!keyListImpl.typed)
223            throw new RuntimeException("Expected id, typed, not delivered");
224
225        // Key pressed
226        keyListImpl = new KeyListenerImpl();
227        keyList = AWTEventMulticaster.add(keyListImpl, null);
228        keyList.keyPressed(new KeyEvent(component,
229                KeyEvent.KEY_PRESSED, 0L, 0, 0));
230        if (keyListImpl.typed || keyListImpl.released)
231            throw new RuntimeException("Wrong id delivered: typed || released");
232
233        if (!keyListImpl.pressed)
234            throw new RuntimeException("Expected id, pressed, not delivered");
235
236        // Key released
237        keyListImpl = new KeyListenerImpl();
238        keyList = AWTEventMulticaster.add(keyListImpl, null);
239        keyList.keyReleased(new KeyEvent(component,
240                KeyEvent.KEY_RELEASED, 0L, 0, 0));
241        if (keyListImpl.pressed || keyListImpl.typed)
242            throw new RuntimeException("Wrong id delivered: pressed || typed");
243
244        if (!keyListImpl.released)
245            throw new RuntimeException("Expected id, released, not delivered");
246    }
247}
248