Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/List/NofocusListDblClickTest/NofocusListDblClickTest.java
38828 views
/*1* Copyright (c) 2013, 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*/22/*23@test24@bug 624020225@summary Tests that non-focusable List in a Window generates ActionEvent.26@author [email protected]: area=awt-list27@run main NofocusListDblClickTest28*/2930import java.awt.*;31import java.awt.event.*;32import java.util.concurrent.atomic.AtomicInteger;33import javax.swing.SwingUtilities;3435public class NofocusListDblClickTest {36static final int EXPECTED_ACTION_COUNT = 2;37static Robot robot;38static final AtomicInteger actionPerformed = new AtomicInteger(0);39static List lst;4041public static void main(String[] args) throws Exception {42SwingUtilities.invokeAndWait(new Runnable() {43public void run() {44createAndShowGUI();45}46});47robot = new Robot();48robot.setAutoDelay(50);49robot.waitForIdle();50Thread.sleep(1000);5152// ACTION_PERFORMED event happens only on even clicks53clickTwiceOn(lst);54Thread.sleep(500);55clickTwiceOn(lst);56robot.waitForIdle();57Thread.sleep(1000);5859synchronized (actionPerformed) {60if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {61try {62actionPerformed.wait(3000);63} catch (InterruptedException e) {64System.out.println("Interrupted unexpectedly!");65throw new RuntimeException(e);66}67}68}6970if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {71System.out.println("No ActionEvent was generated. " + actionPerformed.get());72throw new RuntimeException("Test failed!");73}7475System.out.println("Test passed.");76}7778public static void createAndShowGUI() {79Frame f = new Frame("Owner");80Window w = new Window(f);81lst = new List(3, true);82//this.setLayout (new BorderLayout ());83f.setBounds(800, 0, 100, 100);84w.setLocation(800, 150);8586lst.add("item 0");87lst.add("item 1");88lst.add("item 2");8990lst.setFocusable(false);9192lst.addActionListener(new ActionListener() {93public void actionPerformed(ActionEvent e) {94System.out.println(e.toString());95synchronized (actionPerformed) {96if (EXPECTED_ACTION_COUNT == actionPerformed.incrementAndGet()) {97actionPerformed.notifyAll();98}99}100}101});102103w.add(lst);104w.pack();105106f.setVisible(true);107w.setVisible(true);108}109110static void clickTwiceOn(Component c) throws Exception {111Point p = c.getLocationOnScreen();112Dimension d = c.getSize();113114if (c instanceof Frame) {115robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);116} else {117robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));118}119120robot.mousePress(InputEvent.BUTTON1_MASK);121robot.mouseRelease(InputEvent.BUTTON1_MASK);122Thread.sleep(20);123robot.mousePress(InputEvent.BUTTON1_MASK);124robot.mouseRelease(InputEvent.BUTTON1_MASK);125}126}127128129