blob: 565886c798bf72c44955fd0e7c277bdac08dcb36 [file] [log] [blame]
Artem Iglikov6052ef52017-04-20 17:23:39 +01001/*
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
17package com.android.server.backup;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertFalse;
21import static junit.framework.Assert.assertNull;
22import static junit.framework.Assert.assertTrue;
23
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.verifyNoMoreInteractions;
26import static org.mockito.Mockito.when;
27
28import android.app.Instrumentation;
29import android.app.backup.IBackupManagerMonitor;
30import android.app.backup.IBackupObserver;
31import android.app.backup.IFullBackupRestoreObserver;
32import android.content.ComponentName;
33import android.content.Context;
34import android.content.Intent;
35import android.os.IBinder;
36import android.os.ParcelFileDescriptor;
37import android.os.RemoteException;
38import android.os.UserHandle;
39import android.support.test.InstrumentationRegistry;
40import android.support.test.runner.AndroidJUnit4;
41
42import org.junit.Before;
43import org.junit.Ignore;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.mockito.Mock;
47import org.mockito.MockitoAnnotations;
48
49import java.io.File;
50
51// TODO: Tests for createService().
52// TODO: Tests for setBackupServiceActive().
53// TODO: Tests for dump().
54@RunWith(AndroidJUnit4.class)
55public class TrampolineTest {
56 private static final String PACKAGE_NAME = "some.package.name";
57 private static final String TRANSPORT_NAME = "some.transport.name";
58 private static final String CURRENT_PASSWORD = "current_password";
59 private static final String NEW_PASSWORD = "new_password";
60 private static final String ENCRYPTION_PASSWORD = "encryption_password";
61 private static final String DATA_MANAGEMENT_LABEL = "data_management_label";
62 private static final String DESTINATION_STRING = "destination_string";
63 private static final String[] PACKAGE_NAMES =
64 new String[]{"some.package.name._1", "some.package.name._2"};
65 private static final String[] TRANSPORTS =
66 new String[]{"some.transport.name._1", "some.transport.name._2"};
67 private static final ComponentName TRANSPORT_COMPONENT_NAME = new ComponentName("package",
68 "class");
69 private static final ComponentName[] TRANSPORT_COMPONENTS = new ComponentName[]{
70 new ComponentName("package1", "class1"),
71 new ComponentName("package2", "class2")
72 };
73
74 @Mock private BackupManagerServiceInterface mBackupManagerServiceMock;
75 @Mock private Context mContextMock;
76 @Mock private File mSuppressFileMock;
77 @Mock private File mSuppressFileParentMock;
78 @Mock private IBinder mAgentMock;
79 @Mock private ParcelFileDescriptor mFileDescriptorMock;
80 @Mock private IFullBackupRestoreObserver mFullBackupRestoreObserverMock;
81 @Mock private IBackupObserver mBackupObserverMock;
82 @Mock private IBackupManagerMonitor mBackupManagerMonitorMock;
83
84 private TrampolineTestable mTrampoline;
85
86 @Before
87 public void setUp() {
88 MockitoAnnotations.initMocks(this);
89
90 mTrampoline = new TrampolineTestable(mContextMock);
91
92 when(mSuppressFileMock.getParentFile()).thenReturn(mSuppressFileParentMock);
93 }
94
95 @Test
96 public void constructor_createsSuppressFileDirectory() {
97 new TrampolineTestable(mContextMock, false, mSuppressFileMock);
98 verify(mSuppressFileParentMock).mkdirs();
99 }
100
101 @Test
102 public void initialize_forUserSystem_successfullyInitialized() {
103 mTrampoline.initialize(UserHandle.USER_SYSTEM);
104
105 assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
106 }
107
108 // The BackupManagerService can only be initialized by USER_SYSTEM, so we check that if any
109 // other user trying to initialize it leaves it non-active.
110 @Test
111 public void initialize_forNonUserSystem_nonInitialized() {
112 int nonUserSystem = UserHandle.USER_SYSTEM + 1;
113 mTrampoline.initialize(nonUserSystem);
114
115 assertFalse(mTrampoline.isBackupServiceActive(nonUserSystem));
116 }
117
118 @Test
119 public void initialize_globallyDisabled_nonInitialized() {
120 TrampolineTestable trampoline = new TrampolineTestable(mContextMock,
121 true /* globalDisable */, mSuppressFileMock);
122 trampoline.initialize(UserHandle.USER_SYSTEM);
123
124 assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
125 }
126
127 // Verify that BackupManagerService is not initialized if suppress file exists.
128 @Test
129 public void initialize_suppressFileExists_nonInitialized() {
130 when(mSuppressFileMock.exists()).thenReturn(true);
131
132 TrampolineTestable trampoline = new TrampolineTestable(mContextMock,
133 false /* globalDisable */, mSuppressFileMock);
134 trampoline.initialize(UserHandle.USER_SYSTEM);
135
136 assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
137 }
138
139 @Test
140 public void isBackupServiceActive_calledBeforeInitialize_returnsFalse() {
141 assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM));
142 }
143
144 @Test
145 public void dataChanged_calledBeforeInitialize_ignored() throws RemoteException {
146 mTrampoline.dataChanged(PACKAGE_NAME);
147 verifyNoMoreInteractions(mBackupManagerServiceMock);
148 }
149
150 @Test
151 public void dataChanged_forwarded() throws RemoteException {
152 mTrampoline.initialize(UserHandle.USER_SYSTEM);
153 mTrampoline.dataChanged(PACKAGE_NAME);
154 verify(mBackupManagerServiceMock).dataChanged(PACKAGE_NAME);
155 }
156
157 @Test
158 public void clearBackupData_calledBeforeInitialize_ignored() throws RemoteException {
159 mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME);
160 verifyNoMoreInteractions(mBackupManagerServiceMock);
161 }
162
163 @Test
164 public void clearBackupData_forwarded() throws RemoteException {
165 mTrampoline.initialize(UserHandle.USER_SYSTEM);
166 mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME);
167 verify(mBackupManagerServiceMock).clearBackupData(TRANSPORT_NAME, PACKAGE_NAME);
168 }
169
170 @Test
171 public void agentConnected_calledBeforeInitialize_ignored() throws RemoteException {
172 mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock);
173 verifyNoMoreInteractions(mBackupManagerServiceMock);
174 }
175
176 @Test
177 public void agentConnected_forwarded() throws RemoteException {
178 mTrampoline.initialize(UserHandle.USER_SYSTEM);
179 mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock);
180 verify(mBackupManagerServiceMock).agentConnected(PACKAGE_NAME, mAgentMock);
181 }
182
183 @Test
184 public void agentDisconnected_calledBeforeInitialize_ignored() throws RemoteException {
185 mTrampoline.agentDisconnected(PACKAGE_NAME);
186 verifyNoMoreInteractions(mBackupManagerServiceMock);
187 }
188
189 @Test
190 public void agentDisconnected_forwarded() throws RemoteException {
191 mTrampoline.initialize(UserHandle.USER_SYSTEM);
192 mTrampoline.agentDisconnected(PACKAGE_NAME);
193 verify(mBackupManagerServiceMock).agentDisconnected(PACKAGE_NAME);
194 }
195
196 @Test
197 public void restoreAtInstall_calledBeforeInitialize_ignored() throws RemoteException {
198 mTrampoline.restoreAtInstall(PACKAGE_NAME, 123);
199 verifyNoMoreInteractions(mBackupManagerServiceMock);
200 }
201
202 @Test
203 public void restoreAtInstall_forwarded() throws RemoteException {
204 mTrampoline.initialize(UserHandle.USER_SYSTEM);
205 mTrampoline.restoreAtInstall(PACKAGE_NAME, 123);
206 verify(mBackupManagerServiceMock).restoreAtInstall(PACKAGE_NAME, 123);
207 }
208
209 @Test
210 public void setBackupEnabled_calledBeforeInitialize_ignored() throws RemoteException {
211 mTrampoline.setBackupEnabled(true);
212 verifyNoMoreInteractions(mBackupManagerServiceMock);
213 }
214
215 @Test
216 public void setBackupEnabled_forwarded() throws RemoteException {
217 mTrampoline.initialize(UserHandle.USER_SYSTEM);
218 mTrampoline.setBackupEnabled(true);
219 verify(mBackupManagerServiceMock).setBackupEnabled(true);
220 }
221
222 @Test
223 public void setAutoRestore_calledBeforeInitialize_ignored() throws RemoteException {
224 mTrampoline.setAutoRestore(true);
225 verifyNoMoreInteractions(mBackupManagerServiceMock);
226 }
227
228 @Test
229 public void setAutoRestore_forwarded() throws RemoteException {
230 mTrampoline.initialize(UserHandle.USER_SYSTEM);
231 mTrampoline.setAutoRestore(true);
232 verify(mBackupManagerServiceMock).setAutoRestore(true);
233 }
234
235 @Test
236 public void setBackupProvisioned_calledBeforeInitialize_ignored() throws RemoteException {
237 mTrampoline.setBackupProvisioned(true);
238 verifyNoMoreInteractions(mBackupManagerServiceMock);
239 }
240
241 @Test
242 public void setBackupProvisioned_forwarded() throws RemoteException {
243 mTrampoline.initialize(UserHandle.USER_SYSTEM);
244 mTrampoline.setBackupProvisioned(true);
245 verify(mBackupManagerServiceMock).setBackupProvisioned(true);
246 }
247
248 @Test
249 public void isBackupEnabled_calledBeforeInitialize_ignored() throws RemoteException {
250 assertFalse(mTrampoline.isBackupEnabled());
251 verifyNoMoreInteractions(mBackupManagerServiceMock);
252 }
253
254 @Test
255 public void isBackupEnabled_forwarded() throws RemoteException {
256 mTrampoline.initialize(UserHandle.USER_SYSTEM);
257 mTrampoline.isBackupEnabled();
258 verify(mBackupManagerServiceMock).isBackupEnabled();
259 }
260
261 @Test
262 public void setBackupPassword_calledBeforeInitialize_ignored() throws RemoteException {
263 mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
264 verifyNoMoreInteractions(mBackupManagerServiceMock);
265 }
266
267 @Test
268 public void setBackupPassword_forwarded() throws RemoteException {
269 mTrampoline.initialize(UserHandle.USER_SYSTEM);
270 mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
271 verify(mBackupManagerServiceMock).setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD);
272 }
273
274 @Test
275 public void hasBackupPassword_calledBeforeInitialize_ignored() throws RemoteException {
276 assertFalse(mTrampoline.hasBackupPassword());
277 verifyNoMoreInteractions(mBackupManagerServiceMock);
278 }
279
280 @Test
281 public void hasBackupPassword_forwarded() throws RemoteException {
282 mTrampoline.initialize(UserHandle.USER_SYSTEM);
283 mTrampoline.hasBackupPassword();
284 verify(mBackupManagerServiceMock).hasBackupPassword();
285 }
286
287 @Test
288 public void backupNow_calledBeforeInitialize_ignored() throws RemoteException {
289 mTrampoline.backupNow();
290 verifyNoMoreInteractions(mBackupManagerServiceMock);
291 }
292
293 @Test
294 public void backupNow_forwarded() throws RemoteException {
295 mTrampoline.initialize(UserHandle.USER_SYSTEM);
296 mTrampoline.backupNow();
297 verify(mBackupManagerServiceMock).backupNow();
298 }
299
300 @Test
301 public void adbBackup_calledBeforeInitialize_ignored() throws RemoteException {
302 mTrampoline.adbBackup(mFileDescriptorMock, true, true, true, true, true, true, true, true,
303 PACKAGE_NAMES);
304 verifyNoMoreInteractions(mBackupManagerServiceMock);
305 }
306
307 @Test
308 public void adbBackup_forwarded() throws RemoteException {
309 mTrampoline.initialize(UserHandle.USER_SYSTEM);
310 mTrampoline.adbBackup(mFileDescriptorMock, true, true, true, true, true, true, true, true,
311 PACKAGE_NAMES);
312 verify(mBackupManagerServiceMock).adbBackup(mFileDescriptorMock, true, true, true, true,
313 true, true, true, true, PACKAGE_NAMES);
314 }
315
316 @Test
317 public void fullTransportBackup_calledBeforeInitialize_ignored() throws RemoteException {
318 mTrampoline.fullTransportBackup(PACKAGE_NAMES);
319 verifyNoMoreInteractions(mBackupManagerServiceMock);
320 }
321
322 @Test
323 public void fullTransportBackup_forwarded() throws RemoteException {
324 mTrampoline.initialize(UserHandle.USER_SYSTEM);
325 mTrampoline.fullTransportBackup(PACKAGE_NAMES);
326 verify(mBackupManagerServiceMock).fullTransportBackup(PACKAGE_NAMES);
327 }
328
329 @Test
330 public void adbRestore_calledBeforeInitialize_ignored() throws RemoteException {
331 mTrampoline.adbRestore(mFileDescriptorMock);
332 verifyNoMoreInteractions(mBackupManagerServiceMock);
333 }
334
335 @Test
336 public void adbRestore_forwarded() throws RemoteException {
337 mTrampoline.initialize(UserHandle.USER_SYSTEM);
338 mTrampoline.adbRestore(mFileDescriptorMock);
339 verify(mBackupManagerServiceMock).adbRestore(mFileDescriptorMock);
340 }
341
342 @Test
343 public void acknowledgeFullBackupOrRestore_calledBeforeInitialize_ignored()
344 throws RemoteException {
345 mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD,
346 mFullBackupRestoreObserverMock);
347 verifyNoMoreInteractions(mBackupManagerServiceMock);
348 }
349
350 @Test
351 public void acknowledgeFullBackupOrRestore_forwarded() throws RemoteException {
352 mTrampoline.initialize(UserHandle.USER_SYSTEM);
353 mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD,
354 mFullBackupRestoreObserverMock);
355 verify(mBackupManagerServiceMock).acknowledgeAdbBackupOrRestore(123, true, CURRENT_PASSWORD,
356 ENCRYPTION_PASSWORD, mFullBackupRestoreObserverMock);
357 }
358
359 @Test
360 public void getCurrentTransport_calledBeforeInitialize_ignored() throws RemoteException {
361 assertNull(mTrampoline.getCurrentTransport());
362 verifyNoMoreInteractions(mBackupManagerServiceMock);
363 }
364
365 @Test
366 public void getCurrentTransport_forwarded() throws RemoteException {
367 when(mBackupManagerServiceMock.getCurrentTransport()).thenReturn(TRANSPORT_NAME);
368
369 mTrampoline.initialize(UserHandle.USER_SYSTEM);
370
371 assertEquals(TRANSPORT_NAME, mTrampoline.getCurrentTransport());
372 verify(mBackupManagerServiceMock).getCurrentTransport();
373 }
374
375 @Test
376 public void listAllTransports_calledBeforeInitialize_ignored() throws RemoteException {
377 assertNull(mTrampoline.listAllTransports());
378 verifyNoMoreInteractions(mBackupManagerServiceMock);
379 }
380
381 @Test
382 public void listAllTransports_forwarded() throws RemoteException {
383 when(mBackupManagerServiceMock.listAllTransports()).thenReturn(TRANSPORTS);
384
385 mTrampoline.initialize(UserHandle.USER_SYSTEM);
386 assertEquals(TRANSPORTS, mTrampoline.listAllTransports());
387 verify(mBackupManagerServiceMock).listAllTransports();
388 }
389
390 @Test
391 public void listAllTransportComponents_calledBeforeInitialize_ignored() throws RemoteException {
392 assertNull(mTrampoline.listAllTransportComponents());
393 verifyNoMoreInteractions(mBackupManagerServiceMock);
394 }
395
396 @Test
397 public void listAllTransportComponents_forwarded() throws RemoteException {
398 when(mBackupManagerServiceMock.listAllTransportComponents()).thenReturn(
399 TRANSPORT_COMPONENTS);
400
401 mTrampoline.initialize(UserHandle.USER_SYSTEM);
402 assertEquals(TRANSPORT_COMPONENTS, mTrampoline.listAllTransportComponents());
403 verify(mBackupManagerServiceMock).listAllTransportComponents();
404 }
405
406 @Test
407 public void getTransportWhitelist_calledBeforeInitialize_ignored() throws RemoteException {
408 assertNull(mTrampoline.getTransportWhitelist());
409 verifyNoMoreInteractions(mBackupManagerServiceMock);
410 }
411
412 @Test
413 public void getTransportWhitelist_forwarded() throws RemoteException {
414 when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS);
415
416 mTrampoline.initialize(UserHandle.USER_SYSTEM);
417 assertEquals(TRANSPORTS, mTrampoline.getTransportWhitelist());
418 verify(mBackupManagerServiceMock).getTransportWhitelist();
419 }
420
421 @Test
422 public void selectBackupTransport_calledBeforeInitialize_ignored() throws RemoteException {
423 mTrampoline.selectBackupTransport(TRANSPORT_NAME);
424 verifyNoMoreInteractions(mBackupManagerServiceMock);
425 }
426
427 @Test
428 public void selectBackupTransport_forwarded() throws RemoteException {
429 mTrampoline.initialize(UserHandle.USER_SYSTEM);
430 mTrampoline.selectBackupTransport(TRANSPORT_NAME);
431 verify(mBackupManagerServiceMock).selectBackupTransport(TRANSPORT_NAME);
432 }
433
434 @Test
435 public void selectBackupTransportAsync_calledBeforeInitialize_ignored() throws RemoteException {
436 mTrampoline.selectBackupTransportAsync(TRANSPORT_COMPONENT_NAME, null);
437 verifyNoMoreInteractions(mBackupManagerServiceMock);
438 }
439
440 @Test
441 public void selectBackupTransportAsync_forwarded() throws RemoteException {
442 mTrampoline.initialize(UserHandle.USER_SYSTEM);
443 mTrampoline.selectBackupTransportAsync(TRANSPORT_COMPONENT_NAME, null);
444 verify(mBackupManagerServiceMock).selectBackupTransportAsync(TRANSPORT_COMPONENT_NAME,
445 null);
446 }
447
448 @Test
449 public void getConfigurationIntent_calledBeforeInitialize_ignored() throws RemoteException {
450 mTrampoline.getConfigurationIntent(TRANSPORT_NAME);
451 verifyNoMoreInteractions(mBackupManagerServiceMock);
452 }
453
454 @Test
455 public void getConfigurationIntent_forwarded() throws RemoteException {
456 Intent configurationIntentStub = new Intent();
457 when(mBackupManagerServiceMock.getConfigurationIntent(TRANSPORT_NAME)).thenReturn(
458 configurationIntentStub);
459
460 mTrampoline.initialize(UserHandle.USER_SYSTEM);
461 assertEquals(configurationIntentStub, mTrampoline.getConfigurationIntent(TRANSPORT_NAME));
462 verify(mBackupManagerServiceMock).getConfigurationIntent(TRANSPORT_NAME);
463 }
464
465 @Test
466 public void getDestinationString_calledBeforeInitialize_ignored() throws RemoteException {
467 assertNull(mTrampoline.getDestinationString(TRANSPORT_NAME));
468 verifyNoMoreInteractions(mBackupManagerServiceMock);
469 }
470
471 @Test
472 public void getDestinationString_forwarded() throws RemoteException {
473 when(mBackupManagerServiceMock.getDestinationString(TRANSPORT_NAME)).thenReturn(
474 DESTINATION_STRING);
475
476 mTrampoline.initialize(UserHandle.USER_SYSTEM);
477 assertEquals(DESTINATION_STRING, mTrampoline.getDestinationString(TRANSPORT_NAME));
478 verify(mBackupManagerServiceMock).getDestinationString(TRANSPORT_NAME);
479 }
480
481 @Test
482 public void getDataManagementIntent_calledBeforeInitialize_ignored() throws RemoteException {
483 assertNull(mTrampoline.getDataManagementIntent(TRANSPORT_NAME));
484 verifyNoMoreInteractions(mBackupManagerServiceMock);
485 }
486
487 @Test
488 public void getDataManagementIntent_forwarded() throws RemoteException {
489 Intent dataManagementIntent = new Intent();
490 when(mBackupManagerServiceMock.getDataManagementIntent(TRANSPORT_NAME)).thenReturn(
491 dataManagementIntent);
492
493 mTrampoline.initialize(UserHandle.USER_SYSTEM);
494 assertEquals(dataManagementIntent, mTrampoline.getDataManagementIntent(TRANSPORT_NAME));
495 verify(mBackupManagerServiceMock).getDataManagementIntent(TRANSPORT_NAME);
496 }
497
498 @Test
499 public void getDataManagementLabel_calledBeforeInitialize_ignored() throws RemoteException {
500 assertNull(mTrampoline.getDataManagementLabel(TRANSPORT_NAME));
501 verifyNoMoreInteractions(mBackupManagerServiceMock);
502 }
503
504 @Test
505 public void getDataManagementLabel_forwarded() throws RemoteException {
506 when(mBackupManagerServiceMock.getDataManagementLabel(TRANSPORT_NAME)).thenReturn(
507 DATA_MANAGEMENT_LABEL);
508
509 mTrampoline.initialize(UserHandle.USER_SYSTEM);
510 assertEquals(DATA_MANAGEMENT_LABEL, mTrampoline.getDataManagementLabel(TRANSPORT_NAME));
511 verify(mBackupManagerServiceMock).getDataManagementLabel(TRANSPORT_NAME);
512 }
513
514 @Test
515 public void beginRestoreSession_calledBeforeInitialize_ignored() throws RemoteException {
516 mTrampoline.beginRestoreSession(PACKAGE_NAME, TRANSPORT_NAME);
517 verifyNoMoreInteractions(mBackupManagerServiceMock);
518 }
519
520 @Test
521 public void beginRestoreSession_forwarded() throws RemoteException {
522 mTrampoline.initialize(UserHandle.USER_SYSTEM);
523 mTrampoline.beginRestoreSession(PACKAGE_NAME, TRANSPORT_NAME);
524 verify(mBackupManagerServiceMock).beginRestoreSession(PACKAGE_NAME, TRANSPORT_NAME);
525 }
526
527 @Test
528 public void opComplete_calledBeforeInitialize_ignored() throws RemoteException {
529 mTrampoline.opComplete(1, 2);
530 verifyNoMoreInteractions(mBackupManagerServiceMock);
531 }
532
533 @Test
534 public void opComplete_forwarded() throws RemoteException {
535 mTrampoline.initialize(UserHandle.USER_SYSTEM);
536 mTrampoline.opComplete(1, 2);
537 verify(mBackupManagerServiceMock).opComplete(1, 2);
538 }
539
540 @Test
541 public void getAvailableRestoreToken_calledBeforeInitialize_ignored() throws RemoteException {
542 assertEquals(0, mTrampoline.getAvailableRestoreToken(PACKAGE_NAME));
543 verifyNoMoreInteractions(mBackupManagerServiceMock);
544 }
545
546 @Test
547 public void getAvailableRestoreToken_forwarded() throws RemoteException {
548 when(mBackupManagerServiceMock.getAvailableRestoreToken(PACKAGE_NAME)).thenReturn(123L);
549
550 mTrampoline.initialize(UserHandle.USER_SYSTEM);
551 assertEquals(123, mTrampoline.getAvailableRestoreToken(PACKAGE_NAME));
552 verify(mBackupManagerServiceMock).getAvailableRestoreToken(PACKAGE_NAME);
553 }
554
555 @Test
556 public void isAppEligibleForBackup_calledBeforeInitialize_ignored() throws RemoteException {
557 assertFalse(mTrampoline.isAppEligibleForBackup(PACKAGE_NAME));
558 verifyNoMoreInteractions(mBackupManagerServiceMock);
559 }
560
561 @Test
562 public void isAppEligibleForBackup_forwarded() throws RemoteException {
563 when(mBackupManagerServiceMock.isAppEligibleForBackup(PACKAGE_NAME)).thenReturn(true);
564
565 mTrampoline.initialize(UserHandle.USER_SYSTEM);
566 assertTrue(mTrampoline.isAppEligibleForBackup(PACKAGE_NAME));
567 verify(mBackupManagerServiceMock).isAppEligibleForBackup(PACKAGE_NAME);
568 }
569
570 // Test is ignored until Trampoline.requestBackup() is fixed not to throw NPE.
571 @Ignore
572 @Test
573 public void requestBackup_calledBeforeInitialize_ignored() throws RemoteException {
574 assertEquals(0, mTrampoline.requestBackup(PACKAGE_NAMES, mBackupObserverMock,
575 mBackupManagerMonitorMock, 123));
576 verifyNoMoreInteractions(mBackupManagerServiceMock);
577 }
578
579 @Test
580 public void requestBackup_forwarded() throws RemoteException {
581 when(mBackupManagerServiceMock.requestBackup(PACKAGE_NAMES, mBackupObserverMock,
582 mBackupManagerMonitorMock, 123)).thenReturn(456);
583
584 mTrampoline.initialize(UserHandle.USER_SYSTEM);
585 assertEquals(456, mTrampoline.requestBackup(PACKAGE_NAMES, mBackupObserverMock,
586 mBackupManagerMonitorMock, 123));
587 verify(mBackupManagerServiceMock).requestBackup(PACKAGE_NAMES, mBackupObserverMock,
588 mBackupManagerMonitorMock, 123);
589 }
590
591 @Test
592 public void cancelBackups_calledBeforeInitialize_ignored() throws RemoteException {
593 mTrampoline.cancelBackups();
594 verifyNoMoreInteractions(mBackupManagerServiceMock);
595 }
596
597 @Test
598 public void cancelBackups_forwarded() throws RemoteException {
599 mTrampoline.initialize(UserHandle.USER_SYSTEM);
600 mTrampoline.cancelBackups();
601 verify(mBackupManagerServiceMock).cancelBackups();
602 }
603
604 @Test
605 public void beginFullBackup_calledBeforeInitialize_ignored() throws RemoteException {
606 mTrampoline.beginFullBackup(new FullBackupJob());
607 verifyNoMoreInteractions(mBackupManagerServiceMock);
608 }
609
610 @Test
611 public void beginFullBackup_forwarded() throws RemoteException {
612 FullBackupJob fullBackupJob = new FullBackupJob();
613 when(mBackupManagerServiceMock.beginFullBackup(fullBackupJob)).thenReturn(true);
614
615 mTrampoline.initialize(UserHandle.USER_SYSTEM);
616 assertTrue(mTrampoline.beginFullBackup(fullBackupJob));
617 verify(mBackupManagerServiceMock).beginFullBackup(fullBackupJob);
618 }
619
620 @Test
621 public void endFullBackup_calledBeforeInitialize_ignored() throws RemoteException {
622 mTrampoline.endFullBackup();
623 verifyNoMoreInteractions(mBackupManagerServiceMock);
624 }
625
626 @Test
627 public void endFullBackup_forwarded() throws RemoteException {
628 mTrampoline.initialize(UserHandle.USER_SYSTEM);
629 mTrampoline.endFullBackup();
630 verify(mBackupManagerServiceMock).endFullBackup();
631 }
632
633 private class TrampolineTestable extends Trampoline {
634 public TrampolineTestable(Context context) {
635 super(context);
636 }
637
638 public TrampolineTestable(Context context, boolean globalDisable, File suppressFile) {
639 super(context, globalDisable, suppressFile);
640 }
641
642 @Override
643 protected BackupManagerServiceInterface createService() {
644 return mBackupManagerServiceMock;
645 }
646 }
647}