/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.health.connect;

import static android.health.connect.HealthPermissions.WRITE_EXERCISE;
import static android.health.connect.HealthPermissions.WRITE_SLEEP;
import static android.health.connect.HealthPermissions.WRITE_STEPS;
import static android.healthconnect.testing.shared.phr.PhrDataFactory.DATA_SOURCE_ID;
import static android.healthconnect.testing.shared.phr.PhrDataFactory.getMedicalResourceId;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.health.connect.aidl.HealthConnectExceptionParcel;
import android.health.connect.aidl.ICanConnectMatchingAppsCallback;
import android.health.connect.aidl.IEmptyResponseCallback;
import android.health.connect.aidl.IGetMatchingAppsCallback;
import android.health.connect.aidl.IHealthConnectService;
import android.health.connect.aidl.IMedicalDataSourcesResponseCallback;
import android.health.connect.datatypes.DistanceRecord;
import android.health.connect.datatypes.MedicalDataSource;
import android.health.connect.datatypes.Record;
import android.health.connect.datatypes.SleepSessionRecord;
import android.health.connect.datatypes.StepsRecord;
import android.healthconnect.testing.shared.phr.PhrDataFactory;
import android.os.OutcomeReceiver;
import android.os.RemoteException;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.android.healthfitness.flags.Flags;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.stubbing.Answer;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

@RunWith(AndroidJUnit4.class)
public class HealthConnectManagerTest {

    public static final String PACKAGE_TO_MATCH = "package.to.match";
    @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Mock IHealthConnectService mService;

