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

Commit f9c185e3 authored by Joël Stemmer's avatar Joël Stemmer
Browse files

Use a mock PackageManager instead of the real one

The PackageManagerBackupAgent tests require an installed and
not-installed package to test different cases. Originally I used
`com.android.wallpaperbackup` as the installed package, as I thought
that would always be available. However, it turns out that not all form
factors may have this package.

Rather than trying to find a different package that is guaranteed to
always be installed, I've replaced the package manager with a mock. This
will also prevent future test breakages if the chosen package would ever
get removed.

Test: atest PackageManagerBackupAgent
Bug: 277594991
Bug: 330602059

Change-Id: I3ee899a11949dbb8531ff272d433c964f411c5f7
parent 8747a0a3
Loading
Loading
Loading
Loading
+21 −6
Original line number Diff line number Diff line
@@ -16,14 +16,19 @@

package com.android.server.backup;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.SigningDetails;
import android.content.pm.SigningInfo;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.platform.test.annotations.Presubmit;
@@ -38,6 +43,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
@@ -51,22 +58,30 @@ import java.util.Optional;
public class PackageManagerBackupAgentTest {

    private static final String EXISTING_PACKAGE_NAME = "com.android.wallpaperbackup";
    private static final int EXISTING_PACKAGE_VERSION = 1;

    private static final int USER_ID = 0;

    @Rule public TemporaryFolder folder = new TemporaryFolder();

    private PackageManager mPackageManager;
    @Mock private PackageManager mPackageManager;

    private PackageManagerBackupAgent mPackageManagerBackupAgent;
    private ImmutableList<PackageInfo> mPackages;
    private File mBackupData, mOldState, mNewState;

    @Before
    public void setUp() throws Exception {
        mPackageManager = getApplicationContext().getPackageManager();
        MockitoAnnotations.initMocks(this);

        PackageInfo existingPackageInfo =
                mPackageManager.getPackageInfoAsUser(
                        EXISTING_PACKAGE_NAME, PackageManager.GET_SIGNING_CERTIFICATES, USER_ID);
                createPackage(EXISTING_PACKAGE_NAME, EXISTING_PACKAGE_VERSION);
        Signature sig = new Signature(new byte[256]);
        existingPackageInfo.signingInfo =
                new SigningInfo(new SigningDetails(new Signature[] {sig}, 1, null, null));
        when(mPackageManager.getPackageInfoAsUser(eq(EXISTING_PACKAGE_NAME), anyInt(), anyInt()))
                .thenReturn(existingPackageInfo);

        mPackages = ImmutableList.of(existingPackageInfo);
        mPackageManagerBackupAgent =
                new PackageManagerBackupAgent(mPackageManager, mPackages, USER_ID);