Skip to content

Commit

Permalink
(fix) Make TableViewMatchers work with cell factories.
Browse files Browse the repository at this point in the history
  • Loading branch information
brcolow committed Mar 25, 2017
1 parent b954113 commit 7e4a89d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.Cell;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

Expand Down Expand Up @@ -183,12 +184,18 @@ private static <T> boolean containsRow(TableView<T> tableView, Object...cells) {

private static boolean hasCellValue(Cell cell,
Object value) {
return !cell.isEmpty() && hasItemValue(cell.getItem(), value);
return !cell.isEmpty() && hasItemValue(cell.getText(), value);
}

private static boolean hasItemValue(Object item,
Object value) {
return Objects.equals(item, value) || Objects.equals(item.toString(), value);
if (item == null && value == null) {
return true;
} else if (item == null || value == null) {
return false;
}
return Objects.equals(item, value) || Objects.equals(item.toString(), value) || value.toString() != null ?
Objects.equals(item.toString(), value.toString()) : false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package org.testfx.matcher.control;

import java.util.Locale;
import java.util.Map;

import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
Expand All @@ -30,6 +33,7 @@
import org.junit.rules.ExpectedException;
import org.testfx.api.FxRobot;
import org.testfx.api.FxToolkit;
import org.testfx.util.WaitForAsyncUtils;

import static javafx.collections.FXCollections.observableArrayList;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -45,6 +49,7 @@ public class TableViewMatchersTest extends FxRobot {
public ExpectedException exception = ExpectedException.none();

public TableView<Map> tableView;
TableColumn<Map, String> tableColumn0;

//---------------------------------------------------------------------------------------------
// FIXTURE METHODS.
Expand All @@ -64,8 +69,8 @@ public void setup() throws Exception {
ImmutableMap.of("name", "bob", "age", 31),
ImmutableMap.of("name", "carol"),
ImmutableMap.of("name", "dave")
));
TableColumn<Map, String> tableColumn0 = new TableColumn<>("name");
));
tableColumn0 = new TableColumn<>("name");
tableColumn0.setCellValueFactory(new MapValueFactory<>("name"));
TableColumn<Map, Integer> tableColumn1 = new TableColumn<>("age");
tableColumn1.setCellValueFactory(new MapValueFactory<>("age"));
Expand All @@ -86,6 +91,29 @@ public void hasTableCell() {
assertThat(tableView, TableViewMatchers.hasTableCell("bob"));
}

@Test
public void hasTableCell_customCellValueFactory() {
// given:
tableColumn0.setCellFactory(column -> {
return new TableCell<Map, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
setText(item.toUpperCase(Locale.US).concat("!"));
}
}
};
});

// expect:
WaitForAsyncUtils.waitForFxEvents();
assertThat(tableView, TableViewMatchers.hasTableCell("ALICE!"));
assertThat(tableView, TableViewMatchers.hasTableCell("BOB!"));
}

@Test
public void hasTableCell_fails() {
// expect:
Expand All @@ -95,6 +123,31 @@ public void hasTableCell_fails() {
assertThat(tableView, TableViewMatchers.hasTableCell("foobar"));
}

@Test
public void hasTableCell_fails_customCellValueFactory() {
// given:
tableColumn0.setCellFactory(column -> {
return new TableCell<Map, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

if (item == null || empty) {
setText(null);
} else {
setText(item.toUpperCase(Locale.US).concat("!"));
}
}
};
});

// expect:
WaitForAsyncUtils.waitForFxEvents();
exception.expect(AssertionError.class);
exception.expectMessage("Expected: TableView has table cell \"ALICE!!!\"\n");
assertThat(tableView, TableViewMatchers.hasTableCell("ALICE!!!"));
}

@Test
public void hasTableCell_with_toString() {
// expect:
Expand Down

0 comments on commit 7e4a89d

Please sign in to comment.