blob: 81d17e2f3e365e410ad7ba12aa4490a763d66366 (
plain)
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
|
package com.xtremelabs.robolectric.matchers;
import android.widget.ImageView;
import com.xtremelabs.robolectric.res.ResourceLoader;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.junit.internal.matchers.TypeSafeMatcher;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
public class ImageViewHasDrawableMatcher<T extends ImageView> extends TypeSafeMatcher<T> {
private int expectedResourceId;
private String message;
public ImageViewHasDrawableMatcher(int expectedResourceId) {
this.expectedResourceId = expectedResourceId;
}
@Override
public boolean matchesSafely(T actualImageView) {
if (actualImageView == null) {
return false;
}
ResourceLoader resourceLoader = ResourceLoader.getFrom(actualImageView.getContext());
int actualResourceId = shadowOf(actualImageView).getResourceId();
String actualName = nameOrUnset(resourceLoader, actualResourceId);
String expectedName = nameOrUnset(resourceLoader, expectedResourceId);
message = "[" + actualResourceId + " (" + actualName + ")] to equal [" + expectedResourceId + " (" + expectedName + ")]";
return actualResourceId == expectedResourceId;
}
private String nameOrUnset(ResourceLoader resourceLoader, int resourceId) {
return resourceId == 0 ? "unset" : resourceLoader.getNameForId(resourceId);
}
@Override
public void describeTo(Description description) {
description.appendText(message);
}
@Factory
public static <T extends ImageView> Matcher<T> hasDrawable(int expectedResourceId) {
return new ImageViewHasDrawableMatcher<T>(expectedResourceId);
}
}
|