Path: blob/master/test/jdk/java/awt/Headless/HeadlessAWTEventMulticaster.java
41149 views
/*1* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;2526/*27* @test28* @summary Check for AWTEventMulticaster working in headless mode29* @run main/othervm -Djava.awt.headless=true HeadlessAWTEventMulticaster30*/3132public class HeadlessAWTEventMulticaster {33class ComponentListenerImpl implements ComponentListener {34public boolean hidden = false;35public boolean moved = false;36public boolean resized = false;37public boolean shown = false;3839public void componentHidden(ComponentEvent e) {40hidden = true;41}4243public void componentMoved(ComponentEvent e) {44moved = true;45}4647public void componentResized(ComponentEvent e) {48resized = true;49}5051public void componentShown(ComponentEvent e) {52shown = true;53}54}5556class ContainerListenerImpl implements ContainerListener {57public boolean removed = false;58public boolean added = false;5960public void componentAdded(ContainerEvent e) {61added = true;62}6364public void componentRemoved(ContainerEvent e) {65removed = true;66}67}6869class FocusListenerImpl implements FocusListener {70public boolean gained = false;71public boolean lost = false;7273public void focusGained(FocusEvent e) {74gained = true;75}7677public void focusLost(FocusEvent e) {78lost = true;79}80}8182class KeyListenerImpl implements KeyListener {83public boolean pressed = false;84public boolean released = false;85public boolean typed = false;8687public void keyPressed(KeyEvent e) {88pressed = true;89}9091public void keyReleased(KeyEvent e) {92released = true;93}9495public void keyTyped(KeyEvent e) {96typed = true;97}98}99100public static void main(String args[]) {101new HeadlessAWTEventMulticaster().doTest();102}103104void doTest() {105ComponentListener compList;106ComponentListenerImpl compListImpl;107108ContainerListener contList;109ContainerListenerImpl contListImpl;110111FocusListener focList;112FocusListenerImpl focListImpl;113114KeyListener keyList;115KeyListenerImpl keyListImpl;116117Component component = new Component(){};118119// Component resized120compListImpl = new ComponentListenerImpl();121compList = AWTEventMulticaster.add(compListImpl, null);122compList.componentResized(new ComponentEvent(component,123ComponentEvent.COMPONENT_RESIZED));124if (compListImpl.hidden || compListImpl.moved || compListImpl.shown) {125throw new RuntimeException("Wrong id delivered: hidden || moved || shown");126}127if (!compListImpl.resized) {128throw new RuntimeException("Expected id, resized, not delivered");129}130131// Component moved132compListImpl = new ComponentListenerImpl();133compList = AWTEventMulticaster.add(compListImpl, null);134compList.componentMoved(new ComponentEvent(component,135ComponentEvent.COMPONENT_MOVED));136if (compListImpl.hidden || compListImpl.resized || compListImpl.shown) {137throw new RuntimeException("Wrong id delivered: hidden || resized || shown");138}139if (!compListImpl.moved) {140throw new RuntimeException("Expected id, moved, not delivered");141}142143// Component shown144compListImpl = new ComponentListenerImpl();145compList = AWTEventMulticaster.add(compListImpl, null);146compList.componentShown(new ComponentEvent(component,147ComponentEvent.COMPONENT_SHOWN));148if (compListImpl.hidden || compListImpl.resized || compListImpl.moved) {149throw new RuntimeException("Wrong id delivered: hidden || resized || moved");150}151if (!compListImpl.shown) {152throw new RuntimeException("Expected id, shown, not delivered");153}154155// Component hidden156compListImpl = new ComponentListenerImpl();157compList = AWTEventMulticaster.add(compListImpl, null);158compList.componentHidden(new ComponentEvent(component,159ComponentEvent.COMPONENT_HIDDEN));160if (compListImpl.shown || compListImpl.resized || compListImpl.moved) {161throw new RuntimeException("Wrong id delivered: shown || resized || moved");162}163if (!compListImpl.hidden) {164throw new RuntimeException("Expected id, hidden, not delivered");165}166167// Component added168contListImpl = new ContainerListenerImpl();169contList = AWTEventMulticaster.add(contListImpl, null);170contList.componentAdded(new ContainerEvent(component,171ContainerEvent.COMPONENT_ADDED, component));172if (contListImpl.removed) {173throw new RuntimeException("Wrong id delivered: removed");174}175if (!contListImpl.added) {176throw new RuntimeException("Expected id, added, not delivered");177}178179// Component removed180contListImpl = new ContainerListenerImpl();181contList = AWTEventMulticaster.add(contListImpl, null);182contList.componentRemoved(new ContainerEvent(component,183ContainerEvent.COMPONENT_REMOVED, component));184if (contListImpl.added) {185throw new RuntimeException("Wrong id delivered: added");186}187if (!contListImpl.removed) {188throw new RuntimeException("Expected id, removed, not delivered");189}190191// Focus gained192focListImpl = new FocusListenerImpl();193focList = AWTEventMulticaster.add(focListImpl, null);194focList.focusGained(new FocusEvent(component, FocusEvent.FOCUS_GAINED));195if (focListImpl.lost) {196throw new RuntimeException("Wrong id delivered: lost");197}198if (!focListImpl.gained) {199throw new RuntimeException("Expected id, gained, not delivered");200}201202// Focus lost203focListImpl = new FocusListenerImpl();204focList = AWTEventMulticaster.add(focListImpl, null);205focList.focusLost(new FocusEvent(component, FocusEvent.FOCUS_LOST));206if (focListImpl.gained) {207throw new RuntimeException("Wrong id delivered: gained");208}209if (!focListImpl.lost) {210throw new RuntimeException("Expected id, lost, not delivered");211}212213// Key typed214keyListImpl = new KeyListenerImpl();215keyList = AWTEventMulticaster.add(keyListImpl, null);216keyList.keyTyped(new KeyEvent(component,217KeyEvent.KEY_TYPED, 0L, 0, 0));218if (keyListImpl.pressed || keyListImpl.released)219throw new RuntimeException("Wrong id delivered: pressed || released");220221if (!keyListImpl.typed)222throw new RuntimeException("Expected id, typed, not delivered");223224// Key pressed225keyListImpl = new KeyListenerImpl();226keyList = AWTEventMulticaster.add(keyListImpl, null);227keyList.keyPressed(new KeyEvent(component,228KeyEvent.KEY_PRESSED, 0L, 0, 0));229if (keyListImpl.typed || keyListImpl.released)230throw new RuntimeException("Wrong id delivered: typed || released");231232if (!keyListImpl.pressed)233throw new RuntimeException("Expected id, pressed, not delivered");234235// Key released236keyListImpl = new KeyListenerImpl();237keyList = AWTEventMulticaster.add(keyListImpl, null);238keyList.keyReleased(new KeyEvent(component,239KeyEvent.KEY_RELEASED, 0L, 0, 0));240if (keyListImpl.pressed || keyListImpl.typed)241throw new RuntimeException("Wrong id delivered: pressed || typed");242243if (!keyListImpl.released)244throw new RuntimeException("Expected id, released, not delivered");245}246}247248249