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

This commit is contained in:
Santos Cordon 2022-03-17 14:57:26 +00:00 committed by Android (Google) Code Review
commit 286fd132d6
2 changed files with 82 additions and 1 deletions

View File

@ -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();

View File

@ -0,0 +1,75 @@
/*
* 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);
}
}