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.when;
20 
21 import android.content.pm.PackageManagerInternal;
22 import android.net.Uri;
23 import android.os.Build;
24 import android.testing.TestableContext;
25 
26 import androidx.test.InstrumentationRegistry;
27 
28 import com.android.server.uri.UriGrantsManagerInternal;
29 
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 
35 public class UiServiceTestCase {
36     @Mock protected PackageManagerInternal mPmi;
37     @Mock protected UriGrantsManagerInternal mUgmInternal;
38 
39     protected static final String PKG_N_MR1 = "com.example.n_mr1";
40     protected static final String PKG_O = "com.example.o";
41     protected static final String PKG_P = "com.example.p";
42 
43     @Rule
44     public final TestableContext mContext =
45             new TestableContext(InstrumentationRegistry.getContext(), null);
46 
getContext()47     protected TestableContext getContext() {
48         return mContext;
49     }
50 
51     @Before
setup()52     public final void setup() {
53         MockitoAnnotations.initMocks(this);
54 
55         // Share classloader to allow package access.
56         System.setProperty("dexmaker.share_classloader", "true");
57 
58         // Assume some default packages
59         LocalServices.removeServiceForTest(PackageManagerInternal.class);
60         LocalServices.addService(PackageManagerInternal.class, mPmi);
61         when(mPmi.getPackageTargetSdkVersion(anyString()))
62                 .thenAnswer((iom) -> {
63                     switch ((String) iom.getArgument(0)) {
64                         case PKG_N_MR1:
65                             return Build.VERSION_CODES.N_MR1;
66                         case PKG_O:
67                             return Build.VERSION_CODES.O;
68                         case PKG_P:
69                             return Build.VERSION_CODES.P;
70                         default:
71                             return Build.VERSION_CODES.CUR_DEVELOPMENT;
72                     }
73                 });
74 
75         LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
76         LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
77         when(mUgmInternal.checkGrantUriPermission(
78                 anyInt(), anyString(), any(Uri.class), anyInt(), anyInt())).thenReturn(-1);
79     }
80 }
81