/
TreeItemTextMatcher.java
95 lines (84 loc) · 2.98 KB
/
TreeItemTextMatcher.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.core.matcher;
import org.eclipse.swt.widgets.TreeItem;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsEqual;
import org.eclipse.reddeer.core.handler.TreeItemHandler;
/**
* Matcher matching text of {@link TreeItem}.
*
* @author Lucia Jelinkova
*
*/
public class TreeItemTextMatcher extends TypeSafeMatcher<TreeItem> {
private Matcher<String> expectedTextMatcher;
private int index = 0;
/**
* Creates new TreeItemTextMatcher matching text of {@link TreeItem} located at cell with index 0
* to specified text.
*
* @param expectedText text to match text of {@link TreeItem}
*/
public TreeItemTextMatcher(String expectedText) {
this(expectedText, 0);
}
/**
* Creates new TreeItemTextMatcher matching text of {@link TreeItem} located at cell with specified index
* to specified text.
*
* @param expectedText text to match text of {@link TreeItem}
* @param index index of cell containing text to match
*/
public TreeItemTextMatcher(String expectedText, int index) {
this(new IsEqual<String>(expectedText), index);
}
/**
* Creates new TreeItemTextMatcher matching text of {@link TreeItem} located at cell with index 0
* to specified text matcher.
*
* @param expectedTextMatcher text matcher to match text of {@link TreeItem}
*/
public TreeItemTextMatcher(Matcher<String> expectedTextMatcher) {
this(expectedTextMatcher, 0);
}
/**
* Creates new TreeItemTextMatcher matching text of {@link TreeItem} located at cell with specified index
* to specified text matcher.
*
* @param expectedTextMatcher text matcher to match text of {@link TreeItem}
* @param index index of cell containing text to match
*/
public TreeItemTextMatcher(Matcher<String> expectedTextMatcher, int index) {
this.expectedTextMatcher = expectedTextMatcher;
this.index = index;
}
/* (non-Javadoc)
* @see org.hamcrest.TypeSafeMatcher#matchesSafely(java.lang.Object)
*/
@Override
protected boolean matchesSafely(TreeItem item) {
return expectedTextMatcher.matches(TreeItemHandler.getInstance().getText(item, index));
}
/* (non-Javadoc)
* @see org.hamcrest.SelfDescribing#describeTo(org.hamcrest.Description)
*/
@Override
public void describeTo(Description description) {
description.appendText("tree item text on position ");
description.appendValue(index);
description.appendText(" matches ");
description.appendDescriptionOf(expectedTextMatcher);
}
}