Я хочу утверждать, что компонент рендеринга ячеек моей таблицы имеет ожидаемую всплывающую подсказку.
Как мне это сделать с помощью AssertJ Swing?
Моя попытка:
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;
});
}
}
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;
}
}
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();
}
});
}
}
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
Утверждение всплывающих подсказок ячеек JTable ⇐ JAVA
Программисты JAVA общаются здесь
-
Anonymous
1760457992
Anonymous
Я хочу утверждать, что компонент рендеринга ячеек моей таблицы имеет ожидаемую всплывающую подсказку.
Как мне это сделать с помощью AssertJ Swing?
Моя попытка:
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;
});
}
}
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;
}
}
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();
}
});
}
}
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"
Подробнее здесь: [url]https://stackoverflow.com/questions/79760890/asserting-on-jtable-cell-tooltips[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия