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  @summary <rdar://problem/4307013> Moving Focus Between AWT and Swing Controls Requires a Second <tab> or <shift+tab>
  27  @summary com.apple.junit.java.awt.Event;
  28  @library ../../regtesthelpers
  29  @build RobotUtilities
  30  @build VisibilityValidator
  31  @run main R4323039_SimpleControls
  32  */
  33 
  34 import junit.framework.*;
  35 
  36 import javax.swing.*;
  37 import java.awt.*;
  38 import java.awt.event.*;
  39 
  40 import test.java.awt.regtesthelpers.RobotUtilities;
  41 import test.java.awt.regtesthelpers.VisibilityValidator;
  42 
  43 public class R4323039_SimpleControls extends TestCase {
  44 
  45     public static Test suite() {
  46         return new TestSuite( R4323039_SimpleControls.class);
  47     }
  48 
  49     public static void main (String[] args) throws RuntimeException {
  50         TestResult tr = junit.textui.TestRunner.run(suite());
  51         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  52             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  53         }
  54     }
  55 
  56     volatile int buttonPresses = 0;
  57 
  58     public void testTabFocusTransition() throws Exception {
  59         buttonPresses = 0;
  60 
  61         JFrame frame = new JFrame("Simple Controls Test");
  62         try {
  63             Container content = frame.getContentPane();
  64             content.setLayout(new FlowLayout());
  65 
  66             JButton jbutton = new JButton("JButton");
  67             jbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
  68                 buttonPresses++;
  69             }});
  70             content.add(jbutton);
  71 
  72             content.add(new JComboBox(new String[] {"JComboBox", "Stuff", "Yoink"}));
  73 
  74             content.add(new Button("Button"));
  75 
  76             final Choice choice = new Choice();
  77             choice.add("Choice");
  78             choice.add("Stuff");
  79             choice.add("Yoink");
  80             content.add(choice);
  81 
  82             content.add(new JLabel(System.getProperty("java.version")));
  83 
  84             frame.setSize(400, 300);
  85 
  86             VisibilityValidator.setVisibleAndConfirm(frame);
  87             pause(500);
  88 
  89             // activate the first button
  90             RobotUtilities.typeKey(KeyEvent.VK_SPACE);
  91             pause(500);
  92 
  93             // tab back to the first button
  94             for (int i=0; i<4; i++) {
  95                 RobotUtilities.typeKey(KeyEvent.VK_TAB);
  96                 pause(200);
  97             }
  98 
  99             // activate the first button
 100             RobotUtilities.typeKey(KeyEvent.VK_SPACE);
 101             pause(500);
 102 
 103             // Wait for events to be delivered
 104             try {
 105                 SwingUtilities.invokeAndWait(new Runnable() { public void run() {} });
 106             } catch (Exception e) {}
 107 
 108             // Test output
 109             assertTrue("JButton wasn't pressed exactly twice! " + buttonPresses, (buttonPresses == 2));
 110         } finally {
 111             if (frame != null) {
 112                 frame.setVisible(false);
 113                 frame.dispose();
 114                 frame = null;
 115             }
 116         }
 117     }
 118 
 119     private void pause(long time) {
 120         try {
 121             Thread.sleep(time);
 122         } catch (Exception e) {
 123             e.printStackTrace();
 124         }
 125     }
 126 }