1 /*
   2  * Copyright (c) 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 8206392
  28   @requires (os.family == "mac")
  29   @summary Cycle through frames using keyboard shortcut doesn't work on Mac
  30   @compile CycleThroughFrameTest.java
  31   @run main/manual CycleThroughFrameTest
  32 */
  33 
  34 import java.awt.Frame;
  35 import java.awt.Button;
  36 import java.awt.TextArea;
  37 import java.awt.FlowLayout;
  38 import javax.swing.JFrame;
  39 import javax.swing.SwingUtilities;
  40 
  41 public class CycleThroughFrameTest {
  42 
  43     public static final int maxFrames = 5;
  44     private static JFrame[] frame;
  45     private static Frame instructionFrame;
  46     private static volatile boolean testContinueFlag = true;
  47 
  48     private static final String TEST_INSTRUCTIONS =
  49         " This is a manual test\n\n" +
  50         " 1) Configure Keyboard shortcut if not done in your system:\n" +
  51         " 2) Open System Preferences, go to -> Keyboard -> Shortcuts -> Keyboard\n" +
  52         " 3) Enable 'Move focus to next window' if disabled\n" +
  53         " 4) Enable 'Move focus to next window drawer' if disabled\n" +
  54         " 5) Close System Preferences\n" +
  55         " 5) Press COMMAND + ` keys to cycle through frames in forward order\n" +
  56         " 6) Press FAIL if focus doesn't move to next frame\n" +
  57         " 7) Press COMMAND + SHIFT + ` to cycle through frames in reverse order\n" +
  58         " 8) Press FAIL if focus doesn't move to next frame in reverse order\n" +
  59         " 9) Press PASS otherwise";
  60 
  61     private static final String FAIL_MESSAGE = "Focus doesn't move to next frame";
  62 
  63     public void showJFrame(int frameNumber) {
  64 
  65         String title = "Frame " + frameNumber;
  66         frame[frameNumber] = new JFrame(title);
  67         frame[frameNumber].setSize(300, 200);
  68         frame[frameNumber].setLocation(50+(frameNumber*20), 50+(frameNumber*20));
  69         frame[frameNumber].setVisible(true);
  70     }
  71 
  72     private void createAndShowFrame() throws Exception {
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             public void run() {
  75                 frame = new JFrame[maxFrames];
  76                 for (int i = 0; i < maxFrames; i++) {
  77                     showJFrame(i);
  78                 }
  79             }
  80         });
  81     }
  82 
  83     public void createAndShowInstructionFrame() {
  84         Button passButton = new Button("Pass");
  85         passButton.setEnabled(true);
  86 
  87         Button failButton = new Button("Fail");
  88         failButton.setEnabled(true);
  89 
  90         TextArea instructions = new TextArea(12, 70);
  91         instructions.setText(TEST_INSTRUCTIONS);
  92 
  93         instructionFrame = new Frame("Test Instructions");
  94         instructionFrame.add(passButton);
  95         instructionFrame.add(failButton);
  96         instructionFrame.add(instructions);
  97         instructionFrame.setSize(200,200);
  98         instructionFrame.setLayout(new FlowLayout());
  99         instructionFrame.pack();
 100         instructionFrame.setVisible(true);
 101 
 102         passButton.addActionListener(ae -> {
 103             dispose();
 104             testContinueFlag = false;
 105         });
 106 
 107         failButton.addActionListener(ae -> {
 108             dispose();
 109             testContinueFlag = false;
 110             throw new RuntimeException(FAIL_MESSAGE);
 111         });
 112     }
 113 
 114     private static void dispose() {
 115         for (int i = 0; i < maxFrames; i++) {
 116             frame[i].dispose();
 117         }
 118         instructionFrame.dispose();
 119     }
 120 
 121     public static void main(String[] args)  throws Exception {
 122 
 123         CycleThroughFrameTest testObj = new CycleThroughFrameTest();
 124         testObj.createAndShowFrame();
 125         testObj.createAndShowInstructionFrame();
 126 
 127         final int sleepTime = 300000;
 128         final int sleepLoopTime = 1000;
 129         int remainingSleepTime = sleepTime;
 130         while(remainingSleepTime > 0 && testContinueFlag) {
 131             Thread.sleep(sleepLoopTime);
 132             remainingSleepTime -= sleepLoopTime;
 133         }
 134 
 135         if (testContinueFlag) {
 136             dispose();
 137             throw new RuntimeException("Timed out after " +
 138                     (sleepTime - remainingSleepTime) / 1000 + " seconds");
 139         }
 140     }
 141 }
 142