Path: blob/master/test/jdk/java/awt/Focus/NonFocusableWindowTest/NoEventsTest.java
41152 views
/*1* Copyright (c) 2008, 2016, 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*/2223/*24@test25@key headful26@bug 445238427@summary Tests that non-focusable windows doesn't generate any focus events when accessed.28@author Denis.Mikhalkin: area=awt.focus29@run main NoEventsTest30*/3132import java.awt.*;33import java.awt.event.*;34import java.util.*;3536public class NoEventsTest extends Frame {37public static final int DEF_WIDTH = 400,38DEF_HEIGHT = 300,39DEF_TOP = 1,40DEF_LEFT = 100,41DEF_ROW = 0,42DEF_COL = 0;43static boolean automatic = true;44static Window[] windows;45static Frame main_frame, jumpingFrame;46static Button focus_button;47static Robot robot;48static void pause(int timeout) {49Toolkit.getDefaultToolkit().sync();50robot.waitForIdle();51robot.delay(100);52}53static GlobalListener listener;54public static void main(String[] args) {5556listener = new GlobalListener();57Toolkit.getDefaultToolkit().addAWTEventListener(listener,58AWTEvent.FOCUS_EVENT_MASK |59AWTEvent.WINDOW_EVENT_MASK);60try{61robot = new Robot();62} catch(Exception e) {}63// Create several pairs - focusable Frame with focusable component(button) and non-focusable:64// window, resizable frame, non-resizable frame, dialog, non-resiable dialog65main_frame = new Frame("focusable frame");66focus_button = new Button("button to focus");67main_frame.add(focus_button);68main_frame.pack();69main_frame.setVisible(true);70main_frame.setLocation(10, 600);71main_frame.addWindowListener(new WindowAdapter() {72public void windowClosing(WindowEvent e) {73listener.report();74System.exit(0);75}76});7778jumpingFrame = new Frame("Jumping frame");79jumpingFrame.setBounds(DEF_LEFT, DEF_TOP, DEF_WIDTH, DEF_HEIGHT);8081windows = new Window[7];82windows[0] = new TestWindow(0, 0, false, main_frame);83//windows[1] = new TestWindow(2, 1, true, main_frame);84windows[2] = new NoEventsTest(1, 0, false, true);85windows[3] = new NoEventsTest(2, 0, false, false);86//windows[4] = new Test(3, 0, true, true);87windows[5] = new TestDialog(0, 1, false, true, main_frame);88windows[6] = new TestDialog(1, 1, false, false, main_frame);89if (!automatic) {90int windowInd;91for (windowInd = 0; windowInd < windows.length; windowInd++) {92if (windows[windowInd] != null) {93windows[windowInd].setVisible(true);94}95}96}97// Run the test98// 1. Click on all controls, check for no focus events for non-focusable, right focus events for focusable99// 2. Perform some action with control, check if it works100if (automatic) {101int windowInd;102for (windowInd = 0; windowInd < windows.length; windowInd++) {103if (windows[windowInd] != null) {104windows[windowInd].setVisible(true);105focus_button.requestFocus();106pause(1000);107108// Verify that click on non-focusable window causes no focus lost on active window109performFocusClick(windows[windowInd]);110focus_button.requestFocus();111pause(500);112performActionClick(windows[windowInd]);113114// Verify that toFront, toBack doesn't cause non-focusable window to become active115jumpingFrame.setVisible(true);116pause(1000);117jumpingFrame.toBack();118pause(500);119jumpingFrame.toFront();120pause(500);121windows[windowInd].toBack();122pause(500);123windows[windowInd].toFront();124pause(500);125126// Verify that iconifiyng/deiconfiying and127// zooming/unzooming doesn't cause non-focusable128// window to become active129if (windows[windowInd] instanceof Frame) {130Frame toTest = (Frame)windows[windowInd];131// Deiconification currently doesn't work!132// toTest.setExtendedState(Frame.ICONIFIED);133// pause(500);134// toTest.setExtendedState(Frame.NORMAL);135pause(500);136toTest.setExtendedState(Frame.MAXIMIZED_BOTH);137pause(500);138toTest.setExtendedState(Frame.NORMAL);139}140141windows[windowInd].dispose();142jumpingFrame.dispose();143}144}145pause(1000);146System.err.println("Test finished.");147if (!listener.report()) {148throw new RuntimeException("Test Failed. See error stream output for details");149}150}151}152static void performFocusClick(Window parent) {153if (parent == null) {154return;155}156for (int compInd = 0; compInd < parent.getComponentCount(); compInd++) {157Component child = parent.getComponent(compInd);158if (child instanceof TestPanel) {159TestPanel pan = (TestPanel)child;160pan.performFocusClicks(robot);161pause(100);162}163}164}165static void performActionClick(Window parent) {166if (parent == null) {167return;168}169for (int compInd = 0; compInd < parent.getComponentCount(); compInd++) {170Component child = parent.getComponent(compInd);171if (child instanceof TestPanel) {172TestPanel pan = (TestPanel)child;173pan.performActionClicks(robot);174pause(100);175}176}177}178public NoEventsTest(int row, int col, boolean focusable, boolean resizable) {179super("Frame" + row + "" + col);180TestPanel panel = new TestPanel(row, col);181if (NoEventsTest.automatic) {182row = NoEventsTest.DEF_ROW;183col = NoEventsTest.DEF_COL;184}185setName(getTitle());186add("Center", panel);187Label l = new Label(getClass().getSuperclass().getName() + ", " + (focusable?"focusable":"non-focusable") +188", " + (resizable?"resizable":"non-resizable"));189l.setBackground(Color.green);190add("North", l);191setBounds(NoEventsTest.DEF_LEFT + DEF_WIDTH*col, DEF_TOP + DEF_HEIGHT*row, DEF_WIDTH, DEF_HEIGHT);192if (!focusable) {193setFocusableWindowState(false);194}195if (!resizable) {196setResizable(false);197}198// setVisible(true);199}200}201class TestWindow extends Window {202public TestWindow(int row, int col, boolean focusable, Frame owner) {203super(owner);204setName("Window" + row + "" + col);205TestPanel panel = new TestPanel(row, col);206if (NoEventsTest.automatic) {207row = NoEventsTest.DEF_ROW;208col = NoEventsTest.DEF_COL;209}210211add("Center", panel);212Label l = new Label(getClass().getSuperclass().getName() + ", " + (focusable?"focusable":"non-focusable") +213", " + (false?"resizable":"non-resizable"));214l.setBackground(Color.green);215add("North", l);216217setBounds(NoEventsTest.DEF_LEFT + NoEventsTest.DEF_WIDTH*col, NoEventsTest.DEF_TOP + NoEventsTest.DEF_HEIGHT*row, NoEventsTest.DEF_WIDTH, NoEventsTest.DEF_HEIGHT);218if (!focusable) {219setFocusableWindowState(false);220}221// setVisible(true);222}223}224class TestDialog extends Dialog {225public TestDialog(int row, int col, boolean focusable, boolean resizable, Frame owner) {226super(owner);227setName("Dialog" + row + "" + col);228TestPanel panel = new TestPanel(row, col);229if (NoEventsTest.automatic) {230row = NoEventsTest.DEF_ROW;231col = NoEventsTest.DEF_COL;232}233234add("Center", panel);235Label l = new Label(getClass().getSuperclass().getName() + ", " + (focusable?"focusable":"non-focusable") +236", " + (resizable?"resizable":"non-resizable"));237l.setBackground(Color.green);238add("North", l);239240setBounds(NoEventsTest.DEF_LEFT + NoEventsTest.DEF_WIDTH*col, NoEventsTest.DEF_TOP + NoEventsTest.DEF_HEIGHT*row, NoEventsTest.DEF_WIDTH, NoEventsTest.DEF_HEIGHT);241if (!focusable) {242setFocusableWindowState(false);243}244if (!resizable) {245setResizable(false);246}247// setVisible(true);248}249}250251class TestPanel extends Panel {252253void clickComponent(Component comp, Robot robot) {254if (comp instanceof Choice) {255return;256}257Point compLoc = comp.getLocationOnScreen();258Dimension size = comp.getSize();259robot.mouseMove(compLoc.x + size.width/2, compLoc.y + size.height/2);260robot.mousePress(InputEvent.BUTTON1_MASK);261robot.mouseRelease(InputEvent.BUTTON1_MASK);262}263void performFocusClicks(Robot robot) {264for (int childInd = 0; childInd < getComponentCount(); childInd++) {265performFocusClick(getComponent(childInd), robot);266}267}268void performFocusClick(Component comp, Robot robot) {269clickComponent(comp, robot);270}271272void performActionClicks(Robot robot) {273for (int childInd = 0; childInd < getComponentCount(); childInd++) {274performActionClick(getComponent(childInd), robot);275}276}277void performActionClick(Component comp, Robot robot) {278}279280public TestPanel(int row, int col) {281setLayout(new FlowLayout());282Button b;283add(b = new Button("press"+ row + "" + col));284b.setName(b.getLabel());285// b.addMouseListener(new MouseAdapter() {286// public void mousePressed(MouseEvent e) {287// System.err.println(e);288// }289// });290TextField t;291add(t = new TextField("text" + row + "" + col));292t.setName(t.getText());293294java.awt.List list = new java.awt.List();295add(list);296list.setName("list");297list.add("one");298list.add("two");299list.add("three");300list.setMultipleMode(true);301list.setName("list" + row + "" + col);302303Checkbox check = new Checkbox("checker");304add(check);305check.setName("check" + row + "" + col);306307Choice choice = new Choice();308choice.add("one");309choice.add("two");310choice.add("three");311add(choice);312choice.setName("choice" + row + "" + col);313314Canvas can = new Canvas() {315public Dimension getPreferredSize() {316return new Dimension(10, 10);317}318};319can.setBackground(Color.blue);320add(can);321can.setName("canvas" + row + "" + col);322323TextArea ta = new TextArea("text\ntttt\naaaa\nwwwww\nqqqqqq\neeeeee\nrrrrrr\nyyyyyy\nuuuuu", 3, 5);324add(ta);325ta.setName("textarea" + row + "" + col);326327Scrollbar bar = new Scrollbar(Scrollbar.HORIZONTAL);328add(bar);329bar.setName("scrollbar" + row + "" + col);330331CheckboxGroup group = new CheckboxGroup();332Checkbox ch1 = new Checkbox("one", group, true);333Checkbox ch2 = new Checkbox("two", group, false);334add(ch1);335add(ch2);336ch1.setName("checkbox1 " + row + "" + col);337ch2.setName("checkbox2 " + row + "" + col);338339340ScrollPane pane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);341add(pane);342Button bigButton = new Button("abc") {343public Dimension getPreferredSize() {344return new Dimension(100, 100);345}346};347pane.add(bigButton);348bigButton.setName("bigbutton" + row + "" + col);349}350}351352class GlobalListener implements AWTEventListener {353java.util.List errors = new java.util.LinkedList();354public boolean report() {355if (errors.size() != 0) {356System.err.println("Test FAILED");357} else {358System.err.println("Test PASSED");359return true;360}361ListIterator iter = errors.listIterator();362while (iter.hasNext()) {363System.err.println(iter.next());364}365return false;366}367public GlobalListener() {368}369Window getWindowParent(Component comp) {370while (comp != null && !(comp instanceof Window)) {371comp = comp.getParent();372}373return (Window)comp;374}375void reportError(AWTEvent e, String message) {376String error = "ERROR: " + message + " : " + e;377errors.add(error);378System.err.println(error);379}380public void eventDispatched(AWTEvent e) {381Component comp = (Component)e.getSource();382Window parent = getWindowParent(comp);383if (!(e instanceof WindowEvent || e instanceof FocusEvent)) {384System.err.println("Strange event " + e);385}386387// Skip WINDOW_OPENED388if (e.getID() == WindowEvent.WINDOW_CLOSING) {389System.err.println(e);390}391switch (e.getID()) {392case WindowEvent.WINDOW_OPENED:393case WindowEvent.WINDOW_CLOSING:394case WindowEvent.WINDOW_CLOSED:395case WindowEvent.WINDOW_ICONIFIED:396case WindowEvent.WINDOW_DEICONIFIED:397case WindowEvent.WINDOW_STATE_CHANGED:398return;399case WindowEvent.WINDOW_LOST_FOCUS: {400WindowEvent we = (WindowEvent)e;401if (we.getOppositeWindow() != null && !we.getOppositeWindow().getFocusableWindowState()) {402reportError(e, "frame lost focus because of non-focusable window");403}404break;405}406}407// Check that Window owner is focusable408if (!parent.getFocusableWindowState()) {409reportError(e, "focus event for component in non-focusable window " + parent.getName());410}411if (!comp.isFocusable()) {412reportError(e, "focus event for non-focusable component");413}414// if (e instanceof WindowEvent || e instanceof FocusEvent) {415// // System.err.println(e);416// }417}418}419420421