1 /* 2 * Copyright (C) 2021 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.locksettings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.Manifest; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.pm.PackageManager; 31 import android.content.pm.ResolveInfo; 32 import android.content.pm.ServiceInfo; 33 import android.platform.test.annotations.Presubmit; 34 import android.service.resumeonreboot.ResumeOnRebootService; 35 36 import androidx.test.filters.SmallTest; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Captor; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 47 import java.util.ArrayList; 48 49 @SmallTest 50 @Presubmit 51 @RunWith(JUnit4.class) 52 public class ResumeOnRebootServiceProviderTests { 53 54 @Mock 55 Context mMockContext; 56 @Mock 57 PackageManager mMockPackageManager; 58 59 ResolveInfo mFakeResolvedInfo; 60 ServiceInfo mFakeServiceInfo; 61 @Captor 62 ArgumentCaptor<Intent> mIntentArgumentCaptor; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 when(mMockContext.getUserId()).thenReturn(0); 68 69 mFakeServiceInfo = new ServiceInfo(); 70 mFakeServiceInfo.packageName = "fakePackageName"; 71 mFakeServiceInfo.name = "fakeName"; 72 73 mFakeResolvedInfo = new ResolveInfo(); 74 mFakeResolvedInfo.serviceInfo = mFakeServiceInfo; 75 } 76 77 @Test noServiceFound()78 public void noServiceFound() throws Exception { 79 when(mMockPackageManager.queryIntentServices(any(), 80 eq(PackageManager.MATCH_SYSTEM_ONLY))).thenReturn( 81 null); 82 assertThat(new ResumeOnRebootServiceProvider(mMockContext, 83 mMockPackageManager).getServiceConnection()).isNull(); 84 } 85 86 @Test serviceNotGuardedWithPermission()87 public void serviceNotGuardedWithPermission() throws Exception { 88 ArrayList<ResolveInfo> resultList = new ArrayList<>(); 89 mFakeServiceInfo.permission = ""; 90 resultList.add(mFakeResolvedInfo); 91 when(mMockPackageManager.queryIntentServices(any(), anyInt())).thenReturn(resultList); 92 assertThat(new ResumeOnRebootServiceProvider(mMockContext, 93 mMockPackageManager).getServiceConnection()).isNull(); 94 } 95 96 @Test serviceResolved()97 public void serviceResolved() throws Exception { 98 ArrayList<ResolveInfo> resultList = new ArrayList<>(); 99 resultList.add(mFakeResolvedInfo); 100 mFakeServiceInfo.permission = Manifest.permission.BIND_RESUME_ON_REBOOT_SERVICE; 101 when(mMockPackageManager.queryIntentServices(any(), anyInt())).thenReturn(resultList); 102 103 assertThat(new ResumeOnRebootServiceProvider(mMockContext, 104 mMockPackageManager).getServiceConnection()).isNotNull(); 105 106 verify(mMockPackageManager).queryIntentServices(mIntentArgumentCaptor.capture(), 107 eq(PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_SERVICES)); 108 assertThat(mIntentArgumentCaptor.getValue().getAction()).isEqualTo( 109 ResumeOnRebootService.SERVICE_INTERFACE); 110 } 111 } 112