-
Notifications
You must be signed in to change notification settings - Fork 168
/
HasLabelStringMatcher.java
66 lines (56 loc) · 1.58 KB
/
HasLabelStringMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.loadui.testfx.controls.impl;
import javafx.scene.Node;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextInputControl;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import static com.google.common.base.Preconditions.checkArgument;
import static org.loadui.testfx.GuiTest.find;
public class HasLabelStringMatcher extends TypeSafeMatcher<Object>
{
private final String label;
private String actualText;
public HasLabelStringMatcher( String label )
{
this.label = label;
}
public void describeTo( Description desc )
{
desc.appendText( "Node should have label ");
desc.appendValue( label );
}
@Override
public void describeMismatchSafely( Object query, Description desc )
{
desc.appendText("Label was ");
desc.appendValue( actualText );
}
@Override
public boolean matchesSafely( Object target )
{
if( target instanceof String )
{
return nodeHasLabel( find( ( String )target ) );
}
else if( target instanceof Labeled )
{
return nodeHasLabel( ( Labeled )target );
}
return false;
}
private boolean nodeHasLabel( Node node )
{
checkArgument( node instanceof Labeled || node instanceof TextInputControl, "Target node must be Labeled or TextInputControl." );
if( node instanceof Labeled )
{
Labeled labeled = ( Labeled )node;
actualText = labeled.getText();
}
else
{
TextInputControl textInput = ( TextInputControl )node;
actualText = textInput.getText();
}
return label.equals(actualText);
}
}