1 /*
2  * Copyright (C) 2023 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.backup;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.annotation.NonNull;
22 import android.app.backup.BackupHelper;
23 import android.content.Context;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.platform.test.annotations.Presubmit;
27 import android.util.ArraySet;
28 
29 import static org.mockito.Mockito.when;
30 
31 import androidx.test.ext.junit.runners.AndroidJUnit4;
32 import androidx.test.filters.SmallTest;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 import java.util.Set;
41 
42 @SmallTest
43 @Presubmit
44 @RunWith(AndroidJUnit4.class)
45 public class SystemBackupAgentTest {
46     private static final int NON_SYSTEM_USER_ID = 10;
47 
48     private TestableSystemBackupAgent mSystemBackupAgent;
49 
50     @Mock private Context mContextMock;
51     @Mock private UserManager mUserManagerMock;
52 
53     @Before
setUp()54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56         mSystemBackupAgent = new TestableSystemBackupAgent();
57         when(mContextMock.getSystemService(UserManager.class)).thenReturn(mUserManagerMock);
58     }
59 
60     @Test
onCreate_systemUser_addsAllHelpers()61     public void onCreate_systemUser_addsAllHelpers() {
62         UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM);
63         when(mUserManagerMock.isProfile()).thenReturn(false);
64 
65         mSystemBackupAgent.onCreate(userHandle, /* backupDestination= */ 0);
66 
67         assertThat(mSystemBackupAgent.mAddedHelpers)
68                 .containsExactly(
69                         "account_sync_settings",
70                         "preferred_activities",
71                         "notifications",
72                         "permissions",
73                         "usage_stats",
74                         "shortcut_manager",
75                         "account_manager",
76                         "slices",
77                         "people",
78                         "app_locales",
79                         "app_gender");
80     }
81 
82     @Test
onCreate_profileUser_addsProfileEligibleHelpers()83     public void onCreate_profileUser_addsProfileEligibleHelpers() {
84         UserHandle userHandle = new UserHandle(NON_SYSTEM_USER_ID);
85         when(mUserManagerMock.isProfile()).thenReturn(true);
86 
87         mSystemBackupAgent.onCreate(userHandle, /* backupDestination= */ 0);
88 
89         assertThat(mSystemBackupAgent.mAddedHelpers)
90                 .containsExactly(
91                         "account_sync_settings",
92                         "notifications",
93                         "permissions",
94                         "app_locales");
95     }
96 
97     @Test
onCreate_nonSystemUser_addsNonSystemEligibleHelpers()98     public void onCreate_nonSystemUser_addsNonSystemEligibleHelpers() {
99         UserHandle userHandle = new UserHandle(NON_SYSTEM_USER_ID);
100         when(mUserManagerMock.isProfile()).thenReturn(false);
101 
102         mSystemBackupAgent.onCreate(userHandle, /* backupDestination= */ 0);
103 
104         assertThat(mSystemBackupAgent.mAddedHelpers)
105                 .containsExactly(
106                         "account_sync_settings",
107                         "preferred_activities",
108                         "notifications",
109                         "permissions",
110                         "app_locales",
111                         "account_manager",
112                         "usage_stats",
113                         "shortcut_manager");
114     }
115 
116     private class TestableSystemBackupAgent extends SystemBackupAgent {
117         final Set<String> mAddedHelpers = new ArraySet<>();
118 
119         @Override
addHelper(String keyPrefix, BackupHelper helper)120         public void addHelper(String keyPrefix, BackupHelper helper) {
121             mAddedHelpers.add(keyPrefix);
122         }
123 
124         @Override
createContextAsUser(UserHandle user, @CreatePackageOptions int flags)125         public Context createContextAsUser(UserHandle user, @CreatePackageOptions int flags) {
126             return mContextMock;
127         }
128 
129         @Override
getSystemService(@erviceName @onNull String name)130         public Object getSystemService(@ServiceName @NonNull String name) {
131             return null;
132         }
133     }
134 }
135