From 3115bdf15af01ea43c28e1251657c7154e9acc91 Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Wed, 5 Apr 2017 08:39:40 -0700 Subject: [PATCH] Fix ActivityRecordTests. Tests are not guaranteed to run on the same thread as setup methods. Therefore, we must ensure any loopers used are already created by the time the test runs. We do this now by creating a HandlerThread and referencing its Looper later. Loopers are initialized per HandlerThread, which will allow us to both isolate its usage and guarantee the Looper initialization by test time. Change-Id: If15494783959e5c399375033253cef69b921ff84 Fixes: 36916522 Test: bit FrameworksServicesTests:com.android.server.am.ActivityRecordTests --- .../android/server/am/ActivityRecordTests.java | 3 +-- .../android/server/am/ActivityTestsBase.java | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityRecordTests.java b/services/tests/servicestests/src/com/android/server/am/ActivityRecordTests.java index af3201c7aa97e..54ecab3af542c 100644 --- a/services/tests/servicestests/src/com/android/server/am/ActivityRecordTests.java +++ b/services/tests/servicestests/src/com/android/server/am/ActivityRecordTests.java @@ -33,8 +33,7 @@ import org.junit.Test; * bit FrameworksServicesTests:com.android.server.am.ActivityRecordTests */ @MediumTest -// TODO(b/36916522): Currently failing in CI. -// @Presubmit +@Presubmit @RunWith(AndroidJUnit4.class) public class ActivityRecordTests extends ActivityTestsBase { private final ComponentName testActivityComponent = diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java index c5cc2ff22abd3..52405863e8fee 100644 --- a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java +++ b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java @@ -25,7 +25,7 @@ import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.res.Configuration; import android.graphics.Rect; -import android.os.Handler; +import android.os.HandlerThread; import android.os.Looper; import android.support.test.InstrumentationRegistry; import com.android.server.AttributeCache; @@ -34,6 +34,7 @@ import com.android.server.wm.StackWindowController; import com.android.server.wm.WindowManagerService; import com.android.server.wm.WindowTestUtils; +import org.junit.After; import org.junit.Before; import org.mockito.MockitoAnnotations; @@ -42,8 +43,7 @@ import org.mockito.MockitoAnnotations; */ public class ActivityTestsBase { private final Context mContext = InstrumentationRegistry.getContext(); - private static boolean sLooperPrepared; - private Handler mHandler; + private HandlerThread mHandlerThread; // Grabbing an instance of {@link WindowManagerService} creates it if not present so this must // be called at before any tests. @@ -52,11 +52,13 @@ public class ActivityTestsBase { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); + mHandlerThread = new HandlerThread("ActivityTestsBaseThread"); + mHandlerThread.start(); + } - if (!sLooperPrepared) { - sLooperPrepared = true; - Looper.prepare(); - } + @After + public void tearDown() { + mHandlerThread.quitSafely(); } protected ActivityManagerService createActivityManagerService() { @@ -126,7 +128,7 @@ public class ActivityTestsBase { @Override protected ActivityStackSupervisor createStackSupervisor() { - return new TestActivityStackSupervisor(this, new Handler().getLooper()); + return new TestActivityStackSupervisor(this, mHandlerThread.getLooper()); } }