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