blob: 345c1d7a95fe3b74dcfe2993197a82474c6fdce5 [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";
34
Jason Monk74f5e362017-12-06 08:56:33 -050035 @Rule
36 public final TestableContext mContext =
37 new TestableContext(InstrumentationRegistry.getContext(), null);
38
39 protected TestableContext getContext() {
40 return mContext;
41 }
42
43 @Before
44 public void setup() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060045 MockitoAnnotations.initMocks(this);
46
Jason Monk74f5e362017-12-06 08:56:33 -050047 // Share classloader to allow package access.
48 System.setProperty("dexmaker.share_classloader", "true");
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060049
50 // Assume some default packages
51 LocalServices.removeServiceForTest(PackageManagerInternal.class);
52 LocalServices.addService(PackageManagerInternal.class, mPmi);
53 when(mPmi.getPackageTargetSdkVersion(anyString()))
54 .thenAnswer((iom) -> {
55 switch ((String) iom.getArgument(0)) {
56 case PKG_N_MR1:
57 return Build.VERSION_CODES.N_MR1;
58 case PKG_O:
59 return Build.VERSION_CODES.O;
60 default:
61 return Build.VERSION_CODES.CUR_DEVELOPMENT;
62 }
63 });
Jason Monk74f5e362017-12-06 08:56:33 -050064 }
65}