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 /* @test
  24    @bug 4973721
  25    @summary Up and Down Arrow key buttons are not working for the JSpinner in Synth LAF
  26    @library ../../regtesthelpers
  27    @build Util
  28    @author Oleg Mokhovikov
  29    @run main bug4973721
  30 */
  31 
  32 import java.awt.Robot;
  33 import java.awt.Toolkit;
  34 import javax.swing.event.ChangeListener;
  35 import javax.swing.event.ChangeEvent;
  36 import java.awt.event.KeyEvent;
  37 import java.awt.event.FocusListener;
  38 import java.awt.event.FocusEvent;
  39 import javax.swing.*;
  40 import sun.awt.SunToolkit;
  41 
  42 public class bug4973721 implements ChangeListener, FocusListener {
  43     static volatile boolean bStateChanged = false;
  44     static volatile boolean bFocusGained = false;
  45     static JSpinner spinner;
  46     static final Object listener = new bug4973721();
  47     private static final SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  48 
  49     public void focusLost(FocusEvent e) {}
  50 
  51     public synchronized void focusGained(FocusEvent e) {
  52         System.out.println("focusGained");
  53         bFocusGained = true;
  54         notifyAll();
  55     }
  56 
  57     public synchronized void stateChanged(ChangeEvent e) {
  58         System.out.println("stateChanged");
  59         bStateChanged = true;
  60         notifyAll();
  61     }
  62 
  63     public static void main(String[] args) throws Exception {
  64         UIManager.setLookAndFeel("javax.swing.plaf.synth.SynthLookAndFeel");
  65 
  66         SwingUtilities.invokeAndWait(new Runnable() {
  67             public void run() {
  68                 final JFrame frame = new JFrame();
  69                 spinner = new JSpinner();
  70                 frame.getContentPane().add(spinner);
  71                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  72 
  73                 frame.pack();
  74                 frame.setVisible(true);
  75                 spinner.addChangeListener((ChangeListener)listener);
  76                 spinner.addFocusListener((FocusListener)listener);
  77                 spinner.requestFocus();
  78 
  79             }
  80         });
  81 
  82         synchronized(listener) {
  83             if (!bFocusGained) {
  84                 System.out.println("waiting focusGained...");
  85                 try { listener.wait(5000); } catch (InterruptedException e) {}
  86             }
  87         }
  88 
  89         boolean hasFocus = Util.invokeOnEDT(new java.util.concurrent.Callable<Boolean>() {
  90             @Override
  91             public Boolean call() throws Exception {
  92                 return spinner.hasFocus();
  93             }
  94         });
  95 
  96         if (!bFocusGained && !hasFocus) {
  97             throw new RuntimeException("Couldn't request focus for spinner");
  98         }
  99         Robot robot = new Robot();
 100         robot.setAutoDelay(50);
 101 
 102         Util.hitKeys(robot, KeyEvent.VK_UP);
 103         toolkit.realSync();
 104         Thread.sleep(1000);
 105 
 106         if (!bStateChanged) {
 107             throw new RuntimeException("Up arrow key button doesn't work for a spinner in Synth L&F");
 108         }
 109 
 110         bStateChanged = false;
 111 
 112         Util.hitKeys(robot, KeyEvent.VK_DOWN);
 113         toolkit.realSync();
 114         Thread.sleep(1000);
 115 
 116         if (!bStateChanged) {
 117             throw new RuntimeException("Down arrow key button doesn't work for a spinner in Synth L&F");
 118         }
 119     }
 120 }