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.After;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 
37 public class UiServiceTestCase {
38     @Mock protected PackageManagerInternal mPmi;
39     @Mock protected UriGrantsManagerInternal mUgmInternal;
40 
41     protected static final String PKG_N_MR1 = "com.example.n_mr1";
42     protected static final String PKG_O = "com.example.o";
43     protected static final String PKG_P = "com.example.p";
44     protected static final String PKG_R = "com.example.r";
45 
46     @Rule
47     public final TestableContext mContext =
48             new TestableContext(InstrumentationRegistry.getContext(), null);
49 
getContext()50     protected TestableContext getContext() {
51         return mContext;
52     }
53 
54     @Before
setup()55     public final void setup() {
56         MockitoAnnotations.initMocks(this);
57 
58         // Share classloader to allow package access.
59         System.setProperty("dexmaker.share_classloader", "true");
60 
61         // Assume some default packages
62         LocalServices.removeServiceForTest(PackageManagerInternal.class);
63         LocalServices.addService(PackageManagerInternal.class, mPmi);
64         when(mPmi.getPackageTargetSdkVersion(anyString()))
65                 .thenAnswer((iom) -> {
66                     switch ((String) iom.getArgument(0)) {
67                         case PKG_N_MR1:
68                             return Build.VERSION_CODES.N_MR1;
69                         case PKG_O:
70                             return Build.VERSION_CODES.O;
71                         case PKG_P:
72                             return Build.VERSION_CODES.P;
73                         case PKG_R:
74                             return Build.VERSION_CODES.R;
75                         default:
76                             return Build.VERSION_CODES.CUR_DEVELOPMENT;
77                     }
78                 });
79 
80         LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
81         LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
82         when(mUgmInternal.checkGrantUriPermission(
83                 anyInt(), anyString(), any(Uri.class), anyInt(), anyInt())).thenReturn(-1);
84     }
85 
86     @After
cleanUpMockito()87     public final void cleanUpMockito() {
88         Mockito.framework().clearInlineMocks();
89     }
90 }
91