blob: 569ba941318621a6c6eaa10b9c352f2a4c1c0a41 (
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
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
|
package com.xtremelabs.robolectric.matchers;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import com.xtremelabs.robolectric.shadows.ShadowIntent;
import org.hamcrest.Description;
import org.junit.internal.matchers.TypeSafeMatcher;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
public class StartedMatcher extends TypeSafeMatcher<Context> {
private final Intent expectedIntent;
private String message;
public StartedMatcher(Intent expectedIntent) {
this.expectedIntent = expectedIntent;
}
public StartedMatcher(String packageName, Class<? extends Activity> expectedActivityClass) {
this(createIntent(packageName, expectedActivityClass));
}
public StartedMatcher(Class<? extends Activity> expectedActivityClass) {
this(createIntent(expectedActivityClass));
}
public StartedMatcher(Class<? extends Activity> expectedActivityClass, String expectedAction) {
this(createIntent(expectedActivityClass));
expectedIntent.setAction(expectedAction);
}
@Override
public boolean matchesSafely(Context actualContext) {
if (expectedIntent == null) {
message = "null intent (did you mean to expect null?)";
return false;
}
String expected = expectedIntent.toString();
message = "to start " + expected + ", but ";
Intent actualStartedIntent = shadowOf((ContextWrapper) actualContext).getNextStartedActivity();
if (actualStartedIntent == null) {
message += "didn't start anything";
return false;
}
ShadowIntent shadowIntent = shadowOf(actualStartedIntent);
boolean intentsMatch = shadowOf(expectedIntent).realIntentEquals(shadowIntent);
if (!intentsMatch) {
message += "started " + actualStartedIntent;
}
return intentsMatch;
}
@Override
public void describeTo(Description description) {
description.appendText(message);
}
public static Intent createIntent(Class<? extends Activity> activityClass, String extraKey, String extraValue) {
Intent intent = createIntent(activityClass);
intent.putExtra(extraKey, extraValue);
return intent;
}
public static Intent createIntent(Class<? extends Activity> activityClass, String action) {
Intent intent = createIntent(activityClass);
intent.setAction(action);
return intent;
}
public static Intent createIntent(Class<? extends Activity> activityClass) {
String packageName = activityClass.getPackage().getName();
return createIntent(packageName, activityClass);
}
public static Intent createIntent(String packageName, Class<? extends Activity> activityClass) {
Intent intent = new Intent();
intent.setClassName(packageName, activityClass.getName());
return intent;
}
}
|