    @Test
    public void testHealthConnectManager_getNoGrantedHealthPermissions_succeeds() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);

        List<String> grantedHealthPermissions =
                healthConnectManager.getGrantedHealthPermissions("com.foo.bar");

        assertThat(grantedHealthPermissions).isEmpty();
    }

    @Test
    public void testHealthConnectManager_getSomeGrantedHealthPermissions_succeeds()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        when(mService.getGrantedHealthPermissions(any(), any()))
                .thenReturn(
                        ImmutableList.of(
                                "android.permission.health.READ_HEART_RATE",
                                "android.permission.health.WRITE_HEART_RATE"));

        List<String> grantedHealthPermissions =
                healthConnectManager.getGrantedHealthPermissions("com.foo.bar");

        assertThat(grantedHealthPermissions)
                .containsExactly(
                        "android.permission.health.READ_HEART_RATE",
                        "android.permission.health.WRITE_HEART_RATE");
    }

    @Test
    public void testHealthConnectManager_getGrantedHealthPermissionsException_rethrows()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        when(mService.getGrantedHealthPermissions(any(), any()))
                .thenThrow(new RemoteException("message"));

        assertThrows(
                RuntimeException.class,
                () -> healthConnectManager.getGrantedHealthPermissions("com.foo.bar"));
    }

    @Test
    public void testHealthConnectManager_getDatasourcesByIds_usesExceptionFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<List<MedicalDataSource>> receiver = new TestOutcomeReceiver<>();
        String id = "id";
        HealthConnectExceptionParcel error =
                new HealthConnectExceptionParcel(
                        new HealthConnectException(
                                HealthConnectException.ERROR_UNSUPPORTED_OPERATION));
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IMedicalDataSourcesResponseCallback callback =
                                            invocation.getArgument(2);
                                    callback.onError(error);
                                    return null;
                                })
                .when(mService)
                .getMedicalDataSourcesByIds(any(), any(), any());

        healthConnectManager.getMedicalDataSources(
                ImmutableList.of(id), Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    public void testHealthConnectManager_getDatasourcesByIds_usesResultFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<List<MedicalDataSource>> receiver = new TestOutcomeReceiver<>();
        String id = "id";
        List<MedicalDataSource> response =
                List.of(PhrDataFactory.getMedicalDataSourceRequiredFieldsOnly());
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IMedicalDataSourcesResponseCallback callback =
                                            invocation.getArgument(2);
                                    callback.onResult(response);
                                    return null;
                                })
                .when(mService)
                .getMedicalDataSourcesByIds(any(), any(), any());

        healthConnectManager.getMedicalDataSources(
                ImmutableList.of(id), Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.getResponse()).containsExactlyElementsIn(response);
    }

    @Test
    public void testHealthConnectManager_getDataSourcesByRequest_usesExceptionFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<List<MedicalDataSource>> receiver = new TestOutcomeReceiver<>();
        GetMedicalDataSourcesRequest request = new GetMedicalDataSourcesRequest.Builder().build();
        HealthConnectExceptionParcel error =
                new HealthConnectExceptionParcel(
                        new HealthConnectException(
                                HealthConnectException.ERROR_UNSUPPORTED_OPERATION));
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IMedicalDataSourcesResponseCallback callback =
                                            invocation.getArgument(2);
                                    callback.onError(error);
                                    return null;
                                })
                .when(mService)
                .getMedicalDataSourcesByRequest(any(), any(), any());

        healthConnectManager.getMedicalDataSources(
                request, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    public void testHealthConnectManager_getDataSourcesByRequest_usesResultFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<List<MedicalDataSource>> receiver = new TestOutcomeReceiver<>();
        GetMedicalDataSourcesRequest request = new GetMedicalDataSourcesRequest.Builder().build();
        List<MedicalDataSource> response =
                List.of(PhrDataFactory.getMedicalDataSourceRequiredFieldsOnly());
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IMedicalDataSourcesResponseCallback callback =
                                            invocation.getArgument(2);
                                    callback.onResult(response);
                                    return null;
                                })
                .when(mService)
                .getMedicalDataSourcesByRequest(any(), any(), any());

        healthConnectManager.getMedicalDataSources(
                request, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.getResponse()).isEqualTo(response);
    }

    @Test
    public void testHealthConnectManager_deleteResources_usesExceptionFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(2);
                                    callback.onError(
                                            new HealthConnectExceptionParcel(
                                                    new HealthConnectException(
                                                            HealthConnectException
                                                                    .ERROR_UNSUPPORTED_OPERATION)));
                                    return null;
                                })
                .when(mService)
                .deleteMedicalResourcesByIds(any(), any(), any());

        healthConnectManager.deleteMedicalResources(
                ImmutableList.of(getMedicalResourceId()),
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    public void testHealthConnectManager_deleteResourcesByIds_usesResultFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(2);
                                    callback.onResult();
                                    return null;
                                })
                .when(mService)
                .deleteMedicalResourcesByIds(any(), any(), any());

        healthConnectManager.deleteMedicalResources(
                ImmutableList.of(getMedicalResourceId()),
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.getResponse()).isNull();
    }

    @Test
    public void testHealthConnectManager_deleteResourcesByIds_shortcutsEmptyRequest()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();

        healthConnectManager.deleteMedicalResources(
                ImmutableList.of(), Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.getResponse()).isNull();
    }

    @Test
    public void testHealthConnectManager_deleteResourcesByRequest_usesExceptionFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(2);
                                    callback.onError(
                                            new HealthConnectExceptionParcel(
                                                    new HealthConnectException(
                                                            HealthConnectException
                                                                    .ERROR_UNSUPPORTED_OPERATION)));
                                    return null;
                                })
                .when(mService)
                .deleteMedicalResourcesByRequest(any(), any(), any());
        DeleteMedicalResourcesRequest request =
                new DeleteMedicalResourcesRequest.Builder().addDataSourceId(DATA_SOURCE_ID).build();

        healthConnectManager.deleteMedicalResources(
                request, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    public void testHealthConnectManager_deleteResourcesByRequest_usesResultFromService()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(2);
                                    callback.onResult();
                                    return null;
                                })
                .when(mService)
                .deleteMedicalResourcesByRequest(any(), any(), any());
        DeleteMedicalResourcesRequest request =
                new DeleteMedicalResourcesRequest.Builder().addDataSourceId(DATA_SOURCE_ID).build();

        healthConnectManager.deleteMedicalResources(
                request, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.getResponse()).isNull();
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testCanConnectMatchingApps_usesExceptionFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Boolean> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    ICanConnectMatchingAppsCallback callback =
                                            invocation.getArgument(2);
                                    callback.onError(
                                            new HealthConnectExceptionParcel(
                                                    new HealthConnectException(
                                                            HealthConnectException
                                                                    .ERROR_UNSUPPORTED_OPERATION)));
                                    return null;
                                })
                .when(mService)
                .canConnectMatchingApps(any(), any(), any());

        healthConnectManager.canConnectMatchingApps(
                ImmutableSet.of(), Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testCanConnectMatchingApps_matchingApps_true() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Boolean> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    ICanConnectMatchingAppsCallback callback =
                                            invocation.getArgument(2);
                                    callback.onResult(true);
                                    return null;
                                })
                .when(mService)
                .canConnectMatchingApps(any(), any(), any());

        healthConnectManager.canConnectMatchingApps(
                ImmutableSet.of(StepsRecord.class, SleepSessionRecord.class),
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.getResponse()).isTrue();
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testCanConnectMatchingApps_noMatchingApps_false() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Boolean> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    ICanConnectMatchingAppsCallback callback =
                                            invocation.getArgument(2);
                                    callback.onResult(false);
                                    return null;
                                })
                .when(mService)
                .canConnectMatchingApps(any(), any(), any());

        healthConnectManager.canConnectMatchingApps(
                ImmutableSet.of(StepsRecord.class, SleepSessionRecord.class),
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.getResponse()).isFalse();
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testGetMatchingApps_usesExceptionFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Map<String, Set<String>>> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IGetMatchingAppsCallback callback = invocation.getArgument(2);
                                    callback.onError(
                                            new HealthConnectExceptionParcel(
                                                    new HealthConnectException(
                                                            HealthConnectException
                                                                    .ERROR_UNSUPPORTED_OPERATION)));
                                    return null;
                                })
                .when(mService)
                .getMatchingApps(any(), any(), any());

        healthConnectManager.getMatchingApps(
                ImmutableSet.of(), PACKAGE_TO_MATCH, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testGetMatchingApps_noMatchingApps_emptyMap() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Map<String, Set<String>>> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IGetMatchingAppsCallback callback = invocation.getArgument(2);
                                    callback.onResult(emptyResponse());
                                    return null;
                                })
                .when(mService)
                .getMatchingApps(any(), any(), any());

        healthConnectManager.getMatchingApps(
                ImmutableSet.of(StepsRecord.class, SleepSessionRecord.class),
                PACKAGE_TO_MATCH,
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.getResponse()).isEmpty();
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testGetMatchingApps_matchingApps_usesResultFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Map<String, Set<String>>> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IGetMatchingAppsCallback callback = invocation.getArgument(2);
                                    callback.onResult(getMatchingAppsResponse());
                                    return null;
                                })
                .when(mService)
                .getMatchingApps(any(), any(), any());

        healthConnectManager.getMatchingApps(
                ImmutableSet.of(StepsRecord.class, SleepSessionRecord.class),
                PACKAGE_TO_MATCH,
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.getResponse())
                .containsExactlyEntriesIn(getMatchingAppsResponse().getMatchingApps());
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testRecordMatchmakingDenial_usesResultFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(3);
                                    callback.onResult();
                                    return null;
                                })
                .when(mService)
                .recordMatchmakingDenial(any(), any(), any(), any());

        healthConnectManager.recordMatchmakingDenial(
                PACKAGE_TO_MATCH,
                List.of(WRITE_EXERCISE),
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.getResponse()).isNull();
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testRecordMatchmakingDenial_emptyPermissionsList_callsService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        ArgumentCaptor<List<String>> permissionsCaptor = ArgumentCaptor.forClass(List.class);

        healthConnectManager.recordMatchmakingDenial(
                PACKAGE_TO_MATCH,
                Collections.emptyList(),
                Executors.newSingleThreadExecutor(),
                receiver);

        verify(mService)
                .recordMatchmakingDenial(
                        any(), eq(PACKAGE_TO_MATCH), permissionsCaptor.capture(), any());
        assertThat(permissionsCaptor.getValue()).isEmpty();
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testRecordMatchmakingDenial_withPermissions_callsServiceWithPermissions()
            throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        ArgumentCaptor<List<String>> permissionsCaptor = ArgumentCaptor.forClass(List.class);
        List<String> permissions = List.of(WRITE_STEPS, WRITE_SLEEP);

        healthConnectManager.recordMatchmakingDenial(
                PACKAGE_TO_MATCH, permissions, Executors.newSingleThreadExecutor(), receiver);

        verify(mService)
                .recordMatchmakingDenial(
                        any(), eq(PACKAGE_TO_MATCH), permissionsCaptor.capture(), any());
        assertThat(permissionsCaptor.getValue()).containsExactlyElementsIn(permissions);
    }

    @Test
    @EnableFlags(Flags.FLAG_MATCHMAKING)
    public void testRecordMatchmakingDenial_usesExceptionFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(3);
                                    callback.onError(
                                            new HealthConnectExceptionParcel(
                                                    new HealthConnectException(
                                                            HealthConnectException
                                                                    .ERROR_UNSUPPORTED_OPERATION)));
                                    return null;
                                })
                .when(mService)
                .recordMatchmakingDenial(any(), any(), any(), any());

        healthConnectManager.recordMatchmakingDenial(
                PACKAGE_TO_MATCH,
                List.of(WRITE_EXERCISE),
                Executors.newSingleThreadExecutor(),
                receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    public void setTrackingEnabledFails_usesExceptionFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(2);
                                    callback.onError(
                                            new HealthConnectExceptionParcel(
                                                    new HealthConnectException(
                                                            HealthConnectException
                                                                    .ERROR_UNSUPPORTED_OPERATION)));
                                    return null;
                                })
                .when(mService)
                .setTrackingEnabled(any(), anyBoolean(), any());

        healthConnectManager.setTrackingEnabled(
                StepsRecord.class, true, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.assertAndGetException().getErrorCode())
                .isEqualTo(HealthConnectException.ERROR_UNSUPPORTED_OPERATION);
    }

    @Test
    public void setTrackingEnabled_generatesDataTypeKeys() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();

        healthConnectManager.setTrackingEnabled(
                StepsRecord.class, false, Executors.newSingleThreadExecutor(), receiver);

        verify(mService).setTrackingEnabled(eq("TRACKING_PREF_1"), eq(false), any());
    }

    @Test
    public void setTrackingEnabled_success() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        TestOutcomeReceiver<Void> receiver = new TestOutcomeReceiver<>();
        doAnswer(
                        (Answer<Void>)
                                invocation -> {
                                    IEmptyResponseCallback callback = invocation.getArgument(2);
                                    callback.onResult();
                                    return null;
                                })
                .when(mService)
                .setTrackingEnabled(any(), anyBoolean(), any());

        healthConnectManager.setTrackingEnabled(
                StepsRecord.class, true, Executors.newSingleThreadExecutor(), receiver);

        assertThat(receiver.getResponse()).isNull();
    }

    @Test
    public void isTrackingEnabledFails_usesExceptionFromService() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        when(mService.isTrackingEnabled(any())).thenThrow(new RemoteException("message"));

        assertThrows(
                RuntimeException.class,
                () -> healthConnectManager.isTrackingEnabled(List.of(StepsRecord.class)));
    }

    @Test
    public void isTrackingEnabled_generatesDataTypeKeys() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);

        healthConnectManager.isTrackingEnabled(List.of(StepsRecord.class, DistanceRecord.class));

        verify(mService).isTrackingEnabled(captor.capture());
        assertThat(captor.getValue()).containsExactly("TRACKING_PREF_1", "TRACKING_PREF_7");
    }

    @Test
    public void isTrackingEnabled_success() throws Exception {
        Context context = ApplicationProvider.getApplicationContext();
        HealthConnectManager healthConnectManager = newHealthConnectManager(context, mService);
        when(mService.isTrackingEnabled(any()))
                .thenReturn(Map.of("TRACKING_PREF_1", true, "TRACKING_PREF_7", false));

        Map<Class<? extends Record>, Boolean> result =
                healthConnectManager.isTrackingEnabled(
                        List.of(StepsRecord.class, DistanceRecord.class));

        assertThat(result)
                .containsExactly(
                        StepsRecord.class, true,
                        DistanceRecord.class, false);
    }

    /**
     * Constructs a {@link HealthConnectManager} using reflection to access the constructor.
     *
     * <p>Unfortunately the {@link HealthConnectManager} loads from a different classloader to the
     * unit test, so even though they are in the same package name, technically the packages are
     * different. This leads to calling the constructor giving an {@link IllegalAccessError}. By
     * using reflection we can avoid this error in test cases, but this code should not be used
     * outside of testing.
     */
    private static HealthConnectManager newHealthConnectManager(
            Context context, IHealthConnectService service)
            throws InstantiationException,
                    IllegalAccessException,
                    InvocationTargetException,
                    NoSuchMethodException {
        return HealthConnectManager.class
                .getDeclaredConstructor(Context.class, IHealthConnectService.class)
                .newInstance(context, service);
    }

    private GetMatchingAppsResponse getMatchingAppsResponse() {
        return new GetMatchingAppsResponse(
                Map.of("package.name", Set.of(WRITE_STEPS, WRITE_SLEEP)));
    }

    private GetMatchingAppsResponse emptyResponse() {
        return new GetMatchingAppsResponse(Map.of());
    }

    private static class TestOutcomeReceiver<T>
            implements OutcomeReceiver<T, HealthConnectException> {
        private static final int DEFAULT_TIMEOUT_SECONDS = 5;
        private final CountDownLatch mLatch = new CountDownLatch(1);
        private final AtomicReference<T> mResponse = new AtomicReference<>();
        private final AtomicReference<HealthConnectException> mException = new AtomicReference<>();

        /**
         * Returns the resppnse received. Fails if no response received within the default timeout.
         *
         * @throws InterruptedException if this is interrupted before any response received
         */
        public T getResponse() throws InterruptedException {
            verifyNoExceptionOrThrow();
            return mResponse.get();
        }

        /**
         * Asserts that no exception is received within the default timeout. If an exception is
         * received it is rethrown by this method.
         */
        public void verifyNoExceptionOrThrow() throws InterruptedException {
            verifyNoExceptionOrThrow(DEFAULT_TIMEOUT_SECONDS);
        }

        /**
         * Asserts that no exception is received within the given timeout. If an exception is
         * received it is rethrown by this method.
         */
        public void verifyNoExceptionOrThrow(int timeoutSeconds) throws InterruptedException {
            assertThat(mLatch.await(timeoutSeconds, TimeUnit.SECONDS)).isTrue();
            if (mException.get() != null) {
                throw mException.get();
            }
        }

        /**
         * Returns the exception received. Fails if no response received within the default timeout.
         *
         * @throws InterruptedException if this is interrupted before any response received
         */
        public HealthConnectException assertAndGetException() throws InterruptedException {
            assertThat(mLatch.await(DEFAULT_TIMEOUT_SECONDS, TimeUnit.SECONDS)).isTrue();
            assertThat(mResponse.get()).isNull();
            return mException.get();
        }

        @Override
        public void onResult(T result) {
            mResponse.set(result);
            mLatch.countDown();
        }

        @Override
        public void onError(@NonNull HealthConnectException error) {
            mException.set(error);
            mLatch.countDown();
        }
    }
}
