1 /**
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * ```
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.controller.tests.deletion.api
17 
18 import android.health.connect.DeleteUsingFiltersRequest
19 import android.health.connect.HealthConnectManager
20 import android.health.connect.HealthDataCategory
21 import android.health.connect.TimeInstantRangeFilter
22 import android.health.connect.datatypes.ActiveCaloriesBurnedRecord
23 import android.health.connect.datatypes.CyclingPedalingCadenceRecord
24 import android.health.connect.datatypes.DistanceRecord
25 import android.health.connect.datatypes.ElevationGainedRecord
26 import android.health.connect.datatypes.ExerciseSessionRecord
27 import android.health.connect.datatypes.FloorsClimbedRecord
28 import android.health.connect.datatypes.PlannedExerciseSessionRecord
29 import android.health.connect.datatypes.PowerRecord
30 import android.health.connect.datatypes.SpeedRecord
31 import android.health.connect.datatypes.StepsCadenceRecord
32 import android.health.connect.datatypes.StepsRecord
33 import android.health.connect.datatypes.TotalCaloriesBurnedRecord
34 import android.health.connect.datatypes.Vo2MaxRecord
35 import android.health.connect.datatypes.WheelchairPushesRecord
36 import com.android.healthconnect.controller.deletion.DeletionType
37 import com.android.healthconnect.controller.deletion.api.DeleteCategoryUseCase
38 import com.google.common.truth.Truth.assertThat
39 import dagger.hilt.android.testing.HiltAndroidRule
40 import dagger.hilt.android.testing.HiltAndroidTest
41 import java.time.Instant
42 import kotlinx.coroutines.Dispatchers
43 import kotlinx.coroutines.test.runTest
44 import org.junit.Before
45 import org.junit.Rule
46 import org.junit.Test
47 import org.mockito.ArgumentCaptor
48 import org.mockito.Captor
49 import org.mockito.Mockito
50 import org.mockito.Mockito.any
51 import org.mockito.Mockito.doAnswer
52 import org.mockito.Mockito.mock
53 import org.mockito.MockitoAnnotations
54 import org.mockito.invocation.InvocationOnMock
55 
56 @HiltAndroidTest
57 @Deprecated("This won't be used once the NEW_INFORMATION_ARCHITECTURE feature is enabled.")
58 class DeleteCategoryUseCaseTest {
59     @get:Rule val hiltRule = HiltAndroidRule(this)
60 
61     private lateinit var useCase: DeleteCategoryUseCase
62     var manager: HealthConnectManager = mock(HealthConnectManager::class.java)
63 
64     @Captor lateinit var filtersCaptor: ArgumentCaptor<DeleteUsingFiltersRequest>
65 
66     @Before
67     fun setup() {
68         MockitoAnnotations.initMocks(this)
69         useCase = DeleteCategoryUseCase(manager, Dispatchers.Main)
70     }
71 
72     @Test
73     fun invoke_deleteCategoryData_callsHealthManager() = runTest {
74         doAnswer(prepareAnswer())
75             .`when`(manager)
76             .deleteRecords(any(DeleteUsingFiltersRequest::class.java), any(), any())
77 
78         val startTime = Instant.now().minusSeconds(10)
79         val endTime = Instant.now()
80 
81         val deleteCategory = DeletionType.DeletionTypeCategoryData(HealthDataCategory.ACTIVITY)
82 
83         useCase.invoke(
84             deleteCategory,
85             TimeInstantRangeFilter.Builder().setStartTime(startTime).setEndTime(endTime).build())
86 
87         Mockito.verify(manager, Mockito.times(1))
88             .deleteRecords(filtersCaptor.capture(), any(), any())
89 
90         assertThat((filtersCaptor.value.timeRangeFilter as TimeInstantRangeFilter).startTime)
91             .isEqualTo(startTime)
92         assertThat((filtersCaptor.value.timeRangeFilter as TimeInstantRangeFilter).endTime)
93             .isEqualTo(endTime)
94         assertThat(filtersCaptor.value.dataOrigins).isEmpty()
95         assertThat(filtersCaptor.value.recordTypes)
96             .containsExactly(
97                 TotalCaloriesBurnedRecord::class.java,
98                 ActiveCaloriesBurnedRecord::class.java,
99                 DistanceRecord::class.java,
100                 StepsRecord::class.java,
101                 StepsCadenceRecord::class.java,
102                 SpeedRecord::class.java,
103                 PowerRecord::class.java,
104                 WheelchairPushesRecord::class.java,
105                 FloorsClimbedRecord::class.java,
106                 ElevationGainedRecord::class.java,
107                 Vo2MaxRecord::class.java,
108                 CyclingPedalingCadenceRecord::class.java,
109                 ExerciseSessionRecord::class.java,
110                 PlannedExerciseSessionRecord::class.java)
111     }
112 
113     private fun prepareAnswer(): (InvocationOnMock) -> Nothing? {
114         return { _: InvocationOnMock -> null }
115     }
116 }
117