-
Notifications
You must be signed in to change notification settings - Fork 7
/
AndroidTestNGSupport.java
52 lines (42 loc) · 1.74 KB
/
AndroidTestNGSupport.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
package de.lemona.android.testng;
import android.app.Instrumentation;
import android.content.Context;
/**
* An utility class suitable for accessing the {@link Instrumentation} and
* {@link Context} instances from tests.
* <p>
* NOTE: this is a bit of a hack (static), but it's the easiest way to
* implement it without getting lost in TestNG's hooks.
*/
public abstract class AndroidTestNGSupport {
private AndroidTestNGSupport() {
throw new IllegalStateException("Do not construct");
}
/* ====================================================================== */
private static Instrumentation instrumentation = null;
static final void injectInstrumentation(Instrumentation instrumentation) {
AndroidTestNGSupport.instrumentation = instrumentation;
}
/* ====================================================================== */
/**
* Return the {@link Context} associated with the tests.
*
* @return A <b>non-null</b> {@link Context} instance.
* @throws IllegalStateException If no instance was available.
*/
public static final Context getContext() {
if (instrumentation == null) throw new IllegalStateException("No Android instrumentation available");
final Context context = instrumentation.getTargetContext();
if (context == null) throw new IllegalStateException("No android context available");
return context;
}
/**
* Return the {@link Instrumentation} associated with the tests.
*
* @return A <b>non-null</b> {@link Instrumentation} instance.
* @throws IllegalStateException If no instance was available.
*/
public static final Instrumentation getInstrumentation() {
return instrumentation;
}
}