Код: Выделить всё
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;
});
}
}
Код: Выделить всё
// org.assertj.swing.monitor.WindowStatus
@RunsInCurrentThread
private void makeLargeEnoughToReceiveEvents(@Nonnull Window window) {
if (!shouldResize(window)) {
return;
}
window.setSize(MINIMUM_WINDOW_SIZE);
}
@RunsInCurrentThread
private boolean shouldResize(@Nonnull Window window) {
Insets insets = window.getInsets();
int w = window.getWidth() - (insets.left + insets.right);
if (w < MINIMUM_WINDOW_SIZE.width) {
return true;
}
int h = window.getHeight() - (insets.top + insets.bottom);
return h < MINIMUM_WINDOW_SIZE.height;
}
< /code>
Это вряд ли будет исправлено в ближайшее время (или когда -либо), поскольку в последнее время у Assertj Swing нет новых выпусков (или объединенных PRS, в этом отношении). Поэтому мне интересно, есть ли более элегантное решение, чем наполнение моей панели контента некоторыми компонентами наполнителя. Иногда, например, вам необходимо проверить обработку фокуса с помощью пользовательского JTextComponent Код: Выделить всё
// a workaround
private JFrame createFrame(JComponent component) {
return GuiActionRunner.execute(() -> {
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
contentPane.add(component);
frame.setContentPane(contentPane);
frame.pack(); // packed
return frame;
});
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... -them-tiny