Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 374b2522 authored by Bernardo Rufino's avatar Bernardo Rufino
Browse files

Increase PerformBackupTask unit coverage 3

With KV Refactor in mind:
* Tests around calling agent.
* Tests around agent writing data.
* Tests around transport handling agent data.
* Small modification on ShadowBackupData{Input,Output} to allow empty data.
* Small refactor.

Test: atest PerformBackupTaskTest
Change-Id: Ia9296859926d09d5bbcb0532fb869ace110240e4
parent 86ec41e2
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ import android.os.ServiceManager;
import android.os.SystemClock;
import android.os.Trace;
import android.os.UserHandle;
import android.os.WorkSource;
import android.os.storage.IStorageManager;
import android.os.storage.StorageManager;
import android.provider.Settings;
@@ -387,6 +388,16 @@ public class BackupManagerService implements BackupManagerServiceInterface {
        return mWakelock;
    }

    /**
     * Sets the {@link WorkSource} of the {@link PowerManager.WakeLock} returned by {@link
     * #getWakelock()}.
     */
    @VisibleForTesting
    public void setWorkSource(@Nullable WorkSource workSource) {
        // TODO: This is for testing, unfortunately WakeLock is final and WorkSource is not exposed
        mWakelock.setWorkSource(workSource);
    }

    public void setWakelock(PowerManager.WakeLock wakelock) {
        mWakelock = wakelock;
    }
@@ -1480,8 +1491,8 @@ public class BackupManagerService implements BackupManagerServiceInterface {
        }
    }

    // fire off a backup agent, blocking until it attaches or times out
    @Override
    /** Fires off a backup agent, blocking until it attaches or times out. */
    @Nullable
    public IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode) {
        IBackupAgent agent = null;
        synchronized (mAgentConnectLock) {
@@ -1529,6 +1540,14 @@ public class BackupManagerService implements BackupManagerServiceInterface {
        return agent;
    }

    public void unbindAgent(ApplicationInfo app) {
        try {
            mActivityManager.unbindBackupAgent(app);
        } catch (RemoteException e) {
            // Can't happen - activity manager is local
        }
    }

    // clear an application's data, blocking until the operation completes or times out
    // if keepSystemState is true, we intentionally do not also clear system state that
    // would ordinarily also be cleared, because we aren't actually wiping the app back
+0 −3
Original line number Diff line number Diff line
@@ -49,9 +49,6 @@ public interface BackupManagerServiceInterface {

  boolean hasBackupPassword();

  // fire off a backup agent, blocking until it attaches or times out
  IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode);

  // Get the restore-set token for the best-available restore set for this package:
  // the active set if possible, else the ancestral one.  Returns zero if none available.
  long getAvailableRestoreToken(String packageName);
+2 −2
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ public class KeyValueAdbBackupEngine {
    private static final String BACKUP_KEY_VALUE_BACKUP_DATA_FILENAME_SUFFIX = ".data";
    private static final String BACKUP_KEY_VALUE_NEW_STATE_FILENAME_SUFFIX = ".new";

    private BackupManagerServiceInterface mBackupManagerService;
    private BackupManagerService mBackupManagerService;
    private final PackageManager mPackageManager;
    private final OutputStream mOutput;
    private final PackageInfo mCurrentPackage;
@@ -63,7 +63,7 @@ public class KeyValueAdbBackupEngine {
    private final BackupAgentTimeoutParameters mAgentTimeoutParameters;

    public KeyValueAdbBackupEngine(OutputStream output, PackageInfo packageInfo,
            BackupManagerServiceInterface backupManagerService, PackageManager packageManager,
            BackupManagerService backupManagerService, PackageManager packageManager,
            File baseStateDir, File dataDir) {
        mOutput = output;
        mCurrentPackage = packageInfo;
+6 −0
Original line number Diff line number Diff line
@@ -102,6 +102,12 @@ public class KeyValueBackupJob extends JobService {
        }
    }

    public static boolean isScheduled() {
        synchronized (KeyValueBackupJob.class) {
            return sScheduled;
        }
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        synchronized (KeyValueBackupJob.class) {
+3 −6
Original line number Diff line number Diff line
@@ -468,7 +468,7 @@ public class PerformBackupTask implements BackupRestoreTask {

            IBackupAgent agent = null;
            try {
                backupManagerService.getWakelock().setWorkSource(
                backupManagerService.setWorkSource(
                        new WorkSource(mCurrentPackage.applicationInfo.uid));
                agent = backupManagerService.bindToAgentSynchronous(mCurrentPackage.applicationInfo,
                        ApplicationThreadConstants.BACKUP_MODE_INCREMENTAL);
@@ -494,7 +494,7 @@ public class PerformBackupTask implements BackupRestoreTask {
            backupManagerService.addBackupTrace("no such package");
            mStatus = BackupTransport.AGENT_UNKNOWN;
        } finally {
            backupManagerService.getWakelock().setWorkSource(null);
            backupManagerService.setWorkSource(null);

            // If there was an agent error, no timeout/completion handling will occur.
            // That means we need to direct to the next state ourselves.
@@ -1202,10 +1202,7 @@ public class PerformBackupTask implements BackupRestoreTask {
        // If this was a pseudopackage there's no associated Activity Manager state
        if (mCurrentPackage.applicationInfo != null) {
            backupManagerService.addBackupTrace("unbinding " + mCurrentPackage.packageName);
            try {  // unbind even on timeout, just in case
                backupManagerService.getActivityManager().unbindBackupAgent(
                        mCurrentPackage.applicationInfo);
            } catch (RemoteException e) { /* can't happen; activity manager is local */ }
            backupManagerService.unbindAgent(mCurrentPackage.applicationInfo);
        }
    }

Loading