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