1 /*
   2  * Copyright (c) 2013, 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 import javax.swing.*;
  25 import javax.swing.JLabel;
  26 import javax.swing.JTextArea;
  27 import java.awt.*;
  28 import java.awt.FileDialog;
  29 import java.awt.Label;
  30 import java.awt.event.*;
  31 import java.awt.event.KeyAdapter;
  32 import java.awt.event.KeyEvent;
  33 import java.io.FileWriter;
  34 import java.lang.*;
  35 import java.lang.Override;
  36 import java.lang.String;
  37 import java.lang.System;
  38 import java.lang.Throwable;
  39 import java.util.Hashtable;
  40 
  41 /*
  42 @bug 8010009
  43 @summary [macosx] Unable type into online word games on MacOSX
  44 @author petr.pchelko : area=awt.keyboard
  45 @run clean *
  46 @run build TestApplet
  47 @run applet/manual=yesno KeyReleasedInAppletTest.html
  48 */
  49 
  50 public class KeyReleasedInAppletTest extends JApplet {
  51     private static final String TEST_HTML_NAME = "TestApplet.html";
  52 
  53     public void init() {
  54         //Create instructions for the user here, as well as set up
  55         // the environment -- set the layout manager, add buttons,
  56         // etc.
  57         this.setLayout(new BorderLayout());
  58 
  59         try {
  60             String testFilePath = System.getProperty("test.classes");
  61             FileWriter testHTML = null;
  62             try {
  63                 testHTML = new FileWriter(testFilePath + "/" + TEST_HTML_NAME);
  64                 testHTML.write("<html>\n" +
  65                         "<head>\n" +
  66                         "<title>KeyReleasedInAppletTest </title>\n" +
  67                         "</head>\n" +
  68                         "<body>\n" +
  69                         "<h1>KeyReleasedInAppletTest<br>Bug ID:8010009 </h1>\n" +
  70                         "<p>Make sure the applet is focuced and type any character on the keyboard. <br>"+
  71                         "The applet should show keyPressed, keyTyped and keyReleased messages.</p>\n" +
  72                         "<APPLET CODE=\"TestApplet.class\" WIDTH=400 HEIGHT=200></APPLET>\n" +
  73                         "</body>");
  74             } finally {
  75                 if (testHTML != null) {
  76                     testHTML.close();
  77                 }
  78             }
  79 
  80             String[] instructions =
  81                     {
  82                             "(1) Install the tested JDK to be used by the Java Plugin.\n",
  83                             "(2) Open Java Preferences and set security level to minimal.\n",
  84                             "(3) Open the " + TEST_HTML_NAME + " in Firefox in firefox web browser\n" +
  85                                     " It is located at: " + testFilePath,
  86                             "(5) Continue the test according to the instructions in the applet.\n",
  87                     };
  88             Sysout.createDialogWithInstructions(instructions);
  89         } catch (Throwable e) {
  90             //Fail the test.
  91             throw new RuntimeException(e.getMessage());
  92         }
  93 
  94     }//End  init()
  95 
  96     public void start() {
  97     }// start()
  98 }
  99 
 100 /* Place other classes related to the test after this line */
 101 
 102 /****************************************************
 103  Standard Test Machinery
 104  DO NOT modify anything below -- it's a standard
 105  chunk of code whose purpose is to make user
 106  interaction uniform, and thereby make it simpler
 107  to read and understand someone else's test.
 108  ****************************************************/
 109 
 110 /**
 111  * This is part of the standard test machinery.
 112  * It creates a dialog (with the instructions), and is the interface
 113  * for sending text messages to the user.
 114  * To print the instructions, send an array of strings to Sysout.createDialog
 115  * WithInstructions method.  Put one line of instructions per array entry.
 116  * To display a message for the tester to see, simply call Sysout.println
 117  * with the string to be displayed.
 118  * This mimics System.out.println but works within the test harness as well
 119  * as standalone.
 120  */
 121 
 122 class Sysout {
 123     private static TestDialog dialog;
 124     private static boolean numbering = false;
 125     private static int messageNumber = 0;
 126 
 127     public static void createDialogWithInstructions(String[] instructions) {
 128         dialog = new TestDialog(new Frame(), "Instructions");
 129         dialog.printInstructions(instructions);
 130         dialog.setVisible(true);
 131         println("Any messages for the tester will display here.");
 132     }
 133 
 134     public static void createDialog() {
 135         dialog = new TestDialog(new Frame(), "Instructions");
 136         String[] defInstr = {"Instructions will appear here. ", ""};
 137         dialog.printInstructions(defInstr);
 138         dialog.setVisible(true);
 139         println("Any messages for the tester will display here.");
 140     }
 141 
 142     /* Enables message counting for the tester. */
 143     public static void enableNumbering(boolean enable) {
 144         numbering = enable;
 145     }
 146 
 147     public static void printInstructions(String[] instructions) {
 148         dialog.printInstructions(instructions);
 149     }
 150 
 151 
 152     public static void println(String messageIn) {
 153         if (numbering) {
 154             messageIn = "" + messageNumber + " " + messageIn;
 155             messageNumber++;
 156         }
 157         dialog.displayMessage(messageIn);
 158     }
 159 
 160 }// Sysout  class
 161 
 162 /**
 163  * This is part of the standard test machinery.  It provides a place for the
 164  * test instructions to be displayed, and a place for interactive messages
 165  * to the user to be displayed.
 166  * To have the test instructions displayed, see Sysout.
 167  * To have a message to the user be displayed, see Sysout.
 168  * Do not call anything in this dialog directly.
 169  */
 170 class TestDialog extends Dialog {
 171 
 172     TextArea instructionsText;
 173     TextArea messageText;
 174     int maxStringLength = 80;
 175 
 176     //DO NOT call this directly, go through Sysout
 177     public TestDialog(Frame frame, String name) {
 178         super(frame, name);
 179         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 180         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
 181         add("North", instructionsText);
 182 
 183         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
 184         add("Center", messageText);
 185 
 186         pack();
 187 
 188         setVisible(true);
 189     }// TestDialog()
 190 
 191     //DO NOT call this directly, go through Sysout
 192     public void printInstructions(String[] instructions) {
 193         //Clear out any current instructions
 194         instructionsText.setText("");
 195 
 196         //Go down array of instruction strings
 197 
 198         String printStr, remainingStr;
 199         for (int i = 0; i < instructions.length; i++) {
 200             //chop up each into pieces maxSringLength long
 201             remainingStr = instructions[i];
 202             while (remainingStr.length() > 0) {
 203                 //if longer than max then chop off first max chars to print
 204                 if (remainingStr.length() >= maxStringLength) {
 205                     //Try to chop on a word boundary
 206                     int posOfSpace = remainingStr.
 207                             lastIndexOf(' ', maxStringLength - 1);
 208 
 209                     if (posOfSpace <= 0) posOfSpace = maxStringLength - 1;
 210 
 211                     printStr = remainingStr.substring(0, posOfSpace + 1);
 212                     remainingStr = remainingStr.substring(posOfSpace + 1);
 213                 }
 214                 //else just print
 215                 else {
 216                     printStr = remainingStr;
 217                     remainingStr = "";
 218                 }
 219 
 220                 instructionsText.append(printStr + "\n");
 221 
 222             }// while
 223 
 224         }// for
 225 
 226     }//printInstructions()
 227 
 228     //DO NOT call this directly, go through Sysout
 229     public void displayMessage(String messageIn) {
 230         messageText.append(messageIn + "\n");
 231         System.out.println(messageIn);
 232     }
 233 
 234 }// TestDialog  class