From 54d44962bef76d567ad597a26eff0f0de7e4de6b Mon Sep 17 00:00:00 2001 From: Robin Lee Date: Mon, 17 Dec 2018 17:43:57 +0100 Subject: [PATCH] Fix a race condition in DeviceIdleControllerTest. I noticed that the real ACTION_BATTERY_CHANGED races against the mocked calls to update power state and cycle through idle states. This actually works on a high-end phone or an emulator because the sticky broadcast goes through before the test finishes setting up. But on a slower device we sometimes beat the broadcast queue and wind up getting our state reset halfway through the test. Flaky tests are bad so let's not listen to broadcasts at all in the test. May address issue 118639768 [DeviceIdleControllerTest is flaky]. Bug: 110756616 Bug: 118639768 Test: atest com.android.server.DeviceIdleControllerTest Change-Id: Ie03ed10f2781ccaba4f39f4f34673a3e073e9f7f --- .../src/com/android/server/DeviceIdleControllerTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java b/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java index 04a84081bad84..cff0521bba506 100644 --- a/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java @@ -203,6 +203,8 @@ public class DeviceIdleControllerTest { .strictness(Strictness.LENIENT) .mockStatic(LocalServices.class) .startMocking(); + spyOn(getContext()); + doReturn(null).when(getContext()).registerReceiver(any(), any()); doReturn(mock(ActivityManagerInternal.class)) .when(() -> LocalServices.getService(ActivityManagerInternal.class)); doReturn(mock(ActivityTaskManagerInternal.class))