diff options
4 files changed, 223 insertions, 450 deletions
diff --git a/services/backup/java/com/android/server/backup/BackupManagerService.java b/services/backup/java/com/android/server/backup/BackupManagerService.java index d5a7c818bfb2..814f6daac43c 100644 --- a/services/backup/java/com/android/server/backup/BackupManagerService.java +++ b/services/backup/java/com/android/server/backup/BackupManagerService.java @@ -195,16 +195,20 @@ public class BackupManagerService { } boolean isAbleToServeUser(int userId) { - return getServiceUsers().get(UserHandle.USER_SYSTEM) != null - && getServiceUsers().get(userId) != null; + return getUserServices().get(UserHandle.USER_SYSTEM) != null + && getUserServices().get(userId) != null; } /** - * Returns a lst of users currently unlocked that have a - * {@link UserBackupManagerService} registered. + * Returns a list of users currently unlocked that have a {@link UserBackupManagerService} + * registered. + * + * Warning: Do NOT modify returned object as it's used inside. + * + * TODO: Return a copy or only expose read-only information through other means. */ @VisibleForTesting - public SparseArray<UserBackupManagerService> getServiceUsers() { + public SparseArray<UserBackupManagerService> getUserServices() { return mServiceUsers; } @@ -495,7 +499,8 @@ public class BackupManagerService { /** * Returns a {@link UserHandle} for the user that has {@code ancestralSerialNumber} as the - * serial number of the its ancestral work profile. + * serial number of the its ancestral work profile or null if there is no {@link + * UserBackupManagerService} associated with that user. * * <p> The ancestral work profile is set by {@link #setAncestralSerialNumber(long)} * and it corresponds to the profile that was used to restore to the callers profile. @@ -504,16 +509,18 @@ public class BackupManagerService { public UserHandle getUserForAncestralSerialNumber(long ancestralSerialNumber) { int callingUserId = Binder.getCallingUserHandle().getIdentifier(); long oldId = Binder.clearCallingIdentity(); - int[] userIds; + final int[] userIds; try { - userIds = mContext.getSystemService(UserManager.class).getProfileIds(callingUserId, - false); + userIds = + mContext + .getSystemService(UserManager.class) + .getProfileIds(callingUserId, false); } finally { Binder.restoreCallingIdentity(oldId); } for (int userId : userIds) { - UserBackupManagerService userBackupManagerService = getServiceUsers().get(userId); + UserBackupManagerService userBackupManagerService = getUserServices().get(userId); if (userBackupManagerService != null) { if (userBackupManagerService.getAncestralSerialNumber() == ancestralSerialNumber) { return UserHandle.of(userId); @@ -880,28 +887,35 @@ public class BackupManagerService { } /** Implementation to receive lifecycle event callbacks for system services. */ - public static final class Lifecycle extends SystemService { + public static class Lifecycle extends SystemService { public Lifecycle(Context context) { + this(context, new Trampoline(context)); + } + + @VisibleForTesting + Lifecycle(Context context, Trampoline trampoline) { super(context); - sInstance = new Trampoline(context); + sInstance = trampoline; } @Override public void onStart() { - publishBinderService(Context.BACKUP_SERVICE, sInstance); + publishService(Context.BACKUP_SERVICE, sInstance); } @Override public void onUnlockUser(int userId) { - if (userId == UserHandle.USER_SYSTEM) { - sInstance.initializeService(); - } - sInstance.unlockUser(userId); + sInstance.onUnlockUser(userId); } @Override public void onStopUser(int userId) { - sInstance.stopUser(userId); + sInstance.onStopUser(userId); + } + + @VisibleForTesting + void publishService(String name, IBinder service) { + publishBinderService(name, service); } } } diff --git a/services/backup/java/com/android/server/backup/Trampoline.java b/services/backup/java/com/android/server/backup/Trampoline.java index f4b66456c27b..210a9ef94e53 100644 --- a/services/backup/java/com/android/server/backup/Trampoline.java +++ b/services/backup/java/com/android/server/backup/Trampoline.java @@ -40,12 +40,12 @@ import android.os.ParcelFileDescriptor; import android.os.Process; import android.os.RemoteException; import android.os.SystemProperties; -import android.os.Trace; import android.os.UserHandle; import android.os.UserManager; import android.util.Slog; import com.android.internal.annotations.GuardedBy; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.DumpUtils; import com.android.server.backup.utils.RandomAccessFileUtils; @@ -73,9 +73,6 @@ import java.io.PrintWriter; * Temporary disabling is controlled by {@link #setBackupServiceActive(int, boolean)} through * privileged callers (currently {@link DevicePolicyManager}). This is called on {@link * UserHandle#USER_SYSTEM} and disables backup for all users. - * - * <p>Creation of the backup service is done when {@link UserHandle#USER_SYSTEM} is unlocked. The - * system user is unlocked before any other users. */ public class Trampoline extends IBackupManager.Stub { /** @@ -109,7 +106,10 @@ public class Trampoline extends IBackupManager.Stub { // TODD(b/121198006): remove this object and synchronized all methods on "this". private final Object mStateLock = new Object(); - private volatile BackupManagerService mService; + // TODO: This is not marked as final because of test code. Since we'll merge BMS and Trampoline, + // it doesn't make sense to refactor for final. It's never null. + @VisibleForTesting + protected volatile BackupManagerService mService; private final Handler mHandler; public Trampoline(Context context) { @@ -120,6 +120,13 @@ public class Trampoline extends IBackupManager.Stub { handlerThread.start(); mHandler = new Handler(handlerThread.getLooper()); mUserManager = UserManager.get(context); + mService = new BackupManagerService(mContext, this); + } + + // TODO: Remove this when we implement DI by injecting in the construtor. + @VisibleForTesting + Handler getBackupHandler() { + return mHandler; } protected boolean isBackupDisabled() { @@ -205,7 +212,7 @@ public class Trampoline extends IBackupManager.Stub { // This method should not perform any I/O (e.g. do not call isBackupActivatedForUser), // it's used in multiple places where I/O waits would cause system lock-ups. private boolean isUserReadyForBackup(int userId) { - return mService != null && mService.isAbleToServeUser(userId); + return mService.isAbleToServeUser(userId); } /** @@ -230,68 +237,55 @@ public class Trampoline extends IBackupManager.Stub { return mUserManager; } - protected BackupManagerService createBackupManagerService() { - return new BackupManagerService(mContext, this); - } - protected void postToHandler(Runnable runnable) { mHandler.post(runnable); } /** - * Called from {@link BackupManagerService.Lifecycle} when the system user is unlocked. Attempts - * to initialize {@link BackupManagerService}. Offloads work onto the handler thread {@link - * #mHandlerThread} to keep unlock time low. - */ - void initializeService() { - postToHandler( - () -> { - Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backup init"); - if (mGlobalDisable) { - Slog.i(TAG, "Backup service not supported"); - return; - } - synchronized (mStateLock) { - if (mService == null) { - mService = createBackupManagerService(); - } - } - Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); - }); - } - - /** * Called from {@link BackupManagerService.Lifecycle} when a user {@code userId} is unlocked. * Starts the backup service for this user if backup is active for this user. Offloads work onto - * the handler thread {@link #mHandlerThread} to keep unlock time low. + * the handler thread {@link #mHandlerThread} to keep unlock time low since backup is not + * essential for device functioning. */ - void unlockUser(int userId) { + void onUnlockUser(int userId) { postToHandler(() -> startServiceForUser(userId)); } private void startServiceForUser(int userId) { // We know that the user is unlocked here because it is called from setBackupServiceActive // and unlockUser which have these guarantees. So we can check if the file exists. - if (mService != null && isBackupActivatedForUser(userId)) { - Slog.i(TAG, "Starting service for user: " + userId); - mService.startServiceForUser(userId); + if (mGlobalDisable) { + Slog.i(TAG, "Backup service not supported"); + return; } + if (!isBackupActivatedForUser(userId)) { + Slog.i(TAG, "Backup not activated for user " + userId); + return; + } + Slog.i(TAG, "Starting service for user: " + userId); + mService.startServiceForUser(userId); } /** * Called from {@link BackupManagerService.Lifecycle} when a user {@code userId} is stopped. * Offloads work onto the handler thread {@link #mHandlerThread} to keep stopping time low. */ - void stopUser(int userId) { + void onStopUser(int userId) { postToHandler( () -> { - if (mService != null) { + if (!mGlobalDisable) { Slog.i(TAG, "Stopping service for user: " + userId); mService.stopServiceForUser(userId); } }); } + /** Returns {@link UserBackupManagerService} for user {@code userId}. */ + @Nullable + public UserBackupManagerService getUserService(int userId) { + return mService.getUserServices().get(userId); + } + /** * The system user and managed profiles can only be acted on by callers in the system or root * processes. Other users can be acted on by callers who have both android.permission.BACKUP and @@ -350,9 +344,6 @@ public class Trampoline extends IBackupManager.Stub { synchronized (mStateLock) { Slog.i(TAG, "Making backup " + (makeActive ? "" : "in") + "active"); if (makeActive) { - if (mService == null) { - mService = createBackupManagerService(); - } try { activateBackupForUserLocked(userId); } catch (IOException e) { @@ -380,7 +371,7 @@ public class Trampoline extends IBackupManager.Stub { } //TODO(b/121198006): loop through active users that have work profile and // stop them as well. - stopUser(userId); + onStopUser(userId); } } } @@ -388,8 +379,7 @@ public class Trampoline extends IBackupManager.Stub { // IBackupManager binder API /** - * Querying activity state of backup service. Calling this method before initialize yields - * undefined result. + * Querying activity state of backup service. * * @param userId The user in which the activity state of backup service is queried. * @return true if the service is active. @@ -397,7 +387,7 @@ public class Trampoline extends IBackupManager.Stub { @Override public boolean isBackupServiceActive(int userId) { synchronized (mStateLock) { - return mService != null && isBackupActivatedForUser(userId); + return !mGlobalDisable && isBackupActivatedForUser(userId); } } @@ -598,8 +588,8 @@ public class Trampoline extends IBackupManager.Stub { @Override @Nullable public ComponentName getCurrentTransportComponentForUser(int userId) { - return (isUserReadyForBackup(userId)) ? mService.getCurrentTransportComponent(userId) - : null; + return (isUserReadyForBackup(userId)) + ? mService.getCurrentTransportComponent(userId) : null; } @Override @@ -614,8 +604,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public ComponentName[] listAllTransportComponentsForUser(int userId) throws RemoteException { - return (isUserReadyForBackup(userId)) ? mService.listAllTransportComponents(userId) - : null; + return (isUserReadyForBackup(userId)) + ? mService.listAllTransportComponents(userId) : null; } @Override @@ -648,8 +638,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public String selectBackupTransportForUser(int userId, String transport) throws RemoteException { - return (isUserReadyForBackup(userId)) ? mService.selectBackupTransport(userId, transport) - : null; + return (isUserReadyForBackup(userId)) + ? mService.selectBackupTransport(userId, transport) : null; } @Override @@ -700,8 +690,8 @@ public class Trampoline extends IBackupManager.Stub { @Override public Intent getDataManagementIntentForUser(int userId, String transport) throws RemoteException { - return isUserReadyForBackup(userId) ? mService.getDataManagementIntent(userId, transport) - : null; + return isUserReadyForBackup(userId) + ? mService.getDataManagementIntent(userId, transport) : null; } @Override @@ -784,15 +774,15 @@ public class Trampoline extends IBackupManager.Stub { @Override @Nullable public UserHandle getUserForAncestralSerialNumber(long ancestralSerialNumber) { - if (mService != null) { - return mService.getUserForAncestralSerialNumber(ancestralSerialNumber); + if (mGlobalDisable) { + return null; } - return null; + return mService.getUserForAncestralSerialNumber(ancestralSerialNumber); } @Override public void setAncestralSerialNumber(long ancestralSerialNumber) { - if (mService != null) { + if (!mGlobalDisable) { mService.setAncestralSerialNumber(ancestralSerialNumber); } } diff --git a/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java b/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java index be93d6ba5352..e55c4a677dbc 100644 --- a/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java +++ b/services/robotests/backup/src/com/android/server/backup/BackupManagerServiceTest.java @@ -26,8 +26,12 @@ import static com.android.server.backup.testing.TransportData.backupTransport; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.robolectric.Shadows.shadowOf; import static org.testng.Assert.expectThrows; @@ -121,7 +125,7 @@ public class BackupManagerServiceTest { public void testConstructor_doesNotRegisterUsers() throws Exception { BackupManagerService backupManagerService = createService(); - assertThat(backupManagerService.getServiceUsers().size()).isEqualTo(0); + assertThat(backupManagerService.getUserServices().size()).isEqualTo(0); } /** Test that the constructor handles {@code null} parameters. */ @@ -152,7 +156,7 @@ public class BackupManagerServiceTest { backupManagerService.startServiceForUser(mUserOneId); - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getServiceUsers(); + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); assertThat(serviceUsers.size()).isEqualTo(1); assertThat(serviceUsers.get(mUserOneId)).isNotNull(); } @@ -164,7 +168,7 @@ public class BackupManagerServiceTest { backupManagerService.startServiceForUser(mUserOneId, mUserOneService); - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getServiceUsers(); + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); assertThat(serviceUsers.size()).isEqualTo(1); assertThat(serviceUsers.get(mUserOneId)).isEqualTo(mUserOneService); } @@ -178,7 +182,7 @@ public class BackupManagerServiceTest { backupManagerService.stopServiceForUser(mUserOneId); - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getServiceUsers(); + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); assertThat(serviceUsers.size()).isEqualTo(1); assertThat(serviceUsers.get(mUserOneId)).isNull(); assertThat(serviceUsers.get(mUserTwoId)).isEqualTo(mUserTwoService); @@ -204,7 +208,7 @@ public class BackupManagerServiceTest { backupManagerService.stopServiceForUser(mUserOneId); - SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getServiceUsers(); + SparseArray<UserBackupManagerService> serviceUsers = backupManagerService.getUserServices(); assertThat(serviceUsers.size()).isEqualTo(0); } @@ -1500,6 +1504,48 @@ public class BackupManagerServiceTest { } // --------------------------------------------- + // Lifecycle tests + // --------------------------------------------- + + + /** testOnStart_publishesService */ + @Test + public void testOnStart_publishesService() { + Trampoline trampoline = mock(Trampoline.class); + BackupManagerService.Lifecycle lifecycle = + spy(new BackupManagerService.Lifecycle(mContext, trampoline)); + doNothing().when(lifecycle).publishService(anyString(), any()); + + lifecycle.onStart(); + + verify(lifecycle).publishService(Context.BACKUP_SERVICE, trampoline); + } + + /** testOnUnlockUser_forwards */ + @Test + public void testOnUnlockUser_forwards() { + Trampoline trampoline = mock(Trampoline.class); + BackupManagerService.Lifecycle lifecycle = + new BackupManagerService.Lifecycle(mContext, trampoline); + + lifecycle.onUnlockUser(UserHandle.USER_SYSTEM); + + verify(trampoline).onUnlockUser(UserHandle.USER_SYSTEM); + } + + /** testOnStopUser_forwards */ + @Test + public void testOnStopUser_forwards() { + Trampoline trampoline = mock(Trampoline.class); + BackupManagerService.Lifecycle lifecycle = + new BackupManagerService.Lifecycle(mContext, trampoline); + + lifecycle.onStopUser(UserHandle.USER_SYSTEM); + + verify(trampoline).onStopUser(UserHandle.USER_SYSTEM); + } + + // --------------------------------------------- // Service tests // --------------------------------------------- diff --git a/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java b/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java index 29cbf988fd56..3da2fd3b6a8e 100644 --- a/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java +++ b/services/tests/servicestests/src/com/android/server/backup/TrampolineTest.java @@ -18,7 +18,6 @@ package com.android.server.backup; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; @@ -44,6 +43,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.UserInfo; +import android.os.ConditionVariable; import android.os.IBinder; import android.os.ParcelFileDescriptor; import android.os.Process; @@ -70,7 +70,7 @@ import java.io.File; import java.io.FileDescriptor; import java.io.IOException; import java.io.PrintWriter; -import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @SmallTest @@ -136,7 +136,7 @@ public class TrampolineTest { SparseArray<UserBackupManagerService> serviceUsers = new SparseArray<>(); serviceUsers.append(UserHandle.USER_SYSTEM, mUserBackupManagerService); serviceUsers.append(NON_USER_SYSTEM, mUserBackupManagerService); - when(mBackupManagerServiceMock.getServiceUsers()).thenReturn(serviceUsers); + when(mBackupManagerServiceMock.getUserServices()).thenReturn(serviceUsers); when(mUserManagerMock.getUserInfo(UserHandle.USER_SYSTEM)).thenReturn(mUserInfoMock); when(mUserManagerMock.getUserInfo(NON_USER_SYSTEM)).thenReturn(mUserInfoMock); @@ -182,37 +182,76 @@ public class TrampolineTest { } @Test - public void initializeService_successfullyInitializesBackupService() { - mTrampoline.initializeService(); - + public void testIsBackupServiceActive_whenBackupsNotDisabledAndSuppressFileDoesNotExist() { assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); } @Test - public void initializeService_globallyDisabled_nonInitialized() { + public void testOnUnlockUser_forNonSystemUserWhenBackupsDisabled_doesNotStartUser() { + when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); TrampolineTestable.sBackupDisabled = true; TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + ConditionVariable unlocked = new ConditionVariable(false); - trampoline.initializeService(); + trampoline.onUnlockUser(NON_USER_SYSTEM); - assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); + trampoline.getBackupHandler().post(unlocked::open); + unlocked.block(); + assertNull(trampoline.getUserService(NON_USER_SYSTEM)); } @Test - public void initializeService_doesNotStartServiceForUsers() { - mTrampoline.initializeService(); + public void testOnUnlockUser_forSystemUserWhenBackupsDisabled_doesNotStartUser() { + when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); + TrampolineTestable.sBackupDisabled = true; + TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + ConditionVariable unlocked = new ConditionVariable(false); - verify(mBackupManagerServiceMock, never()).startServiceForUser(anyInt()); + trampoline.onUnlockUser(UserHandle.USER_SYSTEM); + + trampoline.getBackupHandler().post(unlocked::open); + unlocked.block(); + assertNull(trampoline.getUserService(UserHandle.USER_SYSTEM)); } @Test - public void isBackupServiceActive_calledBeforeInitialize_returnsFalse() { - assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); + public void testOnUnlockUser_whenBackupNotActivated_doesNotStartUser() { + when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); + TrampolineTestable.sBackupDisabled = false; + TrampolineTestable trampoline = new TrampolineTestable(mContextMock); + trampoline.setBackupServiceActive(NON_USER_SYSTEM, false); + ConditionVariable unlocked = new ConditionVariable(false); + + trampoline.onUnlockUser(NON_USER_SYSTEM); + + trampoline.getBackupHandler().post(unlocked::open); + unlocked.block(); + assertNull(trampoline.getUserService(NON_USER_SYSTEM)); + verify(mBackupManagerServiceMock, never()).startServiceForUser(NON_USER_SYSTEM); + } + + @Test + public void testIsBackupServiceActive_forSystemUserWhenBackupDisabled_returnsTrue() + throws Exception { + TrampolineTestable.sBackupDisabled = true; + Trampoline trampoline = new TrampolineTestable(mContextMock); + trampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); + + assertFalse(trampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); + } + + @Test + public void testIsBackupServiceActive_forNonSystemUserWhenBackupDisabled_returnsTrue() + throws Exception { + TrampolineTestable.sBackupDisabled = true; + Trampoline trampoline = new TrampolineTestable(mContextMock); + trampoline.setBackupServiceActive(NON_USER_SYSTEM, true); + + assertFalse(trampoline.isBackupServiceActive(NON_USER_SYSTEM)); } @Test public void isBackupServiceActive_forSystemUser_returnsTrueWhenActivated() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); @@ -220,7 +259,6 @@ public class TrampolineTest { @Test public void isBackupServiceActive_forSystemUser_returnsFalseWhenDeactivated() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); assertFalse(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); @@ -229,7 +267,6 @@ public class TrampolineTest { @Test public void isBackupServiceActive_forNonSystemUser_returnsFalseWhenSystemUserDeactivated() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -239,7 +276,6 @@ public class TrampolineTest { @Test public void isBackupServiceActive_forNonSystemUser_returnsFalseWhenNonSystemUserDeactivated() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); // Don't activate non-system user. @@ -250,7 +286,6 @@ public class TrampolineTest { public void isBackupServiceActive_forNonSystemUser_returnsTrueWhenSystemAndNonSystemUserActivated() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -261,7 +296,6 @@ public class TrampolineTest { public void isBackupServiceActive_forUnstartedNonSystemUser_returnsTrueWhenSystemAndUserActivated() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UNSTARTED_NON_USER_SYSTEM, true); assertTrue(mTrampoline.isBackupServiceActive(UNSTARTED_NON_USER_SYSTEM)); @@ -269,7 +303,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forSystemUserAndCallerSystemUid_serviceCreated() { - mTrampoline.initializeService(); TrampolineTestable.sCallingUid = Process.SYSTEM_UID; mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); @@ -279,7 +312,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forSystemUserAndCallerRootUid_serviceCreated() { - mTrampoline.initializeService(); TrampolineTestable.sCallingUid = Process.ROOT_UID; mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); @@ -289,7 +321,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forSystemUserAndCallerNonRootNonSystem_throws() { - mTrampoline.initializeService(); TrampolineTestable.sCallingUid = Process.FIRST_APPLICATION_UID; try { @@ -302,7 +333,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forManagedProfileAndCallerSystemUid_serviceCreated() { when(mUserInfoMock.isManagedProfile()).thenReturn(true); - mTrampoline.initializeService(); TrampolineTestable.sCallingUid = Process.SYSTEM_UID; mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -313,7 +343,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forManagedProfileAndCallerRootUid_serviceCreated() { when(mUserInfoMock.isManagedProfile()).thenReturn(true); - mTrampoline.initializeService(); TrampolineTestable.sCallingUid = Process.ROOT_UID; mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -324,7 +353,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forManagedProfileAndCallerNonRootNonSystem_throws() { when(mUserInfoMock.isManagedProfile()).thenReturn(true); - mTrampoline.initializeService(); TrampolineTestable.sCallingUid = Process.FIRST_APPLICATION_UID; try { @@ -339,7 +367,6 @@ public class TrampolineTest { doThrow(new SecurityException()) .when(mContextMock) .enforceCallingOrSelfPermission(eq(Manifest.permission.BACKUP), anyString()); - mTrampoline.initializeService(); try { mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -354,7 +381,6 @@ public class TrampolineTest { .when(mContextMock) .enforceCallingOrSelfPermission( eq(Manifest.permission.INTERACT_ACROSS_USERS_FULL), anyString()); - mTrampoline.initializeService(); try { mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -367,7 +393,6 @@ public class TrampolineTest { public void setBackupServiceActive_backupDisabled_ignored() { TrampolineTestable.sBackupDisabled = true; TrampolineTestable trampoline = new TrampolineTestable(mContextMock); - trampoline.initializeService(); trampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); @@ -376,19 +401,15 @@ public class TrampolineTest { @Test public void setBackupServiceActive_alreadyActive_ignored() { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); - assertEquals(1, mTrampoline.getCreateServiceCallsCount()); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); - assertEquals(1, mTrampoline.getCreateServiceCallsCount()); } @Test public void setBackupServiceActive_makeNonActive_alreadyNonActive_ignored() { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); @@ -397,7 +418,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_makeActive_serviceCreatedAndSuppressFileDeleted() { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, true); assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); @@ -406,7 +426,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_makeNonActive_serviceDeletedAndSuppressFileCreated() throws IOException { - mTrampoline.initializeService(); assertTrue(mTrampoline.isBackupServiceActive(UserHandle.USER_SYSTEM)); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); @@ -416,7 +435,6 @@ public class TrampolineTest { @Test public void setBackupActive_nonSystemUser_disabledForSystemUser_ignored() { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(UserHandle.USER_SYSTEM, false); mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -425,7 +443,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forOneNonSystemUser_doesNotActivateForAllNonSystemUsers() { - mTrampoline.initializeService(); int otherUser = NON_USER_SYSTEM + 1; File activateFile = new File(mTestDir, "activate-" + otherUser); TrampolineTestable.sActivatedFiles.append(otherUser, activateFile); @@ -440,7 +457,6 @@ public class TrampolineTest { @Test public void setBackupServiceActive_forNonSystemUser_remembersActivated() { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); @@ -450,7 +466,6 @@ public class TrampolineTest { @Test public void setBackupServiceActiveFalse_forNonSystemUser_remembersActivated() { - mTrampoline.initializeService(); mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, false); @@ -460,8 +475,6 @@ public class TrampolineTest { @Test public void setBackupServiceActiveTwice_forNonSystemUser_remembersLastActivated() { - mTrampoline.initializeService(); - mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, true); mTrampoline.setBackupServiceActive(NON_USER_SYSTEM, false); @@ -470,15 +483,7 @@ public class TrampolineTest { } @Test - public void dataChanged_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.dataChanged(PACKAGE_NAME); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void dataChangedForUser_forwarded() throws Exception { - mTrampoline.initializeService(); - mTrampoline.dataChangedForUser(mUserId, PACKAGE_NAME); verify(mBackupManagerServiceMock).dataChanged(mUserId, PACKAGE_NAME); @@ -487,7 +492,6 @@ public class TrampolineTest { @Test public void dataChanged_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.dataChanged(PACKAGE_NAME); @@ -495,14 +499,7 @@ public class TrampolineTest { } @Test - public void clearBackupData_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void clearBackupDataForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.clearBackupDataForUser(mUserId, TRANSPORT_NAME, PACKAGE_NAME); @@ -512,7 +509,6 @@ public class TrampolineTest { @Test public void clearBackupData_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.clearBackupData(TRANSPORT_NAME, PACKAGE_NAME); @@ -520,14 +516,7 @@ public class TrampolineTest { } @Test - public void agentConnected_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void agentConnectedForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.agentConnectedForUser(mUserId, PACKAGE_NAME, mAgentMock); @@ -537,7 +526,6 @@ public class TrampolineTest { @Test public void agentConnected_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.agentConnected(PACKAGE_NAME, mAgentMock); @@ -545,14 +533,7 @@ public class TrampolineTest { } @Test - public void agentDisconnected_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.agentDisconnected(PACKAGE_NAME); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void agentDisconnectedForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.agentDisconnectedForUser(mUserId, PACKAGE_NAME); @@ -562,7 +543,6 @@ public class TrampolineTest { @Test public void agentDisconnected_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.agentDisconnected(PACKAGE_NAME); @@ -570,14 +550,7 @@ public class TrampolineTest { } @Test - public void restoreAtInstall_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.restoreAtInstall(PACKAGE_NAME, 123); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void restoreAtInstallForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.restoreAtInstallForUser(mUserId, PACKAGE_NAME, 123); @@ -587,7 +560,6 @@ public class TrampolineTest { @Test public void restoreAtInstall_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.restoreAtInstall(PACKAGE_NAME, 123); @@ -595,14 +567,7 @@ public class TrampolineTest { } @Test - public void setBackupEnabled_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.setBackupEnabled(true); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void setBackupEnabledForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupEnabledForUser(mUserId, true); @@ -612,7 +577,6 @@ public class TrampolineTest { @Test public void setBackupEnabled_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.setBackupEnabled(true); @@ -620,14 +584,7 @@ public class TrampolineTest { } @Test - public void setAutoRestore_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.setAutoRestore(true); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void setAutoRestoreForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.setAutoRestoreForUser(mUserId, true); @@ -637,7 +594,6 @@ public class TrampolineTest { @Test public void setAutoRestore_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.setAutoRestore(true); @@ -645,14 +601,7 @@ public class TrampolineTest { } @Test - public void isBackupEnabled_calledBeforeInitialize_ignored() throws Exception { - assertFalse(mTrampoline.isBackupEnabled()); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void isBackupEnabledForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.isBackupEnabledForUser(mUserId); @@ -662,7 +611,6 @@ public class TrampolineTest { @Test public void isBackupEnabled_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.isBackupEnabled(); @@ -670,40 +618,19 @@ public class TrampolineTest { } @Test - public void setBackupPassword_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void setBackupPassword_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); verify(mBackupManagerServiceMock).setBackupPassword(CURRENT_PASSWORD, NEW_PASSWORD); } @Test - public void hasBackupPassword_calledBeforeInitialize_ignored() throws Exception { - assertFalse(mTrampoline.hasBackupPassword()); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void hasBackupPassword_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.hasBackupPassword(); verify(mBackupManagerServiceMock).hasBackupPassword(); } @Test - public void backupNow_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.backupNow(); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void backupNowForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.backupNowForUser(mUserId); @@ -713,7 +640,6 @@ public class TrampolineTest { @Test public void backupNow_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.backupNow(); @@ -721,16 +647,7 @@ public class TrampolineTest { } @Test - public void adbBackup_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true, - true, true, true, true, true, true, - PACKAGE_NAMES); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void adbBackup_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.adbBackup(mUserId, mParcelFileDescriptorMock, true, true, true, true, true, true, true, true, PACKAGE_NAMES); @@ -739,14 +656,7 @@ public class TrampolineTest { } @Test - public void fullTransportBackup_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void fullTransportBackupForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.fullTransportBackupForUser(mUserId, PACKAGE_NAMES); @@ -754,29 +664,13 @@ public class TrampolineTest { } @Test - public void adbRestore_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void adbRestore_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.adbRestore(mUserId, mParcelFileDescriptorMock); verify(mBackupManagerServiceMock).adbRestore(mUserId, mParcelFileDescriptorMock); } @Test - public void acknowledgeFullBackupOrRestore_calledBeforeInitialize_ignored() - throws Exception { - mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD, - mFullBackupRestoreObserverMock); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void acknowledgeFullBackupOrRestoreForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.acknowledgeFullBackupOrRestoreForUser( mUserId, @@ -799,7 +693,6 @@ public class TrampolineTest { @Test public void acknowledgeFullBackupOrRestore_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.acknowledgeFullBackupOrRestore(123, true, CURRENT_PASSWORD, ENCRYPTION_PASSWORD, mFullBackupRestoreObserverMock); @@ -815,15 +708,8 @@ public class TrampolineTest { } @Test - public void getCurrentTransport_calledBeforeInitialize_ignored() throws Exception { - assertNull(mTrampoline.getCurrentTransport()); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getCurrentTransportForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.getCurrentTransport(mUserId)).thenReturn(TRANSPORT_NAME); - mTrampoline.initializeService(); assertEquals(TRANSPORT_NAME, mTrampoline.getCurrentTransportForUser(mUserId)); verify(mBackupManagerServiceMock).getCurrentTransport(mUserId); @@ -833,22 +719,14 @@ public class TrampolineTest { public void getCurrentTransport_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.getCurrentTransport(mUserId)).thenReturn(TRANSPORT_NAME); - mTrampoline.initializeService(); assertEquals(TRANSPORT_NAME, mTrampoline.getCurrentTransport()); verify(mBackupManagerServiceMock).getCurrentTransport(mUserId); } @Test - public void listAllTransports_calledBeforeInitialize_ignored() throws Exception { - assertNull(mTrampoline.listAllTransports()); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void listAllTransportsForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.listAllTransports(mUserId)).thenReturn(TRANSPORTS); - mTrampoline.initializeService(); assertEquals(TRANSPORTS, mTrampoline.listAllTransportsForUser(mUserId)); verify(mBackupManagerServiceMock).listAllTransports(mUserId); @@ -859,62 +737,31 @@ public class TrampolineTest { public void listAllTransports_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.listAllTransports(mUserId)).thenReturn(TRANSPORTS); - mTrampoline.initializeService(); assertEquals(TRANSPORTS, mTrampoline.listAllTransports()); verify(mBackupManagerServiceMock).listAllTransports(mUserId); } @Test - public void listAllTransportComponentsForUser_calledBeforeInitialize_ignored() - throws Exception { - assertNull(mTrampoline.listAllTransportComponentsForUser(mUserId)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void listAllTransportComponentsForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.listAllTransportComponents(mUserId)).thenReturn( TRANSPORT_COMPONENTS); - mTrampoline.initializeService(); assertEquals(TRANSPORT_COMPONENTS, mTrampoline.listAllTransportComponentsForUser(mUserId)); verify(mBackupManagerServiceMock).listAllTransportComponents(mUserId); } @Test - public void getTransportWhitelist_calledBeforeInitialize_ignored() throws Exception { - assertNull(mTrampoline.getTransportWhitelist()); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getTransportWhitelist_forwarded() { when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS); - mTrampoline.initializeService(); assertEquals(TRANSPORTS, mTrampoline.getTransportWhitelist()); verify(mBackupManagerServiceMock).getTransportWhitelist(); } @Test - public void updateTransportAttributesForUser_calledBeforeInitialize_ignored() { - mTrampoline.updateTransportAttributesForUser( - mUserId, - TRANSPORT_COMPONENT_NAME, - TRANSPORT_NAME, - null, - "Transport Destination", - null, - "Data Management"); - - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void updateTransportAttributesForUser_forwarded() { when(mBackupManagerServiceMock.getTransportWhitelist()).thenReturn(TRANSPORTS); - mTrampoline.initializeService(); mTrampoline.updateTransportAttributesForUser( mUserId, @@ -937,14 +784,7 @@ public class TrampolineTest { } @Test - public void selectBackupTransport_calledBeforeInitialize_ignored() throws RemoteException { - mTrampoline.selectBackupTransport(TRANSPORT_NAME); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void selectBackupTransportForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.selectBackupTransportForUser(mUserId, TRANSPORT_NAME); @@ -954,7 +794,6 @@ public class TrampolineTest { @Test public void selectBackupTransport_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.selectBackupTransport(TRANSPORT_NAME); @@ -962,75 +801,56 @@ public class TrampolineTest { } @Test - public void selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored() + public void selectBackupTransportAsyncForUser_beforeUserUnlocked_notifiesBackupNotAllowed() throws Exception { - LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue(); - - mTrampoline.selectBackupTransportAsyncForUser( - mUserId, - TRANSPORT_COMPONENT_NAME, - new ISelectBackupTransportCallback() { + when(mBackupManagerServiceMock.getUserServices()).thenReturn(new SparseArray<>()); + CompletableFuture<Integer> future = new CompletableFuture<>(); + ISelectBackupTransportCallback listener = + new ISelectBackupTransportCallback.Stub() { @Override - public void onSuccess(String transportName) throws RemoteException { - + public void onSuccess(String transportName) { + future.completeExceptionally(new AssertionError()); } - @Override - public void onFailure(int reason) throws RemoteException { - q.offer(reason); + public void onFailure(int reason) { + future.complete(reason); } + }; - @Override - public IBinder asBinder() { - return null; - } - }); + mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, listener); - verifyNoMoreInteractions(mBackupManagerServiceMock); - Integer errorCode = q.poll(5, TimeUnit.SECONDS); - assertNotNull(errorCode); - assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, (int) errorCode); + assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, (int) future.get(5, TimeUnit.SECONDS)); } @Test - public void selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored_nullListener() + public void selectBackupTransportAsyncForUser_beforeUserUnlockedWithNullListener_doesNotThrow() throws Exception { mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, null); - verifyNoMoreInteractions(mBackupManagerServiceMock); // No crash. } @Test - public void selectBackupTransportAsyncForUser_calledBeforeInitialize_ignored_listenerThrows() + public void + selectBackupTransportAsyncForUser_beforeUserUnlockedWithThrowingListener_doesNotThrow() throws Exception { - mTrampoline.selectBackupTransportAsyncForUser( - mUserId, - TRANSPORT_COMPONENT_NAME, - new ISelectBackupTransportCallback() { + ISelectBackupTransportCallback.Stub listener = + new ISelectBackupTransportCallback.Stub() { @Override - public void onSuccess(String transportName) throws RemoteException { - - } - + public void onSuccess(String transportName) {} @Override public void onFailure(int reason) throws RemoteException { - throw new RemoteException("Crash"); + throw new RemoteException(); } + }; - @Override - public IBinder asBinder() { - return null; - } - }); + mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, listener); - verifyNoMoreInteractions(mBackupManagerServiceMock); // No crash. } @Test public void selectBackupTransportAsyncForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.selectBackupTransportAsyncForUser(mUserId, TRANSPORT_COMPONENT_NAME, null); @@ -1039,17 +859,10 @@ public class TrampolineTest { } @Test - public void getConfigurationIntent_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.getConfigurationIntent(TRANSPORT_NAME); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getConfigurationIntentForUser_forwarded() throws Exception { Intent configurationIntentStub = new Intent(); when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn( configurationIntentStub); - mTrampoline.initializeService(); assertEquals( configurationIntentStub, @@ -1063,23 +876,15 @@ public class TrampolineTest { Intent configurationIntentStub = new Intent(); when(mBackupManagerServiceMock.getConfigurationIntent(mUserId, TRANSPORT_NAME)).thenReturn( configurationIntentStub); - mTrampoline.initializeService(); assertEquals(configurationIntentStub, mTrampoline.getConfigurationIntent(TRANSPORT_NAME)); verify(mBackupManagerServiceMock).getConfigurationIntent(mUserId, TRANSPORT_NAME); } @Test - public void getDestinationString_calledBeforeInitialize_ignored() throws Exception { - assertNull(mTrampoline.getDestinationString(TRANSPORT_NAME)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getDestinationStringForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn( DESTINATION_STRING); - mTrampoline.initializeService(); assertEquals( DESTINATION_STRING, @@ -1093,23 +898,15 @@ public class TrampolineTest { when(mBackupManagerServiceMock.getDestinationString(mUserId, TRANSPORT_NAME)).thenReturn( DESTINATION_STRING); - mTrampoline.initializeService(); assertEquals(DESTINATION_STRING, mTrampoline.getDestinationString(TRANSPORT_NAME)); verify(mBackupManagerServiceMock).getDestinationString(mUserId, TRANSPORT_NAME); } @Test - public void getDataManagementIntent_calledBeforeInitialize_ignored() throws Exception { - assertNull(mTrampoline.getDataManagementIntent(TRANSPORT_NAME)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getDataManagementIntentForUser_forwarded() throws Exception { Intent dataManagementIntent = new Intent(); when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn( dataManagementIntent); - mTrampoline.initializeService(); assertEquals( dataManagementIntent, @@ -1123,23 +920,15 @@ public class TrampolineTest { Intent dataManagementIntent = new Intent(); when(mBackupManagerServiceMock.getDataManagementIntent(mUserId, TRANSPORT_NAME)).thenReturn( dataManagementIntent); - mTrampoline.initializeService(); assertEquals(dataManagementIntent, mTrampoline.getDataManagementIntent(TRANSPORT_NAME)); verify(mBackupManagerServiceMock).getDataManagementIntent(mUserId, TRANSPORT_NAME); } @Test - public void getDataManagementLabelForUser_calledBeforeInitialize_ignored() throws Exception { - assertNull(mTrampoline.getDataManagementLabelForUser(mUserId, TRANSPORT_NAME)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getDataManagementLabelForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.getDataManagementLabel(mUserId, TRANSPORT_NAME)).thenReturn( DATA_MANAGEMENT_LABEL); - mTrampoline.initializeService(); assertEquals( DATA_MANAGEMENT_LABEL, @@ -1148,14 +937,7 @@ public class TrampolineTest { } @Test - public void beginRestoreSession_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void beginRestoreSessionForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.beginRestoreSessionForUser(mUserId, PACKAGE_NAME, TRANSPORT_NAME); @@ -1164,15 +946,8 @@ public class TrampolineTest { } @Test - public void opComplete_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.opComplete(1, 2); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void opComplete_forwarded() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.opComplete(1, 2); @@ -1180,49 +955,27 @@ public class TrampolineTest { } @Test - public void getAvailableRestoreTokenForUser_calledBeforeInitialize_ignored() { - assertEquals(0, mTrampoline.getAvailableRestoreTokenForUser(mUserId, PACKAGE_NAME)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void getAvailableRestoreTokenForUser_forwarded() { when(mBackupManagerServiceMock.getAvailableRestoreToken(mUserId, PACKAGE_NAME)) .thenReturn(123L); - mTrampoline.initializeService(); assertEquals(123, mTrampoline.getAvailableRestoreTokenForUser(mUserId, PACKAGE_NAME)); verify(mBackupManagerServiceMock).getAvailableRestoreToken(mUserId, PACKAGE_NAME); } @Test - public void isAppEligibleForBackupForUser_calledBeforeInitialize_ignored() { - assertFalse(mTrampoline.isAppEligibleForBackupForUser(mUserId, PACKAGE_NAME)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void isAppEligibleForBackupForUser_forwarded() { when(mBackupManagerServiceMock.isAppEligibleForBackup(mUserId, PACKAGE_NAME)) .thenReturn(true); - mTrampoline.initializeService(); assertTrue(mTrampoline.isAppEligibleForBackupForUser(mUserId, PACKAGE_NAME)); verify(mBackupManagerServiceMock).isAppEligibleForBackup(mUserId, PACKAGE_NAME); } @Test - public void requestBackup_calledBeforeInitialize_ignored() throws RemoteException { - assertEquals(BackupManager.ERROR_BACKUP_NOT_ALLOWED, mTrampoline.requestBackup( - PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void requestBackupForUser_forwarded() throws Exception { when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456); - mTrampoline.initializeService(); assertEquals(456, mTrampoline.requestBackupForUser(mUserId, PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)); @@ -1235,7 +988,6 @@ public class TrampolineTest { TrampolineTestable.sCallingUserId = mUserId; when(mBackupManagerServiceMock.requestBackup(mUserId, PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)).thenReturn(456); - mTrampoline.initializeService(); assertEquals(456, mTrampoline.requestBackup(PACKAGE_NAMES, mBackupObserverMock, mBackupManagerMonitorMock, 123)); @@ -1244,14 +996,7 @@ public class TrampolineTest { } @Test - public void cancelBackups_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.cancelBackups(); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void cancelBackupsForUser_forwarded() throws Exception { - mTrampoline.initializeService(); mTrampoline.cancelBackupsForUser(mUserId); @@ -1261,7 +1006,6 @@ public class TrampolineTest { @Test public void cancelBackups_forwardedToCallingUserId() throws Exception { TrampolineTestable.sCallingUserId = mUserId; - mTrampoline.initializeService(); mTrampoline.cancelBackups(); @@ -1269,30 +1013,16 @@ public class TrampolineTest { } @Test - public void beginFullBackup_calledBeforeInitialize_ignored() throws Exception { - mTrampoline.beginFullBackup(mUserId, new FullBackupJob()); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void beginFullBackup_forwarded() throws Exception { FullBackupJob fullBackupJob = new FullBackupJob(); when(mBackupManagerServiceMock.beginFullBackup(mUserId, fullBackupJob)).thenReturn(true); - mTrampoline.initializeService(); assertTrue(mTrampoline.beginFullBackup(mUserId, fullBackupJob)); verify(mBackupManagerServiceMock).beginFullBackup(mUserId, fullBackupJob); } @Test - public void endFullBackup_calledBeforeInitialize_ignored() { - mTrampoline.endFullBackup(mUserId); - verifyNoMoreInteractions(mBackupManagerServiceMock); - } - - @Test public void endFullBackup_forwarded() { - mTrampoline.initializeService(); mTrampoline.endFullBackup(mUserId); verify(mBackupManagerServiceMock).endFullBackup(mUserId); } @@ -1302,7 +1032,6 @@ public class TrampolineTest { when(mContextMock.checkCallingOrSelfPermission( android.Manifest.permission.DUMP)).thenReturn( PackageManager.PERMISSION_DENIED); - mTrampoline.initializeService(); mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, new String[0]); @@ -1310,26 +1039,30 @@ public class TrampolineTest { } @Test - public void dump_calledBeforeInitialize_ignored() { + public void dump_callerHasPermission_forwarded() { when(mContextMock.checkCallingOrSelfPermission( android.Manifest.permission.DUMP)).thenReturn( PackageManager.PERMISSION_GRANTED); - mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, new String[0]); + mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, null); - verifyNoMoreInteractions(mBackupManagerServiceMock); + verify(mBackupManagerServiceMock).dump(mFileDescriptorStub, mPrintWriterMock, null); } - @Test - public void dump_callerHasPermission_forwarded() { - when(mContextMock.checkCallingOrSelfPermission( - android.Manifest.permission.DUMP)).thenReturn( - PackageManager.PERMISSION_GRANTED); - mTrampoline.initializeService(); + public void testGetUserForAncestralSerialNumber() { + TrampolineTestable.sBackupDisabled = false; + Trampoline trampoline = new TrampolineTestable(mContextMock); - mTrampoline.dump(mFileDescriptorStub, mPrintWriterMock, null); + trampoline.getUserForAncestralSerialNumber(0L); + verify(mBackupManagerServiceMock).getUserForAncestralSerialNumber(anyInt()); + } - verify(mBackupManagerServiceMock).dump(mFileDescriptorStub, mPrintWriterMock, null); + public void testGetUserForAncestralSerialNumber_whenDisabled() { + TrampolineTestable.sBackupDisabled = true; + Trampoline trampoline = new TrampolineTestable(mContextMock); + + trampoline.getUserForAncestralSerialNumber(0L); + verify(mBackupManagerServiceMock, never()).getUserForAncestralSerialNumber(anyInt()); } private static class TrampolineTestable extends Trampoline { @@ -1341,10 +1074,10 @@ public class TrampolineTest { static SparseArray<File> sActivatedFiles = new SparseArray<>(); static SparseArray<File> sRememberActivatedFiles = new SparseArray<>(); static UserManager sUserManagerMock = null; - private int mCreateServiceCallsCount = 0; TrampolineTestable(Context context) { super(context); + mService = sBackupManagerServiceMock; } @Override @@ -1353,7 +1086,7 @@ public class TrampolineTest { } @Override - public boolean isBackupDisabled() { + protected boolean isBackupDisabled() { return sBackupDisabled; } @@ -1382,18 +1115,8 @@ public class TrampolineTest { } @Override - protected BackupManagerService createBackupManagerService() { - mCreateServiceCallsCount++; - return sBackupManagerServiceMock; - } - - @Override protected void postToHandler(Runnable runnable) { runnable.run(); } - - int getCreateServiceCallsCount() { - return mCreateServiceCallsCount; - } } } |