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