-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathAndroidTest.java.tmpl
83 lines (75 loc) · 2.86 KB
/
AndroidTest.java.tmpl
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
package {package_name};
import android.app.Instrumentation;
import android.app.NativeActivity;
import android.content.ContentResolver;
import android.os.Looper;
import android.system.Os;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.apps.common.testing.util.AndroidTestUtil;
import com.google.common.jni.JniLoader;
import java.io.File;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class {java_class_name} {{
static {{
// Blaze packs shared libraries into jar files. JniLoader unpacks them and
// then delegates to System.loadLibrary.
JniLoader.loadLibrary("{so_lib_name}");
}}
private native boolean runAllTest(NativeActivity activity, String filter);
@Rule
public final ActivityTestRule<NativeActivity> rule =
new ActivityTestRule<NativeActivity>(NativeActivity.class);
@Before
public void setupFirestoreEmulatorAddress() {{
// TODO(b/171886053) Refactor to not rely on this call
Looper.prepare();
ContentResolver contentResolver =
InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
Map<String, String> args = AndroidTestUtil.getTestArgs(contentResolver);
if (args.containsKey("firestore_emulator_port")) {{
String address = "10.0.2.2:" + args.get("firestore_emulator_port");
try {{
Os.setenv("FIRESTORE_EMULATOR_HOST", address, true);
Log.i("{java_class_name}", "FIRESTORE_EMULATOR_HOST: " + address);
}}
catch (Exception e) {{
Log.w("{java_class_name}", "Could not set emulator address environment variable: " + e);
// Swallow exception to allow tests to run against actual backend.
}}
}}
}}
// Manually stop the test activity. If we let the test harness do this for us,
// somehow that will catch an expected crash and fail the test. TODO(zxu):
// Investigate the root cause and undo this change, see b/111298684.
@After
public void stopNativeActivity() {{
Instrumentation instrumentation = null;
try {{
instrumentation = InstrumentationRegistry.getInstrumentation();
}} catch (IllegalStateException e) {{
Log.e("{java_class_name}",
"cannot find instrumentation for the test activity");
return;
}}
Log.d("{java_class_name}", "try to stop test activity");
instrumentation.callActivityOnStop(rule.getActivity());
}}
private void run(String test) {{
// Pass the test name as a filter to run the specific test.
boolean result = runAllTest(rule.getActivity(), /*filter=*/test);
Assert.assertTrue(result);
}}
{tests}
}}