1 /*
2  * Copyright (C) 2022 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 package android.healthconnect.cts.ui
17 
18 import android.health.connect.TimeInstantRangeFilter
19 import android.health.connect.datatypes.StepsRecord
20 import android.healthconnect.cts.lib.ActivityLauncher.launchMainActivity
21 import android.healthconnect.cts.lib.TestAppProxy
22 import android.healthconnect.cts.lib.UiTestUtils.clickOnText
23 import android.healthconnect.cts.lib.UiTestUtils.waitDisplayed
24 import android.healthconnect.cts.utils.DataFactory.getEmptyMetadata
25 import android.healthconnect.cts.utils.TestUtils
26 import android.healthconnect.cts.utils.TestUtils.verifyDeleteRecords
27 import androidx.test.filters.FlakyTest
28 import androidx.test.uiautomator.By
29 import com.android.compatibility.common.util.DisableAnimationRule
30 import com.android.compatibility.common.util.FreezeRotationRule
31 import java.time.Duration
32 import java.time.Instant
33 import java.time.temporal.ChronoUnit
34 import org.junit.AfterClass
35 import org.junit.BeforeClass
36 import org.junit.Rule
37 import org.junit.Test
38 
39 /** CTS test for HealthConnect Home screen. */
40 class HomeFragmentTest : HealthConnectBaseTest() {
41 
42     @get:Rule val disableAnimationRule = DisableAnimationRule()
43 
44     @get:Rule val freezeRotationRule = FreezeRotationRule()
45 
46     companion object {
47 
48         private val APP_A_WITH_READ_WRITE_PERMS: TestAppProxy =
49             TestAppProxy.forPackageName("android.healthconnect.cts.testapp.readWritePerms.A")
50 
51         @JvmStatic
52         @BeforeClass
setupnull53         fun setup() {
54             if (!TestUtils.isHardwareSupported()) {
55                 return
56             }
57             val now = Instant.now().truncatedTo(ChronoUnit.MILLIS)
58             APP_A_WITH_READ_WRITE_PERMS.insertRecords(
59                 StepsRecord.Builder(getEmptyMetadata(), now.minusSeconds(30), now, 43).build())
60         }
61 
62         @JvmStatic
63         @AfterClass
teardownnull64         fun teardown() {
65             if (!TestUtils.isHardwareSupported()) {
66                 return
67             }
68             verifyDeleteRecords(
69                 StepsRecord::class.java,
70                 TimeInstantRangeFilter.Builder()
71                     .setStartTime(Instant.EPOCH)
72                     .setEndTime(Instant.now())
73                     .build())
74         }
75     }
76 
77     @Test
78     @FlakyTest(bugId = 328200136)
homeFragment_openAppPermissionsnull79     fun homeFragment_openAppPermissions() {
80         context.launchMainActivity {
81             clickOnText("App permissions")
82 
83             waitDisplayed(By.text("Allowed access"))
84             waitDisplayed(By.text("Not allowed access"), waitTimeout = Duration.ofSeconds(10))
85         }
86     }
87 
88     @Test
homeFragment_openDataManagementnull89     fun homeFragment_openDataManagement() {
90         context.launchMainActivity {
91             clickOnText("Data and access")
92 
93             waitDisplayed(By.text("Browse data"))
94             waitDisplayed(By.text("Manage data"))
95 
96             waitDisplayed(By.text("Delete all data"))
97         }
98     }
99 
100     @Test
homeFragment_openManageDatanull101     fun homeFragment_openManageData() {
102         context.launchMainActivity {
103             clickOnText("Manage data")
104 
105             waitDisplayed(By.text("Auto-delete"))
106             waitDisplayed(By.text("Data sources and priority"))
107             waitDisplayed(By.text("Set units"))
108         }
109     }
110 
111     @Test
homeFragment_recentAccessShownOnHomeScreennull112     fun homeFragment_recentAccessShownOnHomeScreen() {
113         context.launchMainActivity {
114             waitDisplayed(By.textContains("CtsHealthConnectTest"))
115             waitDisplayed(By.text("See all recent access"))
116         }
117     }
118 
119     @Test
120     @FlakyTest(bugId = 328200136)
homeFragment_navigateToRecentAccessnull121     fun homeFragment_navigateToRecentAccess() {
122         context.launchMainActivity {
123             clickOnText("See all recent access")
124 
125             waitDisplayed(By.text("Today"))
126             waitDisplayed(By.textContains("CtsHealthConnectTest"))
127         }
128     }
129 }
130