Skip to content

Commit

Permalink
Merge pull request #252 from brcolow/list-matchers
Browse files Browse the repository at this point in the history
(feat) ListViewMatchers: Add hasPlaceholder(Node placeholder).
  • Loading branch information
hastebrot committed Mar 25, 2016
2 parents c642ea6 + c6c1ab6 commit 38ae989
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
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 @@ -64,6 +65,36 @@ public static Matcher<Node> isEmpty() {
return typeSafeMatcher(ListView.class, descriptionText, ListViewMatchers::isListEmpty);
}

@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 Down Expand Up @@ -91,4 +122,20 @@ private static boolean isListEmpty(ListView listView) {
return listView.getItems().isEmpty();
}

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);
}
}
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 38ae989

Please sign in to comment.