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.backup;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.anyInt;
22 import static org.mockito.Mockito.anyString;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.backup.BackupManager;
29 import android.app.backup.IBackupManager;
30 import android.content.ComponentName;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.pm.PackageManager;
34 import android.content.res.Resources;
35 import android.os.IBinder;
36 import android.os.RemoteException;
37 import android.os.UserHandle;
38 
39 import com.android.settings.R;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.annotation.Implementation;
50 import org.robolectric.annotation.Implements;
51 import org.robolectric.shadow.api.Shadow;
52 import org.robolectric.shadows.ShadowUserManager;
53 
54 @RunWith(RobolectricTestRunner.class)
55 @Config(shadows = BackupSettingsHelperTest.ShadowBackupManagerStub.class)
56 public class BackupSettingsHelperTest {
57 
58     private static final int DEFAULT_SUMMARY_RESOURCE =
59             R.string.backup_configure_account_default_summary;
60 
61     private static final int DEFAULT_LABEL_RESOURCE =
62             R.string.privacy_settings_title;
63 
64     private static final int MANUFACTURER_INTENT_RESOURCE = R.string.config_backup_settings_intent;
65 
66     private static final int MANUFACTURER_LABEL_RESOURCE = R.string.config_backup_settings_label;
67 
68     private Context mContext;
69 
70     private BackupSettingsHelper mBackupSettingsHelper;
71 
72     @Mock
73     private static IBackupManager mBackupManager;
74 
75     private ShadowUserManager mUserManager;
76 
77     @Before
setUp()78     public void setUp() throws Exception {
79         MockitoAnnotations.initMocks(this);
80         mContext = spy(RuntimeEnvironment.application.getApplicationContext());
81         when(mBackupManager.getCurrentTransport()).thenReturn("test_transport");
82         mBackupSettingsHelper = new BackupSettingsHelper(mContext);
83         mUserManager = Shadow.extract(mContext.getSystemService(Context.USER_SERVICE));
84     }
85 
86     @Test
testGetSummary_backupEnabledOnlyOneProfile_showsOn()87     public void testGetSummary_backupEnabledOnlyOneProfile_showsOn() throws Exception {
88         mUserManager.addUserProfile(new UserHandle(0));
89         when(mBackupManager.isBackupEnabled()).thenReturn(true);
90 
91         String backupSummary = mBackupSettingsHelper.getSummary();
92 
93         assertThat(backupSummary).isEqualTo(mContext.getString(R.string.backup_summary_state_on));
94     }
95 
96     @Test
testGetSummary_backupDisabledOnlyOneProfile_showsOff()97     public void testGetSummary_backupDisabledOnlyOneProfile_showsOff() throws Exception {
98         mUserManager.addUserProfile(new UserHandle(0));
99         when(mBackupManager.isBackupEnabled()).thenReturn(false);
100 
101         String backupSummary = mBackupSettingsHelper.getSummary();
102 
103         assertThat(backupSummary).isEqualTo(mContext.getString(R.string.backup_summary_state_off));
104     }
105 
106     @Test
testGetSummary_TwoProfiles_returnsNull()107     public void testGetSummary_TwoProfiles_returnsNull() throws Exception {
108         mUserManager.addUserProfile(new UserHandle(0));
109         mUserManager.addUserProfile(new UserHandle(10));
110         when(mBackupManager.isBackupEnabled()).thenReturn(true);
111 
112         String backupSummary = mBackupSettingsHelper.getSummary();
113 
114         assertThat(backupSummary).isNull();
115     }
116 
117     @Test
testGetIntentFromBackupTransport()118     public void testGetIntentFromBackupTransport() throws Exception {
119         Intent intent = new Intent();
120 
121         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
122 
123         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
124 
125         assertThat(backupIntent).isEqualTo(intent);
126 
127         verify(mBackupManager).getDataManagementIntent(anyString());
128     }
129 
130     @Test
testGetIntentFromBackupTransport_WithIntent()131     public void testGetIntentFromBackupTransport_WithIntent() throws Exception {
132         Intent intent = mock(Intent.class);
133 
134         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
135 
136         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
137 
138         assertThat(backupIntent).isEqualTo(intent);
139     }
140 
141     @Test
testGetIntentFromBackupTransport_WithNullIntent()142     public void testGetIntentFromBackupTransport_WithNullIntent() throws Exception {
143         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null);
144 
145         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
146 
147         assertThat(backupIntent).isNull();
148     }
149 
150     @Test
testGetIntentFromBackupTransport_RemoteException()151     public void testGetIntentFromBackupTransport_RemoteException() throws Exception {
152         when(mBackupManager.getDataManagementIntent(anyString())).thenThrow(new RemoteException());
153 
154         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
155 
156         assertThat(backupIntent).isNull();
157     }
158 
159     @Test
testGetIntentFromBackupTransport_BackupEnabled()160     public void testGetIntentFromBackupTransport_BackupEnabled() throws Exception {
161         Intent intent = new Intent("test_intent");
162 
163         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
164         when(mBackupManager.isBackupServiceActive(anyInt())).thenReturn(true);
165 
166         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
167 
168         assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE))
169                 .isEqualTo(true);
170     }
171 
172     @Test
testGetIntentFromBackupTransport_BackupDisabled()173     public void testGetIntentFromBackupTransport_BackupDisabled() throws Exception {
174         Intent intent = new Intent("test_intent");
175 
176         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
177         when(mBackupManager.isBackupServiceActive(anyInt())).thenReturn(false);
178 
179         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
180 
181         assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE))
182                 .isEqualTo(false);
183     }
184 
185     @Test
testGetIntentFromBackupTransport_BackupStatusException()186     public void testGetIntentFromBackupTransport_BackupStatusException() throws Exception {
187         Intent intent = new Intent("test_intent");
188 
189         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
190         when(mBackupManager.isBackupServiceActive(anyInt())).thenThrow(new RemoteException());
191 
192         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettingsFromTransport();
193 
194         assertThat(backupIntent.getExtras().get(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE))
195                 .isEqualTo(false);
196     }
197 
198     @Test
testIsIntentProvidedByTransport_WithNullIntent()199     public void testIsIntentProvidedByTransport_WithNullIntent() throws Exception {
200         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(null);
201 
202         boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport();
203 
204         assertThat(isIntentProvided).isFalse();
205     }
206 
207     @Test
testIsIntentProvidedByTransport_WithInvalidIntent()208     public void testIsIntentProvidedByTransport_WithInvalidIntent() throws Exception {
209         Intent intent = mock(Intent.class);
210 
211         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
212 
213         PackageManager packageManager = mock(PackageManager.class);
214         when(mContext.getPackageManager()).thenReturn(packageManager);
215         when(intent.resolveActivity(packageManager)).thenReturn(null);
216 
217         boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport();
218 
219         assertThat(isIntentProvided).isFalse();
220     }
221 
222     @Test
testIsIntentProvidedByTransport_WithIntent()223     public void testIsIntentProvidedByTransport_WithIntent() throws Exception {
224         Intent intent = mock(Intent.class);
225 
226         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
227 
228         PackageManager packageManager = mock(PackageManager.class);
229         when(mContext.getPackageManager()).thenReturn(packageManager);
230         when(intent.resolveActivity(packageManager)).thenReturn(mock(ComponentName.class));
231 
232         boolean isIntentProvided = mBackupSettingsHelper.isIntentProvidedByTransport();
233 
234         assertThat(isIntentProvided).isTrue();
235     }
236 
237     @Test
testGetSummaryFromBackupTransport()238     public void testGetSummaryFromBackupTransport() throws Exception {
239         String summary = "test_summary";
240 
241         when(mBackupManager.getDestinationString(anyString())).thenReturn(summary);
242 
243         String backupSummary = mBackupSettingsHelper.getSummaryFromBackupTransport();
244 
245         assertThat(backupSummary).isEqualTo(summary);
246     }
247 
248     @Test
testGetSummaryFromBackupTransport_RemoteException()249     public void testGetSummaryFromBackupTransport_RemoteException() throws Exception {
250         when(mBackupManager.getDestinationString(anyString())).thenThrow(new RemoteException());
251 
252         String backupSummary = mBackupSettingsHelper.getSummaryFromBackupTransport();
253 
254         assertThat(backupSummary).isNull();
255     }
256 
257     @Test
testGetLabelBackupTransport()258     public void testGetLabelBackupTransport() throws Exception {
259         CharSequence label = "test_label";
260 
261         when(mBackupManager.getDataManagementLabelForUser(anyInt(), anyString())).thenReturn(label);
262 
263         CharSequence backupLabel = mBackupSettingsHelper.getLabelFromBackupTransport();
264 
265         assertThat(backupLabel).isEqualTo(label);
266     }
267 
268     @Test
testGetLabelBackupTransport_RemoteException()269     public void testGetLabelBackupTransport_RemoteException() throws Exception {
270         when(mBackupManager.getDataManagementLabelForUser(anyInt(), anyString()))
271                 .thenThrow(new RemoteException());
272 
273         CharSequence backupLabel = mBackupSettingsHelper.getLabelFromBackupTransport();
274 
275         assertThat(backupLabel).isNull();
276     }
277 
278     @Test
testGetIntentForBackupSettings_WithIntentFromTransport()279     public void testGetIntentForBackupSettings_WithIntentFromTransport() throws Exception {
280         Intent intent = mock(Intent.class);
281 
282         when(mBackupManager.getDataManagementIntent(anyString())).thenReturn(intent);
283 
284         PackageManager packageManager = mock(PackageManager.class);
285         when(mContext.getPackageManager()).thenReturn(packageManager);
286         when(intent.resolveActivity(packageManager)).thenReturn(mock(ComponentName.class));
287 
288         Intent backupIntent = mBackupSettingsHelper.getIntentForBackupSettings();
289 
290         assertThat(backupIntent).isEqualTo(intent);
291     }
292 
293     @Test
testGetLabelForBackupSettings_WithLabelFromTransport()294     public void testGetLabelForBackupSettings_WithLabelFromTransport() throws Exception {
295         CharSequence label = "test_label";
296 
297         when(mBackupManager.getDataManagementLabelForUser(anyInt(), anyString())).thenReturn(label);
298 
299         CharSequence backupLabel = mBackupSettingsHelper.getLabelForBackupSettings();
300 
301         assertThat(backupLabel).isEqualTo(label);
302     }
303 
304     @Test
testGetLabelForBackupSettings_WithEmptyLabelFromTransport()305     public void testGetLabelForBackupSettings_WithEmptyLabelFromTransport() throws Exception {
306         CharSequence label = "";
307 
308         when(mBackupManager.getDataManagementLabelForUser(anyInt(), anyString())).thenReturn(label);
309 
310         CharSequence backupLabel = mBackupSettingsHelper.getLabelForBackupSettings();
311 
312         assertThat(backupLabel).isEqualTo(mContext.getString(DEFAULT_LABEL_RESOURCE));
313     }
314 
315     @Test
testGetLabelForBackupSettings_WithoutLabelFromTransport()316     public void testGetLabelForBackupSettings_WithoutLabelFromTransport() throws Exception {
317         when(mBackupManager.getDataManagementLabelForUser(anyInt(), anyString())).thenReturn(null);
318 
319         CharSequence backupLabel = mBackupSettingsHelper.getLabelForBackupSettings();
320 
321         assertThat(backupLabel).isEqualTo(mContext.getString(DEFAULT_LABEL_RESOURCE));
322     }
323 
324     @Test
testGetSummaryForBackupSettings_WithSummaryFromTransport()325     public void testGetSummaryForBackupSettings_WithSummaryFromTransport() throws Exception {
326         String summary = "test_summary";
327 
328         when(mBackupManager.getDestinationString(anyString())).thenReturn(summary);
329 
330         String backupSummary = mBackupSettingsHelper.getSummaryForBackupSettings();
331 
332         assertThat(backupSummary).isEqualTo(summary);
333     }
334 
335     @Test
testGetSummaryForBackupSettings_WithoutSummaryFromTransport()336     public void testGetSummaryForBackupSettings_WithoutSummaryFromTransport() throws Exception {
337         when(mBackupManager.getDestinationString(anyString())).thenReturn(null);
338 
339         String backupSummary = mBackupSettingsHelper.getSummaryForBackupSettings();
340 
341         assertThat(backupSummary).isEqualTo(mContext.getString(DEFAULT_SUMMARY_RESOURCE));
342     }
343 
344     @Test
testIsBackupProvidedByManufacturer_WithIntent()345     public void testIsBackupProvidedByManufacturer_WithIntent() {
346         String intent = "test_intent";
347 
348         when(mContext.getApplicationContext()).thenReturn(mContext);
349         Resources spiedResources = spy(mContext.getResources());
350         when(mContext.getResources()).thenReturn(spiedResources);
351         when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent);
352         mBackupSettingsHelper = new BackupSettingsHelper(mContext);
353 
354         boolean hasManufacturerIntent = mBackupSettingsHelper.isBackupProvidedByManufacturer();
355 
356         assertThat(hasManufacturerIntent).isTrue();
357     }
358 
359     @Test
testIsBackupProvidedByManufacturer_WithoutIntent()360     public void testIsBackupProvidedByManufacturer_WithoutIntent() {
361         String intent = "";
362 
363         when(mContext.getApplicationContext()).thenReturn(mContext);
364         Resources spiedResources = spy(mContext.getResources());
365         when(mContext.getResources()).thenReturn(spiedResources);
366         when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent);
367         mBackupSettingsHelper = new BackupSettingsHelper(mContext);
368 
369         boolean hasManufacturerIntent = mBackupSettingsHelper.isBackupProvidedByManufacturer();
370 
371         assertThat(hasManufacturerIntent).isFalse();
372     }
373 
374     @Test
testGetLabelProvidedByManufacturer()375     public void testGetLabelProvidedByManufacturer() {
376         String label = "test_label";
377 
378         when(mContext.getApplicationContext()).thenReturn(mContext);
379         Resources spiedResources = spy(mContext.getResources());
380         when(mContext.getResources()).thenReturn(spiedResources);
381         when(spiedResources.getString(MANUFACTURER_LABEL_RESOURCE)).thenReturn(label);
382         mBackupSettingsHelper = new BackupSettingsHelper(mContext);
383 
384         String manufacturerLabel = mBackupSettingsHelper.getLabelProvidedByManufacturer();
385 
386         assertThat(manufacturerLabel).isEqualTo(label);
387     }
388 
389     @Test
testGetIntentProvidedByManufacturer()390     public void testGetIntentProvidedByManufacturer() {
391         String intent = "test_intent";
392 
393         when(mContext.getApplicationContext()).thenReturn(mContext);
394         Resources spiedResources = spy(mContext.getResources());
395         when(mContext.getResources()).thenReturn(spiedResources);
396         when(spiedResources.getString(MANUFACTURER_INTENT_RESOURCE)).thenReturn(intent);
397         mBackupSettingsHelper = new BackupSettingsHelper(mContext);
398 
399         Intent manufacturerIntent = mBackupSettingsHelper.getIntentProvidedByManufacturer();
400 
401         assertThat(manufacturerIntent).isNotNull();
402     }
403 
404     @Implements(IBackupManager.Stub.class)
405     public static class ShadowBackupManagerStub {
406         @Implementation
asInterface(IBinder iBinder)407         public static IBackupManager asInterface(IBinder iBinder) {
408             return mBackupManager;
409         }
410     }
411 }
412