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