• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * 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.dataentries.formatters
17 
18 import android.content.Context
19 import android.health.connect.datatypes.BodyFatRecord
20 import android.health.connect.datatypes.units.Percentage
21 import androidx.test.platform.app.InstrumentationRegistry
22 import com.android.healthconnect.controller.dataentries.formatters.BodyFatFormatter
23 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
24 import com.android.healthconnect.controller.tests.utils.NOW
25 import com.android.healthconnect.controller.tests.utils.getMetaData
26 import com.android.healthconnect.controller.tests.utils.setLocale
27 import com.google.common.truth.Truth.assertThat
28 import dagger.hilt.android.testing.HiltAndroidRule
29 import dagger.hilt.android.testing.HiltAndroidTest
30 import java.time.ZoneId
31 import java.util.Locale
32 import java.util.TimeZone
33 import javax.inject.Inject
34 import kotlinx.coroutines.runBlocking
35 import org.junit.Before
36 import org.junit.Rule
37 import org.junit.Test
38 
39 @HiltAndroidTest
40 class BodyFatFormatterTest {
41 
42     @get:Rule val hiltRule = HiltAndroidRule(this)
43 
44     @Inject lateinit var formatter: BodyFatFormatter
45     @Inject lateinit var preferences: UnitPreferences
46     private lateinit var context: Context
47 
48     @Before
setupnull49     fun setup() {
50         hiltRule.inject()
51         context = InstrumentationRegistry.getInstrumentation().context
52         context.setLocale(Locale.US)
53         TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("UTC")))
54     }
55 
56     @Test
<lambda>null57     fun formatValue_zero() = runBlocking {
58         val bodyFat = getBodyFat(0.0)
59         assertThat(formatter.formatValue(bodyFat, preferences)).isEqualTo("0%")
60     }
61 
62     @Test
<lambda>null63     fun formatValue_one() = runBlocking {
64         val bodyFat = getBodyFat(1.0)
65         assertThat(formatter.formatValue(bodyFat, preferences)).isEqualTo("1%")
66     }
67 
68     @Test
<lambda>null69     fun formatValue_fraction() = runBlocking {
70         val bodyFat = getBodyFat(32.2)
71         assertThat(formatter.formatValue(bodyFat, preferences)).isEqualTo("32.2%")
72     }
73 
74     @Test
<lambda>null75     fun formatA11yValue_zero() = runBlocking {
76         val bodyFat = getBodyFat(0.0)
77         assertThat(formatter.formatA11yValue(bodyFat, preferences)).isEqualTo("0 percent")
78     }
79 
80     @Test
<lambda>null81     fun formatA11yValue_one() = runBlocking {
82         val bodyFat = getBodyFat(1.0)
83         assertThat(formatter.formatA11yValue(bodyFat, preferences)).isEqualTo("1 percent")
84     }
85 
86     @Test
<lambda>null87     fun formatA11yValue_fraction() = runBlocking {
88         val bodyFat = getBodyFat(32.2)
89         assertThat(formatter.formatA11yValue(bodyFat, preferences)).isEqualTo("32.2 percent")
90     }
91 
getBodyFatnull92     private fun getBodyFat(value: Double): BodyFatRecord {
93         return BodyFatRecord.Builder(getMetaData(), NOW, Percentage.fromValue(value)).build()
94     }
95 }
96