Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2017 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 com.android.server.pm;
     18 
     19 import android.content.IIntentReceiver;
     20 import android.os.Bundle;
     21 import android.support.test.runner.AndroidJUnit4;
     22 
     23 import org.junit.After;
     24 import org.junit.Assert;
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 
     29 import java.io.File;
     30 
     31 // runtest -c com.android.server.pm.PackageManagerServiceTest frameworks-services
     32 // bit FrameworksServicesTests:com.android.server.pm.PackageManagerServiceTest
     33 @RunWith(AndroidJUnit4.class)
     34 public class PackageManagerServiceTest {
     35     @Before
     36     public void setUp() throws Exception {
     37     }
     38 
     39     @After
     40     public void tearDown() throws Exception {
     41     }
     42 
     43     @Test
     44     public void testPackageRemoval() throws Exception {
     45         class PackageSenderImpl implements PackageSender {
     46             public void sendPackageBroadcast(final String action, final String pkg,
     47                     final Bundle extras, final int flags, final String targetPkg,
     48                     final IIntentReceiver finishedReceiver, final int[] userIds,
     49                     int[] instantUserIds) {
     50             }
     51 
     52             public void sendPackageAddedForNewUsers(String packageName,
     53                     boolean sendBootComplete, boolean includeStopped, int appId,
     54                     int[] userIds, int[] instantUserIds) {
     55             }
     56 
     57             @Override
     58             public void notifyPackageAdded(String packageName) {
     59             }
     60 
     61             @Override
     62             public void notifyPackageRemoved(String packageName) {
     63             }
     64         }
     65 
     66         PackageSenderImpl sender = new PackageSenderImpl();
     67         PackageSetting setting = null;
     68         PackageManagerService.PackageRemovedInfo pri =
     69                 new PackageManagerService.PackageRemovedInfo(sender);
     70 
     71         // Initial conditions: nothing there
     72         Assert.assertNull(pri.removedUsers);
     73         Assert.assertNull(pri.broadcastUsers);
     74 
     75         // populateUsers with nothing leaves nothing
     76         pri.populateUsers(null, setting);
     77         Assert.assertNull(pri.broadcastUsers);
     78 
     79         // Create a real (non-null) PackageSetting and confirm that the removed
     80         // users are copied properly
     81         setting = new PackageSetting("name", "realName", new File("codePath"),
     82                 new File("resourcePath"), "legacyNativeLibraryPathString",
     83                 "primaryCpuAbiString", "secondaryCpuAbiString",
     84                 "cpuAbiOverrideString", 0, 0, 0, "parentPackageName", null, 0,
     85                 null, null);
     86         pri.populateUsers(new int[] {
     87                 1, 2, 3, 4, 5
     88         }, setting);
     89         Assert.assertNotNull(pri.broadcastUsers);
     90         Assert.assertEquals(5, pri.broadcastUsers.length);
     91         Assert.assertNotNull(pri.instantUserIds);
     92         Assert.assertEquals(0, pri.instantUserIds.length);
     93 
     94         // Exclude a user
     95         pri.broadcastUsers = null;
     96         final int EXCLUDED_USER_ID = 4;
     97         setting.setInstantApp(true, EXCLUDED_USER_ID);
     98         pri.populateUsers(new int[] {
     99                 1, 2, 3, EXCLUDED_USER_ID, 5
    100         }, setting);
    101         Assert.assertNotNull(pri.broadcastUsers);
    102         Assert.assertEquals(4, pri.broadcastUsers.length);
    103         Assert.assertNotNull(pri.instantUserIds);
    104         Assert.assertEquals(1, pri.instantUserIds.length);
    105 
    106         // TODO: test that sendApplicationHiddenForUser() actually fills in
    107         // broadcastUsers
    108     }
    109 }
    110