2 * Copyright (C) 2017 The Android Open Source Project
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
14 package com.android.server;
16 import static org.mockito.ArgumentMatchers.anyString;
17 import static org.mockito.Mockito.when;
19 import android.content.pm.PackageManagerInternal;
20 import android.os.Build;
21 import android.support.test.InstrumentationRegistry;
22 import android.testing.TestableContext;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
29 public class UiServiceTestCase {
30 @Mock protected PackageManagerInternal mPmi;
32 protected static final String PKG_N_MR1 = "com.example.n_mr1";
33 protected static final String PKG_O = "com.example.o";
34 protected static final String PKG_P = "com.example.p";
37 public final TestableContext mContext =
38 new TestableContext(InstrumentationRegistry.getContext(), null);
40 protected TestableContext getContext() {
46 MockitoAnnotations.initMocks(this);
48 // Share classloader to allow package access.
49 System.setProperty("dexmaker.share_classloader", "true");
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)) {
58 return Build.VERSION_CODES.N_MR1;
60 return Build.VERSION_CODES.O;
62 return Build.VERSION_CODES.P;
64 return Build.VERSION_CODES.CUR_DEVELOPMENT;