Loading services/tests/mockingservicestests/src/com/android/server/display/DisplayPowerController2Test.java +49 −62 Original line number Diff line number Diff line Loading @@ -38,9 +38,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import android.content.ContentResolver; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Resources; import android.hardware.Sensor; import android.hardware.SensorEventListener; Loading @@ -54,15 +52,16 @@ import android.os.SystemProperties; import android.os.UserHandle; import android.os.test.TestLooper; import android.provider.Settings; import android.testing.TestableContext; import android.util.FloatProperty; import android.view.Display; import android.view.DisplayInfo; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.server.ExtendedMockitoRule; import com.android.server.LocalServices; import com.android.server.am.BatteryStatsService; import com.android.server.display.RampAnimator.DualRampAnimator; Loading @@ -74,12 +73,12 @@ import com.android.server.testutils.OffsettableClock; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoSession; import org.mockito.quality.Strictness; import org.mockito.stubbing.Answer; Loading @@ -97,11 +96,9 @@ public final class DisplayPowerController2Test { private static final String SECOND_FOLLOWER_UNIQUE_DISPLAY_ID = "unique_id_789"; private static final float PROX_SENSOR_MAX_RANGE = 5; private MockitoSession mSession; private OffsettableClock mClock; private TestLooper mTestLooper; private Handler mHandler; private Context mContextSpy; private DisplayPowerControllerHolder mHolder; private Sensor mProxSensor; Loading @@ -118,40 +115,44 @@ public final class DisplayPowerController2Test { @Mock private PowerManager mPowerManagerMock; @Mock private Resources mResourcesMock; @Mock private ColorDisplayService.ColorDisplayServiceInternal mCdsiMock; @Captor private ArgumentCaptor<SensorEventListener> mSensorEventListenerCaptor; @Before public void setUp() throws Exception { mSession = ExtendedMockito.mockitoSession() .initMocks(this) .strictness(Strictness.LENIENT) @Rule public final TestableContext mContext = new TestableContext( InstrumentationRegistry.getInstrumentation().getContext()); @Rule public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this) .setStrictness(Strictness.LENIENT) .spyStatic(SystemProperties.class) .spyStatic(LocalServices.class) .spyStatic(BatteryStatsService.class) .spyStatic(Settings.System.class) .startMocking(); mContextSpy = spy(new ContextWrapper(ApplicationProvider.getApplicationContext())); .build(); @Before public void setUp() throws Exception { mClock = new OffsettableClock.Stopped(); mTestLooper = new TestLooper(mClock::now); mHandler = new Handler(mTestLooper.getLooper()); // Put the system into manual brightness by default, just to minimize unexpected events and // have a consistent starting state Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT); addLocalServiceMock(WindowManagerPolicy.class, mWindowManagerPolicyMock); addLocalServiceMock(ColorDisplayService.ColorDisplayServiceInternal.class, mCdsiMock); when(mContextSpy.getSystemService(eq(PowerManager.class))).thenReturn(mPowerManagerMock); when(mContextSpy.getResources()).thenReturn(mResourcesMock); mContext.addMockSystemService(PowerManager.class, mPowerManagerMock); doAnswer((Answer<Void>) invocationOnMock -> null).when(() -> SystemProperties.set(anyString(), any())); doAnswer((Answer<ColorDisplayService.ColorDisplayServiceInternal>) invocationOnMock -> mCdsiMock).when(() -> LocalServices.getService( ColorDisplayService.ColorDisplayServiceInternal.class)); doAnswer((Answer<Void>) invocationOnMock -> null).when(BatteryStatsService::getService); doAnswer((Answer<Boolean>) invocationOnMock -> true).when(() -> Settings.System.putFloatForUser(any(), any(), anyFloat(), anyInt())); setUpSensors(); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); Loading @@ -159,8 +160,8 @@ public final class DisplayPowerController2Test { @After public void tearDown() { mSession.finishMocking(); LocalServices.removeServiceForTest(WindowManagerPolicy.class); LocalServices.removeServiceForTest(ColorDisplayService.ColorDisplayServiceInternal.class); } @Test Loading Loading @@ -421,11 +422,9 @@ public final class DisplayPowerController2Test { @Test public void testDisplayBrightnessFollowers_AutomaticBrightness() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); final float brightness = 0.4f; final float nits = 300; final float ambientLux = 3000; Loading @@ -436,7 +435,7 @@ public final class DisplayPowerController2Test { when(mHolder.automaticBrightnessController.convertToNits(brightness)).thenReturn(nits); when(mHolder.automaticBrightnessController.getAmbientLux()).thenReturn(ambientLux); when(mHolder.displayPowerState.getScreenState()).thenReturn(Display.STATE_ON); DisplayPowerController followerDpc = mock(DisplayPowerController.class); DisplayPowerController2 followerDpc = mock(DisplayPowerController2.class); mHolder.dpc.addDisplayBrightnessFollower(followerDpc); DisplayPowerRequest dpr = new DisplayPowerRequest(); Loading Loading @@ -542,11 +541,9 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsOff() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; Loading Loading @@ -577,17 +574,14 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsInDoze() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_DOZE; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing, true); mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); advanceTime(1); // Run updatePowerState Loading Loading @@ -615,12 +609,7 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorDisabled_AutoBrightnessIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); // Tests are set up with manual brightness by default, so no need to set it here. DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); Loading @@ -632,11 +621,9 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorDisabled_DisplayIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID, /* isEnabled= */ false); DisplayPowerRequest dpr = new DisplayPowerRequest(); Loading Loading @@ -690,9 +677,9 @@ public final class DisplayPowerController2Test { public void testBrightnessNitsPersistWhenDisplayDeviceChanges() { float brightness = 0.3f; float nits = 500; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay, true); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); when(mHolder.automaticBrightnessController.convertToNits(brightness)).thenReturn(nits); Loading Loading @@ -753,7 +740,7 @@ public final class DisplayPowerController2Test { any(HysteresisLevels.class), any(HysteresisLevels.class), any(HysteresisLevels.class), eq(mContextSpy), eq(mContext), any(HighBrightnessModeController.class), any(BrightnessThrottler.class), isNull(), Loading Loading @@ -862,7 +849,7 @@ public final class DisplayPowerController2Test { setUpDisplay(displayId, uniqueId, display, device, config, isEnabled); final DisplayPowerController2 dpc = new DisplayPowerController2( mContextSpy, injector, mDisplayPowerCallbacksMock, mHandler, mContext, injector, mDisplayPowerCallbacksMock, mHandler, mSensorManagerMock, mDisplayBlankerMock, display, mBrightnessTrackerMock, brightnessSetting, () -> {}, hbmMetadata, /* bootCompleted= */ false); Loading services/tests/mockingservicestests/src/com/android/server/display/DisplayPowerControllerTest.java +50 −61 Original line number Diff line number Diff line Loading @@ -38,9 +38,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import android.content.ContentResolver; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Resources; import android.hardware.Sensor; import android.hardware.SensorEventListener; Loading @@ -54,15 +52,16 @@ import android.os.SystemProperties; import android.os.UserHandle; import android.os.test.TestLooper; import android.provider.Settings; import android.testing.TestableContext; import android.util.FloatProperty; import android.view.Display; import android.view.DisplayInfo; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.server.ExtendedMockitoRule; import com.android.server.LocalServices; import com.android.server.am.BatteryStatsService; import com.android.server.display.RampAnimator.DualRampAnimator; Loading @@ -75,12 +74,12 @@ import com.android.server.testutils.OffsettableClock; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoSession; import org.mockito.quality.Strictness; import org.mockito.stubbing.Answer; Loading @@ -98,11 +97,9 @@ public final class DisplayPowerControllerTest { private static final String SECOND_FOLLOWER_UNIQUE_DISPLAY_ID = "unique_id_789"; private static final float PROX_SENSOR_MAX_RANGE = 5; private MockitoSession mSession; private OffsettableClock mClock; private TestLooper mTestLooper; private Handler mHandler; private Context mContextSpy; private DisplayPowerControllerHolder mHolder; private Sensor mProxSensor; Loading @@ -119,41 +116,45 @@ public final class DisplayPowerControllerTest { @Mock private PowerManager mPowerManagerMock; @Mock private Resources mResourcesMock; @Mock private ColorDisplayService.ColorDisplayServiceInternal mCdsiMock; @Captor private ArgumentCaptor<SensorEventListener> mSensorEventListenerCaptor; @Before public void setUp() throws Exception { mSession = ExtendedMockito.mockitoSession() .initMocks(this) .strictness(Strictness.LENIENT) @Rule public final TestableContext mContext = new TestableContext( InstrumentationRegistry.getInstrumentation().getContext()); @Rule public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this) .setStrictness(Strictness.LENIENT) .spyStatic(SystemProperties.class) .spyStatic(LocalServices.class) .spyStatic(BatteryStatsService.class) .spyStatic(Settings.System.class) .startMocking(); mContextSpy = spy(new ContextWrapper(ApplicationProvider.getApplicationContext())); .build(); @Before public void setUp() throws Exception { mClock = new OffsettableClock.Stopped(); mTestLooper = new TestLooper(mClock::now); mHandler = new Handler(mTestLooper.getLooper()); // Put the system into manual brightness by default, just to minimize unexpected events and // have a consistent starting state Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT); addLocalServiceMock(WindowManagerPolicy.class, mWindowManagerPolicyMock); addLocalServiceMock(ColorDisplayService.ColorDisplayServiceInternal.class, mCdsiMock); when(mContextSpy.getSystemService(eq(PowerManager.class))).thenReturn(mPowerManagerMock); when(mContextSpy.getResources()).thenReturn(mResourcesMock); mContext.addMockSystemService(PowerManager.class, mPowerManagerMock); doAnswer((Answer<Void>) invocationOnMock -> null).when(() -> SystemProperties.set(anyString(), any())); doAnswer((Answer<ColorDisplayService.ColorDisplayServiceInternal>) invocationOnMock -> mCdsiMock).when(() -> LocalServices.getService( ColorDisplayService.ColorDisplayServiceInternal.class)); doAnswer((Answer<Void>) invocationOnMock -> null).when(BatteryStatsService::getService); doAnswer((Answer<Boolean>) invocationOnMock -> true).when(() -> Settings.System.putFloatForUser(any(), any(), anyFloat(), anyInt())); setUpSensors(); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); Loading @@ -161,8 +162,8 @@ public final class DisplayPowerControllerTest { @After public void tearDown() { mSession.finishMocking(); LocalServices.removeServiceForTest(WindowManagerPolicy.class); LocalServices.removeServiceForTest(ColorDisplayService.ColorDisplayServiceInternal.class); } @Test Loading Loading @@ -425,11 +426,9 @@ public final class DisplayPowerControllerTest { @Test public void testDisplayBrightnessFollowers_AutomaticBrightness() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); final float brightness = 0.4f; final float nits = 300; final float ambientLux = 3000; Loading Loading @@ -547,11 +546,9 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsOff() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; Loading Loading @@ -582,17 +579,14 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsInDoze() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_DOZE; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing, true); mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); advanceTime(1); // Run updatePowerState Loading Loading @@ -620,12 +614,7 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorDisabled_AutoBrightnessIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); // Tests are set up with manual brightness by default, so no need to set it here. DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); Loading @@ -637,11 +626,10 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorDisabled_DisplayIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID, /* isEnabled= */ false); DisplayPowerRequest dpr = new DisplayPowerRequest(); Loading Loading @@ -695,9 +683,10 @@ public final class DisplayPowerControllerTest { public void testBrightnessNitsPersistWhenDisplayDeviceChanges() { float brightness = 0.3f; float nits = 500; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay, true); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); when(mHolder.automaticBrightnessController.convertToNits(brightness)).thenReturn(nits); Loading Loading @@ -758,7 +747,7 @@ public final class DisplayPowerControllerTest { any(HysteresisLevels.class), any(HysteresisLevels.class), any(HysteresisLevels.class), eq(mContextSpy), eq(mContext), any(HighBrightnessModeController.class), any(BrightnessThrottler.class), isNull(), Loading Loading @@ -866,7 +855,7 @@ public final class DisplayPowerControllerTest { setUpDisplay(displayId, uniqueId, display, device, config, isEnabled); final DisplayPowerController dpc = new DisplayPowerController( mContextSpy, injector, mDisplayPowerCallbacksMock, mHandler, mContext, injector, mDisplayPowerCallbacksMock, mHandler, mSensorManagerMock, mDisplayBlankerMock, display, mBrightnessTrackerMock, brightnessSetting, () -> {}, hbmMetadata, /* bootCompleted= */ false); Loading Loading
services/tests/mockingservicestests/src/com/android/server/display/DisplayPowerController2Test.java +49 −62 Original line number Diff line number Diff line Loading @@ -38,9 +38,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import android.content.ContentResolver; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Resources; import android.hardware.Sensor; import android.hardware.SensorEventListener; Loading @@ -54,15 +52,16 @@ import android.os.SystemProperties; import android.os.UserHandle; import android.os.test.TestLooper; import android.provider.Settings; import android.testing.TestableContext; import android.util.FloatProperty; import android.view.Display; import android.view.DisplayInfo; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.server.ExtendedMockitoRule; import com.android.server.LocalServices; import com.android.server.am.BatteryStatsService; import com.android.server.display.RampAnimator.DualRampAnimator; Loading @@ -74,12 +73,12 @@ import com.android.server.testutils.OffsettableClock; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoSession; import org.mockito.quality.Strictness; import org.mockito.stubbing.Answer; Loading @@ -97,11 +96,9 @@ public final class DisplayPowerController2Test { private static final String SECOND_FOLLOWER_UNIQUE_DISPLAY_ID = "unique_id_789"; private static final float PROX_SENSOR_MAX_RANGE = 5; private MockitoSession mSession; private OffsettableClock mClock; private TestLooper mTestLooper; private Handler mHandler; private Context mContextSpy; private DisplayPowerControllerHolder mHolder; private Sensor mProxSensor; Loading @@ -118,40 +115,44 @@ public final class DisplayPowerController2Test { @Mock private PowerManager mPowerManagerMock; @Mock private Resources mResourcesMock; @Mock private ColorDisplayService.ColorDisplayServiceInternal mCdsiMock; @Captor private ArgumentCaptor<SensorEventListener> mSensorEventListenerCaptor; @Before public void setUp() throws Exception { mSession = ExtendedMockito.mockitoSession() .initMocks(this) .strictness(Strictness.LENIENT) @Rule public final TestableContext mContext = new TestableContext( InstrumentationRegistry.getInstrumentation().getContext()); @Rule public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this) .setStrictness(Strictness.LENIENT) .spyStatic(SystemProperties.class) .spyStatic(LocalServices.class) .spyStatic(BatteryStatsService.class) .spyStatic(Settings.System.class) .startMocking(); mContextSpy = spy(new ContextWrapper(ApplicationProvider.getApplicationContext())); .build(); @Before public void setUp() throws Exception { mClock = new OffsettableClock.Stopped(); mTestLooper = new TestLooper(mClock::now); mHandler = new Handler(mTestLooper.getLooper()); // Put the system into manual brightness by default, just to minimize unexpected events and // have a consistent starting state Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT); addLocalServiceMock(WindowManagerPolicy.class, mWindowManagerPolicyMock); addLocalServiceMock(ColorDisplayService.ColorDisplayServiceInternal.class, mCdsiMock); when(mContextSpy.getSystemService(eq(PowerManager.class))).thenReturn(mPowerManagerMock); when(mContextSpy.getResources()).thenReturn(mResourcesMock); mContext.addMockSystemService(PowerManager.class, mPowerManagerMock); doAnswer((Answer<Void>) invocationOnMock -> null).when(() -> SystemProperties.set(anyString(), any())); doAnswer((Answer<ColorDisplayService.ColorDisplayServiceInternal>) invocationOnMock -> mCdsiMock).when(() -> LocalServices.getService( ColorDisplayService.ColorDisplayServiceInternal.class)); doAnswer((Answer<Void>) invocationOnMock -> null).when(BatteryStatsService::getService); doAnswer((Answer<Boolean>) invocationOnMock -> true).when(() -> Settings.System.putFloatForUser(any(), any(), anyFloat(), anyInt())); setUpSensors(); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); Loading @@ -159,8 +160,8 @@ public final class DisplayPowerController2Test { @After public void tearDown() { mSession.finishMocking(); LocalServices.removeServiceForTest(WindowManagerPolicy.class); LocalServices.removeServiceForTest(ColorDisplayService.ColorDisplayServiceInternal.class); } @Test Loading Loading @@ -421,11 +422,9 @@ public final class DisplayPowerController2Test { @Test public void testDisplayBrightnessFollowers_AutomaticBrightness() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); final float brightness = 0.4f; final float nits = 300; final float ambientLux = 3000; Loading @@ -436,7 +435,7 @@ public final class DisplayPowerController2Test { when(mHolder.automaticBrightnessController.convertToNits(brightness)).thenReturn(nits); when(mHolder.automaticBrightnessController.getAmbientLux()).thenReturn(ambientLux); when(mHolder.displayPowerState.getScreenState()).thenReturn(Display.STATE_ON); DisplayPowerController followerDpc = mock(DisplayPowerController.class); DisplayPowerController2 followerDpc = mock(DisplayPowerController2.class); mHolder.dpc.addDisplayBrightnessFollower(followerDpc); DisplayPowerRequest dpr = new DisplayPowerRequest(); Loading Loading @@ -542,11 +541,9 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsOff() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; Loading Loading @@ -577,17 +574,14 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsInDoze() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_DOZE; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing, true); mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); advanceTime(1); // Run updatePowerState Loading Loading @@ -615,12 +609,7 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorDisabled_AutoBrightnessIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); // Tests are set up with manual brightness by default, so no need to set it here. DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); Loading @@ -632,11 +621,9 @@ public final class DisplayPowerController2Test { @Test public void testSetScreenOffBrightnessSensorDisabled_DisplayIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID, /* isEnabled= */ false); DisplayPowerRequest dpr = new DisplayPowerRequest(); Loading Loading @@ -690,9 +677,9 @@ public final class DisplayPowerController2Test { public void testBrightnessNitsPersistWhenDisplayDeviceChanges() { float brightness = 0.3f; float nits = 500; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay, true); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); when(mHolder.automaticBrightnessController.convertToNits(brightness)).thenReturn(nits); Loading Loading @@ -753,7 +740,7 @@ public final class DisplayPowerController2Test { any(HysteresisLevels.class), any(HysteresisLevels.class), any(HysteresisLevels.class), eq(mContextSpy), eq(mContext), any(HighBrightnessModeController.class), any(BrightnessThrottler.class), isNull(), Loading Loading @@ -862,7 +849,7 @@ public final class DisplayPowerController2Test { setUpDisplay(displayId, uniqueId, display, device, config, isEnabled); final DisplayPowerController2 dpc = new DisplayPowerController2( mContextSpy, injector, mDisplayPowerCallbacksMock, mHandler, mContext, injector, mDisplayPowerCallbacksMock, mHandler, mSensorManagerMock, mDisplayBlankerMock, display, mBrightnessTrackerMock, brightnessSetting, () -> {}, hbmMetadata, /* bootCompleted= */ false); Loading
services/tests/mockingservicestests/src/com/android/server/display/DisplayPowerControllerTest.java +50 −61 Original line number Diff line number Diff line Loading @@ -38,9 +38,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; import android.content.ContentResolver; import android.content.Context; import android.content.ContextWrapper; import android.content.res.Resources; import android.hardware.Sensor; import android.hardware.SensorEventListener; Loading @@ -54,15 +52,16 @@ import android.os.SystemProperties; import android.os.UserHandle; import android.os.test.TestLooper; import android.provider.Settings; import android.testing.TestableContext; import android.util.FloatProperty; import android.view.Display; import android.view.DisplayInfo; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.server.ExtendedMockitoRule; import com.android.server.LocalServices; import com.android.server.am.BatteryStatsService; import com.android.server.display.RampAnimator.DualRampAnimator; Loading @@ -75,12 +74,12 @@ import com.android.server.testutils.OffsettableClock; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoSession; import org.mockito.quality.Strictness; import org.mockito.stubbing.Answer; Loading @@ -98,11 +97,9 @@ public final class DisplayPowerControllerTest { private static final String SECOND_FOLLOWER_UNIQUE_DISPLAY_ID = "unique_id_789"; private static final float PROX_SENSOR_MAX_RANGE = 5; private MockitoSession mSession; private OffsettableClock mClock; private TestLooper mTestLooper; private Handler mHandler; private Context mContextSpy; private DisplayPowerControllerHolder mHolder; private Sensor mProxSensor; Loading @@ -119,41 +116,45 @@ public final class DisplayPowerControllerTest { @Mock private PowerManager mPowerManagerMock; @Mock private Resources mResourcesMock; @Mock private ColorDisplayService.ColorDisplayServiceInternal mCdsiMock; @Captor private ArgumentCaptor<SensorEventListener> mSensorEventListenerCaptor; @Before public void setUp() throws Exception { mSession = ExtendedMockito.mockitoSession() .initMocks(this) .strictness(Strictness.LENIENT) @Rule public final TestableContext mContext = new TestableContext( InstrumentationRegistry.getInstrumentation().getContext()); @Rule public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this) .setStrictness(Strictness.LENIENT) .spyStatic(SystemProperties.class) .spyStatic(LocalServices.class) .spyStatic(BatteryStatsService.class) .spyStatic(Settings.System.class) .startMocking(); mContextSpy = spy(new ContextWrapper(ApplicationProvider.getApplicationContext())); .build(); @Before public void setUp() throws Exception { mClock = new OffsettableClock.Stopped(); mTestLooper = new TestLooper(mClock::now); mHandler = new Handler(mTestLooper.getLooper()); // Put the system into manual brightness by default, just to minimize unexpected events and // have a consistent starting state Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT); addLocalServiceMock(WindowManagerPolicy.class, mWindowManagerPolicyMock); addLocalServiceMock(ColorDisplayService.ColorDisplayServiceInternal.class, mCdsiMock); when(mContextSpy.getSystemService(eq(PowerManager.class))).thenReturn(mPowerManagerMock); when(mContextSpy.getResources()).thenReturn(mResourcesMock); mContext.addMockSystemService(PowerManager.class, mPowerManagerMock); doAnswer((Answer<Void>) invocationOnMock -> null).when(() -> SystemProperties.set(anyString(), any())); doAnswer((Answer<ColorDisplayService.ColorDisplayServiceInternal>) invocationOnMock -> mCdsiMock).when(() -> LocalServices.getService( ColorDisplayService.ColorDisplayServiceInternal.class)); doAnswer((Answer<Void>) invocationOnMock -> null).when(BatteryStatsService::getService); doAnswer((Answer<Boolean>) invocationOnMock -> true).when(() -> Settings.System.putFloatForUser(any(), any(), anyFloat(), anyInt())); setUpSensors(); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); Loading @@ -161,8 +162,8 @@ public final class DisplayPowerControllerTest { @After public void tearDown() { mSession.finishMocking(); LocalServices.removeServiceForTest(WindowManagerPolicy.class); LocalServices.removeServiceForTest(ColorDisplayService.ColorDisplayServiceInternal.class); } @Test Loading Loading @@ -425,11 +426,9 @@ public final class DisplayPowerControllerTest { @Test public void testDisplayBrightnessFollowers_AutomaticBrightness() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); final float brightness = 0.4f; final float nits = 300; final float ambientLux = 3000; Loading Loading @@ -547,11 +546,9 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsOff() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; Loading Loading @@ -582,17 +579,14 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorEnabled_DisplayIsInDoze() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_DOZE; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing, true); mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); advanceTime(1); // Run updatePowerState Loading Loading @@ -620,12 +614,7 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorDisabled_AutoBrightnessIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); // Tests are set up with manual brightness by default, so no need to set it here. DisplayPowerRequest dpr = new DisplayPowerRequest(); dpr.policy = DisplayPowerRequest.POLICY_OFF; mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false); Loading @@ -637,11 +626,10 @@ public final class DisplayPowerControllerTest { @Test public void testSetScreenOffBrightnessSensorDisabled_DisplayIsDisabled() { doAnswer((Answer<Integer>) invocationOnMock -> Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) .when(() -> Settings.System.getIntForUser(any(ContentResolver.class), eq(Settings.System.SCREEN_BRIGHTNESS_MODE), anyInt(), eq(UserHandle.USER_CURRENT))); Settings.System.putIntForUser(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC, UserHandle.USER_CURRENT); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID, /* isEnabled= */ false); DisplayPowerRequest dpr = new DisplayPowerRequest(); Loading Loading @@ -695,9 +683,10 @@ public final class DisplayPowerControllerTest { public void testBrightnessNitsPersistWhenDisplayDeviceChanges() { float brightness = 0.3f; float nits = 500; when(mResourcesMock.getBoolean( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay)) .thenReturn(true); mContext.getOrCreateTestableResources().addOverride( com.android.internal.R.bool.config_persistBrightnessNitsForDefaultDisplay, true); mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID); when(mHolder.automaticBrightnessController.convertToNits(brightness)).thenReturn(nits); Loading Loading @@ -758,7 +747,7 @@ public final class DisplayPowerControllerTest { any(HysteresisLevels.class), any(HysteresisLevels.class), any(HysteresisLevels.class), eq(mContextSpy), eq(mContext), any(HighBrightnessModeController.class), any(BrightnessThrottler.class), isNull(), Loading Loading @@ -866,7 +855,7 @@ public final class DisplayPowerControllerTest { setUpDisplay(displayId, uniqueId, display, device, config, isEnabled); final DisplayPowerController dpc = new DisplayPowerController( mContextSpy, injector, mDisplayPowerCallbacksMock, mHandler, mContext, injector, mDisplayPowerCallbacksMock, mHandler, mSensorManagerMock, mDisplayBlankerMock, display, mBrightnessTrackerMock, brightnessSetting, () -> {}, hbmMetadata, /* bootCompleted= */ false); Loading