diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityTestsBase.java b/services/tests/wmtests/src/com/android/server/wm/ActivityTestsBase.java index 95732d721ebb2..1fefb0c481d10 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ActivityTestsBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/ActivityTestsBase.java @@ -230,6 +230,7 @@ class ActivityTestsBase extends SystemServiceTestsBase { } ActivityRecord build() { + SystemServicesTestRule.checkHoldsLock(mService.mGlobalLock); try { mService.deferWindowLayout(); return buildInner(); @@ -394,6 +395,8 @@ class ActivityTestsBase extends SystemServiceTestsBase { } Task build() { + SystemServicesTestRule.checkHoldsLock(mSupervisor.mService.mGlobalLock); + if (mStack == null && mCreateStack) { TaskDisplayArea displayArea = mTaskDisplayArea != null ? mTaskDisplayArea : mSupervisor.mRootWindowContainer.getDefaultTaskDisplayArea(); @@ -501,6 +504,8 @@ class ActivityTestsBase extends SystemServiceTestsBase { } ActivityStack build() { + SystemServicesTestRule.checkHoldsLock(mRootWindowContainer.mWmService.mGlobalLock); + final int stackId = mStackId >= 0 ? mStackId : mTaskDisplayArea.getNextStackId(); final ActivityStack stack = mTaskDisplayArea.createStackUnchecked( mWindowingMode, mActivityType, stackId, mOnTop, mInfo, mIntent, diff --git a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java index bce1320f1d7ef..218261b010295 100644 --- a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java +++ b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java @@ -100,7 +100,6 @@ public class SystemServicesTestRule implements TestRule { static int sNextDisplayId = DEFAULT_DISPLAY + 100; static int sNextTaskId = 100; - private final AtomicBoolean mCurrentMessagesProcessed = new AtomicBoolean(false); private static final int[] TEST_USER_PROFILE_IDS = {}; private Context mContext; @@ -419,20 +418,20 @@ public class SystemServicesTestRule implements TestRule { if (wm == null) { return; } - synchronized (mCurrentMessagesProcessed) { - // Add a message to the handler queue and make sure it is fully processed before we move - // on. This makes sure all previous messages in the handler are fully processed vs. just - // popping them from the message queue. - mCurrentMessagesProcessed.set(false); - wm.mAnimator.getChoreographer().postFrameCallback(time -> { - synchronized (mCurrentMessagesProcessed) { - mCurrentMessagesProcessed.set(true); - mCurrentMessagesProcessed.notifyAll(); - } - }); - while (!mCurrentMessagesProcessed.get()) { + // Add a message to the handler queue and make sure it is fully processed before we move on. + // This makes sure all previous messages in the handler are fully processed vs. just popping + // them from the message queue. + final AtomicBoolean currentMessagesProcessed = new AtomicBoolean(false); + wm.mAnimator.getChoreographer().postFrameCallback(time -> { + synchronized (currentMessagesProcessed) { + currentMessagesProcessed.set(true); + currentMessagesProcessed.notifyAll(); + } + }); + while (!currentMessagesProcessed.get()) { + synchronized (currentMessagesProcessed) { try { - mCurrentMessagesProcessed.wait(); + currentMessagesProcessed.wait(); } catch (InterruptedException e) { } } diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowTestUtils.java b/services/tests/wmtests/src/com/android/server/wm/WindowTestUtils.java index fc95556750f17..49993610bbedd 100644 --- a/services/tests/wmtests/src/com/android/server/wm/WindowTestUtils.java +++ b/services/tests/wmtests/src/com/android/server/wm/WindowTestUtils.java @@ -34,14 +34,11 @@ class WindowTestUtils { /** Creates a {@link Task} and adds it to the specified {@link ActivityStack}. */ static Task createTaskInStack(WindowManagerService service, ActivityStack stack, int userId) { - synchronized (service.mGlobalLock) { - final Task task = new ActivityTestsBase.TaskBuilder( - stack.mStackSupervisor) - .setUserId(userId) - .setStack(stack) - .build(); - return task; - } + final Task task = new ActivityTestsBase.TaskBuilder(stack.mStackSupervisor) + .setUserId(userId) + .setStack(stack) + .build(); + return task; } /** Creates an {@link ActivityRecord} and adds it to the specified {@link Task}. */ @@ -52,23 +49,18 @@ class WindowTestUtils { } static ActivityRecord createTestActivityRecord(ActivityStack stack) { - synchronized (stack.mAtmService.mGlobalLock) { - final ActivityRecord activity = new ActivityTestsBase.ActivityBuilder( - stack.mAtmService) - .setStack(stack) - .setCreateTask(true) - .build(); - postCreateActivitySetup(activity, stack.getDisplayContent()); - return activity; - } + final ActivityRecord activity = new ActivityTestsBase.ActivityBuilder(stack.mAtmService) + .setStack(stack) + .setCreateTask(true) + .build(); + postCreateActivitySetup(activity, stack.getDisplayContent()); + return activity; } static ActivityRecord createTestActivityRecord(DisplayContent dc) { - synchronized (dc.mWmService.mGlobalLock) { - final ActivityRecord activity = new ActivityBuilder(dc.mWmService.mAtmService).build(); - postCreateActivitySetup(activity, dc); - return activity; - } + final ActivityRecord activity = new ActivityBuilder(dc.mWmService.mAtmService).build(); + postCreateActivitySetup(activity, dc); + return activity; } private static void postCreateActivitySetup(ActivityRecord activity, DisplayContent dc) { @@ -84,9 +76,9 @@ class WindowTestUtils { static TestWindowToken createTestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) { - synchronized (dc.mWmService.mGlobalLock) { - return new TestWindowToken(type, dc, persistOnEmpty); - } + SystemServicesTestRule.checkHoldsLock(dc.mWmService.mGlobalLock); + + return new TestWindowToken(type, dc, persistOnEmpty); } /* Used so we can gain access to some protected members of the {@link WindowToken} class */ diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java b/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java index 0eaab527267a7..a1e5b80eb2eda 100644 --- a/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java @@ -55,11 +55,7 @@ import com.android.server.AttributeCache; import org.junit.Before; import org.junit.BeforeClass; -/** - * Common base class for window manager unit test classes. - * - * Make sure any requests to WM hold the WM lock if needed b/73966377 - */ +/** Common base class for window manager unit test classes. */ class WindowTestsBase extends SystemServiceTestsBase { private static final String TAG = WindowTestsBase.class.getSimpleName(); @@ -94,43 +90,40 @@ class WindowTestsBase extends SystemServiceTestsBase { @Before public void setUpBase() { + mWm = mSystemServicesTestRule.getWindowManagerService(); + SystemServicesTestRule.checkHoldsLock(mWm.mGlobalLock); + + mTransaction = mSystemServicesTestRule.mTransaction; + mMockSession = mock(Session.class); + final Context context = getInstrumentation().getTargetContext(); // If @Before throws an exception, the error isn't logged. This will make sure any failures // in the set up are clear. This can be removed when b/37850063 is fixed. try { - mMockSession = mock(Session.class); - - final Context context = getInstrumentation().getTargetContext(); - - mWm = mSystemServicesTestRule.getWindowManagerService(); - mTransaction = mSystemServicesTestRule.mTransaction; - beforeCreateDisplay(); context.getDisplay().getDisplayInfo(mDisplayInfo); mDisplayContent = createNewDisplay(true /* supportIme */); // Set-up some common windows. - synchronized (mWm.mGlobalLock) { - mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow"); - mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow"); - mDisplayContent.mInputMethodWindow = mImeWindow; - mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, - "mImeDialogWindow"); - mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow"); - mNotificationShadeWindow = createCommonWindow(null, TYPE_NOTIFICATION_SHADE, - "mNotificationShadeWindow"); - mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow"); - mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, - "mDockedDividerWindow"); - mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow"); - mChildAppWindowAbove = createCommonWindow(mAppWindow, - TYPE_APPLICATION_ATTACHED_DIALOG, - "mChildAppWindowAbove"); - mChildAppWindowBelow = createCommonWindow(mAppWindow, - TYPE_APPLICATION_MEDIA_OVERLAY, - "mChildAppWindowBelow"); - mDisplayContent.getDisplayPolicy().setForceShowSystemBars(false); - } + mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow"); + mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow"); + mDisplayContent.mInputMethodWindow = mImeWindow; + mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, + "mImeDialogWindow"); + mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow"); + mNotificationShadeWindow = createCommonWindow(null, TYPE_NOTIFICATION_SHADE, + "mNotificationShadeWindow"); + mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow"); + mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, + "mDockedDividerWindow"); + mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow"); + mChildAppWindowAbove = createCommonWindow(mAppWindow, + TYPE_APPLICATION_ATTACHED_DIALOG, + "mChildAppWindowAbove"); + mChildAppWindowBelow = createCommonWindow(mAppWindow, + TYPE_APPLICATION_MEDIA_OVERLAY, + "mChildAppWindowBelow"); + mDisplayContent.getDisplayPolicy().setForceShowSystemBars(false); // Adding a display will cause freezing the display. Make sure to wait until it's // unfrozen to not run into race conditions with the tests. @@ -146,23 +139,19 @@ class WindowTestsBase extends SystemServiceTestsBase { } private WindowState createCommonWindow(WindowState parent, int type, String name) { - synchronized (mWm.mGlobalLock) { - final WindowState win = createWindow(parent, type, name); - // Prevent common windows from been IMe targets - win.mAttrs.flags |= FLAG_NOT_FOCUSABLE; - return win; - } + final WindowState win = createWindow(parent, type, name); + // Prevent common windows from been IME targets. + win.mAttrs.flags |= FLAG_NOT_FOCUSABLE; + return win; } private WindowToken createWindowToken( DisplayContent dc, int windowingMode, int activityType, int type) { - synchronized (mWm.mGlobalLock) { - if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) { - return WindowTestUtils.createTestWindowToken(type, dc); - } - - return createActivityRecord(dc, windowingMode, activityType); + if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) { + return WindowTestUtils.createTestWindowToken(type, dc); } + + return createActivityRecord(dc, windowingMode, activityType); } ActivityRecord createActivityRecord(DisplayContent dc, int windowingMode, int activityType) { @@ -176,78 +165,60 @@ class WindowTestsBase extends SystemServiceTestsBase { } WindowState createWindow(WindowState parent, int type, String name) { - synchronized (mWm.mGlobalLock) { - return (parent == null) - ? createWindow(parent, type, mDisplayContent, name) - : createWindow(parent, type, parent.mToken, name); - } + return (parent == null) + ? createWindow(parent, type, mDisplayContent, name) + : createWindow(parent, type, parent.mToken, name); } WindowState createWindow(WindowState parent, int type, String name, int ownerId) { - synchronized (mWm.mGlobalLock) { - return (parent == null) - ? createWindow(parent, type, mDisplayContent, name, ownerId) - : createWindow(parent, type, parent.mToken, name, ownerId); - } + return (parent == null) + ? createWindow(parent, type, mDisplayContent, name, ownerId) + : createWindow(parent, type, parent.mToken, name, ownerId); } WindowState createWindowOnStack(WindowState parent, int windowingMode, int activityType, int type, DisplayContent dc, String name) { - synchronized (mWm.mGlobalLock) { - final WindowToken token = createWindowToken(dc, windowingMode, activityType, type); - return createWindow(parent, type, token, name); - } + final WindowToken token = createWindowToken(dc, windowingMode, activityType, type); + return createWindow(parent, type, token, name); } WindowState createAppWindow(Task task, int type, String name) { - synchronized (mWm.mGlobalLock) { - final ActivityRecord activity = - WindowTestUtils.createTestActivityRecord(task.getDisplayContent()); - task.addChild(activity, 0); - return createWindow(null, type, activity, name); - } + final ActivityRecord activity = + WindowTestUtils.createTestActivityRecord(task.getDisplayContent()); + task.addChild(activity, 0); + return createWindow(null, type, activity, name); } WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) { - synchronized (mWm.mGlobalLock) { - final WindowToken token = createWindowToken( - dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); - return createWindow(parent, type, token, name, 0 /* ownerId */); - } + final WindowToken token = createWindowToken( + dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); + return createWindow(parent, type, token, name, 0 /* ownerId */); } WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name, int ownerId) { - synchronized (mWm.mGlobalLock) { - final WindowToken token = createWindowToken( - dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); - return createWindow(parent, type, token, name, ownerId); - } + final WindowToken token = createWindowToken( + dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); + return createWindow(parent, type, token, name, ownerId); } WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name, boolean ownerCanAddInternalSystemWindow) { - synchronized (mWm.mGlobalLock) { - final WindowToken token = createWindowToken( - dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); - return createWindow(parent, type, token, name, 0 /* ownerId */, - ownerCanAddInternalSystemWindow); - } + final WindowToken token = createWindowToken( + dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); + return createWindow(parent, type, token, name, 0 /* ownerId */, + ownerCanAddInternalSystemWindow); } WindowState createWindow(WindowState parent, int type, WindowToken token, String name) { - synchronized (mWm.mGlobalLock) { - return createWindow(parent, type, token, name, 0 /* ownerId */, - false /* ownerCanAddInternalSystemWindow */); - } + return createWindow(parent, type, token, name, 0 /* ownerId */, + false /* ownerCanAddInternalSystemWindow */); } WindowState createWindow(WindowState parent, int type, WindowToken token, String name, int ownerId) { - synchronized (mWm.mGlobalLock) { - return createWindow(parent, type, token, name, ownerId, - false /* ownerCanAddInternalSystemWindow */); - } + return createWindow(parent, type, token, name, ownerId, + false /* ownerCanAddInternalSystemWindow */); } WindowState createWindow(WindowState parent, int type, WindowToken token, String name, @@ -261,19 +232,19 @@ class WindowTestsBase extends SystemServiceTestsBase { String name, int ownerId, int userId, boolean ownerCanAddInternalSystemWindow, WindowManagerService service, Session session, IWindow iWindow, WindowState.PowerManagerWrapper powerManagerWrapper) { - synchronized (service.mGlobalLock) { - final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); - attrs.setTitle(name); + SystemServicesTestRule.checkHoldsLock(service.mGlobalLock); - final WindowState w = new WindowState(service, session, iWindow, token, parent, - OP_NONE, 0, attrs, VISIBLE, ownerId, userId, - ownerCanAddInternalSystemWindow, - powerManagerWrapper); - // TODO: Probably better to make this call in the WindowState ctor to avoid errors with - // adding it to the token... - token.addWindow(w); - return w; - } + final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); + attrs.setTitle(name); + + final WindowState w = new WindowState(service, session, iWindow, token, parent, + OP_NONE, 0, attrs, VISIBLE, ownerId, userId, + ownerCanAddInternalSystemWindow, + powerManagerWrapper); + // TODO: Probably better to make this call in the WindowState ctor to avoid errors with + // adding it to the token... + token.addWindow(w); + return w; } static void makeWindowVisible(WindowState... windows) { @@ -292,30 +263,24 @@ class WindowTestsBase extends SystemServiceTestsBase { } ActivityStack createTaskStackOnDisplay(int windowingMode, int activityType, DisplayContent dc) { - synchronized (mWm.mGlobalLock) { - return new ActivityTestsBase.StackBuilder( - dc.mWmService.mAtmService.mRootWindowContainer) - .setDisplay(dc) - .setWindowingMode(windowingMode) - .setActivityType(activityType) - .setCreateActivity(false) - .setIntent(new Intent()) - .build(); - } + return new ActivityTestsBase.StackBuilder(dc.mWmService.mRoot) + .setDisplay(dc) + .setWindowingMode(windowingMode) + .setActivityType(activityType) + .setCreateActivity(false) + .setIntent(new Intent()) + .build(); } - ActivityStack createTaskStackOnTaskDisplayArea( - int windowingMode, int activityType, TaskDisplayArea tda) { - synchronized (mWm.mGlobalLock) { - return new ActivityTestsBase.StackBuilder( - tda.mDisplayContent.mWmService.mAtmService.mRootWindowContainer) - .setTaskDisplayArea(tda) - .setWindowingMode(windowingMode) - .setActivityType(activityType) - .setCreateActivity(false) - .setIntent(new Intent()) - .build(); - } + ActivityStack createTaskStackOnTaskDisplayArea(int windowingMode, int activityType, + TaskDisplayArea tda) { + return new ActivityTestsBase.StackBuilder(tda.mWmService.mRoot) + .setTaskDisplayArea(tda) + .setWindowingMode(windowingMode) + .setActivityType(activityType) + .setCreateActivity(false) + .setIntent(new Intent()) + .build(); } /** Creates a {@link Task} and adds it to the specified {@link ActivityStack}. */ @@ -365,9 +330,9 @@ class WindowTestsBase extends SystemServiceTestsBase { /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */ WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs, WindowToken token) { - synchronized (mWm.mGlobalLock) { - return new WindowTestUtils.TestWindowState(mWm, mMockSession, mIWindow, attrs, token); - } + SystemServicesTestRule.checkHoldsLock(mWm.mGlobalLock); + + return new WindowTestUtils.TestWindowState(mWm, mMockSession, mIWindow, attrs, token); } /** Creates a {@link DisplayContent} as parts of simulate display info for test. */