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