blob: 3c2d55058c3efc0d5eae79149034be881ff8ed21 [file] [log] [blame]
Jason Monk74f5e362017-12-06 08:56:33 -05001/*
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 */
14package com.android.server;
15
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070016import static org.mockito.ArgumentMatchers.any;
17import static org.mockito.ArgumentMatchers.anyInt;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060018import static org.mockito.ArgumentMatchers.anyString;
19import static org.mockito.Mockito.when;
20
21import android.content.pm.PackageManagerInternal;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070022import android.net.Uri;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060023import android.os.Build;
Jason Monk74f5e362017-12-06 08:56:33 -050024import android.testing.TestableContext;
25
Brett Chabot84151d92019-02-27 15:37:59 -080026import androidx.test.InstrumentationRegistry;
27
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070028import com.android.server.uri.UriGrantsManagerInternal;
29
Julia Reynoldsb6c83742019-07-30 18:03:40 -040030import org.junit.After;
Jason Monk74f5e362017-12-06 08:56:33 -050031import org.junit.Before;
32import org.junit.Rule;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060033import org.mockito.Mock;
Julia Reynoldsb6c83742019-07-30 18:03:40 -040034import org.mockito.Mockito;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060035import org.mockito.MockitoAnnotations;
Jason Monk74f5e362017-12-06 08:56:33 -050036
37public class UiServiceTestCase {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060038 @Mock protected PackageManagerInternal mPmi;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070039 @Mock protected UriGrantsManagerInternal mUgmInternal;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060040
41 protected static final String PKG_N_MR1 = "com.example.n_mr1";
42 protected static final String PKG_O = "com.example.o";
Julia Reynolds218871e2018-06-13 10:45:21 -040043 protected static final String PKG_P = "com.example.p";
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060044
Jason Monk74f5e362017-12-06 08:56:33 -050045 @Rule
46 public final TestableContext mContext =
47 new TestableContext(InstrumentationRegistry.getContext(), null);
48
49 protected TestableContext getContext() {
50 return mContext;
51 }
52
53 @Before
Sunny Goyal12776ab2019-06-18 14:54:21 -070054 public final void setup() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060055 MockitoAnnotations.initMocks(this);
56
Jason Monk74f5e362017-12-06 08:56:33 -050057 // Share classloader to allow package access.
58 System.setProperty("dexmaker.share_classloader", "true");
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060059
60 // Assume some default packages
61 LocalServices.removeServiceForTest(PackageManagerInternal.class);
62 LocalServices.addService(PackageManagerInternal.class, mPmi);
63 when(mPmi.getPackageTargetSdkVersion(anyString()))
64 .thenAnswer((iom) -> {
65 switch ((String) iom.getArgument(0)) {
66 case PKG_N_MR1:
67 return Build.VERSION_CODES.N_MR1;
68 case PKG_O:
69 return Build.VERSION_CODES.O;
Julia Reynolds218871e2018-06-13 10:45:21 -040070 case PKG_P:
71 return Build.VERSION_CODES.P;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060072 default:
73 return Build.VERSION_CODES.CUR_DEVELOPMENT;
74 }
75 });
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070076
77 LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
78 LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
79 when(mUgmInternal.checkGrantUriPermission(
80 anyInt(), anyString(), any(Uri.class), anyInt(), anyInt())).thenReturn(-1);
Jason Monk74f5e362017-12-06 08:56:33 -050081 }
Julia Reynoldsb6c83742019-07-30 18:03:40 -040082
83 @After
84 public final void cleanUpMockito() {
85 Mockito.framework().clearInlineMocks();
86 }
Jason Monk74f5e362017-12-06 08:56:33 -050087}