Skip to content

Showing lean windows makes them tiny #278

@NadChel

Description

@NadChel

If a Window has little content, AssertJ makes the window even smaller.

Here's a failing test:

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<Dimension> captor = ArgumentCaptor.forClass(Dimension.class);
        then(frame).should(atLeastOnce()).setSize(captor.capture());
        List<Dimension> 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.

// org.assertj.swing.monitor.WindowStatus#makeLargeEnoughToReceiveEvents
  @RunsInCurrentThread
  private void makeLargeEnoughToReceiveEvents(@Nonnull Window window) {
    if (!shouldResize(window)) {
      return;
    }
    window.setSize(MINIMUM_WINDOW_SIZE);
  }

Java 8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions