1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.server; 15 16 import static org.mockito.ArgumentMatchers.any; 17 import static org.mockito.ArgumentMatchers.anyInt; 18 import static org.mockito.ArgumentMatchers.anyString; 19 import static org.mockito.Mockito.spy; 20 import static org.mockito.Mockito.when; 21 22 import android.annotation.UserIdInt; 23 import android.content.Intent; 24 import android.content.pm.PackageManagerInternal; 25 import android.net.Uri; 26 import android.os.Binder; 27 import android.os.Build; 28 import android.os.UserHandle; 29 import android.testing.TestableContext; 30 31 import androidx.test.InstrumentationRegistry; 32 33 import com.android.server.pm.UserManagerInternal; 34 import com.android.server.uri.UriGrantsManagerInternal; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.mockito.Mock; 40 import org.mockito.Mockito; 41 import org.mockito.MockitoAnnotations; 42 43 public class UiServiceTestCase { 44 @Mock protected PackageManagerInternal mPmi; 45 @Mock protected UserManagerInternal mUmi; 46 @Mock protected UriGrantsManagerInternal mUgmInternal; 47 48 protected static final String PKG_N_MR1 = "com.example.n_mr1"; 49 protected static final String PKG_O = "com.example.o"; 50 protected static final String PKG_P = "com.example.p"; 51 protected static final String PKG_R = "com.example.r"; 52 53 protected static final int UID_N_MR1 = 10001; 54 protected static final int UID_O = 10002; 55 protected static final int UID_P = 10003; 56 protected static final int UID_R = 10004; 57 58 @Rule 59 public TestableContext mContext = 60 spy(new TestableContext(InstrumentationRegistry.getContext(), null)); 61 62 protected final int mUid = Binder.getCallingUid(); 63 protected final @UserIdInt int mUserId = UserHandle.getUserId(mUid); 64 protected final UserHandle mUser = UserHandle.of(mUserId); 65 protected final String mPkg = mContext.getPackageName(); 66 getContext()67 protected TestableContext getContext() { 68 return mContext; 69 } 70 71 @Before setup()72 public final void setup() { 73 MockitoAnnotations.initMocks(this); 74 75 // Share classloader to allow package access. 76 System.setProperty("dexmaker.share_classloader", "true"); 77 78 // Assume some default packages 79 LocalServices.removeServiceForTest(PackageManagerInternal.class); 80 LocalServices.addService(PackageManagerInternal.class, mPmi); 81 when(mPmi.getPackageTargetSdkVersion(anyString())) 82 .thenAnswer((iom) -> { 83 switch ((String) iom.getArgument(0)) { 84 case PKG_N_MR1: 85 return Build.VERSION_CODES.N_MR1; 86 case PKG_O: 87 return Build.VERSION_CODES.O; 88 case PKG_P: 89 return Build.VERSION_CODES.P; 90 case PKG_R: 91 return Build.VERSION_CODES.R; 92 default: 93 return Build.VERSION_CODES.CUR_DEVELOPMENT; 94 } 95 }); 96 97 LocalServices.removeServiceForTest(UserManagerInternal.class); 98 LocalServices.addService(UserManagerInternal.class, mUmi); 99 LocalServices.removeServiceForTest(UriGrantsManagerInternal.class); 100 LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal); 101 when(mUgmInternal.checkGrantUriPermission( 102 anyInt(), anyString(), any(Uri.class), anyInt(), anyInt())).thenReturn(-1); 103 104 Mockito.doReturn(new Intent()).when(mContext).registerReceiverAsUser( 105 any(), any(), any(), any(), any()); 106 Mockito.doReturn(new Intent()).when(mContext).registerReceiver(any(), any()); 107 Mockito.doReturn(new Intent()).when(mContext).registerReceiver(any(), any(), anyInt()); 108 Mockito.doNothing().when(mContext).unregisterReceiver(any()); 109 } 110 111 @After cleanUpMockito()112 public final void cleanUpMockito() { 113 Mockito.framework().clearInlineMocks(); 114 } 115 } 116