Home | History | Annotate | Download | only in nonrequiredapps
      1 /*
      2  * Copyright 2016, 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.managedprovisioning.task.nonrequiredapps;
     18 
     19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.mockito.Matchers.anyInt;
     23 import static org.mockito.Mockito.never;
     24 import static org.mockito.Mockito.verify;
     25 import static org.mockito.Mockito.when;
     26 
     27 import android.content.pm.IPackageManager;
     28 import android.content.pm.PackageManager;
     29 import android.support.test.filters.SmallTest;
     30 
     31 import com.android.managedprovisioning.common.Utils;
     32 import com.android.managedprovisioning.model.ProvisioningParams;
     33 
     34 import java.util.Collections;
     35 import java.util.HashSet;
     36 import java.util.Set;
     37 
     38 import org.junit.Before;
     39 import org.junit.Test;
     40 import org.mockito.Mock;
     41 import org.mockito.MockitoAnnotations;
     42 
     43 /**
     44  * Unit tests for {@link NonRequiredAppsLogic}.
     45  */
     46 @SmallTest
     47 public class NonRequiredAppsLogicTest {
     48     private static final String TEST_DPC_PACKAGE_NAME = "dpc.package.name";
     49     private static final int TEST_USER_ID = 123;
     50     private static final String[] APPS = {
     51             "app.a", "app.b", "app.c", "app.d", "app.e", "app.f", "app.g", "app.h",
     52     };
     53     private static final int[] SNAPSHOT_APPS = {0, 1, 2, 3};
     54     private static final int[] SYSTEM_APPS = {0, 1, 4, 5};
     55     private static final int[] BLACKLIST_APPS = {0, 2, 4, 6};
     56 
     57     @Mock private PackageManager mPackageManager;
     58     @Mock private IPackageManager mIPackageManager;
     59     @Mock private SystemAppsSnapshot mSnapshot;
     60     @Mock private OverlayPackagesProvider mProvider;
     61     @Mock private Utils mUtils;
     62 
     63     @Before
     64     public void setUp() {
     65         MockitoAnnotations.initMocks(this);
     66     }
     67 
     68     @Test
     69     public void testGetSystemAppsToRemove_NewLeave() throws Exception {
     70         // GIVEN that a new profile is being created and that system apps should not be deleted
     71         final NonRequiredAppsLogic logic = createLogic(true, true);
     72         // GIVEN that a combination of apps is present
     73         initializeApps();
     74 
     75         // THEN getSystemAppsToRemove should be empty
     76         assertTrue(logic.getSystemAppsToRemove(TEST_USER_ID).isEmpty());
     77     }
     78 
     79     @Test
     80     public void testGetSystemAppsToRemove_NewDelete() throws Exception {
     81         // GIVEN that a new profile is being created and that system apps should be deleted
     82         final NonRequiredAppsLogic logic = createLogic(true, false);
     83         // GIVEN that a combination of apps is present
     84         initializeApps();
     85 
     86         // THEN getSystemAppsToRemove should return a non-empty list with the only app to removed
     87         // being the one that is a current system app and non required
     88         assertEquals(getSetFromArray(new int[] { 0, 4 }),
     89                 logic.getSystemAppsToRemove(TEST_USER_ID));
     90     }
     91 
     92     @Test
     93     public void testGetSystemAppsToRemove_OtaLeave() throws Exception {
     94         // GIVEN that an OTA occurs and that system apps should not be deleted (indicated by the
     95         // fact that no snapshot currently exists)
     96         final NonRequiredAppsLogic logic = createLogic(false, false);
     97         // GIVEN that a combination of apps is present
     98         initializeApps();
     99         // GIVEN that no snapshot currently exists
    100         when(mSnapshot.hasSnapshot(TEST_USER_ID)).thenReturn(false);
    101 
    102         // THEN getSystemAppsToRemove should be empty
    103         assertTrue(logic.getSystemAppsToRemove(TEST_USER_ID).isEmpty());
    104     }
    105 
    106     @Test
    107     public void testGetSystemAppsToRemove_OtaDelete() throws Exception {
    108         // GIVEN that an OTA occurs and that system apps should be deleted (indicated by the fact
    109         // that a snapshot currently exists)
    110         final NonRequiredAppsLogic logic = createLogic(false, false);
    111         // GIVEN that a combination of apps is present
    112         initializeApps();
    113 
    114         // THEN getSystemAppsToRemove should return a non-empty list with the only app to removed
    115         // being the one that is a current system app, non required and not in the last
    116         // snapshot.
    117         assertEquals(Collections.singleton(APPS[4]), logic.getSystemAppsToRemove(TEST_USER_ID));
    118     }
    119 
    120     @Test
    121     public void testMaybeTakeSnapshot_NewLeave() {
    122         // GIVEN that a new profile is being created and that system apps should not be deleted
    123         final NonRequiredAppsLogic logic = createLogic(true, true);
    124 
    125         // WHEN calling maybeTakeSystemAppsSnapshot
    126         logic.maybeTakeSystemAppsSnapshot(TEST_USER_ID);
    127 
    128         // THEN no snapshot should be taken
    129         verify(mSnapshot, never()).takeNewSnapshot(anyInt());
    130     }
    131 
    132     @Test
    133     public void testMaybeTakeSnapshot_NewDelete() {
    134         // GIVEN that a new profile is being created and that system apps should be deleted
    135         final NonRequiredAppsLogic logic = createLogic(true, false);
    136 
    137         // WHEN calling maybeTakeSystemAppsSnapshot
    138         logic.maybeTakeSystemAppsSnapshot(TEST_USER_ID);
    139 
    140         // THEN a snapshot should be taken
    141         verify(mSnapshot).takeNewSnapshot(TEST_USER_ID);
    142     }
    143 
    144     @Test
    145     public void testMaybeTakeSnapshot_OtaLeave() {
    146         // GIVEN that an OTA occurs and that system apps should not be deleted (indicated by the
    147         // fact that no snapshot currently exists)
    148         final NonRequiredAppsLogic logic = createLogic(false, false);
    149         when(mSnapshot.hasSnapshot(TEST_USER_ID)).thenReturn(false);
    150 
    151         // WHEN calling maybeTakeSystemAppsSnapshot
    152         logic.maybeTakeSystemAppsSnapshot(TEST_USER_ID);
    153 
    154         // THEN no snapshot should be taken
    155         verify(mSnapshot, never()).takeNewSnapshot(anyInt());
    156     }
    157 
    158     @Test
    159     public void testMaybeTakeSnapshot_OtaDelete() {
    160         // GIVEN that an OTA occurs and that system apps should be deleted (indicated by the fact
    161         // that a snapshot currently exists)
    162         final NonRequiredAppsLogic logic = createLogic(false, false);
    163         when(mSnapshot.hasSnapshot(TEST_USER_ID)).thenReturn(true);
    164 
    165         // WHEN calling maybeTakeSystemAppsSnapshot
    166         logic.maybeTakeSystemAppsSnapshot(TEST_USER_ID);
    167 
    168         // THEN a snapshot should be taken
    169         verify(mSnapshot).takeNewSnapshot(TEST_USER_ID);
    170     }
    171 
    172     private void initializeApps() throws Exception {
    173         setCurrentSystemApps(getSetFromArray(SYSTEM_APPS));
    174         setLastSnapshot(getSetFromArray(SNAPSHOT_APPS));
    175         setNonRequiredApps(getSetFromArray(BLACKLIST_APPS));
    176     }
    177 
    178     private void setCurrentSystemApps(Set<String> set) {
    179         when(mUtils.getCurrentSystemApps(mIPackageManager, TEST_USER_ID)).thenReturn(set);
    180     }
    181 
    182     private void setLastSnapshot(Set<String> set) {
    183         when(mSnapshot.getSnapshot(TEST_USER_ID)).thenReturn(set);
    184         when(mSnapshot.hasSnapshot(TEST_USER_ID)).thenReturn(true);
    185     }
    186 
    187     private void setNonRequiredApps(Set<String> set) {
    188         when(mProvider.getNonRequiredApps(TEST_USER_ID)).thenReturn(set);
    189     }
    190 
    191     private Set<String> getSetFromArray(int[] ids) {
    192         Set<String> set = new HashSet<>(ids.length);
    193         for (int id : ids) {
    194             set.add(APPS[id]);
    195         }
    196         return set;
    197     }
    198 
    199     private NonRequiredAppsLogic createLogic(boolean newProfile, boolean leaveSystemAppsEnabled) {
    200         ProvisioningParams params = new ProvisioningParams.Builder()
    201                 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
    202                 .setDeviceAdminPackageName(TEST_DPC_PACKAGE_NAME)
    203                 .setLeaveAllSystemAppsEnabled(leaveSystemAppsEnabled)
    204                 .build();
    205         return new NonRequiredAppsLogic(
    206                 mPackageManager,
    207                 mIPackageManager,
    208                 newProfile,
    209                 params,
    210                 mSnapshot,
    211                 mProvider,
    212                 mUtils);
    213     }
    214 }
    215