• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.content.pm.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.content.pm.ServiceInfo;
27 import android.os.Parcel;
28 import android.platform.test.annotations.AppModeFull;
29 import android.platform.test.annotations.DisabledOnRavenwood;
30 import android.platform.test.ravenwood.RavenwoodRule;
31 import android.util.StringBuilderPrinter;
32 
33 import androidx.test.platform.app.InstrumentationRegistry;
34 import androidx.test.runner.AndroidJUnit4;
35 
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 @RunWith(AndroidJUnit4.class)
41 @AppModeFull // TODO(Instant) Figure out which APIs should work.
42 public class ServiceInfoTest {
43     @Rule
44     public final RavenwoodRule mRavenwood = new RavenwoodRule();
45 
46     private static final String PACKAGE_NAME = "android.content.cts";
47     private static final String SERVICE_NAME = "android.content.pm.cts.TestPmService";
48 
getContext()49     private Context getContext() {
50         return InstrumentationRegistry.getInstrumentation().getTargetContext();
51     }
52 
53     @Test
testSimple()54     public void testSimple() {
55         ServiceInfo info = new ServiceInfo();
56         new ServiceInfo(info);
57         assertNotNull(info.toString());
58         info.dump(new StringBuilderPrinter(new StringBuilder()), "");
59     }
60 
61     @Test
62     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testServiceInfo()63     public void testServiceInfo() throws NameNotFoundException {
64         PackageManager pm = getContext().getPackageManager();
65         ComponentName componentName = new ComponentName(PACKAGE_NAME, SERVICE_NAME);
66         Parcel p = Parcel.obtain();
67 
68         // Test ServiceInfo()
69         new ServiceInfo();
70 
71         ServiceInfo serviceInfo = pm.getServiceInfo(componentName, 0);
72         // Test ServiceInfo(ServiceInfo orig)
73         ServiceInfo infoFromExisted = new ServiceInfo(serviceInfo);
74         checkInfoSame(serviceInfo, infoFromExisted);
75         // Test toString, describeContents
76         assertNotNull(serviceInfo.toString());
77         assertEquals(0, serviceInfo.describeContents());
78 
79         // Test writeToParcel
80         serviceInfo.writeToParcel(p, 0);
81         p.setDataPosition(0);
82         ServiceInfo infoFromParcel = ServiceInfo.CREATOR.createFromParcel(p);
83         checkInfoSame(serviceInfo, infoFromParcel);
84     }
85 
checkInfoSame(ServiceInfo expected, ServiceInfo actual)86     private void checkInfoSame(ServiceInfo expected, ServiceInfo actual) {
87         assertEquals(expected.name, actual.name);
88         assertEquals(expected.permission, actual.permission);
89     }
90 }
91