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