package org.commcare.androidTests;

import android.content.Intent;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import org.commcare.AsyncRestoreHelperMock;
import org.commcare.utils.ProgressIdlingResource;
import org.commcare.utils.HQApi;
import org.commcare.utils.InstrumentationUtility;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

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

    private final String CCZ_NAME = "integration_test_app.ccz";
    private final String APP_NAME = "Integration Tests";

    @After
    public void logout() {
        InstrumentationUtility.logout();
    }

    @Test
    public void testRestoreOnLogin() {
        String userId = "13a0910ea963acbf9f4b59dcc9a0f9aa";
        String groupId = "78185f2132bd8ba3af30b488f9974b41";
        AsyncRestoreHelperMock.clear();

        // Make sure user is present in the group.
        HQApi.addUserInGroup(userId, groupId);

        installAppAndClearCache();

        assertFalse(AsyncRestoreHelperMock.isRetryCalled());
        assertFalse(AsyncRestoreHelperMock.isServerProgressReportingStarted());

        // Register commcareidling resource before login
        ProgressIdlingResource idlingResource = new ProgressIdlingResource();
        IdlingRegistry.getInstance().register(idlingResource);

        // Login into the app
        InstrumentationUtility.login("many.cases1", "123");

        // Confirm Async Restore is done.
        assertTrue(AsyncRestoreHelperMock.isRetryCalled());
        assertTrue(AsyncRestoreHelperMock.isServerProgressReportingStarted());

        // Unregister idling resource
        IdlingRegistry.getInstance().unregister(idlingResource);
    }

    @Test
    public void testRestoreOnSync() {
        String userId = "81f1645b41d85b539a7e407b035bfbf1";
        String groupId = "78185f2132bd8ba3af30b488f9974b41";
        AsyncRestoreHelperMock.clear();

        // Make sure user is not present in the group.
        HQApi.removeUserFromGroup(userId, groupId);

        installAppAndClearCache();

        // Register commcareidling resource before login
        ProgressIdlingResource idlingResource = new ProgressIdlingResource();
        IdlingRegistry.getInstance().register(idlingResource);

        // Login into the app
        InstrumentationUtility.login("many.cases2", "123");

        // Confirm No Restore happened during login.
        assertFalse(AsyncRestoreHelperMock.isRetryCalled());
        assertFalse(AsyncRestoreHelperMock.isServerProgressReportingStarted());

        // Add user to the group.
        HQApi.addUserInGroup(userId, groupId);

        // Sync with server.
        onView(withText("Sync with Server"))
                .perform(click());

        // Confirm AsyncRestore happened after sync
        assertTrue(AsyncRestoreHelperMock.isRetryCalled());
        assertTrue(AsyncRestoreHelperMock.isServerProgressReportingStarted());

        // Unregister idling resource
        IdlingRegistry.getInstance().unregister(idlingResource);
    }

    private void installAppAndClearCache() {
        installApp(APP_NAME, CCZ_NAME);

        Intent intent = new Intent();
        intent.setAction("org.commcare.dalvik.api.action.ClearCacheOnRestore");
        getInstrumentation()
                .getTargetContext()
                .sendBroadcast(intent);
    }

}
@@ -56,198 +56,449 @@
 ent.
-Intent;%0Aimport androidx.test.espresso.IdlingRegistry;%0Aimport androidx.test.ext.junit.runners.AndroidJUnit4;%0Aimport androidx.test.filters.LargeTest;%0Aimport org.commcare.AsyncRestoreHelperMock
+Context;%0Aimport android.content.Intent;%0Aimport android.content.IntentFilter;%0A%0Aimport androidx.localbroadcastmanager.content.LocalBroadcastManager;%0Aimport androidx.test.espresso.IdlingRegistry;%0Aimport androidx.test.ext.junit.runners.AndroidJUnit4;%0Aimport androidx.test.filters.LargeTest;%0Aimport androidx.test.platform.app.InstrumentationRegistry;%0A%0Aimport org.commcare.AsyncRestoreHelperMock;%0Aimport org.commcare.provider.DebugControlsReceiver
 ;%0Aim
@@ -648,24 +648,49 @@
 unit.After;%0A
+import org.junit.Before;%0A
 import org.j
@@ -923,93 +923,8 @@
 xt;%0A
-import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;%0A
 impo
@@ -1251,46 +1251,606 @@
 s%22;%0A
-%0A    @After%0A    public void logout() %7B
+    private final String CLEAR_CACHE_ACTION = %22org.commcare.dalvik.api.action.ClearCacheOnRestore%22;%0A    private Context mContext;%0A    private DebugControlsReceiver mReceiver = new DebugControlsReceiver();%0A%0A    @Before%0A    public void setup() %7B%0A        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();%0A        LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, new IntentFilter(CLEAR_CACHE_ACTION));%0A    %7D%0A%0A    @After%0A    public void logout() %7B%0A        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);
 %0A   
@@ -4422,172 +4422,80 @@
 ent(
-);%0A        intent.setAction(%22org.commcare.dalvik.api.action.ClearCacheOnRestore%22);%0A        getInstrumentation()%0A                .getTargetContext()%0A                
+CLEAR_CACHE_ACTION);%0A        LocalBroadcastManager.getInstance(mContext)
 .sen
@@ -4504,16 +4504,20 @@
 roadcast
+Sync
 (intent)

Use sendBroadcastSync in asyncRestore tests