package org.commcare.androidTests;

import android.Manifest;

import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;
import org.commcare.CommCareApplication;
import org.commcare.activities.DispatchActivity;
import org.commcare.activities.FormEntryActivity;
import org.commcare.utils.InstrumentationUtility;
import org.junit.Rule;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.pressBack;

/**
 * @author $|-|!˅@M
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public abstract class BaseTest {

    @Rule
    public ActivityTestRule<DispatchActivity> activityTestRule = new ActivityTestRule<>(DispatchActivity.class);

    @Rule
    public IntentsTestRule<FormEntryActivity> intentsRule = new IntentsTestRule<>(FormEntryActivity.class);

    @Rule
    public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA
    );

    protected void installApp(String appName, String ccz) {
        if (CommCareApplication.instance().getCurrentApp() == null) {
            InstrumentationUtility.installApp(ccz);
        } else if (!appName.equals(CommCareApplication.instance().getCurrentApp().getAppRecord().getDisplayName())) {
            // We already have an installed app, But not the one we need for this test.
            InstrumentationUtility.uninstallCurrentApp();
            InstrumentationUtility.installApp(ccz);
            // App installation doesn't take back to login screen. Is this an issue?
            pressBack();
        }
    }

}

@@ -212,52 +212,8 @@
 st;%0A
-import androidx.test.rule.ActivityTestRule;%0A
 impo
@@ -251,24 +251,24 @@
 issionRule;%0A
+
 import org.c
@@ -349,58 +349,8 @@
 ty;%0A
-import org.commcare.activities.FormEntryActivity;%0A
 impo
@@ -638,74 +638,15 @@
 lic 
-ActivityTestRule%3CDispatchActivity%3E activityTestRule = new Activity
+Intents
 Test
@@ -650,18 +650,16 @@
 estRule%3C
-%3E(
 Dispatch
@@ -670,72 +670,8 @@
 vity
-.class);%0A%0A    @Rule%0A    public IntentsTestRule%3CFormEntryActivity
 %3E in
@@ -708,17 +708,16 @@
 e%3C%3E(
-FormEntry
+Dispatch
 Acti

Use intent test rule on dispatch activity