1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.car.apitest; 17 18 import android.car.content.pm.AppBlockingPackageInfo; 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.content.pm.Signature; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.os.Parcel; 24 import android.test.AndroidTestCase; 25 import android.test.suitebuilder.annotation.SmallTest; 26 import android.util.Log; 27 28 @SmallTest 29 public class AppBlockingPackageInfoTest extends AndroidTestCase { 30 private static final String TAG = AppBlockingPackageInfoTest.class.getSimpleName(); 31 32 public void testParcellingSystemInfo() throws Exception { 33 AppBlockingPackageInfo carServiceInfo = createInfoCarService(getContext()); 34 Parcel dest = Parcel.obtain(); 35 carServiceInfo.writeToParcel(dest, 0); 36 dest.setDataPosition(0); 37 AppBlockingPackageInfo carServiceInfoRead = new AppBlockingPackageInfo(dest); 38 Log.i(TAG, "expected:" + carServiceInfo + ",read:" + carServiceInfoRead); 39 assertEquals(carServiceInfo, carServiceInfoRead); 40 } 41 42 public void testParcellingNonSystemInfo() throws Exception { 43 AppBlockingPackageInfo selfInfo = createInfoSelf(getContext()); 44 Parcel dest = Parcel.obtain(); 45 selfInfo.writeToParcel(dest, 0); 46 dest.setDataPosition(0); 47 AppBlockingPackageInfo selfInfoRead = new AppBlockingPackageInfo(dest); 48 Log.i(TAG, "expected:" + selfInfo + ",read:" + selfInfoRead); 49 assertEquals(selfInfo, selfInfoRead); 50 } 51 52 public static AppBlockingPackageInfo createInfoCarService(Context context) { 53 final String packageName = "android.car"; 54 return new AppBlockingPackageInfo(packageName, 0, 0, AppBlockingPackageInfo.FLAG_SYSTEM_APP, 55 null, null); 56 } 57 58 public static final AppBlockingPackageInfo createInfoSelf(Context context) { 59 final String packageName = "android.car.apitest"; 60 PackageManager pm = context.getPackageManager(); 61 Signature[] signatures; 62 try { 63 signatures = pm.getPackageInfo(packageName, 64 PackageManager.GET_SIGNATURES).signatures; 65 } catch (NameNotFoundException e) { 66 return null; 67 } 68 String[] activties = new String[] { "Hello", "World" }; 69 return new AppBlockingPackageInfo(packageName, 0, 100, 0, signatures, activties); 70 } 71 } 72