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 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 16 | import static org.mockito.ArgumentMatchers.anyString; |
| 17 | import static org.mockito.Mockito.when; |
| 18 | |
| 19 | import android.content.pm.PackageManagerInternal; |
| 20 | import android.os.Build; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 21 | import android.support.test.InstrumentationRegistry; |
| 22 | import android.testing.TestableContext; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Rule; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 26 | import org.mockito.Mock; |
| 27 | import org.mockito.MockitoAnnotations; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 28 | |
| 29 | public class UiServiceTestCase { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 30 | @Mock protected PackageManagerInternal mPmi; |
| 31 | |
| 32 | protected static final String PKG_N_MR1 = "com.example.n_mr1"; |
| 33 | protected static final String PKG_O = "com.example.o"; |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 34 | protected static final String PKG_P = "com.example.p"; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 35 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 36 | @Rule |
| 37 | public final TestableContext mContext = |
| 38 | new TestableContext(InstrumentationRegistry.getContext(), null); |
| 39 | |
| 40 | protected TestableContext getContext() { |
| 41 | return mContext; |
| 42 | } |
| 43 | |
| 44 | @Before |
| 45 | public void setup() { |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 46 | MockitoAnnotations.initMocks(this); |
| 47 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 48 | // Share classloader to allow package access. |
| 49 | System.setProperty("dexmaker.share_classloader", "true"); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 50 | |
| 51 | // Assume some default packages |
| 52 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
| 53 | LocalServices.addService(PackageManagerInternal.class, mPmi); |
| 54 | when(mPmi.getPackageTargetSdkVersion(anyString())) |
| 55 | .thenAnswer((iom) -> { |
| 56 | switch ((String) iom.getArgument(0)) { |
| 57 | case PKG_N_MR1: |
| 58 | return Build.VERSION_CODES.N_MR1; |
| 59 | case PKG_O: |
| 60 | return Build.VERSION_CODES.O; |
Julia Reynolds | 89945c5 | 2018-06-13 10:45:21 -0400 | [diff] [blame] | 61 | case PKG_P: |
| 62 | return Build.VERSION_CODES.P; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 63 | default: |
| 64 | return Build.VERSION_CODES.CUR_DEVELOPMENT; |
| 65 | } |
| 66 | }); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 67 | } |
| 68 | } |