Home | History | Annotate | Download | only in shadow
      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.settings.testutils.shadow;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 import android.support.annotation.NonNull;
     22 import android.support.v4.content.PermissionChecker;
     23 import android.text.TextUtils;
     24 
     25 import org.robolectric.annotation.Implementation;
     26 import org.robolectric.annotation.Implements;
     27 
     28 import java.util.HashMap;
     29 import java.util.Map;
     30 import java.util.Objects;
     31 
     32 /**
     33  * This class provides shadow for API that is not supported in current roboletric
     34  */
     35 @Implements(PermissionChecker.class)
     36 public class ShadowPermissionChecker {
     37 
     38     private static Map<PermissionInfo, Integer> sPermissions = new HashMap<>();
     39 
     40     public static void clear() {
     41         sPermissions.clear();
     42     }
     43 
     44     public static void addPermission(String permission, int pid, int uid, String packageName,
     45             int permissionValue) {
     46         sPermissions.put(new PermissionInfo(permission, pid, uid, packageName), permissionValue);
     47     }
     48 
     49     @Implementation
     50     public static int checkPermission(@NonNull Context context, @NonNull String permission,
     51             int pid, int uid, String packageName) {
     52         return sPermissions.getOrDefault(new PermissionInfo(permission, pid, uid, packageName),
     53                 PackageManager.PERMISSION_DENIED);
     54     }
     55 
     56     private static class PermissionInfo {
     57         private final int mPid;
     58         private final int mUid;
     59         private final String mPackageName;
     60         private final String mPermission;
     61 
     62         public PermissionInfo(String permission, int pid, int uid, String packageName) {
     63             mPid = pid;
     64             mUid = uid;
     65             mPackageName = packageName;
     66             mPermission = permission;
     67         }
     68 
     69         @Override
     70         public boolean equals(Object obj) {
     71             if (this == obj) {
     72                 return true;
     73             }
     74             if (!(obj instanceof PermissionInfo)) {
     75                 return false;
     76             }
     77 
     78             final PermissionInfo other = (PermissionInfo) obj;
     79             return mPid == other.mPid
     80                     && mUid == other.mUid
     81                     && TextUtils.equals(mPackageName, other.mPackageName)
     82                     && TextUtils.equals(mPermission, other.mPermission);
     83         }
     84 
     85         @Override
     86         public int hashCode() {
     87             return Objects.hash(mPid, mUid, mPackageName, mPermission);
     88         }
     89     }
     90 }
     91