Skip to content

Commit

Permalink
Add StudioOptionPane mocking
Browse files Browse the repository at this point in the history
After long debugging, it looks AssertJ (and Java robot) failed to send Mouse event into JOptionPane (I guess into modal dialog). Under Ubuntu virtual machine mouse event (move, press, release) either send or not send.
Potentially this thread is about the same:
assertj/assertj-swing#229
  • Loading branch information
dzmipt committed Jan 7, 2024
1 parent c5ff566 commit 3560d14
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
29 changes: 25 additions & 4 deletions src/studio/ui/StudioOptionPane.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package studio.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

import static javax.swing.JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;

Expand Down Expand Up @@ -67,6 +70,22 @@ public String toString() {
public static final int CANCEL_RESULT = 1;
public static final int IGNOREALL_RESULT = 2;

private static boolean mocked = false;
private static int mockedResult = JOptionPane.OK_OPTION;

private final static Logger log = LogManager.getLogger();

public static void setMocked(boolean isMocked) {
if (mocked == isMocked) return;
mocked = isMocked;
log.info("setMocked: {}", isMocked);
}

public static void setMockedResult(int result) {
setMocked(true);
mockedResult = result;
}

public static void showError(String message, String title) {
showError(null, message, title);
}
Expand Down Expand Up @@ -112,6 +131,8 @@ private static void findButtons(List<JButton> buttons, Container container) {
}

public static int showOptionDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Option[] options, Option initialValue) {
if (mocked) return mockedResult;

JOptionPane pane = new JOptionPane(message, messageType, JOptionPane.DEFAULT_OPTION, icon, options, initialValue);
ArrayList<JButton> buttons = new ArrayList<>();
findButtons(buttons, pane);
Expand Down
31 changes: 16 additions & 15 deletions test-integration/studio/ui/EditorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import org.assertj.swing.finder.WindowFinder;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JMenuItemFixture;
import org.assertj.swing.timing.Condition;
import org.assertj.swing.timing.Timeout;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -106,8 +105,14 @@ public void testUndoActionEnable() {
frameFixture.button("toolbarUndo").requireEnabled();
}

@AfterClass
public static void resetMockedOptionPane() {
StudioOptionPane.setMocked(false);
}

@Test
public void testCancelOnFrameClosure() {
StudioOptionPane.setMocked(true);
frameFixture.menuItem("New Window").click();
FrameFixture newFrameFixture = WindowFinder.findFrame(
new GenericTypeMatcher<StudioWindow>(StudioWindow.class, true) {
Expand All @@ -117,26 +122,22 @@ protected boolean isMatching(StudioWindow f) {
}
}).using(robot());
newFrameFixture.textBox("editor1").enterText("x");

StudioOptionPane.setMockedResult(JOptionPane.CANCEL_OPTION);
newFrameFixture.menuItem("Close Window").click();
optionPaneButtonClick("Cancel");
pause(50, TimeUnit.MILLISECONDS); // wait as closure happens asynchronously
newFrameFixture.requireVisible();

StudioOptionPane.setMockedResult(JOptionPane.CLOSED_OPTION);
newFrameFixture.menuItem("Close Window").click();
pause(50, TimeUnit.MILLISECONDS); // wait as closure happens asynchronously
newFrameFixture.requireVisible();


//tear down
log.info("Before close");
StudioOptionPane.setMockedResult(JOptionPane.NO_OPTION);
newFrameFixture.close();
log.info("After close");
pause(50, TimeUnit.MILLISECONDS); // wait as closure happens asynchronously
log.info("Before No click");
optionPaneButtonClick("No");
log.info("After No click");

pause(new Condition("Wait till new window is closed") {
@Override
public boolean test() {
return !execute(newFrameFixture.target()::isVisible);
}
}, Timeout.timeout(5, TimeUnit.SECONDS));
newFrameFixture.requireNotVisible();
}
}
13 changes: 0 additions & 13 deletions test-integration/studio/ui/StudioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import org.assertj.swing.annotation.GUITest;
import org.assertj.swing.core.EmergencyAbortListener;
import org.assertj.swing.core.MouseButton;
import org.assertj.swing.finder.JOptionPaneFinder;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.fixture.JOptionPaneFixture;
import org.assertj.swing.fixture.JTabbedPaneFixture;
import org.assertj.swing.fixture.JTextComponentFixture;
import org.assertj.swing.junit.runner.GUITestRunner;
Expand Down Expand Up @@ -237,15 +235,4 @@ protected void waitForQueryExecution(Runnable runQuery) {
waitForQueryExecution(runQuery,1);
}


protected void optionPaneButtonClick(FrameFixture frameFixture, String btnText) {
JOptionPaneFixture optionPane = JOptionPaneFinder.findOptionPane().using(robot());
optionPane.buttonWithText(btnText).click();

}

protected void optionPaneButtonClick(String btnText) {
optionPaneButtonClick(frameFixture, btnText);
}

}

0 comments on commit 3560d14

Please sign in to comment.