-
Notifications
You must be signed in to change notification settings - Fork 787
/
Asserter.java
executable file
·115 lines (97 loc) · 3.79 KB
/
Asserter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.jayway.android.robotium.solo;
import junit.framework.Assert;
import android.app.Activity;
import android.app.ActivityManager;
/**
* This class contains assertActivity() methods.
*
* @author Renas Reda, renas.reda@jayway.com
*
*/
class Asserter {
private final ActivityUtils activityUtils;
private final Sleeper sleeper;
/**
* Constructs this object.
*
* @param activityUtils the {@code ActivityUtils} instance.
* @param sleeper the {@code Sleeper} instance.
*
*/
public Asserter(ActivityUtils activityUtils, Sleeper sleeper) {
this.activityUtils = activityUtils;
this.sleeper = sleeper;
}
/**
* Asserts that an expected {@link Activity} is currently active one.
*
* @param message the message that should be displayed if the assert fails
* @param name the name of the {@code Activity} that is expected to be active e.g. {@code "MyActivity"}
*
*/
public void assertCurrentActivity(String message, String name)
{
sleeper.sleep();
Assert.assertEquals(message, name, activityUtils.getCurrentActivity()
.getClass().getSimpleName());
}
/**
* Asserts that an expected {@link Activity} is currently active one.
*
* @param message the message that should be displayed if the assert fails
* @param expectedClass the {@code Class} object that is expected to be active e.g. {@code MyActivity.class}
*
*/
public void assertCurrentActivity(String message, Class<? extends Activity> expectedClass)
{
sleeper.sleep();
Assert.assertEquals(message, expectedClass.getName(), activityUtils
.getCurrentActivity().getClass().getName());
}
/**
* Asserts that an expected {@link Activity} is currently active one, with the possibility to
* verify that the expected {@code Activity} is a new instance of the {@code Activity}.
*
* @param message the message that should be displayed if the assert fails
* @param name the name of the {@code Activity} that is expected to be active e.g. {@code "MyActivity"}
* @param isNewInstance {@code true} if the expected {@code Activity} is a new instance of the {@code Activity}
*
*/
public void assertCurrentActivity(String message, String name, boolean isNewInstance)
{
assertCurrentActivity(message, name);
assertCurrentActivity(message, activityUtils.getCurrentActivity().getClass(),
isNewInstance);
}
/**
* Asserts that an expected {@link Activity} is currently active one, with the possibility to
* verify that the expected {@code Activity} is a new instance of the {@code Activity}.
*
* @param message the message that should be displayed if the assert fails
* @param expectedClass the {@code Class} object that is expected to be active e.g. {@code MyActivity.class}
* @param isNewInstance {@code true} if the expected {@code Activity} is a new instance of the {@code Activity}
*
*/
public void assertCurrentActivity(String message, Class<? extends Activity> expectedClass,
boolean isNewInstance) {
boolean found = false;
assertCurrentActivity(message, expectedClass);
Activity activity = activityUtils.getCurrentActivity();
for (int i = 0; i < activityUtils.getAllOpenedActivities().size() - 1; i++) {
String instanceString = activityUtils.getAllOpenedActivities().get(i).toString();
if (instanceString.equals(activity.toString()))
found = true;
}
Assert.assertNotSame(message + ", isNewInstance: actual and ", isNewInstance, found);
}
/**
* Asserts that the available memory in the system is not low.
*
*/
public void assertNotLowMemory()
{
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
((ActivityManager)activityUtils.getCurrentActivity().getSystemService("activity")).getMemoryInfo(mi);
Assert.assertFalse("Low memory available: " + mi.availMem + " bytes", mi.lowMemory);
}
}