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

Commit 286fd132 authored by Santos Cordon's avatar Santos Cordon Committed by Android (Google) Code Review
Browse files

Merge "Add NPE check in ColorFade." into tm-dev

parents 2e096e65 ba75b774
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -156,9 +156,15 @@ final class ColorFade {

        mMode = mode;

        DisplayInfo displayInfo = mDisplayManagerInternal.getDisplayInfo(mDisplayId);
        if (displayInfo == null) {
            // displayInfo can be null if the associated display has been removed. There
            // is a delay between the display being removed and ColorFade being dismissed.
            return false;
        }

        // Get the display size and layer stack.
        // This is not expected to change while the color fade surface is showing.
        DisplayInfo displayInfo = mDisplayManagerInternal.getDisplayInfo(mDisplayId);
        mDisplayLayerStack = displayInfo.layerStack;
        mDisplayWidth = displayInfo.getNaturalWidth();
        mDisplayHeight = displayInfo.getNaturalHeight();
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.display;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.hardware.display.DisplayManagerInternal;
import android.platform.test.annotations.Presubmit;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.server.LocalServices;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class ColorFadeTest {
    private static final int DISPLAY_ID = 123;

    private Context mContext;

    @Mock private DisplayManagerInternal mDisplayManagerInternalMock;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        addLocalServiceMock(DisplayManagerInternal.class, mDisplayManagerInternalMock);
        mContext = getInstrumentation().getTargetContext();
    }

    @After
    public void tearDown() {
        LocalServices.removeServiceForTest(DisplayManagerInternal.class);
    }

    @Test
    public void testPrepareColorFadeForInvalidDisplay() {
        when(mDisplayManagerInternalMock.getDisplayInfo(eq(DISPLAY_ID))).thenReturn(null);
        ColorFade colorFade = new ColorFade(DISPLAY_ID);
        assertFalse(colorFade.prepare(mContext, ColorFade.MODE_FADE));
    }

    private static <T> void addLocalServiceMock(Class<T> clazz, T mock) {
        LocalServices.removeServiceForTest(clazz);
        LocalServices.addService(clazz, mock);
    }

}