Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 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 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 16 | import static org.mockito.ArgumentMatchers.any; |
| 17 | import static org.mockito.ArgumentMatchers.anyInt; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 18 | import static org.mockito.ArgumentMatchers.anyString; |
| 19 | import static org.mockito.Mockito.when; |
| 20 | |
| 21 | import android.content.pm.PackageManagerInternal; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 22 | import android.net.Uri; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 23 | import android.os.Build; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 24 | import android.testing.TestableContext; |
| 25 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 26 | import androidx.test.InstrumentationRegistry; |
| 27 | |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 28 | import com.android.server.uri.UriGrantsManagerInternal; |
| 29 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 30 | import org.junit.Before; |
| 31 | import org.junit.Rule; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 32 | import org.mockito.Mock; |
| 33 | import org.mockito.MockitoAnnotations; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 34 | |
| 35 | public class UiServiceTestCase { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 36 | @Mock protected PackageManagerInternal mPmi; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 37 | @Mock protected UriGrantsManagerInternal mUgmInternal; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 38 | |
| 39 | protected static final String PKG_N_MR1 = "com.example.n_mr1"; |
| 40 | protected static final String PKG_O = "com.example.o"; |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 41 | protected static final String PKG_P = "com.example.p"; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 42 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 43 | @Rule |
| 44 | public final TestableContext mContext = |
| 45 | new TestableContext(InstrumentationRegistry.getContext(), null); |
| 46 | |
| 47 | protected TestableContext getContext() { |
| 48 | return mContext; |
| 49 | } |
| 50 | |
| 51 | @Before |
Sunny Goyal | 12776ab | 2019-06-18 14:54:21 -0700 | [diff] [blame^] | 52 | public final void setup() { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 53 | MockitoAnnotations.initMocks(this); |
| 54 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 55 | // Share classloader to allow package access. |
| 56 | System.setProperty("dexmaker.share_classloader", "true"); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 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; |
Julia Reynolds | 218871e | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 68 | case PKG_P: |
| 69 | return Build.VERSION_CODES.P; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 70 | default: |
| 71 | return Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 72 | } |
| 73 | }); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 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); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 79 | } |
| 80 | } |