1 /*
   2  * Copyright (c) 2011, 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 /*
  25   @test
  26   @bug 7050935
  27   @summary closed/java/awt/Choice/WheelEventsConsumed/WheelEventsConsumed.html fails on win32
  28   @library ../../regtesthelpers
  29   @author Oleg Pekhovskiy: area=awt-choice
  30   @build Util
  31   @run main ChoiceMouseWheelTest
  32 */
  33 
  34 import test.java.awt.regtesthelpers.Util;
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 
  39 public class ChoiceMouseWheelTest extends Frame {
  40 
  41     private volatile boolean itemChanged = false;
  42     private volatile boolean wheelMoved = false;
  43     private volatile boolean frameExited = false;
  44 
  45     public static void main(String[] args) {
  46         new ChoiceMouseWheelTest();
  47     }
  48 
  49     ChoiceMouseWheelTest() {
  50         super("ChoiceMouseWheelTest");
  51         setLayout(new FlowLayout());
  52 
  53         Choice choice = new Choice();
  54 
  55         addWindowListener(new WindowAdapter() {
  56             @Override
  57             public void windowClosing(WindowEvent e) {
  58                 System.exit(0);
  59             }
  60         });
  61 
  62         for(Integer i = 0; i < 50; i++) {
  63             choice.add(i.toString());
  64         }
  65 
  66         choice.addItemListener(new ItemListener() {
  67             public void itemStateChanged(ItemEvent e) {
  68                 itemChanged = true;
  69             }
  70         });
  71         choice.addMouseWheelListener(new MouseWheelListener() {
  72             public void mouseWheelMoved(MouseWheelEvent e) {
  73                 wheelMoved = true;
  74             }
  75         });
  76 
  77         addMouseListener(new MouseAdapter() {
  78             @Override
  79             public void mouseExited(MouseEvent e) {
  80                 frameExited = true;
  81             }
  82         });
  83 
  84         add(choice);
  85         setSize(200, 300);
  86         setVisible(true);
  87         toFront();
  88 
  89         try {
  90             Robot robot = new Robot();
  91             robot.setAutoDelay(20);
  92             Util.waitForIdle(robot);
  93 
  94             Point pt = choice.getLocationOnScreen();
  95             Dimension size = choice.getSize();
  96             int x = pt.x + size.width / 3;
  97             robot.mouseMove(x, pt.y + size.height / 2);
  98 
  99             // Test mouse wheel over the choice
 100             String name = Toolkit.getDefaultToolkit().getClass().getName();
 101 
 102             // mouse wheel doesn't work for the choice on X11 and Mac, so skip it
 103             if(!name.equals("sun.awt.X11.XToolkit")
 104                && !name.equals("sun.lwawt.macosx.LWCToolkit")) {
 105                 robot.mouseWheel(1);
 106                 Util.waitForIdle(robot);
 107 
 108                 if(!wheelMoved || !itemChanged) {
 109                     throw new RuntimeException("Mouse Wheel over the choice failed!");
 110                 }
 111             }
 112 
 113             // Test mouse wheel over the drop-down list
 114             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 115             Util.waitForIdle(robot);
 116             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 117             Util.waitForIdle(robot);
 118 
 119             int y = getLocationOnScreen().y + getSize().height;
 120             while(!frameExited && y >= 0) { // move to the bottom of drop-down list
 121                 robot.mouseMove(x, --y);
 122                 Util.waitForIdle(robot);
 123             }
 124 
 125             if(x < 0) {
 126                 throw new RuntimeException("Could not enter drop-down list!");
 127             }
 128 
 129             y -= choice.getHeight() / 2;
 130             robot.mouseMove(x, y); // move to the last visible item in the drop-down list
 131             Util.waitForIdle(robot);
 132 
 133             robot.mouseWheel(choice.getItemCount()); // wheel to the last item
 134             Util.waitForIdle(robot);
 135 
 136             // click the last item
 137             itemChanged = false;
 138             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 139             Util.waitForIdle(robot);
 140             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 141             Util.waitForIdle(robot);
 142 
 143             if(!itemChanged || choice.getSelectedIndex() != choice.getItemCount() - 1) {
 144                 throw new RuntimeException("Mouse Wheel scroll position error!");
 145             }
 146 
 147             dispose();
 148         } catch (AWTException e) {
 149             throw new RuntimeException("AWTException occurred - problem creating robot!");
 150         }
 151     }
 152 }
 153