1 /*
   2  * Copyright (c) 2005, 2018, 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   @key headful
  27   @bug 6190768 6190778
  28   @summary Tests that triggering events on AWT list by pressing CTRL + HOME,
  29            CTRL + END, PG-UP, PG-DOWN similar Motif behavior
  30   @library /test/lib
  31   @build jdk.test.lib.Platform
  32   @run main KeyEventsTest
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import java.lang.reflect.*;
  38 
  39 import jdk.test.lib.Platform;
  40 
  41 public class KeyEventsTest extends Frame implements ItemListener, FocusListener, KeyListener
  42 {
  43     TestState currentState;
  44     final Object LOCK = new Object();
  45     final int ACTION_TIMEOUT = 500;
  46 
  47     List single = new List(3, false);
  48     List multiple = new List(3, true);
  49 
  50     Panel p1 = new Panel ();
  51     Panel p2 = new Panel ();
  52 
  53     public static void main(final String[] args) {
  54         KeyEventsTest app = new KeyEventsTest();
  55         app.init();
  56         app.start();
  57     }
  58 
  59     public void init()
  60     {
  61         setLayout (new BorderLayout ());
  62 
  63         single.add("0");
  64         single.add("1");
  65         single.add("2");
  66         single.add("3");
  67         single.add("4");
  68         single.add("5");
  69         single.add("6");
  70         single.add("7");
  71         single.add("8");
  72 
  73         multiple.add("0");
  74         multiple.add("1");
  75         multiple.add("2");
  76         multiple.add("3");
  77         multiple.add("4");
  78         multiple.add("5");
  79         multiple.add("6");
  80         multiple.add("7");
  81         multiple.add("8");
  82 
  83         single.addKeyListener(this);
  84         single.addItemListener(this);
  85         single.addFocusListener(this);
  86         p1.add(single);
  87         add("North", p1);
  88 
  89         multiple.addKeyListener(this);
  90         multiple.addItemListener(this);
  91         multiple.addFocusListener(this);
  92         p2.add(multiple);
  93         add("South", p2);
  94 
  95     }//End  init()
  96 
  97     public void start ()
  98     {
  99 
 100         try{
 101             setSize (200,200);
 102             validate();
 103             setUndecorated(true);
 104             setLocationRelativeTo(null);
 105             setVisible(true);
 106 
 107             doTest();
 108             System.out.println("Test passed.");
 109         } catch (Exception e) {
 110             e.printStackTrace();
 111             throw new RuntimeException("The test failed.");
 112         }
 113 
 114     }// start()
 115 
 116     public void itemStateChanged (ItemEvent ie) {
 117         System.out.println("itemStateChanged-"+ie);
 118         this.currentState.setAction(true);
 119     }
 120 
 121     public void focusGained(FocusEvent e){
 122 
 123         synchronized (LOCK) {
 124             LOCK.notifyAll();
 125         }
 126 
 127     }
 128 
 129     public void focusLost(FocusEvent e){
 130     }
 131 
 132     public void keyPressed(KeyEvent e){
 133         System.out.println("keyPressed-"+e);
 134     }
 135 
 136     public void keyReleased(KeyEvent e){
 137         System.out.println("keyReleased-"+e);
 138     }
 139 
 140     public void keyTyped(KeyEvent e){
 141         System.out.println("keyTyped-"+e);
 142     }
 143 
 144     private void test(TestState currentState)
 145       throws InterruptedException, InvocationTargetException {
 146 
 147         synchronized (LOCK) {
 148 
 149             this.currentState = currentState;
 150             System.out.println(this.currentState);
 151 
 152             List list;
 153             if (currentState.getMultiple()){
 154                 list = multiple;
 155             }else{
 156                 list = single;
 157             }
 158 
 159             Robot r;
 160             try {
 161                 r = new Robot();
 162             } catch(AWTException e) {
 163                 throw new RuntimeException(e.getMessage());
 164             }
 165 
 166             r.delay(10);
 167             Point loc = this.getLocationOnScreen();
 168 
 169             r.mouseMove(loc.x+10, loc.y+10);
 170             r.mousePress(InputEvent.BUTTON1_MASK);
 171             r.delay(10);
 172             r.mouseRelease(InputEvent.BUTTON1_MASK);
 173             r.delay(10);
 174 
 175             list.requestFocusInWindow();
 176             LOCK.wait(ACTION_TIMEOUT);
 177             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list){
 178                 throw new RuntimeException("Test failed - list isn't focus owner.");
 179             }
 180 
 181             list.deselect(0);
 182             list.deselect(1);
 183             list.deselect(2);
 184             list.deselect(3);
 185             list.deselect(4);
 186             list.deselect(5);
 187             list.deselect(6);
 188             list.deselect(7);
 189             list.deselect(8);
 190 
 191             int selectIndex = 0;
 192             int visibleIndex = 0;
 193 
 194             if (currentState.getScrollMoved()){
 195 
 196                 if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP ||
 197                     currentState.getKeyID() == KeyEvent.VK_HOME){
 198                     selectIndex = 8;
 199                     visibleIndex = 8;
 200                 }else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN ||
 201                     currentState.getKeyID() == KeyEvent.VK_END){
 202                     selectIndex = 0;
 203                     visibleIndex = 0;
 204                 }
 205 
 206             }else{
 207 
 208                 if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP ||
 209                     currentState.getKeyID() == KeyEvent.VK_HOME){
 210 
 211                     if (currentState.getSelectedMoved()){
 212                         selectIndex = 1;
 213                         visibleIndex = 0;
 214                     }else{
 215                         selectIndex = 0;
 216                         visibleIndex = 0;
 217                     }
 218 
 219                 }else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN ||
 220                     currentState.getKeyID() == KeyEvent.VK_END){
 221 
 222                     if (currentState.getSelectedMoved()){
 223                         selectIndex = 7;
 224                         visibleIndex = 8;
 225                     }else{
 226                         selectIndex = 8;
 227                         visibleIndex = 8;
 228                     }
 229 
 230                 }
 231 
 232             }
 233 
 234             list.select(selectIndex);
 235             list.makeVisible(visibleIndex);
 236 
 237             r.delay(10);
 238 
 239             if (currentState.getKeyID() == KeyEvent.VK_HOME ||
 240                 currentState.getKeyID() == KeyEvent.VK_END){
 241                 r.keyPress(KeyEvent.VK_CONTROL);
 242             }
 243 
 244             r.delay(10);
 245             r.keyPress(currentState.getKeyID());
 246             r.delay(10);
 247             r.keyRelease(currentState.getKeyID());
 248             r.delay(10);
 249 
 250             if (currentState.getKeyID() == KeyEvent.VK_HOME ||
 251                 currentState.getKeyID() == KeyEvent.VK_END){
 252                 r.keyRelease(KeyEvent.VK_CONTROL);
 253             }
 254 
 255             r.waitForIdle();
 256             r.delay(200);
 257 
 258             if (currentState.getTemplate() != currentState.getAction())
 259                 throw new RuntimeException("Test failed.");
 260 
 261         }
 262 
 263     }
 264 
 265     private void doTest()
 266       throws InterruptedException, InvocationTargetException {
 267 
 268         boolean isWin = false;
 269         if (Platform.isWindows()) {
 270             isWin = true;
 271         } else if (Platform.isOSX()) {
 272             System.out.println("Not for OS X");
 273             return;
 274         }
 275 
 276         System.out.println("multiple? selectedMoved? ?scrollMoved keyID? template? action?");
 277         test(new TestState(false, false, false, KeyEvent.VK_PAGE_UP, isWin?false:false));
 278         // SelectedMoved (false) != ScrollMoved (true) for single list not emulated
 279         test(new TestState(false, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false));
 280         test(new TestState(false, true, true, KeyEvent.VK_PAGE_UP, isWin?true:true));
 281         test(new TestState(true, false, false, KeyEvent.VK_PAGE_UP, isWin?true:false));
 282         test(new TestState(true, false, true, KeyEvent.VK_PAGE_UP, isWin?true:false));
 283         test(new TestState(true, true, false, KeyEvent.VK_PAGE_UP, isWin?true:false));
 284         test(new TestState(true, true, true, KeyEvent.VK_PAGE_UP, isWin?true:false));
 285 
 286         test(new TestState(false, false, false, KeyEvent.VK_PAGE_DOWN, isWin?false:false));
 287         test(new TestState(false, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));
 288         test(new TestState(false, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:true));
 289         test(new TestState(true, false, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));
 290         test(new TestState(true, false, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false));
 291         test(new TestState(true, true, false, KeyEvent.VK_PAGE_DOWN, isWin?true:false));
 292         test(new TestState(true, true, true, KeyEvent.VK_PAGE_DOWN, isWin?true:false));
 293 
 294         test(new TestState(false, false, false, KeyEvent.VK_HOME, isWin?false:true));
 295         test(new TestState(false, true, false, KeyEvent.VK_HOME, isWin?true:true));
 296         test(new TestState(false, true, true, KeyEvent.VK_HOME, isWin?true:true));
 297         test(new TestState(true, false, false, KeyEvent.VK_HOME, isWin?true:false));
 298         test(new TestState(true, false, true, KeyEvent.VK_HOME, isWin?true:false));
 299         test(new TestState(true, true, false, KeyEvent.VK_HOME, isWin?true:false));
 300         test(new TestState(true, true, true, KeyEvent.VK_HOME, isWin?true:false));
 301 
 302         test(new TestState(false, false, false, KeyEvent.VK_END, isWin?false:true));
 303         test(new TestState(false, true, false, KeyEvent.VK_END, isWin?true:true));
 304         test(new TestState(false, true, true, KeyEvent.VK_END, isWin?true:true));
 305         test(new TestState(true, false, false, KeyEvent.VK_END, isWin?true:false));
 306         test(new TestState(true, false, true, KeyEvent.VK_END, isWin?true:false));
 307         test(new TestState(true, true, false, KeyEvent.VK_END, isWin?true:false));
 308         test(new TestState(true, true, true, KeyEvent.VK_END, isWin?true:false));
 309 
 310     }
 311 }// class KeyEventsTest
 312 
 313 class TestState{
 314 
 315     private boolean multiple;
 316     // after key pressing selected item moved
 317     private final boolean selectedMoved;
 318     // after key pressing scroll moved
 319     private final boolean scrollMoved;
 320     private final int keyID;
 321     private final boolean template;
 322     private boolean action;
 323 
 324     public TestState(boolean multiple, boolean selectedMoved, boolean scrollMoved, int keyID, boolean template){
 325         this.multiple = multiple;
 326         this.selectedMoved = selectedMoved;
 327         this.scrollMoved = scrollMoved;
 328         this.keyID = keyID;
 329         this.template = template;
 330         this.action = false;
 331     }
 332 
 333     public boolean getMultiple(){
 334         return multiple;
 335     }
 336     public boolean getSelectedMoved(){
 337         return selectedMoved;
 338     }
 339 
 340     public boolean getScrollMoved(){
 341         return scrollMoved;
 342     }
 343 
 344     public int getKeyID(){
 345         return keyID;
 346     }
 347 
 348     public boolean getTemplate(){
 349         return template;
 350     }
 351 
 352     public boolean getAction(){
 353         return action;
 354     }
 355 
 356     public void setAction(boolean action){
 357         this.action = action;
 358     }
 359 
 360     public String toString(){
 361         return multiple + "," + selectedMoved + "," + scrollMoved + "," + keyID + "," + template + "," + action;
 362     }
 363 }// TestState