1 /*
2  * Copyright (C) 2018 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.testing.shadows;
18 
19 import android.annotation.Nullable;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageInfo;
22 
23 import com.android.server.backup.transport.TransportConnection;
24 import com.android.server.backup.utils.BackupEligibilityRules;
25 
26 import org.robolectric.annotation.Implementation;
27 import org.robolectric.annotation.Implements;
28 import org.robolectric.annotation.Resetter;
29 
30 import java.util.HashSet;
31 import java.util.Set;
32 
33 @Implements(BackupEligibilityRules.class)
34 public class ShadowBackupEligibilityRules {
35     private static final Set<String> sAppsRunningAndEligibleForBackupWithTransport =
36             new HashSet<>();
37     private static final Set<String> sAppsEligibleForBackup = new HashSet<>();
38     private static final Set<String> sAppsGetFullBackup = new HashSet<>();
39 
setAppRunningAndEligibleForBackupWithTransport(String packageName)40     public static void setAppRunningAndEligibleForBackupWithTransport(String packageName) {
41         sAppsEligibleForBackup.add(packageName);
42         sAppsRunningAndEligibleForBackupWithTransport.add(packageName);
43     }
44 
setAppEligibleForBackup(String packageName)45     public static void setAppEligibleForBackup(String packageName) {
46         sAppsEligibleForBackup.add(packageName);
47     }
48 
49     /** By default the app will be key-value. */
setAppGetsFullBackup(String packageName)50     public static void setAppGetsFullBackup(String packageName) {
51         sAppsGetFullBackup.add(packageName);
52     }
53 
54     @Implementation
appIsRunningAndEligibleForBackupWithTransport( @ullable TransportConnection transportConnection, String packageName)55     protected boolean appIsRunningAndEligibleForBackupWithTransport(
56             @Nullable TransportConnection transportConnection,
57             String packageName) {
58         return sAppsRunningAndEligibleForBackupWithTransport.contains(packageName);
59     }
60 
61     @Implementation
appIsEligibleForBackup(ApplicationInfo app)62     protected boolean appIsEligibleForBackup(ApplicationInfo app) {
63         return sAppsEligibleForBackup.contains(app.packageName);
64     }
65 
66     @Implementation
appGetsFullBackup(PackageInfo packageInfo)67     protected boolean appGetsFullBackup(PackageInfo packageInfo) {
68         return sAppsGetFullBackup.contains(packageInfo.packageName);
69     }
70 
71     @Resetter
reset()72     public static void reset() {
73         sAppsRunningAndEligibleForBackupWithTransport.clear();
74         sAppsEligibleForBackup.clear();
75         sAppsGetFullBackup.clear();
76     }
77 }
78