1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24   @test
  25   @bug       6240202
  26   @summary   Tests that non-focusable List in a Window generates ActionEvent.
  27   @author    anton.tarasov@sun.com: area=awt-list
  28   @run       main NofocusListDblClickTest
  29 */
  30 
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import java.util.concurrent.atomic.AtomicInteger;
  34 import javax.swing.SwingUtilities;
  35 
  36 public class NofocusListDblClickTest {
  37     static final int EXPECTED_ACTION_COUNT = 2;
  38     static Robot robot;
  39     static final AtomicInteger actionPerformed = new AtomicInteger(0);
  40     static List lst;
  41 
  42     public static void main(String[] args) throws Exception {
  43         SwingUtilities.invokeAndWait(new Runnable() {
  44             public void run() {
  45                 createAndShowGUI();
  46             }
  47         });
  48         robot = new Robot();
  49         robot.setAutoDelay(50);
  50         robot.waitForIdle();
  51         Thread.sleep(1000);
  52 
  53         // ACTION_PERFORMED event happens only on even clicks
  54         clickTwiceOn(lst);
  55         Thread.sleep(500);
  56         clickTwiceOn(lst);
  57         robot.waitForIdle();
  58         Thread.sleep(1000);
  59 
  60         synchronized (actionPerformed) {
  61             if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {
  62                 try {
  63                     actionPerformed.wait(3000);
  64                 } catch (InterruptedException e) {
  65                     System.out.println("Interrupted unexpectedly!");
  66                     throw new RuntimeException(e);
  67                 }
  68             }
  69         }
  70 
  71         if (actionPerformed.get() != EXPECTED_ACTION_COUNT) {
  72             System.out.println("No ActionEvent was generated. " + actionPerformed.get());
  73             throw new RuntimeException("Test failed!");
  74         }
  75 
  76         System.out.println("Test passed.");
  77     }
  78 
  79     public static void createAndShowGUI() {
  80         Frame f = new Frame("Owner");
  81         Window w = new Window(f);
  82         lst = new List(3, true);
  83         //this.setLayout (new BorderLayout ());
  84         f.setBounds(800, 0, 100, 100);
  85         w.setLocation(800, 150);
  86 
  87         lst.add("item 0");
  88         lst.add("item 1");
  89         lst.add("item 2");
  90 
  91         lst.setFocusable(false);
  92 
  93         lst.addActionListener(new ActionListener() {
  94                 public void actionPerformed(ActionEvent e) {
  95                     System.out.println(e.toString());
  96                     synchronized (actionPerformed) {
  97                         if (EXPECTED_ACTION_COUNT == actionPerformed.incrementAndGet()) {
  98                             actionPerformed.notifyAll();
  99                         }
 100                     }
 101                 }
 102             });
 103 
 104         w.add(lst);
 105         w.pack();
 106 
 107         f.setVisible(true);
 108         w.setVisible(true);
 109     }
 110 
 111     static void clickTwiceOn(Component c) throws Exception {
 112         Point p = c.getLocationOnScreen();
 113         Dimension d = c.getSize();
 114 
 115         if (c instanceof Frame) {
 116             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 117         } else {
 118             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 119         }
 120 
 121         robot.mousePress(InputEvent.BUTTON1_MASK);
 122         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 123         Thread.sleep(20);
 124         robot.mousePress(InputEvent.BUTTON1_MASK);
 125         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 126     }
 127 }