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

Commit 96b9e35a authored by Bryce Lee's avatar Bryce Lee
Browse files

Handle exceptions in getting Dream Metadata.

This changelist catches any exception thrown when fetching
dream metadata and returns null instead.

Test: atest DreamServiceTest#testMetadataParsing_exceptionReading
Fixes: 337879822
Flag: N/A
Change-Id: I95fbc34e94810dcd28c604a882d31721a66d4260
parent 7b328130
Loading
Loading
Loading
Loading
+16 −10
Original line number Original line Diff line number Diff line
@@ -1383,16 +1383,22 @@ public class DreamService extends Service implements Window.Callback {
                DreamService.DREAM_META_DATA, DREAM_META_DATA_ROOT_TAG,
                DreamService.DREAM_META_DATA, DREAM_META_DATA_ROOT_TAG,
                com.android.internal.R.styleable.Dream)) {
                com.android.internal.R.styleable.Dream)) {
            if (rawMetadata == null) return null;
            if (rawMetadata == null) return null;
            try {
                return new DreamMetadata(
                return new DreamMetadata(
                        convertToComponentName(
                        convertToComponentName(
                                rawMetadata.getString(
                                rawMetadata.getString(
                            com.android.internal.R.styleable.Dream_settingsActivity), serviceInfo),
                                        com.android.internal.R.styleable.Dream_settingsActivity),
                                serviceInfo),
                        rawMetadata.getDrawable(
                        rawMetadata.getDrawable(
                                com.android.internal.R.styleable.Dream_previewImage),
                                com.android.internal.R.styleable.Dream_previewImage),
                        rawMetadata.getBoolean(R.styleable.Dream_showClockAndComplications,
                        rawMetadata.getBoolean(R.styleable.Dream_showClockAndComplications,
                                DEFAULT_SHOW_COMPLICATIONS),
                                DEFAULT_SHOW_COMPLICATIONS),
                        rawMetadata.getInt(R.styleable.Dream_dreamCategory, DREAM_CATEGORY_DEFAULT)
                        rawMetadata.getInt(R.styleable.Dream_dreamCategory, DREAM_CATEGORY_DEFAULT)
                );
                );
            } catch (Exception exception) {
                Log.e(TAG, "Failed to create read metadata", exception);
                return null;
            }
        }
        }
    }
    }


+18 −0
Original line number Original line Diff line number Diff line
@@ -20,12 +20,17 @@ import static com.google.common.truth.Truth.assertThat;


import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


import android.content.ComponentName;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.content.pm.ServiceInfo;
import android.content.res.TypedArray;
import android.os.Looper;
import android.os.Looper;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.EnableFlags;
import android.service.dreams.DreamService;
import android.service.dreams.DreamService;
@@ -41,6 +46,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mockito;


@SmallTest
@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
@@ -83,6 +89,18 @@ public class DreamServiceTest {
        assertThat(metadata.dreamCategory).isEqualTo(DreamService.DREAM_CATEGORY_DEFAULT);
        assertThat(metadata.dreamCategory).isEqualTo(DreamService.DREAM_CATEGORY_DEFAULT);
    }
    }


    @Test
    public void testMetadataParsing_exceptionReading() {
        final PackageManager packageManager = Mockito.mock(PackageManager.class);
        final ServiceInfo serviceInfo = Mockito.mock(ServiceInfo.class);
        final TypedArray rawMetadata = Mockito.mock(TypedArray.class);
        when(packageManager.extractPackageItemInfoAttributes(eq(serviceInfo), any(), any(), any()))
                .thenReturn(rawMetadata);
        when(rawMetadata.getString(anyInt())).thenThrow(new RuntimeException("failure"));

        assertThat(DreamService.getDreamMetadata(packageManager, serviceInfo)).isNull();
    }

    private DreamService.DreamMetadata getDreamMetadata(String dreamClassName)
    private DreamService.DreamMetadata getDreamMetadata(String dreamClassName)
            throws PackageManager.NameNotFoundException {
            throws PackageManager.NameNotFoundException {
        final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();