From fc6e42f3263aaf415bdb0fbd22897d90e4db0916 Mon Sep 17 00:00:00 2001 From: Robert Horvath Date: Thu, 19 Mar 2020 17:09:50 +0100 Subject: [PATCH] Deflake InattentiveSleep tests Some inattentive sleep tests showed flakiness because of timing issues. Adjusting sleeps helps increase chance scheduled messages have been handled. The change in PowerManagerService is for the case when now == mLastUserActivityTime + attentiveTimeout. updateAttentiveStateLocked considered this time as time to go to sleep, but isAttentiveTimeoutExpired would report false for another millisecond as it used > comparison instead of >= comparison. Bug: 149340215 Test: atest --iterations 200 PowerManagerServiceTest#testInattentive* Change-Id: Ibe5168664c1e01c6e480610686e19573c8df6c6f --- .../server/power/PowerManagerService.java | 2 +- .../server/power/PowerManagerServiceTest.java | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java index 294deba459fe7..33d185e50d49d 100644 --- a/services/core/java/com/android/server/power/PowerManagerService.java +++ b/services/core/java/com/android/server/power/PowerManagerService.java @@ -2432,7 +2432,7 @@ public final class PowerManagerService extends SystemService private boolean isAttentiveTimeoutExpired(long now) { long attentiveTimeout = getAttentiveTimeoutLocked(); - return attentiveTimeout >= 0 && now > mLastUserActivityTime + attentiveTimeout; + return attentiveTimeout >= 0 && now >= mLastUserActivityTime + attentiveTimeout; } /** diff --git a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java index 5e8de42f8ce02..6434027b52bf4 100644 --- a/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/power/PowerManagerServiceTest.java @@ -63,7 +63,6 @@ import android.os.PowerManager; import android.os.PowerSaveState; import android.os.SystemClock; import android.os.UserHandle; -import android.platform.test.annotations.FlakyTest; import android.provider.Settings; import android.service.dreams.DreamManagerInternal; import android.test.mock.MockContentResolver; @@ -682,7 +681,10 @@ public class PowerManagerServiceTest { @Test public void testInattentiveSleep_hideWarningIfStayOnIsEnabledAndPluggedIn() throws Exception { - setAttentiveTimeout(15000); + setMinimumScreenOffTimeoutConfig(5); + setAttentiveWarningDuration(120); + setAttentiveTimeout(100); + Settings.Global.putInt(mContextSpy.getContentResolver(), Settings.Global.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_AC); @@ -698,10 +700,10 @@ public class PowerManagerServiceTest { } @Test - public void testInattentive_userActivityDismissesWarning() throws Exception { + public void testInattentiveSleep_userActivityDismissesWarning() throws Exception { setMinimumScreenOffTimeoutConfig(5); - setAttentiveWarningDuration(30); - setAttentiveTimeout(100); + setAttentiveWarningDuration(1900); + setAttentiveTimeout(2000); createService(); startSystem(); @@ -710,7 +712,7 @@ public class PowerManagerServiceTest { PowerManager.USER_ACTIVITY_EVENT_TOUCH, 0); verify(mInattentiveSleepWarningControllerMock, never()).show(); - SystemClock.sleep(70); + SystemClock.sleep(150); verify(mInattentiveSleepWarningControllerMock, times(1)).show(); verify(mInattentiveSleepWarningControllerMock, never()).dismiss(anyBoolean()); when(mInattentiveSleepWarningControllerMock.isShown()).thenReturn(true); @@ -723,16 +725,18 @@ public class PowerManagerServiceTest { @Test public void testInattentiveSleep_warningHiddenAfterWakingUp() throws Exception { setMinimumScreenOffTimeoutConfig(5); - setAttentiveWarningDuration(20); - setAttentiveTimeout(30); + setAttentiveWarningDuration(70); + setAttentiveTimeout(100); createService(); startSystem(); - SystemClock.sleep(10); + SystemClock.sleep(50); verify(mInattentiveSleepWarningControllerMock, atLeastOnce()).show(); when(mInattentiveSleepWarningControllerMock.isShown()).thenReturn(true); - SystemClock.sleep(30); + SystemClock.sleep(70); + assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP); forceAwake(); + assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_AWAKE); verify(mInattentiveSleepWarningControllerMock, atLeastOnce()).dismiss(false); } @@ -754,7 +758,6 @@ public class PowerManagerServiceTest { assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP); } - @FlakyTest @Test public void testInattentiveSleep_goesToSleepWithWakeLock() throws Exception { final String pkg = mContextSpy.getOpPackageName(); @@ -762,7 +765,7 @@ public class PowerManagerServiceTest { final String tag = "sleep_testWithWakeLock"; setMinimumScreenOffTimeoutConfig(5); - setAttentiveTimeout(10); + setAttentiveTimeout(30); createService(); startSystem(); @@ -770,7 +773,7 @@ public class PowerManagerServiceTest { PowerManager.SCREEN_BRIGHT_WAKE_LOCK, tag, pkg, null /* workSource */, null /* historyTag */); - SystemClock.sleep(11); + SystemClock.sleep(60); assertThat(mService.getWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP); }