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