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