1 /*
<lambda>null2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *
17  */
18 
19 package com.android.healthconnect.controller.tests.shared
20 
21 import android.health.connect.HealthDataCategory.ACTIVITY
22 import android.health.connect.HealthDataCategory.BODY_MEASUREMENTS
23 import android.health.connect.HealthDataCategory.CYCLE_TRACKING
24 import android.health.connect.HealthDataCategory.NUTRITION
25 import android.health.connect.HealthDataCategory.SLEEP
26 import android.health.connect.HealthDataCategory.VITALS
27 import com.android.healthconnect.controller.R
28 import com.android.healthconnect.controller.permissions.data.HealthPermission.FitnessPermission
29 import com.android.healthconnect.controller.permissions.data.HealthPermissionType
30 import com.android.healthconnect.controller.shared.HEALTH_DATA_CATEGORIES
31 import com.android.healthconnect.controller.shared.HealthDataCategoryExtensions.fromHealthPermissionType
32 import com.android.healthconnect.controller.shared.HealthDataCategoryExtensions.healthPermissionTypes
33 import com.android.healthconnect.controller.shared.HealthDataCategoryExtensions.lowercaseTitle
34 import com.android.healthconnect.controller.shared.HealthDataCategoryExtensions.uppercaseTitle
35 import com.android.healthconnect.controller.shared.HealthPermissionReader
36 import com.google.common.truth.Truth.assertThat
37 import dagger.hilt.android.testing.HiltAndroidRule
38 import dagger.hilt.android.testing.HiltAndroidTest
39 import javax.inject.Inject
40 import org.junit.Assert.assertThrows
41 import org.junit.Before
42 import org.junit.Rule
43 import org.junit.Test
44 
45 @HiltAndroidTest
46 class HealthDataCategoryExtensionsTest {
47 
48     @get:Rule val hiltRule = HiltAndroidRule(this)
49 
50     @Inject lateinit var healthPermissionReader: HealthPermissionReader
51 
52     @Before
53     fun setup() {
54         hiltRule.inject()
55     }
56 
57     @Test
58     fun allHealthPermission_haveParentCategory() {
59         val allPermissions =
60             healthPermissionReader.getHealthPermissions().filterNot { perm ->
61                 healthPermissionReader.isAdditionalPermission(perm) ||
62                     healthPermissionReader.isMedicalPermission(perm)
63             }
64         for (permissionString in allPermissions) {
65             val fitnessPermission = FitnessPermission.fromPermissionString(permissionString)
66             assertThat(
67                     HEALTH_DATA_CATEGORIES.any {
68                         it.healthPermissionTypes().contains(fitnessPermission.healthPermissionType)
69                     })
70                 .isEqualTo(true)
71         }
72     }
73 
74     @Test
75     fun uppercaseTitles() {
76         assertThat(ACTIVITY.uppercaseTitle()).isEqualTo(R.string.activity_category_uppercase)
77         assertThat(BODY_MEASUREMENTS.uppercaseTitle())
78             .isEqualTo(R.string.body_measurements_category_uppercase)
79         assertThat(CYCLE_TRACKING.uppercaseTitle())
80             .isEqualTo(R.string.cycle_tracking_category_uppercase)
81         assertThat(NUTRITION.uppercaseTitle()).isEqualTo(R.string.nutrition_category_uppercase)
82         assertThat(SLEEP.uppercaseTitle()).isEqualTo(R.string.sleep_category_uppercase)
83         assertThat(VITALS.uppercaseTitle()).isEqualTo(R.string.vitals_category_uppercase)
84     }
85 
86     @Test
87     fun uppercaseTitles_categoryNotSupported_throws() {
88         assertThrows("Category 100 is not supported", IllegalArgumentException::class.java) {
89             100.uppercaseTitle()
90         }
91     }
92 
93     @Test
94     fun lowercaseTitles() {
95         assertThat(ACTIVITY.lowercaseTitle()).isEqualTo(R.string.activity_category_lowercase)
96         assertThat(BODY_MEASUREMENTS.lowercaseTitle())
97             .isEqualTo(R.string.body_measurements_category_lowercase)
98         assertThat(CYCLE_TRACKING.lowercaseTitle())
99             .isEqualTo(R.string.cycle_tracking_category_lowercase)
100         assertThat(NUTRITION.lowercaseTitle()).isEqualTo(R.string.nutrition_category_lowercase)
101         assertThat(SLEEP.lowercaseTitle()).isEqualTo(R.string.sleep_category_lowercase)
102         assertThat(VITALS.lowercaseTitle()).isEqualTo(R.string.vitals_category_lowercase)
103     }
104 
105     @Test
106     fun lowercaseTitles_categoryNotSupported_throws() {
107         assertThrows("Category 100 is not supported", IllegalArgumentException::class.java) {
108             100.lowercaseTitle()
109         }
110     }
111 
112     @Test
113     fun fromHealthPermissionType() {
114         assertThat(fromHealthPermissionType(HealthPermissionType.HEART_RATE)).isEqualTo(VITALS)
115         assertThat(fromHealthPermissionType(HealthPermissionType.PLANNED_EXERCISE))
116             .isEqualTo(ACTIVITY)
117         assertThat(fromHealthPermissionType(HealthPermissionType.EXERCISE_ROUTE))
118             .isEqualTo(ACTIVITY)
119     }
120 }
121