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.uri.UriGrantsManagerInternal;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 
42 public class UiServiceTestCase {
43     @Mock protected PackageManagerInternal mPmi;
44     @Mock protected UriGrantsManagerInternal mUgmInternal;
45 
46     protected static final String PKG_N_MR1 = "com.example.n_mr1";
47     protected static final String PKG_O = "com.example.o";
48     protected static final String PKG_P = "com.example.p";
49     protected static final String PKG_R = "com.example.r";
50 
51     protected static final int UID_N_MR1 = 10001;
52     protected static final int UID_O = 10002;
53     protected static final int UID_P = 10003;
54     protected static final int UID_R = 10004;
55 
56     @Rule
57     public TestableContext mContext =
58             spy(new TestableContext(InstrumentationRegistry.getContext(), null));
59 
60     protected final int mUid = Binder.getCallingUid();
61     protected final @UserIdInt int mUserId = UserHandle.getUserId(mUid);
62     protected final UserHandle mUser = UserHandle.of(mUserId);
63     protected final String mPkg = mContext.getPackageName();
64 
getContext()65     protected TestableContext getContext() {
66         return mContext;
67     }
68 
69     @Before
setup()70     public final void setup() {
71         MockitoAnnotations.initMocks(this);
72 
73         // Share classloader to allow package access.
74         System.setProperty("dexmaker.share_classloader", "true");
75 
76         // Assume some default packages
77         LocalServices.removeServiceForTest(PackageManagerInternal.class);
78         LocalServices.addService(PackageManagerInternal.class, mPmi);
79         when(mPmi.getPackageTargetSdkVersion(anyString()))
80                 .thenAnswer((iom) -> {
81                     switch ((String) iom.getArgument(0)) {
82                         case PKG_N_MR1:
83                             return Build.VERSION_CODES.N_MR1;
84                         case PKG_O:
85                             return Build.VERSION_CODES.O;
86                         case PKG_P:
87                             return Build.VERSION_CODES.P;
88                         case PKG_R:
89                             return Build.VERSION_CODES.R;
90                         default:
91                             return Build.VERSION_CODES.CUR_DEVELOPMENT;
92                     }
93                 });
94 
95         LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
96         LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
97         when(mUgmInternal.checkGrantUriPermission(
98                 anyInt(), anyString(), any(Uri.class), anyInt(), anyInt())).thenReturn(-1);
99 
100         Mockito.doReturn(new Intent()).when(mContext).registerReceiverAsUser(
101                 any(), any(), any(), any(), any());
102         Mockito.doReturn(new Intent()).when(mContext).registerReceiver(any(), any());
103         Mockito.doReturn(new Intent()).when(mContext).registerReceiver(any(), any(), anyInt());
104         Mockito.doNothing().when(mContext).unregisterReceiver(any());
105     }
106 
107     @After
cleanUpMockito()108     public final void cleanUpMockito() {
109         Mockito.framework().clearInlineMocks();
110     }
111 }
112