Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8333403: Write a test to check various components events are triggered properly #19521

Closed
wants to merge 11 commits into from
178 changes: 54 additions & 124 deletions test/jdk/java/awt/event/ComponentEvent/ComponentEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.awt.event.InputEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;

/*
* @test
Expand All @@ -53,17 +54,17 @@
public class ComponentEventTest {

private static Frame frame;
private static int DELAY = 500;
private static final int DELAY = 500;
private static Robot robot;
private volatile static Component[] components;
private volatile static boolean componentHidden = false;
private volatile static boolean componentShown = false;
private volatile static boolean componentMoved = false;
private volatile static boolean componentResized = false;
private static Component[] components;
private volatile static boolean componentHidden;
private volatile static boolean componentShown;
private volatile static boolean componentMoved;
private volatile static boolean componentResized;
private volatile static Point compAt;
private volatile static Dimension compSize;
private volatile static ArrayList<ComponentEvent> events =
new ArrayList<>();
private volatile static java.util.List<ComponentEvent> events =
Collections.synchronizedList(new ArrayList<ComponentEvent>());

private static final ComponentListener componentListener =
new ComponentListener() {
Expand Down Expand Up @@ -137,8 +138,9 @@ public static void main(String[] args) throws Exception {
robot.setAutoWaitForIdle(true);

EventQueue.invokeAndWait(ComponentEventTest::initializeGUI);
robot.delay(DELAY);
robot.waitForIdle();
robot.delay(DELAY);

doTest();

System.out.println("Test PASSED");
Expand All @@ -161,9 +163,10 @@ private static void doTest()
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

// Hide all components and check if the ComponentEvent is triggered
for (int i = 0; i < components.length; i++) {
doTestForComponent(i);
for (boolean state : new boolean[] { true, false }) {
doTest(i, state);
}
}

robot.delay(DELAY);
Expand All @@ -176,12 +179,7 @@ private static void doTest()
robot.delay(DELAY);
if (componentShown || componentHidden || componentMoved
|| componentResized) {
System.err.print("Events triggered are: ");
for (int j = 0; j < events.size();
System.err.print(events.get(j) + "; "), j++);
System.err.println();
throw new RuntimeException(
"FAIL: ComponentEvent triggered when frame is iconified");
printErrEvents("ComponentEvent triggered when frame is iconified");
}

resetValues();
Expand All @@ -191,49 +189,60 @@ private static void doTest()

robot.delay(DELAY);
if (componentShown || componentHidden) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that any other event is triggered? Or do you expect other events?

When you inconify the frame, you verify of all the event flags but you verify only two flags here.

Copy link
Contributor Author

@ravigupta30 ravigupta30 Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we expect events should not triggered.

When inconify i expect all events should not triggered , but when form Frame.ICONIFIED to Frame.NORMAL componentMoved and componentResized events are triggered , Events order on Windows and Mac are different.

Order of the events for Frame from Frame.ICONIFIED to Frame.NORMAL

On Windows

Component Shown
Frame is Visible with ExtendedState as ICONIFIED
Component Moved
Component Resized
Frame is Visible with ExtendedState as NORMAL

On MAC
Component Moved
Component Resized
Component Shown
Frame is Visible with ExtendedState as ICONIFIED
Frame is Visible with ExtendedState as NORMAL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when form Frame.ICONIFIED to Frame.NORMAL componentMoved and componentResized events are triggered

Then verify that these events are triggered:

-        if (componentShown || componentHidden) {
+        if (componentShown || componentHidden || !componentMoved
+            || !componentResized) {

Otherwise it looks as if you forgot to test something. You may have two if statements to print more precise error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We observed different behavior between MS Windows and other OS ,we natively received WM_SIZE/WM_MOVE events when set frame iconify to
deiconify , The AWT sends the events to components when it receives the events from the native system.

Now we are applying the OS specific check to test this scenario.

Please check the details at JDK-6754618

System.err.print("Events triggered are: ");
for (int j = 0; j < events.size();
System.err.print(events.get(j) + "; "), j++);
System.err.println();
throw new RuntimeException(
"FAIL: ComponentEvent triggered when frame is set to normal state");
printErrEvents("ComponentEvent triggered when frame set to normal");
}
}

private static void doTestForComponent(int i)
private static void printErrEvents(String errorMsg) {
System.err.print("Events triggered are: ");
for (int j = 0; j < events.size(); j++) {
System.err.print(events.get(j) + "; ");
}
System.err.println();
throw new RuntimeException("FAIL: " + errorMsg);
}

private static void doTest(int i, boolean enable)
throws InvocationTargetException, InterruptedException {

Component currentComponent = components[i];

EventQueue.invokeAndWait(() -> {
currentComponent.setEnabled(enable);
revalidateFrame();
});

robot.delay(DELAY);

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(false);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
if (!componentHidden) {
throw new RuntimeException(
"FAIL: ComponentHidden not triggered for "
+ currentComponent.getClass());
throw new RuntimeException("FAIL: ComponentHidden not triggered for"
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(false);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
if (componentHidden) {
throw new RuntimeException(
"FAIL: ComponentHidden triggered when setVisible(false) "
+ "called for a hidden " + components[i].getClass());
throw new RuntimeException("FAIL: ComponentHidden triggered when "
+ "setVisible(false) called for a hidden "
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(true);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
Expand All @@ -245,21 +254,21 @@ private static void doTestForComponent(int i)
resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(true);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
if (componentShown) {
throw new RuntimeException(
"FAIL: ComponentShown triggered when setVisible(true) "
+ "called for a shown " + components[i].getClass());
throw new RuntimeException("FAIL: ComponentShown triggered when "
+ "setVisible(true) called for a shown "
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setLocation(currentComponent.getLocation().x + 1,
currentComponent.getLocation().y);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
Expand All @@ -272,109 +281,30 @@ private static void doTestForComponent(int i)
EventQueue.invokeAndWait(() -> {
currentComponent.setSize(currentComponent.getSize().width + 1,
currentComponent.getSize().height);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
if (!componentResized) {
throw new RuntimeException(
"FAIL: ComponentResized not triggered for "
+ components[i].getClass());
}

// Disable the components and do the same set of operations

EventQueue.invokeAndWait(() -> {
currentComponent.setEnabled(false);
resetFrame();
});
robot.delay(DELAY);

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(false);
resetFrame();
});

robot.delay(DELAY);
if (!componentHidden) {
throw new RuntimeException(
"FAIL: ComponentHidden not triggered for disabled "
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(false);
resetFrame();
});

robot.delay(DELAY);
if (componentHidden) {
throw new RuntimeException(
"FAIL: ComponentHidden triggered when setVisible(false) "
+ "called for a hidden disabled "
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(true);
resetFrame();
});

robot.delay(DELAY);
if (!componentShown) {
throw new RuntimeException(
"FAIL: ComponentShown not triggered for disabled "
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setVisible(true);
resetFrame();
});

robot.delay(DELAY);
if (componentShown) {
throw new RuntimeException(
"FAIL: ComponentShown triggered when setVisible(true) "
+ "called for a shown disabled "
+ components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setLocation(currentComponent.getLocation().x - 1,
currentComponent.getLocation().y);
resetFrame();
});

robot.delay(DELAY);
if (!componentMoved) {
throw new RuntimeException(
"FAIL: ComponentMoved not triggered for disabled "
+ components[i].getClass());
throw new RuntimeException("FAIL: ComponentResized not triggered "
+ "when size increase for " + components[i].getClass());
}

resetValues();
EventQueue.invokeAndWait(() -> {
currentComponent.setSize(currentComponent.getSize().width - 1,
currentComponent.getSize().height);
resetFrame();
revalidateFrame();
});

robot.delay(DELAY);
if (!componentResized) {
throw new RuntimeException(
"FAIL: ComponentResized not triggered for disabled "
+ components[i].getClass());
throw new RuntimeException("FAIL: ComponentResized not triggered "
+ "when size decrease for " + components[i].getClass());
}

}

private static void resetFrame() {
private static void revalidateFrame() {
frame.invalidate();
frame.validate();
}
Expand Down