Home | History | Annotate | Download | only in deletionhelper
      1 /*
      2  * Copyright (C) 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.storagemanager.deletionhelper;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.content.pm.IPackageDeleteObserver;
     21 import android.content.pm.PackageManager;
     22 import android.os.RemoteException;
     23 import android.test.mock.MockPackageManager;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 
     26 import com.android.storagemanager.deletionhelper.PackageDeletionTask;
     27 import com.android.storagemanager.deletionhelper.PackageDeletionTask.Callback;
     28 
     29 import java.util.Set;
     30 import java.util.HashSet;
     31 
     32 public class PackageDeletionTaskTest extends AndroidTestCase {
     33     private FakePackageManager mPackageManager;
     34     private Set<String> mDeletedApps;
     35 
     36     @Override
     37     protected void setUp() throws Exception {
     38         mPackageManager = new FakePackageManager();
     39         mDeletedApps = new HashSet<String>();
     40     }
     41 
     42     @SmallTest
     43     public void testDeleteNoApps() throws Exception {
     44         runTask(new HashSet<String>(), false);
     45     }
     46 
     47     @SmallTest
     48     public void testDeleteOneApp() throws Exception {
     49         HashSet<String> appsToDelete = new HashSet<String>();
     50         appsToDelete.add("app.test1");
     51         runTask(appsToDelete, false);
     52     }
     53 
     54     @SmallTest
     55     public void testDeleteManyApps() throws Exception {
     56         HashSet<String> appsToDelete = new HashSet<String>();
     57         appsToDelete.add("app.test1");
     58         appsToDelete.add("app.test2");
     59         runTask(appsToDelete, false);
     60     }
     61 
     62     @SmallTest
     63     public void testDeleteFails() throws Exception {
     64         HashSet<String> appsToDelete = new HashSet<String>();
     65         appsToDelete.add("app.test1");
     66         mPackageManager.deletionSucceeds = false;
     67         runTask(appsToDelete, true);
     68     }
     69 
     70     private void runTask(HashSet<String> appsToDelete, boolean shouldFail) {
     71         PackageDeletionTask task = new PackageDeletionTask(mPackageManager, appsToDelete,
     72                 new VerifierCallback(appsToDelete, shouldFail));
     73         task.run();
     74     }
     75 
     76     class FakePackageManager extends MockPackageManager {
     77         public boolean deletionSucceeds = true;
     78 
     79         @Override
     80         public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer,
     81                                         int flags, int userId) {
     82             int resultCode;
     83             if (deletionSucceeds) {
     84                 resultCode = PackageManager.DELETE_SUCCEEDED;
     85                 mDeletedApps.add(packageName);
     86             } else {
     87                 resultCode = PackageManager.DELETE_FAILED_INTERNAL_ERROR;
     88             }
     89 
     90             try {
     91                 observer.packageDeleted(packageName, resultCode);
     92             } catch (RemoteException e) {
     93                 fail(e.toString());
     94             }
     95         }
     96     }
     97 
     98     class VerifierCallback extends Callback {
     99         private Set<String> mExpectedDeletedApps;
    100         private boolean mShouldFail;
    101 
    102         public VerifierCallback(HashSet<String> expectedDeletedApps, boolean shouldFail) {
    103             mExpectedDeletedApps = expectedDeletedApps;
    104             mShouldFail = shouldFail;
    105         }
    106 
    107         @Override
    108         public void onSuccess() {
    109             assertFalse(mShouldFail);
    110             assertEquals(mExpectedDeletedApps, mDeletedApps);
    111         }
    112 
    113         @Override
    114         public void onError() {
    115             assertTrue(mShouldFail);
    116         }
    117     }
    118 
    119 }
    120