blob: 56d3a20df9a7cdf1aba7b348a0e5baaec2664126 [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;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070019import static org.mockito.Mockito.doNothing;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060020import static org.mockito.Mockito.when;
21
22import android.content.pm.PackageManagerInternal;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070023import android.net.Uri;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060024import android.os.Build;
Jason Monk74f5e362017-12-06 08:56:33 -050025import android.support.test.InstrumentationRegistry;
26import android.testing.TestableContext;
27
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070028import com.android.server.uri.UriGrantsManagerInternal;
29
Jason Monk74f5e362017-12-06 08:56:33 -050030import org.junit.Before;
31import org.junit.Rule;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060032import org.mockito.Mock;
33import org.mockito.MockitoAnnotations;
Jason Monk74f5e362017-12-06 08:56:33 -050034
35public class UiServiceTestCase {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060036 @Mock protected PackageManagerInternal mPmi;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070037 @Mock protected UriGrantsManagerInternal mUgmInternal;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060038
39 protected static final String PKG_N_MR1 = "com.example.n_mr1";
40 protected static final String PKG_O = "com.example.o";
Julia Reynolds218871e2018-06-13 10:45:21 -040041 protected static final String PKG_P = "com.example.p";
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060042
Jason Monk74f5e362017-12-06 08:56:33 -050043 @Rule
44 public final TestableContext mContext =
45 new TestableContext(InstrumentationRegistry.getContext(), null);
46
47 protected TestableContext getContext() {
48 return mContext;
49 }
50
51 @Before
52 public void setup() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060053 MockitoAnnotations.initMocks(this);
54
Jason Monk74f5e362017-12-06 08:56:33 -050055 // Share classloader to allow package access.
56 System.setProperty("dexmaker.share_classloader", "true");
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060057
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;
Julia Reynolds218871e2018-06-13 10:45:21 -040068 case PKG_P:
69 return Build.VERSION_CODES.P;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060070 default:
71 return Build.VERSION_CODES.CUR_DEVELOPMENT;
72 }
73 });
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070074
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);
Jason Monk74f5e362017-12-06 08:56:33 -050079 }
80}