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 17 package android.car.apitest; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import static org.junit.Assert.assertThrows; 23 24 import android.car.Car; 25 import android.car.CarVersion; 26 import android.car.content.pm.CarPackageManager; 27 import android.content.pm.PackageManager.NameNotFoundException; 28 29 import androidx.test.filters.MediumTest; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 34 @MediumTest 35 public class CarPackageManagerTest extends CarApiTestBase { 36 37 private static final String TAG = CarPackageManagerTest.class.getSimpleName(); 38 39 private CarPackageManager mCarPackageManager; 40 41 @Before setFixtures()42 public void setFixtures() { 43 mCarPackageManager = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE); 44 } 45 46 @Test testCreate()47 public void testCreate() throws Exception { 48 assertThat(mCarPackageManager).isNotNull(); 49 } 50 51 @Test testGetTargetCarVersion_self()52 public void testGetTargetCarVersion_self() throws Exception { 53 CarVersion apiVersion = mCarPackageManager.getTargetCarVersion(); 54 55 assertWithMessage("getTargetCarVersion()").that(apiVersion).isNotNull(); 56 assertWithMessage("major version") 57 .that(apiVersion.getMajorVersion()) 58 .isEqualTo(108); 59 assertWithMessage("minor version for") 60 .that(apiVersion.getMinorVersion()) 61 .isEqualTo(42); 62 } 63 64 @Test testgetTargetCarVersion_noPackage()65 public void testgetTargetCarVersion_noPackage() throws Exception { 66 String pkg = "I can't believe a package with this name exist. If so, well, too bad!"; 67 68 NameNotFoundException e = assertThrows(NameNotFoundException.class, 69 () -> mCarPackageManager.getTargetCarVersion(pkg)); 70 71 assertWithMessage("exception msg").that(e.getMessage()).contains(pkg); 72 } 73 74 @Test testGetTargetCarMajorAndMinorVersion_set()75 public void testGetTargetCarMajorAndMinorVersion_set() throws Exception { 76 String pkg = getContext().getPackageName(); 77 78 CarVersion apiVersion = mCarPackageManager.getTargetCarVersion(pkg); 79 80 assertWithMessage("getTargetCarVersion(%s)", pkg).that(apiVersion).isNotNull(); 81 assertWithMessage("major version for %s", pkg) 82 .that(apiVersion.getMajorVersion()) 83 .isEqualTo(108); 84 assertWithMessage("minor version for %s", pkg) 85 .that(apiVersion.getMinorVersion()) 86 .isEqualTo(42); 87 } 88 } 89