• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.test.ApiCheckerRule.Builder;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.os.Build;
30 import android.test.suitebuilder.annotation.MediumTest;
31 import android.util.Log;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 
36 @MediumTest
37 public class CarPackageManagerTest extends CarApiTestBase {
38 
39     private static final String TAG = CarPackageManagerTest.class.getSimpleName();
40 
41     private CarPackageManager mCarPackageManager;
42 
43     // TODO(b/242350638): add missing annotations, remove (on child bug of 242350638)
44     @Override
configApiCheckerRule(Builder builder)45     protected void configApiCheckerRule(Builder builder) {
46         Log.w(TAG, "Disabling API requirements check");
47         builder.disableAnnotationsCheck();
48     }
49 
50     @Before
setFixtures()51     public void setFixtures() {
52         mCarPackageManager = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE);
53     }
54 
55     @Test
testCreate()56     public void testCreate() throws Exception {
57         assertThat(mCarPackageManager).isNotNull();
58     }
59 
60     @Test
testGetTargetCarVersion_self()61     public void testGetTargetCarVersion_self() throws Exception {
62         CarVersion apiVersion = mCarPackageManager.getTargetCarVersion();
63 
64         assertWithMessage("getTargetCarVersion()").that(apiVersion).isNotNull();
65         assertWithMessage("major version")
66                 .that(apiVersion.getMajorVersion())
67                 .isEqualTo(108);
68         assertWithMessage("minor version for")
69                 .that(apiVersion.getMinorVersion())
70                 .isEqualTo(42);
71     }
72 
73     @Test
testgetTargetCarVersion_noPackage()74     public void testgetTargetCarVersion_noPackage() throws Exception {
75         String pkg = "I can't believe a package with this name exist. If so, well, too bad!";
76 
77         NameNotFoundException e = assertThrows(NameNotFoundException.class,
78                 () -> mCarPackageManager.getTargetCarVersion(pkg));
79 
80         assertWithMessage("exception msg").that(e.getMessage()).contains(pkg);
81     }
82 
83     @Test
testGetTargetCarMajorAndMinorVersion_notSet()84     public void testGetTargetCarMajorAndMinorVersion_notSet() throws Exception {
85         String pkg = "com.android.car";
86         // TODO(b/228506662): it would be better add another app that explicitly sets sdkTarget
87         // version instead, so it doesn't depend on com.android.car's version (which this test
88         // doesn't control)
89         int expectedMajor = Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
90         CarVersion apiVersion = mCarPackageManager.getTargetCarVersion(pkg);
91 
92         assertWithMessage("getTargetCarVersion(%s)", pkg).that(apiVersion).isNotNull();
93         assertWithMessage("major version for %s", pkg)
94                 .that(apiVersion.getMajorVersion())
95                 .isEqualTo(expectedMajor);
96         assertWithMessage("minor version for %s", pkg)
97                 .that(apiVersion.getMinorVersion())
98                 .isEqualTo(0);
99     }
100 
101     @Test
testGetTargetCarMajorAndMinorVersion_set()102     public void testGetTargetCarMajorAndMinorVersion_set() throws Exception {
103         String pkg = sContext.getPackageName();
104 
105         CarVersion apiVersion = mCarPackageManager.getTargetCarVersion(pkg);
106 
107         assertWithMessage("getTargetCarVersion(%s)", pkg).that(apiVersion).isNotNull();
108         assertWithMessage("major version for %s", pkg)
109                 .that(apiVersion.getMajorVersion())
110                 .isEqualTo(108);
111         assertWithMessage("minor version for %s", pkg)
112                 .that(apiVersion.getMinorVersion())
113                 .isEqualTo(42);
114     }
115 }
116