Merge "Clean up unnecessary lock of WmTests" into rvc-dev

This commit is contained in:
Riddle Hsu
2020-05-11 05:02:53 +00:00
committed by Android (Google) Code Review
4 changed files with 126 additions and 165 deletions

View File

@@ -230,6 +230,7 @@ class ActivityTestsBase extends SystemServiceTestsBase {
} }
ActivityRecord build() { ActivityRecord build() {
SystemServicesTestRule.checkHoldsLock(mService.mGlobalLock);
try { try {
mService.deferWindowLayout(); mService.deferWindowLayout();
return buildInner(); return buildInner();
@@ -394,6 +395,8 @@ class ActivityTestsBase extends SystemServiceTestsBase {
} }
Task build() { Task build() {
SystemServicesTestRule.checkHoldsLock(mSupervisor.mService.mGlobalLock);
if (mStack == null && mCreateStack) { if (mStack == null && mCreateStack) {
TaskDisplayArea displayArea = mTaskDisplayArea != null ? mTaskDisplayArea TaskDisplayArea displayArea = mTaskDisplayArea != null ? mTaskDisplayArea
: mSupervisor.mRootWindowContainer.getDefaultTaskDisplayArea(); : mSupervisor.mRootWindowContainer.getDefaultTaskDisplayArea();
@@ -501,6 +504,8 @@ class ActivityTestsBase extends SystemServiceTestsBase {
} }
ActivityStack build() { ActivityStack build() {
SystemServicesTestRule.checkHoldsLock(mRootWindowContainer.mWmService.mGlobalLock);
final int stackId = mStackId >= 0 ? mStackId : mTaskDisplayArea.getNextStackId(); final int stackId = mStackId >= 0 ? mStackId : mTaskDisplayArea.getNextStackId();
final ActivityStack stack = mTaskDisplayArea.createStackUnchecked( final ActivityStack stack = mTaskDisplayArea.createStackUnchecked(
mWindowingMode, mActivityType, stackId, mOnTop, mInfo, mIntent, mWindowingMode, mActivityType, stackId, mOnTop, mInfo, mIntent,

View File

@@ -100,7 +100,6 @@ public class SystemServicesTestRule implements TestRule {
static int sNextDisplayId = DEFAULT_DISPLAY + 100; static int sNextDisplayId = DEFAULT_DISPLAY + 100;
static int sNextTaskId = 100; static int sNextTaskId = 100;
private final AtomicBoolean mCurrentMessagesProcessed = new AtomicBoolean(false);
private static final int[] TEST_USER_PROFILE_IDS = {}; private static final int[] TEST_USER_PROFILE_IDS = {};
private Context mContext; private Context mContext;
@@ -419,20 +418,20 @@ public class SystemServicesTestRule implements TestRule {
if (wm == null) { if (wm == null) {
return; return;
} }
synchronized (mCurrentMessagesProcessed) { // Add a message to the handler queue and make sure it is fully processed before we move on.
// Add a message to the handler queue and make sure it is fully processed before we move // This makes sure all previous messages in the handler are fully processed vs. just popping
// on. This makes sure all previous messages in the handler are fully processed vs. just // them from the message queue.
// popping them from the message queue. final AtomicBoolean currentMessagesProcessed = new AtomicBoolean(false);
mCurrentMessagesProcessed.set(false); wm.mAnimator.getChoreographer().postFrameCallback(time -> {
wm.mAnimator.getChoreographer().postFrameCallback(time -> { synchronized (currentMessagesProcessed) {
synchronized (mCurrentMessagesProcessed) { currentMessagesProcessed.set(true);
mCurrentMessagesProcessed.set(true); currentMessagesProcessed.notifyAll();
mCurrentMessagesProcessed.notifyAll(); }
} });
}); while (!currentMessagesProcessed.get()) {
while (!mCurrentMessagesProcessed.get()) { synchronized (currentMessagesProcessed) {
try { try {
mCurrentMessagesProcessed.wait(); currentMessagesProcessed.wait();
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
} }

View File

@@ -34,14 +34,11 @@ class WindowTestUtils {
/** Creates a {@link Task} and adds it to the specified {@link ActivityStack}. */ /** Creates a {@link Task} and adds it to the specified {@link ActivityStack}. */
static Task createTaskInStack(WindowManagerService service, ActivityStack stack, int userId) { static Task createTaskInStack(WindowManagerService service, ActivityStack stack, int userId) {
synchronized (service.mGlobalLock) { final Task task = new ActivityTestsBase.TaskBuilder(stack.mStackSupervisor)
final Task task = new ActivityTestsBase.TaskBuilder( .setUserId(userId)
stack.mStackSupervisor) .setStack(stack)
.setUserId(userId) .build();
.setStack(stack) return task;
.build();
return task;
}
} }
/** Creates an {@link ActivityRecord} and adds it to the specified {@link Task}. */ /** Creates an {@link ActivityRecord} and adds it to the specified {@link Task}. */
@@ -52,23 +49,18 @@ class WindowTestUtils {
} }
static ActivityRecord createTestActivityRecord(ActivityStack stack) { static ActivityRecord createTestActivityRecord(ActivityStack stack) {
synchronized (stack.mAtmService.mGlobalLock) { final ActivityRecord activity = new ActivityTestsBase.ActivityBuilder(stack.mAtmService)
final ActivityRecord activity = new ActivityTestsBase.ActivityBuilder( .setStack(stack)
stack.mAtmService) .setCreateTask(true)
.setStack(stack) .build();
.setCreateTask(true) postCreateActivitySetup(activity, stack.getDisplayContent());
.build(); return activity;
postCreateActivitySetup(activity, stack.getDisplayContent());
return activity;
}
} }
static ActivityRecord createTestActivityRecord(DisplayContent dc) { static ActivityRecord createTestActivityRecord(DisplayContent dc) {
synchronized (dc.mWmService.mGlobalLock) { final ActivityRecord activity = new ActivityBuilder(dc.mWmService.mAtmService).build();
final ActivityRecord activity = new ActivityBuilder(dc.mWmService.mAtmService).build(); postCreateActivitySetup(activity, dc);
postCreateActivitySetup(activity, dc); return activity;
return activity;
}
} }
private static void postCreateActivitySetup(ActivityRecord activity, DisplayContent dc) { private static void postCreateActivitySetup(ActivityRecord activity, DisplayContent dc) {
@@ -84,9 +76,9 @@ class WindowTestUtils {
static TestWindowToken createTestWindowToken(int type, DisplayContent dc, static TestWindowToken createTestWindowToken(int type, DisplayContent dc,
boolean persistOnEmpty) { boolean persistOnEmpty) {
synchronized (dc.mWmService.mGlobalLock) { SystemServicesTestRule.checkHoldsLock(dc.mWmService.mGlobalLock);
return new TestWindowToken(type, dc, persistOnEmpty);
} return new TestWindowToken(type, dc, persistOnEmpty);
} }
/* Used so we can gain access to some protected members of the {@link WindowToken} class */ /* Used so we can gain access to some protected members of the {@link WindowToken} class */

View File

@@ -55,11 +55,7 @@ import com.android.server.AttributeCache;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
/** /** Common base class for window manager unit test classes. */
* Common base class for window manager unit test classes.
*
* Make sure any requests to WM hold the WM lock if needed b/73966377
*/
class WindowTestsBase extends SystemServiceTestsBase { class WindowTestsBase extends SystemServiceTestsBase {
private static final String TAG = WindowTestsBase.class.getSimpleName(); private static final String TAG = WindowTestsBase.class.getSimpleName();
@@ -94,43 +90,40 @@ class WindowTestsBase extends SystemServiceTestsBase {
@Before @Before
public void setUpBase() { 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 // 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. // in the set up are clear. This can be removed when b/37850063 is fixed.
try { try {
mMockSession = mock(Session.class);
final Context context = getInstrumentation().getTargetContext();
mWm = mSystemServicesTestRule.getWindowManagerService();
mTransaction = mSystemServicesTestRule.mTransaction;
beforeCreateDisplay(); beforeCreateDisplay();
context.getDisplay().getDisplayInfo(mDisplayInfo); context.getDisplay().getDisplayInfo(mDisplayInfo);
mDisplayContent = createNewDisplay(true /* supportIme */); mDisplayContent = createNewDisplay(true /* supportIme */);
// Set-up some common windows. // Set-up some common windows.
synchronized (mWm.mGlobalLock) { mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow"); mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow"); mDisplayContent.mInputMethodWindow = mImeWindow;
mDisplayContent.mInputMethodWindow = mImeWindow; mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG,
mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "mImeDialogWindow");
"mImeDialogWindow"); mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow"); mNotificationShadeWindow = createCommonWindow(null, TYPE_NOTIFICATION_SHADE,
mNotificationShadeWindow = createCommonWindow(null, TYPE_NOTIFICATION_SHADE, "mNotificationShadeWindow");
"mNotificationShadeWindow"); mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow"); mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER,
mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "mDockedDividerWindow");
"mDockedDividerWindow"); mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow"); mChildAppWindowAbove = createCommonWindow(mAppWindow,
mChildAppWindowAbove = createCommonWindow(mAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
TYPE_APPLICATION_ATTACHED_DIALOG, "mChildAppWindowAbove");
"mChildAppWindowAbove"); mChildAppWindowBelow = createCommonWindow(mAppWindow,
mChildAppWindowBelow = createCommonWindow(mAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
TYPE_APPLICATION_MEDIA_OVERLAY, "mChildAppWindowBelow");
"mChildAppWindowBelow"); mDisplayContent.getDisplayPolicy().setForceShowSystemBars(false);
mDisplayContent.getDisplayPolicy().setForceShowSystemBars(false);
}
// Adding a display will cause freezing the display. Make sure to wait until it's // 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. // 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) { private WindowState createCommonWindow(WindowState parent, int type, String name) {
synchronized (mWm.mGlobalLock) { final WindowState win = createWindow(parent, type, name);
final WindowState win = createWindow(parent, type, name); // Prevent common windows from been IME targets.
// Prevent common windows from been IMe targets win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
win.mAttrs.flags |= FLAG_NOT_FOCUSABLE; return win;
return win;
}
} }
private WindowToken createWindowToken( private WindowToken createWindowToken(
DisplayContent dc, int windowingMode, int activityType, int type) { DisplayContent dc, int windowingMode, int activityType, int type) {
synchronized (mWm.mGlobalLock) { if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) { return WindowTestUtils.createTestWindowToken(type, dc);
return WindowTestUtils.createTestWindowToken(type, dc);
}
return createActivityRecord(dc, windowingMode, activityType);
} }
return createActivityRecord(dc, windowingMode, activityType);
} }
ActivityRecord createActivityRecord(DisplayContent dc, int windowingMode, int 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) { WindowState createWindow(WindowState parent, int type, String name) {
synchronized (mWm.mGlobalLock) { return (parent == null)
return (parent == null) ? createWindow(parent, type, mDisplayContent, name)
? createWindow(parent, type, mDisplayContent, name) : createWindow(parent, type, parent.mToken, name);
: createWindow(parent, type, parent.mToken, name);
}
} }
WindowState createWindow(WindowState parent, int type, String name, int ownerId) { WindowState createWindow(WindowState parent, int type, String name, int ownerId) {
synchronized (mWm.mGlobalLock) { return (parent == null)
return (parent == null) ? createWindow(parent, type, mDisplayContent, name, ownerId)
? createWindow(parent, type, mDisplayContent, name, ownerId) : createWindow(parent, type, parent.mToken, name, ownerId);
: createWindow(parent, type, parent.mToken, name, ownerId);
}
} }
WindowState createWindowOnStack(WindowState parent, int windowingMode, int activityType, WindowState createWindowOnStack(WindowState parent, int windowingMode, int activityType,
int type, DisplayContent dc, String name) { int type, DisplayContent dc, String name) {
synchronized (mWm.mGlobalLock) { final WindowToken token = createWindowToken(dc, windowingMode, activityType, type);
final WindowToken token = createWindowToken(dc, windowingMode, activityType, type); return createWindow(parent, type, token, name);
return createWindow(parent, type, token, name);
}
} }
WindowState createAppWindow(Task task, int type, String name) { WindowState createAppWindow(Task task, int type, String name) {
synchronized (mWm.mGlobalLock) { final ActivityRecord activity =
final ActivityRecord activity = WindowTestUtils.createTestActivityRecord(task.getDisplayContent());
WindowTestUtils.createTestActivityRecord(task.getDisplayContent()); task.addChild(activity, 0);
task.addChild(activity, 0); return createWindow(null, type, activity, name);
return createWindow(null, type, activity, name);
}
} }
WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) { WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
synchronized (mWm.mGlobalLock) { final WindowToken token = createWindowToken(
final WindowToken token = createWindowToken( dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); return createWindow(parent, type, token, name, 0 /* ownerId */);
return createWindow(parent, type, token, name, 0 /* ownerId */);
}
} }
WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name, WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
int ownerId) { int ownerId) {
synchronized (mWm.mGlobalLock) { final WindowToken token = createWindowToken(
final WindowToken token = createWindowToken( dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); return createWindow(parent, type, token, name, ownerId);
return createWindow(parent, type, token, name, ownerId);
}
} }
WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name, WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
boolean ownerCanAddInternalSystemWindow) { boolean ownerCanAddInternalSystemWindow) {
synchronized (mWm.mGlobalLock) { final WindowToken token = createWindowToken(
final WindowToken token = createWindowToken( dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type); return createWindow(parent, type, token, name, 0 /* ownerId */,
return createWindow(parent, type, token, name, 0 /* ownerId */, ownerCanAddInternalSystemWindow);
ownerCanAddInternalSystemWindow);
}
} }
WindowState createWindow(WindowState parent, int type, WindowToken token, String name) { WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
synchronized (mWm.mGlobalLock) { return createWindow(parent, type, token, name, 0 /* ownerId */,
return createWindow(parent, type, token, name, 0 /* ownerId */, false /* ownerCanAddInternalSystemWindow */);
false /* ownerCanAddInternalSystemWindow */);
}
} }
WindowState createWindow(WindowState parent, int type, WindowToken token, String name, WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
int ownerId) { int ownerId) {
synchronized (mWm.mGlobalLock) { return createWindow(parent, type, token, name, ownerId,
return createWindow(parent, type, token, name, ownerId, false /* ownerCanAddInternalSystemWindow */);
false /* ownerCanAddInternalSystemWindow */);
}
} }
WindowState createWindow(WindowState parent, int type, WindowToken token, String name, 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, String name, int ownerId, int userId, boolean ownerCanAddInternalSystemWindow,
WindowManagerService service, Session session, IWindow iWindow, WindowManagerService service, Session session, IWindow iWindow,
WindowState.PowerManagerWrapper powerManagerWrapper) { WindowState.PowerManagerWrapper powerManagerWrapper) {
synchronized (service.mGlobalLock) { SystemServicesTestRule.checkHoldsLock(service.mGlobalLock);
final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
attrs.setTitle(name);
final WindowState w = new WindowState(service, session, iWindow, token, parent, final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
OP_NONE, 0, attrs, VISIBLE, ownerId, userId, attrs.setTitle(name);
ownerCanAddInternalSystemWindow,
powerManagerWrapper); final WindowState w = new WindowState(service, session, iWindow, token, parent,
// TODO: Probably better to make this call in the WindowState ctor to avoid errors with OP_NONE, 0, attrs, VISIBLE, ownerId, userId,
// adding it to the token... ownerCanAddInternalSystemWindow,
token.addWindow(w); powerManagerWrapper);
return w; // 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) { static void makeWindowVisible(WindowState... windows) {
@@ -292,30 +263,24 @@ class WindowTestsBase extends SystemServiceTestsBase {
} }
ActivityStack createTaskStackOnDisplay(int windowingMode, int activityType, DisplayContent dc) { ActivityStack createTaskStackOnDisplay(int windowingMode, int activityType, DisplayContent dc) {
synchronized (mWm.mGlobalLock) { return new ActivityTestsBase.StackBuilder(dc.mWmService.mRoot)
return new ActivityTestsBase.StackBuilder( .setDisplay(dc)
dc.mWmService.mAtmService.mRootWindowContainer) .setWindowingMode(windowingMode)
.setDisplay(dc) .setActivityType(activityType)
.setWindowingMode(windowingMode) .setCreateActivity(false)
.setActivityType(activityType) .setIntent(new Intent())
.setCreateActivity(false) .build();
.setIntent(new Intent())
.build();
}
} }
ActivityStack createTaskStackOnTaskDisplayArea( ActivityStack createTaskStackOnTaskDisplayArea(int windowingMode, int activityType,
int windowingMode, int activityType, TaskDisplayArea tda) { TaskDisplayArea tda) {
synchronized (mWm.mGlobalLock) { return new ActivityTestsBase.StackBuilder(tda.mWmService.mRoot)
return new ActivityTestsBase.StackBuilder( .setTaskDisplayArea(tda)
tda.mDisplayContent.mWmService.mAtmService.mRootWindowContainer) .setWindowingMode(windowingMode)
.setTaskDisplayArea(tda) .setActivityType(activityType)
.setWindowingMode(windowingMode) .setCreateActivity(false)
.setActivityType(activityType) .setIntent(new Intent())
.setCreateActivity(false) .build();
.setIntent(new Intent())
.build();
}
} }
/** Creates a {@link Task} and adds it to the specified {@link ActivityStack}. */ /** 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} */ /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs, WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
WindowToken token) { WindowToken token) {
synchronized (mWm.mGlobalLock) { SystemServicesTestRule.checkHoldsLock(mWm.mGlobalLock);
return new WindowTestUtils.TestWindowState(mWm, mMockSession, mIWindow, attrs, token);
} return new WindowTestUtils.TestWindowState(mWm, mMockSession, mIWindow, attrs, token);
} }
/** Creates a {@link DisplayContent} as parts of simulate display info for test. */ /** Creates a {@link DisplayContent} as parts of simulate display info for test. */