1 /* 2 * 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 package com.android.server.healthconnect; 18 19 import static com.android.server.healthconnect.backuprestore.BackupRestore.BackupRestoreJobService.BACKUP_RESTORE_JOBS_NAMESPACE; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.anyString; 24 import static org.mockito.Mockito.eq; 25 import static org.mockito.Mockito.timeout; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.app.job.JobScheduler; 31 import android.content.Context; 32 import android.content.pm.PackageInfo; 33 import android.content.pm.PackageManager; 34 import android.content.pm.PermissionGroupInfo; 35 import android.content.pm.PermissionInfo; 36 import android.os.UserHandle; 37 import android.os.UserManager; 38 import android.permission.PermissionManager; 39 40 import androidx.test.ext.junit.runners.AndroidJUnit4; 41 import androidx.test.platform.app.InstrumentationRegistry; 42 43 import com.android.modules.utils.testing.ExtendedMockitoRule; 44 import com.android.server.SystemService; 45 import com.android.server.appop.AppOpsManagerLocal; 46 import com.android.server.healthconnect.injector.HealthConnectInjector; 47 import com.android.server.healthconnect.migration.MigrationStateChangeJob; 48 import com.android.server.healthconnect.testing.fixtures.EnvironmentFixture; 49 import com.android.server.healthconnect.testing.fixtures.SQLiteDatabaseFixture; 50 51 import com.google.common.truth.Truth; 52 53 import org.junit.Before; 54 import org.junit.Rule; 55 import org.junit.Test; 56 import org.junit.runner.RunWith; 57 import org.mockito.Mock; 58 import org.mockito.quality.Strictness; 59 60 @RunWith(AndroidJUnit4.class) 61 public class HealthConnectManagerServiceTest { 62 63 private static final String HEALTH_CONNECT_DAILY_JOB_NAMESPACE = "HEALTH_CONNECT_DAILY_JOB"; 64 private static final String HEALTH_CONNECT_IMPORT_EXPORT_JOBS_NAMESPACE = 65 "HEALTH_CONNECT_IMPORT_EXPORT_JOBS"; 66 private static final String ANDROID_SERVER_PACKAGE_NAME = "com.android.server"; 67 68 @Rule 69 public final ExtendedMockitoRule mExtendedMockitoRule = 70 new ExtendedMockitoRule.Builder(this) 71 .addStaticMockFixtures(EnvironmentFixture::new, SQLiteDatabaseFixture::new) 72 .setStrictness(Strictness.LENIENT) 73 .build(); 74 75 @Mock Context mContext; 76 @Mock private SystemService.TargetUser mMockTargetUser; 77 @Mock private JobScheduler mMainJobScheduler; 78 @Mock private JobScheduler mDailyJobScheduler; 79 @Mock private JobScheduler mImportExportJobScheduler; 80 @Mock private JobScheduler mMigrationJobScheduler; 81 @Mock private JobScheduler mBackupRestoreJobScheduler; 82 @Mock private UserManager mUserManager; 83 @Mock private PackageManager mPackageManager; 84 @Mock private PermissionManager mPermissionManager; 85 @Mock private AppOpsManagerLocal mAppOpsManagerLocal; 86 private HealthConnectManagerService mHealthConnectManagerService; 87 88 @Before setUp()89 public void setUp() throws PackageManager.NameNotFoundException { 90 HealthConnectInjector.resetInstanceForTest(); 91 when(mMainJobScheduler.forNamespace(HEALTH_CONNECT_DAILY_JOB_NAMESPACE)) 92 .thenReturn(mDailyJobScheduler); 93 when(mMainJobScheduler.forNamespace(MigrationStateChangeJob.class.toString())) 94 .thenReturn(mMigrationJobScheduler); 95 when(mMainJobScheduler.forNamespace(HEALTH_CONNECT_IMPORT_EXPORT_JOBS_NAMESPACE)) 96 .thenReturn(mImportExportJobScheduler); 97 when(mMainJobScheduler.forNamespace(BACKUP_RESTORE_JOBS_NAMESPACE)) 98 .thenReturn(mBackupRestoreJobScheduler); 99 PermissionGroupInfo permissionGroupInfo = new PermissionGroupInfo(); 100 permissionGroupInfo.packageName = "test"; 101 PackageInfo mockPackageInfo = new PackageInfo(); 102 mockPackageInfo.permissions = new PermissionInfo[1]; 103 mockPackageInfo.permissions[0] = new PermissionInfo(); 104 105 when(mPackageManager.getPermissionGroupInfo( 106 eq(android.health.connect.HealthPermissions.HEALTH_PERMISSION_GROUP), 107 eq(0))) 108 .thenThrow(new PackageManager.NameNotFoundException()); 109 when(mPackageManager.getPackageInfo( 110 anyString(), 111 eq(PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS)))) 112 .thenThrow(new PackageManager.NameNotFoundException()); 113 when(mContext.getSystemService(JobScheduler.class)).thenReturn(mMainJobScheduler); 114 when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); 115 when(mContext.getSystemService(PackageManager.class)).thenReturn(mPackageManager); 116 when(mContext.getPackageManager()).thenReturn(mPackageManager); 117 when(mContext.getSystemService(PermissionManager.class)).thenReturn(mPermissionManager); 118 when(mContext.getSystemService(AppOpsManagerLocal.class)).thenReturn(mAppOpsManagerLocal); 119 when(mContext.getUser()).thenReturn(UserHandle.CURRENT); 120 when(mContext.getPackageName()).thenReturn(ANDROID_SERVER_PACKAGE_NAME); 121 when(mContext.getDatabasePath(anyString())) 122 .thenReturn( 123 InstrumentationRegistry.getInstrumentation() 124 .getContext() 125 .getDatabasePath("mock")); 126 when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext); 127 when(mMockTargetUser.getUserHandle()).thenReturn(UserHandle.CURRENT); 128 when(mContext.getApplicationContext()).thenReturn(mContext); 129 mHealthConnectManagerService = new HealthConnectManagerService(mContext); 130 } 131 132 @Test testCreateService()133 public void testCreateService() { 134 Truth.assertThat(mHealthConnectManagerService).isNotNull(); 135 } 136 137 @Test testUserSupport()138 public void testUserSupport() { 139 when(mUserManager.isProfile()).thenReturn(true); 140 Truth.assertThat(mHealthConnectManagerService.isUserSupported(mMockTargetUser)).isFalse(); 141 when(mUserManager.isProfile()).thenReturn(false); 142 Truth.assertThat(mHealthConnectManagerService.isUserSupported(mMockTargetUser)).isTrue(); 143 } 144 145 @Test testUserSwitch_userLocked()146 public void testUserSwitch_userLocked() { 147 when(mUserManager.isUserUnlocked(any())).thenReturn(false); 148 mHealthConnectManagerService.onUserSwitching(mMockTargetUser, mMockTargetUser); 149 verify(mDailyJobScheduler, times(1)).cancelAll(); 150 verify(mMigrationJobScheduler, times(1)).cancelAll(); 151 } 152 153 @Test testUserSwitch_userUnlocked()154 public void testUserSwitch_userUnlocked() { 155 when(mUserManager.isUserUnlocked(any())).thenReturn(true); 156 mHealthConnectManagerService.onUserSwitching(mMockTargetUser, mMockTargetUser); 157 verify(mDailyJobScheduler, times(1)).cancelAll(); 158 verify(mDailyJobScheduler, timeout(5000).times(1)).schedule(any()); 159 verify(mBackupRestoreJobScheduler, times(1)).cancelAll(); 160 } 161 } 162