提交 4b0fe58c 编写于 作者: A ant

6616792: five AWT focus regression tests should be fixed

Summary: Fixed/refactored the tests.
Reviewed-by: volk
上级 6ae611c9
...@@ -25,8 +25,10 @@ ...@@ -25,8 +25,10 @@
@test @test
@bug 6314575 @bug 6314575
@summary Tests that previosly focused owned window doesn't steal focus when an owner's component requests focus. @summary Tests that previosly focused owned window doesn't steal focus when an owner's component requests focus.
@author Anton Tarasov: area=awt-focus @author Anton.Tarasov: area=awt.focus
@run applet ActualFocusedWindowBlockingTest.html @library ../../regtesthelpers
@build Util
@run main ActualFocusedWindowBlockingTest
*/ */
import java.awt.*; import java.awt.*;
...@@ -35,9 +37,10 @@ import java.applet.Applet; ...@@ -35,9 +37,10 @@ import java.applet.Applet;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
import test.java.awt.regtesthelpers.Util;
public class ActualFocusedWindowBlockingTest extends Applet { public class ActualFocusedWindowBlockingTest extends Applet {
Robot robot; Robot robot = Util.createRobot();
Frame owner = new Frame("Owner Frame"); Frame owner = new Frame("Owner Frame");
Window win = new Window(owner); Window win = new Window(owner);
Frame frame = new Frame("Auxiliary Frame"); Frame frame = new Frame("Auxiliary Frame");
...@@ -52,28 +55,12 @@ public class ActualFocusedWindowBlockingTest extends Applet { ...@@ -52,28 +55,12 @@ public class ActualFocusedWindowBlockingTest extends Applet {
} }
public void init() { public void init() {
// Create instructions for the user here, as well as set up
// the environment -- set the layout manager, add buttons,
// etc.
this.setLayout (new BorderLayout ());
Sysout.createDialogWithInstructions(new String[]
{"Automatic test. Simply wait until it's done."});
if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
return;
}
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) { public void eventDispatched(AWTEvent e) {
Sysout.println("--> " + e); System.out.println("--> " + e);
} }
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK); }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException("Error: unable to create robot", e);
}
owner.add(fButton); owner.add(fButton);
win.add(wButton); win.add(wButton);
frame.add(aButton); frame.add(aButton);
...@@ -87,19 +74,18 @@ public class ActualFocusedWindowBlockingTest extends Applet { ...@@ -87,19 +74,18 @@ public class ActualFocusedWindowBlockingTest extends Applet {
public void start() { public void start() {
if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) { if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
Sysout.println("No testing on Motif. Test passed."); System.out.println("No testing on Motif. Test passed.");
return; return;
} }
Sysout.println("\nTest started:\n"); System.out.println("\nTest started:\n");
// Test 1. // Test 1.
clickOnCheckFocus(wButton); clickOnCheckFocus(wButton);
clickOnCheckFocus(aButton); clickOnCheckFocus(aButton);
clickOn(fButton); Util.clickOnComp(fButton, robot);
if (!testFocused(fButton)) { if (!testFocused(fButton)) {
throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by click"); throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by click");
} }
...@@ -107,11 +93,10 @@ public class ActualFocusedWindowBlockingTest extends Applet { ...@@ -107,11 +93,10 @@ public class ActualFocusedWindowBlockingTest extends Applet {
// Test 2. // Test 2.
clickOnCheckFocus(wButton); clickOnCheckFocus(wButton);
clickOnCheckFocus(aButton); clickOnCheckFocus(aButton);
fButton.requestFocus(); fButton.requestFocus();
realSync(); Util.waitForIdle(robot);
if (!testFocused(fButton)) { if (!testFocused(fButton)) {
throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by request"); throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by request");
} }
...@@ -119,19 +104,16 @@ public class ActualFocusedWindowBlockingTest extends Applet { ...@@ -119,19 +104,16 @@ public class ActualFocusedWindowBlockingTest extends Applet {
// Test 3. // Test 3.
clickOnCheckFocus(wButton); clickOnCheckFocus(wButton);
clickOnCheckFocus(aButton); clickOnCheckFocus(aButton);
clickOnCheckFocus(fButton); clickOnCheckFocus(fButton);
clickOnCheckFocus(aButton); clickOnCheckFocus(aButton);
clickOn(owner); Util.clickOnTitle(owner, robot);
if (!testFocused(fButton)) { if (!testFocused(fButton)) {
throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused as the most recent focus owner"); throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused as the most recent focus owner");
} }
Sysout.println("Test passed."); System.out.println("Test passed.");
} }
void tuneAndShowWindows(Window[] arr) { void tuneAndShowWindows(Window[] arr) {
...@@ -142,33 +124,18 @@ public class ActualFocusedWindowBlockingTest extends Applet { ...@@ -142,33 +124,18 @@ public class ActualFocusedWindowBlockingTest extends Applet {
w.setBackground(Color.blue); w.setBackground(Color.blue);
w.setVisible(true); w.setVisible(true);
y += 200; y += 200;
realSync(); Util.waitForIdle(robot);
} }
} }
void clickOn(Component c) { void clickOnCheckFocus(Component c) {
Sysout.println("Test: clicking " + c);
Point p = c.getLocationOnScreen();
Dimension d = c.getSize();
if (c instanceof Frame) { if (c instanceof Frame) {
robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2); Util.clickOnTitle((Frame)c, robot);
Sysout.println((p.x + (int)(d.getWidth()/2)) + " " + (p.y + ((Frame)c).getInsets().top/2));
} else { } else {
robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2)); Util.clickOnComp(c, robot);
} }
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
realSync();
}
void clickOnCheckFocus(Component c) {
clickOn(c);
if (!testFocused(c)) { if (!testFocused(c)) {
throw new RuntimeException("Error: [" + c + "] couldn't get focus by click."); throw new TestErrorException(c + "couldn't get focus by click.");
} }
} }
...@@ -177,157 +144,22 @@ public class ActualFocusedWindowBlockingTest extends Applet { ...@@ -177,157 +144,22 @@ public class ActualFocusedWindowBlockingTest extends Applet {
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) { if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) {
return true; return true;
} }
realSync(); Util.waitForIdle(robot);
} }
return false; return false;
} }
void realSync() { // Thrown when the behavior being verified is found wrong.
((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
}
class TestFailedException extends RuntimeException { class TestFailedException extends RuntimeException {
public TestFailedException(String cause) { TestFailedException(String msg) {
super("Test failed. " + cause); super("Test failed: " + msg);
Sysout.println(cause);
} }
} }
}
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setLocation(500,0);
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
setVisible(true);
}// TestDialog()
//DO NOT call this directly, go through Sysout // Thrown when an error not related to the behavior being verified is encountered.
public void printInstructions( String[] instructions ) class TestErrorException extends RuntimeException {
{ TestErrorException(String msg) {
//Clear out any current instructions super("Unexpected error: " + msg);
instructionsText.setText( "" ); }
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
System.out.println(messageIn);
} }
}
}// TestDialog class
...@@ -23,16 +23,19 @@ ...@@ -23,16 +23,19 @@
/* /*
@test @test
@bug 4823903 @bug 4823903
@summary Tests actual focused window retaining. @summary Tests actual focused window retaining.
@author Anton Tarasov: area=awt.focus @author Anton.Tarasov: area=awt.focus
@run applet ActualFocusedWindowRetaining.html @library ../../regtesthelpers
@build Util
@run main ActualFocusedWindowRetaining
*/ */
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.applet.*; import java.applet.*;
import test.java.awt.regtesthelpers.Util;
public class ActualFocusedWindowRetaining extends Applet { public class ActualFocusedWindowRetaining extends Applet {
public static Frame frame = new Frame("Other Frame"); public static Frame frame = new Frame("Other Frame");
...@@ -46,7 +49,7 @@ public class ActualFocusedWindowRetaining extends Applet { ...@@ -46,7 +49,7 @@ public class ActualFocusedWindowRetaining extends Applet {
public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200); public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200);
public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300); public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300);
public static int step; public static int step;
public static Robot robot; public static Robot robot = Util.createRobot();
public static void main(String[] args) { public static void main(String[] args) {
ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining(); ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining();
...@@ -54,53 +57,25 @@ public class ActualFocusedWindowRetaining extends Applet { ...@@ -54,53 +57,25 @@ public class ActualFocusedWindowRetaining extends Applet {
a.start(); a.start();
} }
public void init() public void start () {
{
//Create instructions for the user here, as well as set up
// the environment -- set the layout manager, add buttons,
// etc.
this.setLayout (new BorderLayout ());
String[] instructions =
{
"This is an AUTOMATIC test",
"simply wait until it is done"
};
Sysout.createDialogWithInstructions( instructions );
}
public void start ()
{
if (Toolkit.getDefaultToolkit().getClass()
.getName().equals("sun.awt.motif.MToolkit")) {
Sysout.println("No testing on Motif.");
return;
}
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException("Error: unable to create robot", e);
}
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) { public void eventDispatched(AWTEvent e) {
Object src = e.getSource(); Object src = e.getSource();
Class cls = src.getClass(); Class cls = src.getClass();
if (cls == TestWindow.class) { if (cls == TestWindow.class) {
Sysout.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">"); System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">");
} else if (cls == Frame.class) { } else if (cls == Frame.class) {
Sysout.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">"); System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">");
} else if (cls == Button.class) { } else if (cls == Button.class) {
Sysout.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">"); System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">");
} else { } else {
Sysout.println(e.paramString() + " on <Non-testing component>"); System.out.println(e.paramString() + " on <Non-testing component>");
} }
} }
}, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK); }, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
setSize (200,200); setSize (500, 200);
setVisible(true); setVisible(true);
validate(); validate();
...@@ -117,15 +92,15 @@ public class ActualFocusedWindowRetaining extends Applet { ...@@ -117,15 +92,15 @@ public class ActualFocusedWindowRetaining extends Applet {
owner.setSize(new Dimension(400, 100)); owner.setSize(new Dimension(400, 100));
owner.setVisible(true); owner.setVisible(true);
owner.toFront(); owner.toFront();
waitTillShown(owner); Util.waitTillShown(owner);
window1.setVisible(true); window1.setVisible(true);
window2.setVisible(true); window2.setVisible(true);
window1.toFront(); window1.toFront();
window2.toFront(); window2.toFront();
// Wait longer... // Wait longer...
waitTillShown(window1); Util.waitTillShown(window1);
waitTillShown(window2); Util.waitTillShown(window2);
test(); test();
...@@ -134,85 +109,39 @@ public class ActualFocusedWindowRetaining extends Applet { ...@@ -134,85 +109,39 @@ public class ActualFocusedWindowRetaining extends Applet {
} }
public void test() { public void test() {
Button[] butArr = new Button[] {testButton3, testButton2, testButton1}; Button[] butArr = new Button[] {testButton3, testButton2, testButton1};
Window[] winArr = new Window[] {window2, window1, owner}; Window[] winArr = new Window[] {window2, window1, owner};
step = 1; step = 1;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
clickOnCheckFocusOwner(butArr[i]); clickInSeriesCheckFocus(null, butArr[i], frame);
clickOnCheckFocusedWindow(frame); clickOwnerCheckFocus(winArr[i], butArr[i]);
clickOn(owner);
if (!checkFocusedWindow(winArr[i])) {
stopTest("Test failed: actual focused window didn't get a focus");
}
if (!checkFocusOwner(butArr[i])) {
stopTest("Test failed: actual focus owner didn't get a focus");
}
step++; step++;
} }
step = 4; step = 4;
clickOnCheckFocusOwner(testButton3); clickInSeriesCheckFocus(testButton3, testButton1, frame);
clickOnCheckFocusOwner(testButton1); clickOwnerCheckFocus(owner, testButton1);
clickOnCheckFocusedWindow(frame);
clickOn(owner);
if (!checkFocusedWindow(owner)) {
stopTest("Test failed: actual focused window didn't get a focus");
}
if (!checkFocusOwner(testButton1)) {
stopTest("Test failed: actual focus owner didn't get a focus");
}
step = 5; step = 5;
clickOnCheckFocusOwner(testButton3); clickInSeriesCheckFocus(testButton3, testButton2, frame);
clickOnCheckFocusOwner(testButton2); clickOwnerCheckFocus(window1, testButton2);
clickOnCheckFocusedWindow(frame);
clickOn(owner);
if (!checkFocusedWindow(window1)) {
stopTest("Test failed: actual focused window didn't get a focus");
}
if (!checkFocusOwner(testButton2)) {
stopTest("Test failed: actual focus owner didn't get a focus");
}
step = 6; step = 6;
clickOnCheckFocusOwner(testButton1); clickInSeriesCheckFocus(testButton1, testButton2, frame);
clickOnCheckFocusOwner(testButton2); clickOwnerCheckFocus(window1, testButton2);
clickOnCheckFocusedWindow(frame);
clickOn(owner);
if (!checkFocusedWindow(window1)) {
stopTest("Test failed: actual focused window didn't get a focus");
}
if (!checkFocusOwner(testButton2)) {
stopTest("Test failed: actual focus owner didn't get a focus");
}
step = 7; step = 7;
clickOnCheckFocusOwner(testButton1); clickInSeriesCheckFocus(testButton1, testButton2, frame);
clickOnCheckFocusOwner(testButton2);
clickOnCheckFocusedWindow(frame);
window1.setVisible(false); window1.setVisible(false);
clickOn(owner); Util.waitForIdle(robot);
if (!checkFocusedWindow(owner)) { clickOwnerCheckFocus(owner, testButton1);
stopTest("Test failed: actual focused window didn't get a focus");
}
if (!checkFocusOwner(testButton1)) {
stopTest("Test failed: actual focus owner didn't get a focus");
}
step = 8; step = 8;
window1.setVisible(true); window1.setVisible(true);
waitTillShown(window1); Util.waitTillShown(window1);
clickOnCheckFocusOwner(testButton2); clickInSeriesCheckFocus(null, testButton2, frame);
clickOnCheckFocusedWindow(frame); clickOwnerCheckFocus(window1, testButton2);
clickOn(owner);
if (!checkFocusedWindow(window1)) {
stopTest("Test failed: actual focused window didn't get a focus");
}
if (!checkFocusOwner(testButton2)) {
stopTest("Test failed: actual focus owner didn't get a focus");
}
} }
boolean checkFocusOwner(Component comp) { boolean checkFocusOwner(Component comp) {
...@@ -223,53 +152,43 @@ public class ActualFocusedWindowRetaining extends Applet { ...@@ -223,53 +152,43 @@ public class ActualFocusedWindowRetaining extends Applet {
return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()); return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow());
} }
void waitTillShown(Component c) { void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) {
((sun.awt.SunToolkit) Toolkit.getDefaultToolkit()).realSync(); Util.clickOnTitle(owner, robot);
} robot.delay(500);
void clickOnCheckFocusOwner(Component c) { if (!checkFocusedWindow(focusedWindow)) {
clickOn(c); stopTest("Test failed: actual focused window didn't get a focus");
if (!checkFocusOwner(c)) { }
stopTest("Error: can't bring a focus on Component by clicking on it"); if (!checkFocusOwner(focusedComp)) {
stopTest("Test failed: actual focus owner didn't get a focus");
} }
} }
void clickOnCheckFocusedWindow(Frame f) { void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) {
clickOn(f); if (comp1 != null) {
if (!checkFocusedWindow(f)) { clickOnCheckFocusOwner(comp1);
stopTest("Error: can't bring a focus on Frame by clicking on it"); }
if (comp2 != null) {
clickOnCheckFocusOwner(comp2);
} }
clickOnCheckFocusedWindow(frame);
} }
void clickOn(Component c) void clickOnCheckFocusOwner(Component c) {
{ Util.clickOnComp(c, robot);
Point p = c.getLocationOnScreen(); robot.delay(500);
Dimension d = c.getSize();
if (c instanceof Frame) { if (!checkFocusOwner(c)) {
robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2); stopTest("Error: can't bring a focus on Component by clicking on it");
} else {
robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
} }
pause(100);
robot.mousePress(InputEvent.BUTTON1_MASK);
pause(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
waitForIdle();
} }
void waitForIdle() { void clickOnCheckFocusedWindow(Frame f) {
((sun.awt.SunToolkit) Toolkit.getDefaultToolkit()).realSync(); Util.clickOnTitle(f, robot);
} robot.delay(500);
void pause(int msec) { if (!checkFocusedWindow(f)) {
try { stopTest("Error: can't bring a focus on Frame by clicking on it");
Thread.sleep(msec);
} catch (InterruptedException e) {
Sysout.println("pause: non-fatal exception caught:");
e.printStackTrace();
} }
} }
...@@ -290,141 +209,3 @@ class TestWindow extends Window { ...@@ -290,141 +209,3 @@ class TestWindow extends Window {
setBackground(Color.green); setBackground(Color.green);
} }
} }
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
setVisible(true);
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
System.out.println(messageIn);
}
}// TestDialog class
...@@ -22,238 +22,65 @@ ...@@ -22,238 +22,65 @@
*/ */
/* /*
test @test
@bug 4752312 @bug 4752312
@summary Tests that after moving non-focusable window it ungrabs mouse pointer @summary Tests that after moving non-focusable window it ungrabs mouse pointer
@author dom@sparc.spb.su: area=awt.focus @author Denis Mikhalkin: area=awt.focus
@run applet FrameJumpingToMouse.html @library ../../regtesthelpers
@build Util
@run main FrameJumpingToMouse
*/ */
// Note there is no @ in front of test above. This is so that the
// harness will not mistake this file as a test file. It should
// only see the html file as a test file. (the harness runs all
// valid test files, so it would run this test twice if this file
// were valid as well as the html file.)
// Also, note the area= after Your Name in the author tag. Here, you
// should put which functional area the test falls in. See the
// AWT-core home page -> test areas and/or -> AWT team for a list of
// areas.
// Note also the 'FrameJumpingToMouse.html' in the run tag. This should
// be changed to the name of the test.
/**
* FrameJumpingToMouse.java
*
* summary:
*/
import java.applet.Applet; import java.applet.Applet;
import java.awt.*; import java.awt.BorderLayout;
import java.awt.event.*; import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import javax.swing.JFrame; import javax.swing.JFrame;
import test.java.awt.regtesthelpers.Util;
//Automated tests should run as applet tests if possible because they
// get their environments cleaned up, including AWT threads, any
// test created threads, and any system resources used by the test
// such as file descriptors. (This is normally not a problem as
// main tests usually run in a separate VM, however on some platforms
// such as the Mac, separate VMs are not possible and non-applet
// tests will cause problems). Also, you don't have to worry about
// synchronisation stuff in Applet tests they way you do in main
// tests...
public class FrameJumpingToMouse extends Applet public class FrameJumpingToMouse extends Applet
{ {
//Declare things used in the test, like buttons and labels here
JFrame frame = new JFrame("Test jumping frame"); JFrame frame = new JFrame("Test jumping frame");
Robot robot = null; Robot robot = Util.createRobot();
public void init()
{
//Create instructions for the user here, as well as set up
// the environment -- set the layout manager, add buttons,
// etc.
this.setLayout (new BorderLayout ()); public static void main(String[] args) {
FrameJumpingToMouse test = new FrameJumpingToMouse();
test.init();
test.start();
}
public void init() {
frame.setFocusableWindowState(false); frame.setFocusableWindowState(false);
frame.setBounds(100, 100, 100, 100); frame.setBounds(100, 100, 100, 100);
}//End init() }
public void start ()
{
//Get things going. Request focus, set size, et cetera
setSize (200,200);
setVisible(true);
validate();
try {
robot = new Robot();
} catch (Exception e) {
throw new RuntimeException("Can't create robot");
}
public void start() {
frame.setVisible(true); frame.setVisible(true);
Util.waitTillShown(frame);
robot.delay(1000); Point loc = frame.getLocationOnScreen();
robot.mouseMove(loc.x + frame.getWidth() / 4, loc.y + frame.getInsets().top / 2);
Point frameLoc = frame.getLocationOnScreen(); robot.delay(50);
robot.mouseMove(frameLoc.x+frame.getWidth()/4, frameLoc.y+frame.getInsets().top/2);
robot.mousePress(InputEvent.BUTTON1_MASK); robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(frameLoc.x+100, frameLoc.y+50); robot.delay(50);
robot.mouseMove(loc.x + 100, loc.y + 50);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK);
Toolkit.getDefaultToolkit().sync(); Util.waitForIdle(robot);
robot.waitForIdle();
frameLoc = frame.getLocation();
robot.mouseMove(frameLoc.x+frame.getWidth()/2, frameLoc.y+frame.getHeight()/2); loc = frame.getLocation();
robot.mouseMove(loc.x + frame.getWidth() / 2, loc.y + frame.getHeight() / 2);
Util.waitForIdle(robot);
Toolkit.getDefaultToolkit().sync(); if (!(frame.getLocation().equals(loc))) {
robot.waitForIdle(); throw new RuntimeException("Test failed: frame is moving to mouse with grab!");
if (!(frame.getLocation().equals(frameLoc))) {
throw new RuntimeException("Frame is moving to mouse with grab");
} }
}// start() System.out.println("Test passed.");
}// class FrameJumpingToMouse
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
} }
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
show();
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
System.out.println(messageIn);
}
}// TestDialog class
...@@ -20,19 +20,20 @@ ...@@ -20,19 +20,20 @@
* CA 95054 USA or visit www.sun.com if you need additional information or * CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions. * have any questions.
*/ */
/* /*
@test @test
@bug 4452384 @bug 4452384
@summary Tests that non-focusable windows doesn't generate any focus events when accessed. @summary Tests that non-focusable windows doesn't generate any focus events when accessed.
@author dom: area=awt.focus @author Denis.Mikhalkin: area=awt.focus
@run main Test @run main NoEventsTest
*/ */
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.util.*; import java.util.*;
public class Test extends Frame { public class NoEventsTest extends Frame {
public static final int DEF_WIDTH = 400, public static final int DEF_WIDTH = 400,
DEF_HEIGHT = 300, DEF_HEIGHT = 300,
DEF_TOP = 1, DEF_TOP = 1,
...@@ -80,8 +81,8 @@ public class Test extends Frame { ...@@ -80,8 +81,8 @@ public class Test extends Frame {
windows = new Window[7]; windows = new Window[7];
windows[0] = new TestWindow(0, 0, false, main_frame); windows[0] = new TestWindow(0, 0, false, main_frame);
//windows[1] = new TestWindow(2, 1, true, main_frame); //windows[1] = new TestWindow(2, 1, true, main_frame);
windows[2] = new Test(1, 0, false, true); windows[2] = new NoEventsTest(1, 0, false, true);
windows[3] = new Test(2, 0, false, false); windows[3] = new NoEventsTest(2, 0, false, false);
//windows[4] = new Test(3, 0, true, true); //windows[4] = new Test(3, 0, true, true);
windows[5] = new TestDialog(0, 1, false, true, main_frame); windows[5] = new TestDialog(0, 1, false, true, main_frame);
windows[6] = new TestDialog(1, 1, false, false, main_frame); windows[6] = new TestDialog(1, 1, false, false, main_frame);
...@@ -174,12 +175,12 @@ public class Test extends Frame { ...@@ -174,12 +175,12 @@ public class Test extends Frame {
} }
} }
} }
public Test(int row, int col, boolean focusable, boolean resizable) { public NoEventsTest(int row, int col, boolean focusable, boolean resizable) {
super("Frame" + row + "" + col); super("Frame" + row + "" + col);
TestPanel panel = new TestPanel(row, col); TestPanel panel = new TestPanel(row, col);
if (Test.automatic) { if (NoEventsTest.automatic) {
row = Test.DEF_ROW; row = NoEventsTest.DEF_ROW;
col = Test.DEF_COL; col = NoEventsTest.DEF_COL;
} }
setName(getTitle()); setName(getTitle());
add("Center", panel); add("Center", panel);
...@@ -187,7 +188,7 @@ public class Test extends Frame { ...@@ -187,7 +188,7 @@ public class Test extends Frame {
", " + (resizable?"resizable":"non-resizable")); ", " + (resizable?"resizable":"non-resizable"));
l.setBackground(Color.green); l.setBackground(Color.green);
add("North", l); add("North", l);
setBounds(Test.DEF_LEFT + DEF_WIDTH*col, DEF_TOP + DEF_HEIGHT*row, DEF_WIDTH, DEF_HEIGHT); setBounds(NoEventsTest.DEF_LEFT + DEF_WIDTH*col, DEF_TOP + DEF_HEIGHT*row, DEF_WIDTH, DEF_HEIGHT);
if (!focusable) { if (!focusable) {
setFocusableWindowState(false); setFocusableWindowState(false);
} }
...@@ -202,9 +203,9 @@ class TestWindow extends Window { ...@@ -202,9 +203,9 @@ class TestWindow extends Window {
super(owner); super(owner);
setName("Window" + row + "" + col); setName("Window" + row + "" + col);
TestPanel panel = new TestPanel(row, col); TestPanel panel = new TestPanel(row, col);
if (Test.automatic) { if (NoEventsTest.automatic) {
row = Test.DEF_ROW; row = NoEventsTest.DEF_ROW;
col = Test.DEF_COL; col = NoEventsTest.DEF_COL;
} }
add("Center", panel); add("Center", panel);
...@@ -213,7 +214,7 @@ class TestWindow extends Window { ...@@ -213,7 +214,7 @@ class TestWindow extends Window {
l.setBackground(Color.green); l.setBackground(Color.green);
add("North", l); add("North", l);
setBounds(Test.DEF_LEFT + Test.DEF_WIDTH*col, Test.DEF_TOP + Test.DEF_HEIGHT*row, Test.DEF_WIDTH, Test.DEF_HEIGHT); setBounds(NoEventsTest.DEF_LEFT + NoEventsTest.DEF_WIDTH*col, NoEventsTest.DEF_TOP + NoEventsTest.DEF_HEIGHT*row, NoEventsTest.DEF_WIDTH, NoEventsTest.DEF_HEIGHT);
if (!focusable) { if (!focusable) {
setFocusableWindowState(false); setFocusableWindowState(false);
} }
...@@ -225,9 +226,9 @@ class TestDialog extends Dialog { ...@@ -225,9 +226,9 @@ class TestDialog extends Dialog {
super(owner); super(owner);
setName("Dialog" + row + "" + col); setName("Dialog" + row + "" + col);
TestPanel panel = new TestPanel(row, col); TestPanel panel = new TestPanel(row, col);
if (Test.automatic) { if (NoEventsTest.automatic) {
row = Test.DEF_ROW; row = NoEventsTest.DEF_ROW;
col = Test.DEF_COL; col = NoEventsTest.DEF_COL;
} }
add("Center", panel); add("Center", panel);
...@@ -236,7 +237,7 @@ class TestDialog extends Dialog { ...@@ -236,7 +237,7 @@ class TestDialog extends Dialog {
l.setBackground(Color.green); l.setBackground(Color.green);
add("North", l); add("North", l);
setBounds(Test.DEF_LEFT + Test.DEF_WIDTH*col, Test.DEF_TOP + Test.DEF_HEIGHT*row, Test.DEF_WIDTH, Test.DEF_HEIGHT); setBounds(NoEventsTest.DEF_LEFT + NoEventsTest.DEF_WIDTH*col, NoEventsTest.DEF_TOP + NoEventsTest.DEF_HEIGHT*row, NoEventsTest.DEF_WIDTH, NoEventsTest.DEF_HEIGHT);
if (!focusable) { if (!focusable) {
setFocusableWindowState(false); setFocusableWindowState(false);
} }
...@@ -415,3 +416,5 @@ class GlobalListener implements AWTEventListener { ...@@ -415,3 +416,5 @@ class GlobalListener implements AWTEventListener {
// } // }
} }
} }
...@@ -25,8 +25,10 @@ ...@@ -25,8 +25,10 @@
@test @test
@bug 6182359 @bug 6182359
@summary Tests that Window having non-focusable owner can't be a focus owner. @summary Tests that Window having non-focusable owner can't be a focus owner.
@author Anton Tarasov: area=awt.focus @author Anton.Tarasov: area=awt.focus
@run applet NonfocusableOwnerTest.html @library ../../regtesthelpers
@build Util
@run main NonfocusableOwnerTest
*/ */
import java.awt.*; import java.awt.*;
...@@ -34,319 +36,124 @@ import java.awt.event.*; ...@@ -34,319 +36,124 @@ import java.awt.event.*;
import java.applet.Applet; import java.applet.Applet;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.io.*; import java.io.*;
import test.java.awt.regtesthelpers.Util;
public class NonfocusableOwnerTest extends Applet { public class NonfocusableOwnerTest extends Applet {
Robot robot; Robot robot = Util.createRobot();
Frame frame; Frame frame;
Dialog dialog; Dialog dialog;
Window window1; Window window1;
Window window2; Window window2;
Button button = new Button("button"); Button button = new Button("button");
// PrintStream Sysout = System.out;
public static void main(String[] args) { public static void main(String[] args) {
NonfocusableOwnerTest test = new NonfocusableOwnerTest(); NonfocusableOwnerTest test = new NonfocusableOwnerTest();
test.init();
test.start(); test.start();
} }
public void init() {
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException("Error: unable to create robot", e);
}
// Create instructions for the user here, as well as set up
// the environment -- set the layout manager, add buttons,
// etc.
this.setLayout (new BorderLayout ());
}
public void start() { public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) { public void eventDispatched(AWTEvent e) {
Sysout.println(e.toString()); System.out.println(e.toString());
} }
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK); }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
frame = new Frame("Frame"); frame = new Frame("Frame");
frame.setName("Frame-owner"); frame.setName("Frame-owner");
frame.setBounds(100, 0, 100, 100);
dialog = new Dialog(frame, "Dialog"); dialog = new Dialog(frame, "Dialog");
dialog.setName("Dialog-owner"); dialog.setName("Dialog-owner");
dialog.setBounds(100, 0, 100, 100);
window1 = new Window(frame); window1 = new Window(frame);
window1.setName("1st child"); window1.setName("1st child");
window1.setBounds(100, 300, 100, 100);
window2 = new Window(window1); window2 = new Window(window1);
window2.setName("2nd child"); window2.setName("2nd child");
window2.setBounds(100, 500, 100, 100);
test1(frame, window1); test1(frame, window1);
test2(frame, window1, window2); test2(frame, window1, window2);
test3(frame, window1, window2); test3(frame, window1, window2);
window1 = new Window(dialog); window1 = new Window(dialog);
window1.setBounds(100, 300, 100, 100);
window1.setName("1st child"); window1.setName("1st child");
window2 = new Window(window1); window2 = new Window(window1);
window2.setName("2nd child"); window2.setName("2nd child");
window2.setBounds(100, 500, 100, 100);
test1(dialog, window1); test1(dialog, window1);
test2(dialog, window1, window2); test2(dialog, window1, window2);
test3(dialog, window1, window2); test3(dialog, window1, window2);
Sysout.println("Test passed."); System.out.println("Test passed.");
} }
void test1(Window owner, Window child) { void test1(Window owner, Window child) {
Sysout.println("* * * STAGE 1 * * *\nowner=" + owner); System.out.println("* * * STAGE 1 * * *\nWindow owner: " + owner);
owner.setFocusableWindowState(false); owner.setFocusableWindowState(false);
owner.setSize(100, 100);
owner.setVisible(true); owner.setVisible(true);
child.add(button); child.add(button);
child.setBounds(0, 300, 100, 100);
child.setVisible(true); child.setVisible(true);
waitTillShown(child); Util.waitTillShown(child);
clickOn(button); Util.clickOnComp(button, robot);
if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) { if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
throw new RuntimeException("Test Failed."); throw new RuntimeException("Test Failed.");
} }
owner.dispose();
child.dispose(); child.dispose();
owner.dispose();
} }
void test2(Window owner, Window child1, Window child2) { void test2(Window owner, Window child1, Window child2) {
Sysout.println("* * * STAGE 2 * * *\nowner=" + owner); System.out.println("* * * STAGE 2 * * *\nWindow nowner: " + owner);
owner.setFocusableWindowState(false); owner.setFocusableWindowState(false);
owner.setSize(100, 100);
owner.setVisible(true); owner.setVisible(true);
child1.setFocusableWindowState(true); child1.setFocusableWindowState(true);
child1.setBounds(0, 300, 100, 100);
child1.setVisible(true); child1.setVisible(true);
child2.add(button); child2.add(button);
child2.setBounds(0, 500, 100, 100);
child2.setVisible(true); child2.setVisible(true);
clickOn(button); Util.waitTillShown(child2);
Util.clickOnComp(button, robot);
if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) { if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
throw new RuntimeException("Test failed."); throw new RuntimeException("Test failed.");
} }
owner.dispose();
child1.dispose();
child2.dispose(); child2.dispose();
child1.dispose();
owner.dispose();
} }
void test3(Window owner, Window child1, Window child2) { void test3(Window owner, Window child1, Window child2) {
Sysout.println("* * * STAGE 3 * * *\nowner=" + owner); System.out.println("* * * STAGE 3 * * *\nWidow owner: " + owner);
owner.setFocusableWindowState(true); owner.setFocusableWindowState(true);
owner.setSize(100, 100);
owner.setVisible(true); owner.setVisible(true);
child1.setFocusableWindowState(false); child1.setFocusableWindowState(false);
child1.setBounds(0, 300, 100, 100);
child1.setVisible(true); child1.setVisible(true);
child2.setFocusableWindowState(true); child2.setFocusableWindowState(true);
child2.add(button); child2.add(button);
child2.setBounds(0, 500, 100, 100);
child2.setVisible(true); child2.setVisible(true);
clickOn(button); Util.waitTillShown(child2);
Util.clickOnComp(button, robot);
System.err.println("focus owner: " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()); System.err.println("focus owner: " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
if (button != KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) { if (button != KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
throw new RuntimeException("Test failed."); throw new RuntimeException("Test failed.");
} }
owner.dispose();
child1.dispose(); child1.dispose();
child2.dispose(); child2.dispose();
} owner.dispose();
void clickOn(Component c) {
Point p = c.getLocationOnScreen();
Dimension d = c.getSize();
Sysout.println("Clicking " + c);
if (c instanceof Frame) {
robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
} else {
robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
}
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
waitForIdle();
}
void waitTillShown(Component c) {
while (true) {
try {
Thread.sleep(100);
c.getLocationOnScreen();
break;
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (IllegalComponentStateException e) {}
}
}
void waitForIdle() {
try {
Toolkit.getDefaultToolkit().sync();
sun.awt.SunToolkit.flushPendingEvents();
EventQueue.invokeAndWait( new Runnable() {
public void run() {} // Dummy implementation
});
} catch(InterruptedException ie) {
Sysout.println("waitForIdle, non-fatal exception caught:");
ie.printStackTrace();
} catch(InvocationTargetException ite) {
Sysout.println("waitForIdle, non-fatal exception caught:");
ite.printStackTrace();
}
// wait longer...
robot.delay(200);
} }
} }
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void printInstructions( String[] instructions )
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
System.err.println(messageIn);
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
setVisible(true);
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
System.out.println(messageIn);
}
}// TestDialog class
...@@ -22,467 +22,118 @@ ...@@ -22,467 +22,118 @@
*/ */
/* /*
@test @test
@bug 6183877 6216005 6225560 @bug 6183877 6216005 6225560
@summary Tests that keyboard input doesn't freeze due to type-ahead problems @library ../../regtesthelpers
@author Denis Mikhalkin: area=awt.focus @build Util
@run main/timeout=300 TestFocusFreeze @summary Tests that keyboard input doesn't freeze due to type-ahead problems
@author Denis.Mikhalkin, Anton.Tarasov: area=awt.focus
@run main TestFocusFreeze
*/ */
// Note the area= after Your Name in the author tag. Here, you import java.awt.Component;
// should put which functional area the test falls in. See the import java.awt.DefaultKeyboardFocusManager;
// AWT-core home page -> test areas and/or -> AWT team for a list of import java.awt.KeyboardFocusManager;
// areas. import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import test.java.awt.regtesthelpers.Util;
public class TestFocusFreeze {
private static JFrame frame;
private static JDialog dialog;
private static JButton dlgButton;
private static JButton frameButton;
private static AtomicBoolean lock = new AtomicBoolean(false);
private static Robot robot = Util.createRobot();
public static void main(String[] args) {
boolean all_passed = true;
KeyboardFocusManager testKFM = new TestKFM(robot);
KeyboardFocusManager defKFM = KeyboardFocusManager.getCurrentKeyboardFocusManager();
for (int i = 0; i < 10; i++) {
/** test(testKFM, defKFM);
* TestFocusFreeze.java Util.waitForIdle(robot);
* System.out.println("Iter " + i + ": " + (lock.get() ? "passed." : "failed!"));
* summary: XXX A brief summary of what this tests if (!lock.get()) {
*/ all_passed = false;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.*;
import sun.awt.SunToolkit;
public class TestFocusFreeze
{
//*** test-writer defined static variables go here ***
private static void init()
{
//*** Create instructions for the user here ***
FocusTest.test();
}//End init()
/*****************************************************
* Standard Test Machinery Section
* DO NOT modify anything in this section -- it's a
* standard chunk of code which has all of the
* synchronisation necessary for the test harness.
* By keeping it the same in all tests, it is easier
* to read and understand someone else's test, as
* well as insuring that all tests behave correctly
* with the test harness.
* There is a section following this for test-
* classes
******************************************************/
private static boolean theTestPassed = false;
private static boolean testGeneratedInterrupt = false;
private static String failureMessage = "";
private static Thread mainThread = null;
private static int sleepTime = 300000;
// Not sure about what happens if multiple of this test are
// instantiated in the same VM. Being static (and using
// static vars), it aint gonna work. Not worrying about
// it for now.
public static void main( String args[] ) throws InterruptedException
{
mainThread = Thread.currentThread();
try
{
init();
}
catch( TestPassedException e )
{
//The test passed, so just return from main and harness will
// interepret this return as a pass
return;
}
//At this point, neither test pass nor test fail has been
// called -- either would have thrown an exception and ended the
// test, so we know we have multiple threads.
//Test involves other threads, so sleep and wait for them to
// called pass() or fail()
try
{
Thread.sleep( sleepTime );
//Timed out, so fail the test
throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
}
catch (InterruptedException e)
{
//The test harness may have interrupted the test. If so, rethrow the exception
// so that the harness gets it and deals with it.
if( ! testGeneratedInterrupt ) throw e;
//reset flag in case hit this code more than once for some reason (just safety)
testGeneratedInterrupt = false;
if ( theTestPassed == false )
{
throw new RuntimeException( failureMessage );
} }
} }
if (!all_passed) {
}//main throw new RuntimeException("Test failed: not all iterations passed!");
public static synchronized void setTimeoutTo( int seconds )
{
sleepTime = seconds * 1000;
}
public static synchronized void pass()
{
Sysout.println( "The test passed." );
Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
//first check if this is executing in main thread
if ( mainThread == Thread.currentThread() )
{
//Still in the main thread, so set the flag just for kicks,
// and throw a test passed exception which will be caught
// and end the test.
theTestPassed = true;
throw new TestPassedException();
}
theTestPassed = true;
testGeneratedInterrupt = true;
mainThread.interrupt();
}//pass()
public static synchronized void fail()
{
//test writer didn't specify why test failed, so give generic
fail( "it just plain failed! :-)" );
}
public static synchronized void fail( String whyFailed )
{
Sysout.println( "The test failed: " + whyFailed );
Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
//check if this called from main thread
if ( mainThread == Thread.currentThread() )
{
//If main thread, fail now 'cause not sleeping
throw new RuntimeException( whyFailed );
} }
theTestPassed = false; System.out.println("Test passed.");
testGeneratedInterrupt = true;
failureMessage = whyFailed;
mainThread.interrupt();
}//fail()
}// class TestFocusFreeze
//This exception is used to exit from any level of call nesting
// when it's determined that the test has passed, and immediately
// end the test.
class TestPassedException extends RuntimeException
{
}
//*********** End Standard Test Machinery Section **********
//************ Begin classes defined for the test ****************
// if want to make listeners, here is the recommended place for them, then instantiate
// them in init()
/* Example of a class which may be written as part of a test
class NewClass implements anInterface
{
static int newVar = 0;
public void eventDispatched(AWTEvent e)
{
//Counting events to see if we get enough
eventCount++;
if( eventCount == 20 )
{
//got enough events, so pass
TestFocusFreeze.pass();
}
else if( tries == 20 )
{
//tried too many times without getting enough events so fail
TestFocusFreeze.fail();
}
}// eventDispatched()
}// NewClass class
*/
//************** End classes defined for the test *******************
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
} }
public static void test(final KeyboardFocusManager testKFM, final KeyboardFocusManager defKFM) {
public static void printInstructions( String[] instructions ) frame = new JFrame("Frame");
{ dialog = new JDialog(frame, "Dialog", true);
dialog.printInstructions( instructions ); dlgButton = new JButton("Dialog_Button");
} frameButton = new JButton("Frame_Button");
lock.set(false);
public static void println( String messageIn )
{ dialog.add(dlgButton);
System.out.println(messageIn); dialog.setLocation(200, 0);
} dialog.pack();
frame.add(frameButton);
}// Sysout class frame.pack();
/** dlgButton.addActionListener(new ActionListener() {
This is part of the standard test machinery. It provides a place for the public void actionPerformed(ActionEvent e) {
test instructions to be displayed, and a place for interactive messages dialog.dispose();
to the user to be displayed. frame.dispose();
To have the test instructions displayed, see Sysout. synchronized (lock) {
To have a message to the user be displayed, see Sysout. lock.set(true);
Do not call anything in this dialog directly. lock.notifyAll();
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
setVisible(true);
}// TestDialog()
//DO NOT call this directly, go through Sysout
public void printInstructions( String[] instructions )
{
//Clear out any current instructions
instructionsText.setText( "" );
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
} }
}
});
instructionsText.append( printStr + "\n" ); frameButton.addActionListener(new ActionListener() {
}// while
}// for
}//printInstructions()
//DO NOT call this directly, go through Sysout
public void displayMessage( String messageIn )
{
messageText.append( messageIn + "\n" );
System.out.println(messageIn);
}
}// TestDialog class
class FocusTest extends JFrame implements ActionListener {
private JDialog pageDialog = null;
private AtomicBoolean passed = new AtomicBoolean();
private static volatile boolean all_passed = true;
private static volatile long pressTime;
private static Object testLock = new Object();
private static Object resultLock = new Object();
public FocusTest() {
final GraphicsConfiguration gc =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
pageDialog = new JDialog(this, "JDialog", true, gc);
Container c = pageDialog.getContentPane();
c.setLayout(new BorderLayout());
JButton btnApprove =new JButton("Exit Button");
btnApprove.addActionListener(this);
c.add("South", btnApprove);
pageDialog.pack();
JButton bb = new JButton("Action");
bb.addActionListener(new ActionListener() {
final JDialog pageDialog = FocusTest.this.pageDialog;
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
synchronized(testLock) { // Right before the dialog will be shown, there will be called
testLock.notify(); // enqueuKeyEvents() method. We are to catch it.
} KeyboardFocusManager.setCurrentKeyboardFocusManager(testKFM);
pageDialog.setVisible(true); dialog.setVisible(true);
KeyboardFocusManager.setCurrentKeyboardFocusManager(defKFM);
} }
}); });
add(bb); Runnable showAction = new Runnable() {
pack();
setVisible(true);
((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
bb.requestFocus();
((SunToolkit)Toolkit.getDefaultToolkit()).realSync();
if (!bb.isFocusOwner()) {
System.err.println("Couldn't make bb focused");
all_passed = false;
return;
}
new Thread() {
public void run() { public void run() {
try { frame.setVisible(true);
Robot r = new Robot();
synchronized (testLock) {
testLock.wait();
}
r.keyPress(KeyEvent.VK_SPACE);
r.delay(50);
r.keyRelease(KeyEvent.VK_SPACE);
synchronized(passed) {
passed.wait(1000);
}
pageDialog.dispose();
FocusTest.this.dispose();
if (!passed.get()) {
all_passed = false;
}
} catch (Exception e) {
e.printStackTrace();
}
synchronized (resultLock) {
resultLock.notifyAll();
}
} }
}.start(); };
if (!Util.trackFocusGained(frameButton, showAction, 2000, false)) {
try { System.out.println("Test error: wrong initial focus!");
Robot r = new Robot();
r.keyPress(KeyEvent.VK_SPACE);
r.delay(50);
r.keyRelease(KeyEvent.VK_SPACE);
} catch (Exception e) {
e.printStackTrace();
all_passed = false;
return; return;
} }
try { robot.keyPress(KeyEvent.VK_SPACE);
synchronized (resultLock) { robot.delay(50);
resultLock.wait(); robot.keyRelease(KeyEvent.VK_SPACE);
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae) { Util.waitForCondition(lock, 2000);
System.err.println("Action performed"); Util.waitForIdle(robot);
passed.set(true);
synchronized(passed) {
passed.notify();
}
} }
}
public static void test() { class TestKFM extends DefaultKeyboardFocusManager {
FocusTest test = null; Robot robot;
for (int i = 0; i < 10; i++) { public TestKFM(Robot robot) {
test = new FocusTest(); this.robot = robot;
try { }
Thread.sleep(500); protected synchronized void enqueueKeyEvents(long after, Component untilFocused) {
} catch (InterruptedException e) { super.enqueueKeyEvents(after, untilFocused);
break; robot.keyPress(KeyEvent.VK_SPACE);
} robot.delay(50);
} robot.keyRelease(KeyEvent.VK_SPACE);
if (!all_passed) {
TestFocusFreeze.fail("Not all passed");
} else {
TestFocusFreeze.pass();
}
} }
} }
...@@ -22,21 +22,16 @@ ...@@ -22,21 +22,16 @@
*/ */
/* /*
test @test
@bug 4782886 @bug 4782886
@summary FocusManager consumes wrong KEYTYPED-Events @summary FocusManager consumes wrong KEY_TYPED events
@author son: area=awt.focus @author Oleg.Sukhodolsky: area=awt.focus
@run applet WrongKeyTypedConsumedTest.html @library ../../regtesthelpers
@build Util
@run main WrongKeyTypedConsumedTest
*/ */
/**
* WrongKeyTypedConsumedTest.java
*
* summary: FocusManager consumes wrong KEYTYPED-Events
*/
import java.applet.Applet; import java.applet.Applet;
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.AWTKeyStroke; import java.awt.AWTKeyStroke;
import java.awt.BorderLayout; import java.awt.BorderLayout;
...@@ -58,29 +53,19 @@ import javax.swing.JCheckBox; ...@@ -58,29 +53,19 @@ import javax.swing.JCheckBox;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import test.java.awt.regtesthelpers.Util;
public class WrongKeyTypedConsumedTest extends Applet public class WrongKeyTypedConsumedTest extends Applet
{ {
//Declare things used in the test, like buttons and labels here Robot robot = Util.createRobot();
public void init()
{
//Create instructions for the user here, as well as set up
// the environment -- set the layout manager, add buttons,
// etc.
String[] instructions = public static void main(String[] args) {
{ WrongKeyTypedConsumedTest test = new WrongKeyTypedConsumedTest();
"This is an AUTOMATIC test", test.start();
"simply wait until it is done" }
};
Sysout.createDialog( );
Sysout.printInstructions( instructions );
}//End init()
public void start () public void start ()
{ {
//Get things going. Request focus, set size, et cetera
setSize (200,200); setSize (200,200);
setVisible(true); setVisible(true);
validate(); validate();
...@@ -100,194 +85,45 @@ public class WrongKeyTypedConsumedTest extends Applet ...@@ -100,194 +85,45 @@ public class WrongKeyTypedConsumedTest extends Applet
frame.pack(); frame.pack();
frame.setVisible(true); frame.setVisible(true);
Util.waitForIdle(robot);
try { if (!frame.isActive()) {
Robot robot = new Robot(); throw new RuntimeException("Test Fialed: frame isn't active");
}
// wait for activation
robot.delay(2000);
if (!frame.isActive()) {
Point loc = frame.getLocationOnScreen();
Dimension size = frame.getSize();
robot.mouseMove(loc.x + size.width/2,
loc.y + size.height/2);
frame.toFront();
robot.delay(1000);
if (!frame.isActive()) {
throw new RuntimeException("Test Fialed: frame isn't active");
}
}
// verify if checkbox has focus // verify if checkbox has focus
if (!checkbox.isFocusOwner()) {
checkbox.requestFocusInWindow();
Util.waitForIdle(robot);
if (!checkbox.isFocusOwner()) { if (!checkbox.isFocusOwner()) {
checkbox.requestFocusInWindow(); throw new RuntimeException("Test Failed: checkbox doesn't have focus");
robot.delay(1000);
if (!checkbox.isFocusOwner()) {
throw new RuntimeException("Test Failed: checkbox doesn't have focus");
}
}
// press VK_DOWN
robot.keyPress(KeyEvent.VK_DOWN);
robot.delay(250);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.delay(1000);
// verify if text area has focus
if (!textarea.isFocusOwner()) {
throw new RuntimeException("Test Failed: focus wasn't transfered to text area");
} }
// press '1'
robot.keyPress(KeyEvent.VK_1);
robot.delay(250);
robot.keyRelease(KeyEvent.VK_1);
robot.delay(1000);
// verify if KEY_TYPED arraived
if (!"1".equals(textarea.getText())) {
throw new RuntimeException("Test Failed: text area text is \"" + textarea.getText() + "\", not \"1\"");
}
} catch(AWTException e) {
e.printStackTrace();
throw new RuntimeException("Test failed because of some internal exception");
} }
Sysout.println("Test Passed");
}// start()
}// class WrongKeyTypedConsumedTest
/****************************************************
Standard Test Machinery
DO NOT modify anything below -- it's a standard
chunk of code whose purpose is to make user
interaction uniform, and thereby make it simpler
to read and understand someone else's test.
****************************************************/
/**
This is part of the standard test machinery.
It creates a dialog (with the instructions), and is the interface
for sending text messages to the user.
To print the instructions, send an array of strings to Sysout.createDialog
WithInstructions method. Put one line of instructions per array entry.
To display a message for the tester to see, simply call Sysout.println
with the string to be displayed.
This mimics System.out.println but works within the test harness as well
as standalone.
*/
class Sysout
{
private static TestDialog dialog;
public static void createDialogWithInstructions( String[] instructions )
{
dialog = new TestDialog( new Frame(), "Instructions" );
dialog.printInstructions( instructions );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
public static void createDialog( )
{
dialog = new TestDialog( new Frame(), "Instructions" );
String[] defInstr = { "Instructions will appear here. ", "" } ;
dialog.printInstructions( defInstr );
dialog.setVisible(true);
println( "Any messages for the tester will display here." );
}
// press VK_DOWN
robot.keyPress(KeyEvent.VK_DOWN);
robot.delay(50);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.delay(50);
public static void printInstructions( String[] instructions ) Util.waitForIdle(robot);
{
dialog.printInstructions( instructions );
}
public static void println( String messageIn )
{
dialog.displayMessage( messageIn );
}
}// Sysout class
/**
This is part of the standard test machinery. It provides a place for the
test instructions to be displayed, and a place for interactive messages
to the user to be displayed.
To have the test instructions displayed, see Sysout.
To have a message to the user be displayed, see Sysout.
Do not call anything in this dialog directly.
*/
class TestDialog extends Dialog
{
TextArea instructionsText;
TextArea messageText;
int maxStringLength = 80;
//DO NOT call this directly, go through Sysout
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
pack();
setVisible(true); // verify if text area has focus
}// TestDialog() if (!textarea.isFocusOwner()) {
throw new RuntimeException("Test Failed: focus wasn't transfered to text area");
//DO NOT call this directly, go through Sysout }
public void printInstructions( String[] instructions ) // press '1'
{ robot.keyPress(KeyEvent.VK_1);
//Clear out any current instructions robot.delay(50);
instructionsText.setText( "" ); robot.keyRelease(KeyEvent.VK_1);
robot.delay(50);
//Go down array of instruction strings
String printStr, remainingStr;
for( int i=0; i < instructions.length; i++ )
{
//chop up each into pieces maxSringLength long
remainingStr = instructions[ i ];
while( remainingStr.length() > 0 )
{
//if longer than max then chop off first max chars to print
if( remainingStr.length() >= maxStringLength )
{
//Try to chop on a word boundary
int posOfSpace = remainingStr.
lastIndexOf( ' ', maxStringLength - 1 );
if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
printStr = remainingStr.substring( 0, posOfSpace + 1 );
remainingStr = remainingStr.substring( posOfSpace + 1 );
}
//else just print
else
{
printStr = remainingStr;
remainingStr = "";
}
instructionsText.append( printStr + "\n" );
}// while
}// for
}//printInstructions() Util.waitForIdle(robot);
//DO NOT call this directly, go through Sysout // verify if KEY_TYPED arrived
public void displayMessage( String messageIn ) if (!"1".equals(textarea.getText())) {
{ throw new RuntimeException("Test Failed: text area text is \"" + textarea.getText() + "\", not \"1\"");
messageText.append( messageIn + "\n" ); }
System.out.println(messageIn); System.out.println("Test Passed");
} }
}
}// TestDialog class
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册