Created
February 23, 2019 15:01
-
-
Save kleopatra/df13d03df6075def17471573bd9824b2 to your computer and use it in GitHub Desktop.
TestFx TableViewMatchers.containsRowAtIndex: does not test "live" cell?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.function.Supplier; | |
| import org.junit.Test; | |
| import org.testfx.framework.junit.ApplicationTest; | |
| import org.testfx.matcher.control.TableViewMatchersTest.Person; | |
| import org.testfx.util.WaitForAsyncUtils; | |
| import static org.junit.Assert.*; | |
| import static org.testfx.api.FxAssert.*; | |
| import static org.testfx.matcher.control.TableViewMatchers.*; | |
| import static org.testfx.util.DebugUtils.*; | |
| import javafx.application.Platform; | |
| import javafx.collections.FXCollections; | |
| import javafx.collections.ObservableList; | |
| import javafx.scene.Parent; | |
| import javafx.scene.Scene; | |
| import javafx.scene.control.TableCell; | |
| import javafx.scene.control.TableColumn; | |
| import javafx.scene.control.TableView; | |
| import javafx.scene.control.cell.PropertyValueFactory; | |
| import javafx.scene.layout.StackPane; | |
| import javafx.stage.Stage; | |
| //import utils.Person; | |
| /** | |
| * Quick check of assumption: live state of cells not tested | |
| * | |
| * @author Jeanette Winzenburg, Berlin | |
| */ | |
| public class TableCellLiveTest extends ApplicationTest { | |
| public static final String PREFIX = "winner! " ; | |
| public static final Supplier<ObservableList<Person>> ITEM_SUPPLIER = () -> { | |
| ObservableList<Person> persons = FXCollections.observableArrayList( | |
| new Person("A-Name", 12), new Person("Dummy", 13)); | |
| return persons; | |
| }; | |
| @Test | |
| public void testTableCellUpdate() { | |
| TableView<Person> tableView = lookup(".table-view").query(); | |
| int index = 0; | |
| Person person = tableView.getItems().get(index); | |
| // sanity | |
| verifyThat(tableView, containsRowAtIndex(index, person.nameProperty().get())); | |
| String replacedFirst = PREFIX + person.nameProperty().get(); | |
| Platform.runLater(() -> { | |
| tableView.getSelectionModel().select(index); | |
| }); | |
| WaitForAsyncUtils.waitForFxEvents(); | |
| assertEquals("sanity: ", index, tableView.getSelectionModel().getSelectedIndex()); | |
| // update | |
| verifyThat(tableView, containsRowAtIndex(index, replacedFirst), saveScreenshot()); | |
| } | |
| @Override | |
| public void start(Stage stage) { | |
| Parent sceneRoot = new TablePane(); | |
| Scene scene = new Scene(sceneRoot, 100, 100); | |
| stage.setScene(scene); | |
| stage.show(); | |
| } | |
| static int count; | |
| public static class TablePane extends StackPane { | |
| public TableView<Person> table; | |
| public TablePane() { | |
| super(); | |
| table = new TableView<>(ITEM_SUPPLIER.get()); | |
| // need cell selection node to get update of cell's selected state | |
| table.getSelectionModel().setCellSelectionEnabled(true); | |
| table.getColumns().addAll( | |
| createTableColumn("name") | |
| ); | |
| getChildren().add(table); | |
| } | |
| private TableColumn<Person, String> createTableColumn(String property) { | |
| TableColumn<Person, String> column = new TableColumn<>(property + count++); | |
| column.setCellValueFactory(new PropertyValueFactory<>(property)); | |
| column.setCellFactory(cc -> { | |
| // custom cell with text depending on selection state | |
| TableCell<Person, String> cell = new TableCell<>() { | |
| @Override | |
| protected void updateItem(String item, boolean empty) { | |
| super.updateItem(item, empty); | |
| if (item == null || empty) { | |
| setText(""); | |
| } else { | |
| if (isSelected()) { | |
| item = PREFIX + item; | |
| } | |
| setText(item); | |
| } | |
| } | |
| /** | |
| * Hack around https://bugs.openjdk.java.net/browse/JDK-8145588 | |
| * variant for TableCell | |
| * @param selected | |
| */ | |
| @Override | |
| public void updateSelected(boolean selected) { | |
| super.updateSelected(selected); | |
| updateItem(getItem(), isEmpty()); | |
| } | |
| }; | |
| return cell; | |
| }); | |
| return column; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment