Home | History | Annotate | Download | only in server
      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 
     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;
     21 import android.support.test.InstrumentationRegistry;
     22 import android.testing.TestableContext;
     23 
     24 import org.junit.Before;
     25 import org.junit.Rule;
     26 import org.mockito.Mock;
     27 import org.mockito.MockitoAnnotations;
     28 
     29 public class UiServiceTestCase {
     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";
     34     protected static final String PKG_P = "com.example.p";
     35 
     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() {
     46         MockitoAnnotations.initMocks(this);
     47 
     48         // Share classloader to allow package access.
     49         System.setProperty("dexmaker.share_classloader", "true");
     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;
     61                         case PKG_P:
     62                             return Build.VERSION_CODES.P;
     63                         default:
     64                             return Build.VERSION_CODES.CUR_DEVELOPMENT;
     65                     }
     66                 });
     67     }
     68 }
     69