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

Commit b4d45425 authored by Eric Biggers's avatar Eric Biggers
Browse files

Handle teardown after incomplete setup in locksettings tests

In JUnit, @After methods are called even if a @Before method throws an
exception.  Therefore @After methods need to handle the case where
objects created by @Before have not been created yet.

This only makes a difference when there is already a test failure, but
this prevents a misleading error message.

Bug: 396780038
Test: atest FrameworksServicesTests:com.android.server.locksettings
Change-Id: Ia07bca84849dfe7fac54e89abdba2cfb5a823cae
parent 755f3741
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -354,15 +354,21 @@ public abstract class BaseLockSettingsServiceTests {

    @After
    public void tearDown_baseServices() throws Exception {
        if (mStorage != null) {
            mStorage.closeDatabase();
        }
        File db = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
        assertTrue(!db.exists() || db.delete());

        if (mStorage != null) {
            File storageDir = mStorage.mStorageDir;
            assertTrue(FileUtils.deleteContents(storageDir));
        }

        if (mPasswordSlotManager != null) {
            mPasswordSlotManager.cleanup();
        }
    }

    protected void flushHandlerTasks() {
        mService.mHandler.runWithScissors(() -> { }, 0 /*now*/); // Flush runnables on handler
+3 −1
Original line number Diff line number Diff line
@@ -124,8 +124,10 @@ public class LockSettingsStorageTests {

    @After
    public void tearDown() throws Exception {
        if (mStorage != null) {
            mStorage.closeDatabase();
        }
    }

    @Test
    public void testKeyValue_InitializeWorked() {
+3 −1
Original line number Diff line number Diff line
@@ -49,8 +49,10 @@ public class PasswordSlotManagerTests {

    @After
    public void tearDown() throws Exception {
        if (mManager != null) {
            mManager.cleanup();
        }
    }

    @Test
    public void testBasicSlotUse() throws Exception {
+6 −3
Original line number Diff line number Diff line
@@ -156,9 +156,12 @@ public class KeySyncTaskTest {

    @After
    public void tearDown() {
        if (mRecoverableKeyStoreDb != null) {
            mRecoverableKeyStoreDb.close();
        }
        if (mDatabaseFile != null) {
            mDatabaseFile.delete();

        }
        File file = new File(InstrumentationRegistry.getTargetContext().getFilesDir(),
                SNAPSHOT_TOP_LEVEL_DIRECTORY);
        FileUtils.deleteContentsAndDir(file);
+6 −2
Original line number Diff line number Diff line
@@ -117,9 +117,13 @@ public class PlatformKeyManagerTest {

    @After
    public void tearDown() {
        if (mRecoverableKeyStoreDb != null) {
            mRecoverableKeyStoreDb.close();
        }
        if (mDatabaseFile != null) {
            mDatabaseFile.delete();
        }
    }

    @Test
    public void init_createsEncryptKeyWithCorrectAlias() throws Exception {
Loading