1 /* 2 * Copyright (c) 1998, 2015, 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 import java.awt.*; 25 import java.awt.event.*; 26 27 /** 28 * @test 29 * @bug 4449139 30 * @summary test MouseWheelEvent generation by Scrollbar component 31 */ 32 public final class ScrollbarMouseWheelTest 33 implements MouseWheelListener, WindowListener { 34 35 final static String tk = Toolkit.getDefaultToolkit().getClass().getName(); 36 final static int REPS = 5; 37 // There is a bug on Windows: 4616935. 38 // Wheel events comes to every component in the hierarchy so we should 39 // check a platform. 40 // There are two scrollbars within one Panel and both accept 5 clicks, so 41 // Panel would accept 5*2 clicks on Windows. 42 final static int PANEL_REPS = tk.equals("sun.awt.windows.WToolkit")? REPS * 2: REPS; 43 44 Scrollbar sb1; 45 Scrollbar sb2; 46 Panel pnl; 47 class Sema { 48 boolean flag; 49 boolean getVal() { return flag;} 50 void setVal(boolean b) { flag = b;} 51 } 52 Sema sema = new Sema(); 53 54 Robot robot; 55 56 int sb1upevents, sb2upevents, pnlupevents; 57 int sb1downevents, sb2downevents, pnldownevents; 58 59 public static void main(final String[] args) { 60 new ScrollbarMouseWheelTest().test(); 61 } 62 63 public void test() { 64 // Move mouse to upper-right area 65 try { 66 robot = new Robot(); 67 } catch (AWTException e) { 68 System.out.println("Problem creating Robot. FAIL."); 69 throw new RuntimeException("Problem creating Robot. FAIL."); 70 71 } 72 73 robot.setAutoDelay(500); 74 robot.setAutoWaitForIdle(true); 75 76 // Show test Frame 77 Frame frame = new Frame("ScrollbarMouseWheelTest"); 78 frame.addWindowListener(this); 79 pnl = new Panel(); 80 pnl.setLayout(new GridLayout(1, 2)); 81 pnl.addMouseWheelListener(this); 82 sb1 = new Scrollbar(); 83 sb1.addMouseWheelListener(this); 84 pnl.add(sb1); 85 sb2 = new Scrollbar(); 86 pnl.add(sb2); 87 frame.add(pnl); 88 frame.setSize(200, 400); 89 frame.setLocationRelativeTo(null); 90 frame.setVisible(true); 91 frame.toFront(); 92 93 // When Frame is active, start testing (handled in windowActivated()) 94 while (true) { 95 synchronized (sema) { 96 if (sema.getVal()) { 97 break; 98 } 99 } 100 } 101 // up on sb1 102 testComp(sb1, true); 103 // down on sb1 104 testComp(sb1, false); 105 // up on sb2 106 testComp(sb2, true); 107 // down on sb2 108 testComp(sb2, false); 109 frame.dispose(); 110 System.out.println("Test done."); 111 if (sb1upevents == REPS && 112 sb2upevents == 0 && 113 pnlupevents == PANEL_REPS && 114 sb1downevents == REPS && 115 sb2downevents == 0 && 116 pnldownevents == PANEL_REPS) { 117 System.out.println("PASSED."); 118 } else { 119 System.out.println("Test Failed:" + 120 "\n\tsb1upevents =" + sb1upevents + 121 "\n\tsb2upevents = " + sb2upevents + 122 "\n\tpnlupevents = " + pnlupevents + 123 "\n\tsb1downevents =" + sb1downevents + 124 "\n\tsb2downevents = " + sb2downevents + 125 "\n\tpnldownevents = " + pnldownevents); 126 throw new RuntimeException("Test FAILED."); 127 } 128 } 129 130 public void testComp(Component comp, boolean up) { 131 Point loc = comp.getLocationOnScreen(); 132 robot.mouseMove(loc.x + comp.getWidth() / 2, 133 loc.y + comp.getHeight() / 2); 134 for (int loop = 0; loop < REPS; loop++) { 135 System.out.println("Robot.mouseWheel() on " + comp.getName()); 136 robot.mouseWheel(up ? -1 : 1); 137 } 138 } 139 140 public void mouseWheelMoved(MouseWheelEvent mwe) { 141 Component src = mwe.getComponent(); 142 System.out.println("mouseWheelMoved() on " + src.getName()); 143 if (mwe.getWheelRotation() == -1) { 144 if (src == sb1) { 145 sb1upevents++; 146 } else if (src == sb2) { 147 sb2upevents++; 148 } else if (src == pnl) { 149 pnlupevents++; 150 } else { 151 System.out.println("weird source component"); 152 } 153 } else if (mwe.getWheelRotation() == 1) { 154 if (src == sb1) { 155 sb1downevents++; 156 } else if (src == sb2) { 157 sb2downevents++; 158 } else if (src == pnl) { 159 pnldownevents++; 160 } else { 161 System.out.println("weird source component"); 162 } 163 } else { 164 System.out.println("weird wheel rotation"); 165 } 166 } 167 168 public void windowActivated(WindowEvent we) { 169 synchronized (sema) { 170 sema.setVal(true); 171 } 172 } 173 174 public void windowClosed(WindowEvent we) {} 175 public void windowClosing(WindowEvent we) {} 176 public void windowDeactivated(WindowEvent we) {} 177 public void windowDeiconified(WindowEvent we) {} 178 public void windowIconified(WindowEvent we) {} 179 public void windowOpened(WindowEvent we) {} 180 }// class ScrollbarMouseWheelTest