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