|
| 1 | +package {package_name}; |
| 2 | + |
| 3 | +import android.app.Instrumentation; |
| 4 | +import android.app.NativeActivity; |
| 5 | +import android.content.ContentResolver; |
| 6 | +import android.system.Os; |
| 7 | +import android.util.Log; |
| 8 | +import androidx.test.InstrumentationRegistry; |
| 9 | +import androidx.test.filters.LargeTest; |
| 10 | +import androidx.test.rule.ActivityTestRule; |
| 11 | +import androidx.test.runner.AndroidJUnit4; |
| 12 | +import com.google.android.apps.common.testing.util.AndroidTestUtil; |
| 13 | +import com.google.common.jni.JniLoader; |
| 14 | +import java.io.File; |
| 15 | +import java.util.Map; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | + |
| 23 | +@RunWith(AndroidJUnit4.class) |
| 24 | +@LargeTest |
| 25 | +public class {java_class_name} {{ |
| 26 | + static {{ |
| 27 | + // Blaze packs shared libraries into jar files. JniLoader unpacks them and |
| 28 | + // then delegates to System.loadLibrary. |
| 29 | + JniLoader.loadLibrary("{so_lib_name}"); |
| 30 | + }} |
| 31 | + |
| 32 | + private native boolean runAllTest(NativeActivity activity, String filter); |
| 33 | + |
| 34 | + @Rule |
| 35 | + public final ActivityTestRule<NativeActivity> rule = |
| 36 | + new ActivityTestRule<NativeActivity>(NativeActivity.class); |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setupFirestoreEmulatorAddress() {{ |
| 40 | + ContentResolver contentResolver = |
| 41 | + InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver(); |
| 42 | + Map<String, String> args = AndroidTestUtil.getTestArgs(contentResolver); |
| 43 | + if (args.containsKey("firestore_emulator_port")) {{ |
| 44 | + String address = "10.0.2.2:" + args.get("firestore_emulator_port"); |
| 45 | + try {{ |
| 46 | + Os.setenv("FIRESTORE_EMULATOR_HOST", address, true); |
| 47 | + Log.i("{java_class_name}", "FIRESTORE_EMULATOR_HOST: " + address); |
| 48 | + }} |
| 49 | + catch (Exception e) {{ |
| 50 | + Log.w("{java_class_name}", "Could not set emulator address environment variable: " + e); |
| 51 | + // Swallow exception to allow tests to run against actual backend. |
| 52 | + }} |
| 53 | + }} |
| 54 | + }} |
| 55 | + |
| 56 | + // Manually stop the test activity. If we let the test harness do this for us, |
| 57 | + // somehow that will catch an expected crash and fail the test. TODO(zxu): |
| 58 | + // Investigate the root cause and undo this change, see b/111298684. |
| 59 | + @After |
| 60 | + public void stopNativeActivity() {{ |
| 61 | + Instrumentation instrumentation = null; |
| 62 | + try {{ |
| 63 | + instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 64 | + }} catch (IllegalStateException e) {{ |
| 65 | + Log.e("{java_class_name}", |
| 66 | + "cannot find instrumentation for the test activity"); |
| 67 | + return; |
| 68 | + }} |
| 69 | + Log.d("{java_class_name}", "try to stop test activity"); |
| 70 | + instrumentation.callActivityOnStop(rule.getActivity()); |
| 71 | + }} |
| 72 | + |
| 73 | + private void run(String test) {{ |
| 74 | + // Pass the test name as a filter to run the specific test. |
| 75 | + boolean result = runAllTest(rule.getActivity(), /*filter=*/test); |
| 76 | + Assert.assertTrue(result); |
| 77 | + }} |
| 78 | + |
| 79 | + {tests} |
| 80 | +}} |
0 commit comments