1 /*
   2  * Copyright (c) 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 /*
  25  @test
  26  @bug 8041928
  27  @summary Confirm that the Alt-Gr Modifier bit is set correctly.
  28  @run main/manual AltGraphModifierTest
  29  */
  30 import java.awt.Button;
  31 import java.awt.Dialog;
  32 import java.awt.Frame;
  33 import java.awt.Panel;
  34 import java.awt.TextArea;
  35 import java.awt.event.ActionEvent;
  36 import java.awt.event.ActionListener;
  37 import java.awt.event.InputEvent;
  38 import java.awt.event.MouseAdapter;
  39 import java.awt.event.MouseEvent;
  40 
  41 public class AltGraphModifierTest {
  42     private static void init() throws Exception {
  43         String[] instructions
  44                 = {
  45                     "This test is for verifying Alt-Gr modifier of an event.",
  46                     "Windows :-",
  47                     "1. Please check if Alt-Gr key is present on keyboard.",
  48                     "2. If present, press the Alt-Gr key and perform",
  49                     "   mouse click on the TestWindow.",
  50                     "3. If Alt-Gr key is not present, press Ctrl+Alt keys &",
  51                     "   perform mouse click on the TestWindow.",
  52                     "4. Test will exit by itself with appropriate result.",
  53                     "",
  54                     "Linux :-",
  55                     "1. Please check if Alt-Gr key is present on keyboard.",
  56                     "2. If present, press the Alt-Gr key and perform",
  57                     "   mouse click on the TestWindow.",
  58                     "3. Navigate to System Settings-> Keyboard-> Shortcuts->",
  59                     "   Typing.",
  60                     "4. Select an option for the Alternative Characters Key",
  61                     "   For example. Right Alt",
  62                     "5. Close the settings and navigate to test",
  63                     "6. Press Right Alt Key & perform mouse click on the",
  64                     "   TestWindow",
  65                     "7. Test will exit by itself with appropriate result.",
  66                     "",
  67                     "Mac :-",
  68                     "   Mac fix is under progress, & will be fixed soon.",
  69                     "   Please click Fail"
  70                 };
  71 
  72         Sysout.createDialog();
  73         Sysout.printInstructions(instructions);
  74     }
  75 
  76     static Frame mainFrame;
  77     public static void initTestWindow() {
  78         mainFrame = new Frame();
  79         mainFrame.setTitle("TestWindow");
  80         mainFrame.setSize(300, 200);
  81         mainFrame.addMouseListener(new MouseAdapter() {
  82             @Override
  83             public void mousePressed(MouseEvent e) {
  84                 int ex = e.getModifiersEx();
  85                 if ((ex & InputEvent.ALT_GRAPH_DOWN_MASK) == 0) {
  86                     AltGraphModifierTest.fail("Alt-Gr Modifier bit is not set.");
  87                 } else {
  88                     AltGraphModifierTest.pass();
  89                 }
  90             }
  91         });
  92         mainFrame.setVisible(true);
  93     }
  94 
  95     public static void dispose() {
  96         Sysout.dispose();
  97         mainFrame.dispose();
  98     }
  99 
 100     /**
 101      * ***************************************************
 102      * Standard Test Machinery Section DO NOT modify anything in this section --
 103      * it's a standard chunk of code which has all of the synchronisation
 104      * necessary for the test harness. By keeping it the same in all tests, it
 105      * is easier to read and understand someone else's test, as well as insuring
 106      * that all tests behave correctly with the test harness. There is a section
 107      * following this for test-defined classes
 108      * ****************************************************
 109      */
 110     private static boolean theTestPassed = false;
 111     private static boolean testGeneratedInterrupt = false;
 112     private static String failureMessage = "";
 113     private static Thread mainThread = null;
 114     final private static int sleepTime = 300000;
 115 
 116     public static void main(String args[]) throws Exception {
 117         mainThread = Thread.currentThread();
 118         try {
 119             init();
 120             initTestWindow();
 121         } catch (Exception e) {
 122             e.printStackTrace();
 123         }
 124         try {
 125             mainThread.sleep(sleepTime);
 126         } catch (InterruptedException e) {
 127             dispose();
 128             if (testGeneratedInterrupt && !theTestPassed) {
 129                 throw new Exception(failureMessage);
 130             }
 131         }
 132         if (!testGeneratedInterrupt) {
 133             dispose();
 134             throw new RuntimeException("Timed out after " + sleepTime / 1000
 135                     + " seconds");
 136         }
 137     }
 138 
 139     public static synchronized void pass() {
 140         theTestPassed = true;
 141         testGeneratedInterrupt = true;
 142         mainThread.interrupt();
 143     }
 144 
 145     public static synchronized void fail(String whyFailed) {
 146         theTestPassed = false;
 147         testGeneratedInterrupt = true;
 148         failureMessage = whyFailed;
 149         mainThread.interrupt();
 150     }
 151 }
 152 
 153 // *********** End Standard Test Machinery Section **********
 154 /**
 155  * **************************************************
 156  * Standard Test Machinery DO NOT modify anything below -- it's a standard chunk
 157  * of code whose purpose is to make user interaction uniform, and thereby make
 158  * it simpler to read and understand someone else's test.
 159  * **************************************************
 160  */
 161 /**
 162  * This is part of the standard test machinery. It creates a dialog (with the
 163  * instructions), and is the interface for sending text messages to the user. To
 164  * print the instructions, send an array of strings to Sysout.createDialog
 165  * WithInstructions method. Put one line of instructions per array entry. To
 166  * display a message for the tester to see, simply call Sysout.println with the
 167  * string to be displayed. This mimics System.out.println but works within the
 168  * test harness as well as standalone.
 169  */
 170 class Sysout {
 171     private static TestDialog dialog;
 172     private static Frame frame;
 173 
 174     public static void createDialog() {
 175         frame = new Frame();
 176         dialog = new TestDialog(frame, "Instructions");
 177         String[] defInstr = {"Instructions will appear here. ", ""};
 178         dialog.printInstructions(defInstr);
 179         dialog.show();
 180         println("Any messages for the tester will display here.");
 181     }
 182 
 183     public static void printInstructions(String[] instructions) {
 184         dialog.printInstructions(instructions);
 185     }
 186 
 187     public static void println(String messageIn) {
 188         dialog.displayMessage(messageIn);
 189     }
 190 
 191     public static void dispose() {
 192         dialog.dispose();
 193         frame.dispose();
 194     }
 195 }
 196 
 197 /**
 198  * This is part of the standard test machinery. It provides a place for the test
 199  * instructions to be displayed, and a place for interactive messages to the
 200  * user to be displayed. To have the test instructions displayed, see Sysout. To
 201  * have a message to the user be displayed, see Sysout. Do not call anything in
 202  * this dialog directly.
 203  */
 204 class TestDialog extends Dialog implements ActionListener {
 205     TextArea instructionsText;
 206     TextArea messageText;
 207     int maxStringLength = 80;
 208     Panel buttonP;
 209     Button failB;
 210 
 211     // DO NOT call this directly, go through Sysout
 212     public TestDialog(Frame frame, String name) {
 213         super(frame, name);
 214         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 215         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
 216         add("North", instructionsText);
 217 
 218         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
 219         add("Center", messageText);
 220 
 221         buttonP = new Panel();
 222         failB = new Button("Fail");
 223         failB.setActionCommand("fail");
 224         failB.addActionListener(this);
 225         buttonP.add("Center", failB);
 226 
 227         add("South", buttonP);
 228         pack();
 229         setVisible(true);
 230     }
 231 
 232     // DO NOT call this directly, go through Sysout
 233     public void printInstructions(String[] instructions) {
 234         instructionsText.setText("");
 235         String printStr, remainingStr;
 236         for (int i = 0; i < instructions.length; i++) {
 237             remainingStr = instructions[i];
 238             while (remainingStr.length() > 0) {
 239                 if (remainingStr.length() >= maxStringLength) {
 240                     int posOfSpace = remainingStr.
 241                             lastIndexOf(' ', maxStringLength - 1);
 242 
 243                     if (posOfSpace <= 0) {
 244                         posOfSpace = maxStringLength - 1;
 245                     }
 246 
 247                     printStr = remainingStr.substring(0, posOfSpace + 1);
 248                     remainingStr = remainingStr.substring(posOfSpace + 1);
 249                 }
 250                 else {
 251                     printStr = remainingStr;
 252                     remainingStr = "";
 253                 }
 254                 instructionsText.append(printStr + "\n");
 255             }
 256         }
 257     }
 258 
 259     public void displayMessage(String messageIn) {
 260         messageText.append(messageIn + "\n");
 261     }
 262 
 263     public void actionPerformed(ActionEvent e) {
 264         if (e.getActionCommand() == "fail") {
 265             AltGraphModifierTest.fail("User Clicked Fail");
 266         }
 267     }
 268 }