Я хочу сделать утверждение, что компонент рендеринга моей таблицы имеет ожидаемую всплывающую подсказку.class GuiCheckBoxCellRendererTest extends SwingTest {
JFrame frame;
@AfterEach
void tearDown() {
runAndWait(frame::dispose);
}
@Test
void getTableCellRendererComponent_givenValue_returnsExpectedCheckBox() {
String tooltip = "Some tooltip";
CheckBoxCellRenderer renderer = callAndWait(() -> CheckBoxCellRenderer.from(tooltip));
DefaultTableModel tableModel = callAndWait(() -> new DefaultTableModel(new Object[][]{{1, "Alex"}}, new String[]{"Status", "Name"}));
table = callAndWait(() -> new JTable(tableModel));
runAndWait(() -> table.setDefaultRenderer(Object.class, renderer));
frame = createFrame(table);
robot.showWindow(frame);
robot.focusAndWaitForFocusGain(frame);
assumeThat(callAndWait(frame::isVisible)).isTrue();
assumeThat(callAndWait(frame::isShowing)).isTrue();
assumeThat(callAndWait(table::isVisible)).isTrue();
assumeThat(callAndWait(table::isShowing)).isTrue();
JTableFixture tableFixture = new JTableFixture(robot, table);
tableFixture.cell(TableCell.row(0).column(0)).select();
robot.waitForIdle();
tableFixture.requireToolTip(tooltip); // requires tooltip for the entire table, apparently
}
JFrame createFrame(JComponent component) {
return callAndWait(() -> {
JFrame frame = new JFrame();
JScrollPane scrollPane = new JScrollPane(component);
scrollPane.setPreferredSize(new Dimension(200, 150));
frame.add(scrollPane);
frame.pack();
return frame;
});
}
}
< /code>
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Color;
import java.awt.Component;
import java.util.function.Function;
public class CheckBoxCellRenderer extends DefaultTableCellRenderer {
private final JCheckBox checkBox;
private final Function tooltipFunction;
private CheckBoxCellRenderer(Function tooltipFunction) {
this.checkBox = new JCheckBox();
this.tooltipFunction = tooltipFunction;
this.checkBox.setToolTipText(tooltipFunction.apply(checkBox.isSelected()));
}
public static CheckBoxCellRenderer from(String tooltip) {
return from(selected -> tooltip);
}
public static CheckBoxCellRenderer from(Function tooltipFunction) {
return new CheckBoxCellRenderer(tooltipFunction);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
checkBox.setSelected(value instanceof Number && ((Number) value),intValue() == 1);
checkBox.setToolTipText(tooltipFunction.apply(checkBox.isSelected()));
return checkBox;
}
}
< /code>
import org.assertj.swing.core.Robot;
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.edt.GuiTask;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import javax.swing.SwingUtilities;
public abstract class SwingTest {
protected Robot robot;
@BeforeAll
static void superSetUpOnce() {
FailOnThreadViolationRepaintManager.install();
}
@AfterAll
static void superTearDownOnce() {
FailOnThreadViolationRepaintManager.uninstall();
}
@BeforeEach
void superSetUp() {
robot = BasicRobot.robotWithCurrentAwtHierarchy();
}
@AfterEach
void superTearDown() {
robot.cleanUp();
}
protected static void runLater(Runnable edtRunnable) {
SwingUtilities.invokeLater(() -> {
try {
edtRunnable.run();
} catch (Throwable e) {
throw new RuntimeException(e);
}
});
}
protected static void runAndWait(Runnable edtRunnable) {
GuiActionRunner.execute(new GuiTask() {
@Override
protected void executeInEDT() throws Throwable {
edtRunnable.run();
}
});
}
protected static T callAndWait(Callable edtCallable) {
return GuiActionRunner.execute(new GuiQuery() {
@Override
protected T executeInEDT() throws Throwable {
return edtCallable.call();
}
});
}
}
< /code>
java.lang.AssertionError: [javax.swing.JTable[name=null, rowCount=1, columnCount=1, enabled=true, visible=true, showing=true] - property:'toolTipText']
Expecting:
null
to match pattern:
"Some tooltip"
Подробнее здесь: https://stackoverflow.com/questions/797 ... l-tooltips