assertj / assertj/assertj-swing
Showing lean windows makes them tiny
- Dominant language
- Java
- Stars
- 121
- Forks
- 52
- PR merge metrics
- No merged PRs in 30d
Description
If a `Window` has little content, AssertJ makes the window even smaller.
Here's a failing test:
```java
import org.assertj.swing.core.BasicRobot;
import org.assertj.swing.core.Robot;
import org.assertj.swing.edt.GuiActionRunner;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.util.List;
import static org.assertj.core.api.Assertions.within;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.spy;
class RobotTest {
private Robot robot;
@BeforeEach
void setUp() {
robot = BasicRobot.robotWithCurrentAwtHierarchy();
}
@AfterEach
void tearDown() {
robot.cleanUp();
}
@Test
void showWindow_ifPreferredWindowHeightExceedsTitleBarHeight_doesNotShrinkWindowToTitleBarHeight() {
JTextField field = new JTextField();
field.setColumns(10);
JFrame frame = spy(createFrame(field));
int titleBarInset = frame.getInsets().top;
assumeThat(titleBarInset).isCloseTo(30, within(1));
assumeThat(frame.getPreferredSize().height).isGreaterThan(titleBarInset);
robot.showWindow(frame);
ArgumentCaptor captor = ArgumentCaptor.forClass(Dimension.class);
then(frame).should(atLeastOnce()).setSize(captor.capture());
List setSizes = captor.getAllValues();
Dimension lastSetDimension = setSizes.get(setSizes.size() - 1);
assertThat(lastSetDimension.height).isGreaterThan(titleBarInset);
}
private JFrame createFrame(JComponent component) {
return GuiActionRunner.execute(() -> {
JFrame frame = new JFrame();
frame.add(component);
frame.pack(); // packed
return frame;
});
}
}
```
It's all because of the `shouldResize()` method (see `org.assertj.swing.monitor.WindowStatus#makeLargeEnoughToReceiveEvents`). If the window's preferred size (which doesn't include insets) minus ≈30 pixels of the title bar top inset is lower than 30 pixels, the window is guaranteed to be shrunk to `MINIMUM_WINDOW_SIZE` (50,30). You also get ugly jittering of the pointer for several seconds.
```java
// org.assertj.swing.monitor.WindowStatus#makeLargeEnoughToReceiveEvents
@RunsInCurrentThread
private void makeLargeEnoughToReceiveEvents(@Nonnull Window window) {
if (!shouldResize(window)) {
return;
}
window.setSize(MINIMUM_WINDOW_SIZE);
}
```
Java 8.
Contributor guide
Assessment
This issue has not been assessed yet.