blob: eec852bd77c717fa3f20cfd044996d5569fd952f [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
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060016import static org.mockito.ArgumentMatchers.anyString;
17import static org.mockito.Mockito.when;
18
19import android.content.pm.PackageManagerInternal;
20import android.os.Build;
Jason Monk74f5e362017-12-06 08:56:33 -050021import android.support.test.InstrumentationRegistry;
22import android.testing.TestableContext;
23
24import org.junit.Before;
25import org.junit.Rule;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060026import org.mockito.Mock;
27import org.mockito.MockitoAnnotations;
Jason Monk74f5e362017-12-06 08:56:33 -050028
29public class UiServiceTestCase {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060030 @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 Reynolds89945c52018-06-13 10:45:21 -040034 protected static final String PKG_P = "com.example.p";
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060035
Jason Monk74f5e362017-12-06 08:56:33 -050036 @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 Sharkey6a97cc32018-04-17 12:16:20 -060046 MockitoAnnotations.initMocks(this);
47
Jason Monk74f5e362017-12-06 08:56:33 -050048 // Share classloader to allow package access.
49 System.setProperty("dexmaker.share_classloader", "true");
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060050
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 Reynolds89945c52018-06-13 10:45:21 -040061 case PKG_P:
62 return Build.VERSION_CODES.P;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060063 default:
64 return Build.VERSION_CODES.CUR_DEVELOPMENT;
65 }
66 });
Jason Monk74f5e362017-12-06 08:56:33 -050067 }
68}