Path: blob/master/test/jdk/java/awt/List/NofocusListDblClickTest/NofocusListDblClickTest.java
41153 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@key headful25@bug 624020226@summary Tests that non-focusable List in a Window generates ActionEvent.27@author [email protected]: area=awt-list28@run main NofocusListDblClickTest29*/3031import java.awt.*;32import java.awt.event.*;33import java.util.concurrent.atomic.AtomicInteger;34import javax.swing.SwingUtilities;3536public class NofocusListDblClickTest {37static final int EXPECTED_ACTION_COUNT = 2;38static Robot robot;39static final AtomicInteger actionPerformed = new AtomicInteger(0);40static List lst;4142public static void main(String[] args) throws Exception {43SwingUtilities.invokeAndWait(new Runnable() {44public void run() {45createAndShowGUI();46}47});48robot = new Robot();49robot.setAutoDelay(50);50robot.waitForIdle();51Thread.sleep(1000);5253// ACTION_PERFORMED event happens only on even clicks54clickTwiceOn(lst);55Thread.sleep(500);56clickTwiceOn(lst);57robot.waitForIdle();58Thread.sleep(1000);5960synchronized (actionPerformed) {61if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {62try {63actionPerformed.wait(3000);64} catch (InterruptedException e) {65System.out.println("Interrupted unexpectedly!");66throw new RuntimeException(e);67}68}69}7071if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {72System.out.println("No ActionEvent was generated. " + actionPerformed.get());73throw new RuntimeException("Test failed!");74}7576System.out.println("Test passed.");77}7879public static void createAndShowGUI() {80Frame f = new Frame("Owner");81Window w = new Window(f);82lst = new List(3, true);83//this.setLayout (new BorderLayout ());84f.setBounds(800, 0, 100, 100);85w.setLocation(800, 150);8687lst.add("item 0");88lst.add("item 1");89lst.add("item 2");9091lst.setFocusable(false);9293lst.addActionListener(new ActionListener() {94public void actionPerformed(ActionEvent e) {95System.out.println(e.toString());96synchronized (actionPerformed) {97if (EXPECTED_ACTION_COUNT == actionPerformed.incrementAndGet()) {98actionPerformed.notifyAll();99}100}101}102});103104w.add(lst);105w.pack();106107f.setVisible(true);108w.setVisible(true);109}110111static void clickTwiceOn(Component c) throws Exception {112Point p = c.getLocationOnScreen();113Dimension d = c.getSize();114115if (c instanceof Frame) {116robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);117} else {118robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));119}120121robot.mousePress(InputEvent.BUTTON1_MASK);122robot.mouseRelease(InputEvent.BUTTON1_MASK);123Thread.sleep(20);124robot.mousePress(InputEvent.BUTTON1_MASK);125robot.mouseRelease(InputEvent.BUTTON1_MASK);126}127}128129130