Skip to content

Commit

Permalink
Add has(Visible)Placeholder(Node placeHolder) to ListViewMatchers.
Browse files Browse the repository at this point in the history
The visible variant is used when one also wants to assert that
the placeholder is currently visible (e.g. that the ListView is empty).
  • Loading branch information
brcolow committed Mar 13, 2016
1 parent 43f8277 commit bc1823c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;
import javafx.scene.Node;
import javafx.scene.control.Cell;
import javafx.scene.control.Labeled;
import javafx.scene.control.ListView;

import org.hamcrest.Factory;
Expand Down Expand Up @@ -57,6 +58,36 @@ public static Matcher<Node> hasItems(int amount) {
return typeSafeMatcher(ListView.class, descriptionText, node -> hasItems(node, amount));
}

@Factory
@Unstable(reason = "is missing apidocs")
public static Matcher<Node> hasPlaceholder(Node placeHolder) {
String descriptionText = "has ";
// better description messages for Labeled nodes
if (Labeled.class.isAssignableFrom(placeHolder.getClass())) {
descriptionText += "labeled placeholder containing text: \""
+ ((Labeled) placeHolder).getText() + "\"";
} else {
descriptionText += "placeholder " + placeHolder;
}
return typeSafeMatcher(ListView.class, descriptionText,
node -> hasPlaceholder(node, placeHolder));
}

@Factory
@Unstable(reason = "is missing apidocs")
public static Matcher<Node> hasVisiblePlaceholder(Node placeHolder) {
String descriptionText = "has visible";
// better description messages for Labeled nodes
if (Labeled.class.isAssignableFrom(placeHolder.getClass())) {
descriptionText += "labeled placeholder containing text: \""
+ ((Labeled) placeHolder).getText() + "\"";
} else {
descriptionText += "placeholder " + placeHolder;
}
return typeSafeMatcher(ListView.class, descriptionText,
node -> hasVisiblePlaceholder(node, placeHolder));
}

//---------------------------------------------------------------------------------------------
// PRIVATE STATIC METHODS.
//---------------------------------------------------------------------------------------------
Expand All @@ -80,4 +111,20 @@ private static boolean hasCellValue(Cell cell,
return !cell.isEmpty() && Objects.equals(cell.getItem(), value);
}

private static boolean hasPlaceholder(ListView listView,
Node placeHolder) {
if (Labeled.class.isAssignableFrom(placeHolder.getClass())
&& Labeled.class.isAssignableFrom(listView.getPlaceholder().getClass())) {
return ((Labeled) listView.getPlaceholder()).getText()
.equals(((Labeled) placeHolder).getText());
} else {
return Objects.equals(listView.getPlaceholder(), placeHolder);
}
}

private static boolean hasVisiblePlaceholder(ListView listView,
Node placeHolder) {
return listView.getPlaceholder().isVisible()
&& hasPlaceholder(listView, placeHolder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.testfx.matcher.control;

import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;

Expand Down Expand Up @@ -55,6 +56,7 @@ public void setup() throws Exception {
FxToolkit.setupSceneRoot(() -> {
listView = new ListView<>();
listView.setItems(observableArrayList("alice", "bob", "carol", "dave"));
listView.setPlaceholder(new Label("Empty!"));
return new StackPane(listView);
});
FxToolkit.showStage();
Expand Down Expand Up @@ -103,4 +105,18 @@ public void hasItems_fails() {
assertThat(listView, ListViewMatchers.hasItems(0));
}

@Test
public void hasPlaceholder() {
// expect:
assertThat(listView, ListViewMatchers.hasPlaceholder(new Label("Empty!")));
}

@Test
public void hasPlaceholder_fails() {
// expect:
exception.expect(AssertionError.class);
exception.expectMessage("Expected: ListView has labeled placeholder containing text: \"foobar\"\n");

assertThat(listView, ListViewMatchers.hasPlaceholder(new Label("foobar")));
}
}

0 comments on commit bc1823c

Please sign in to comment.