Loading libs/WindowManager/Shell/src/com/android/wm/shell/taskview/TaskView.java +8 −1 Original line number Diff line number Diff line Loading @@ -151,7 +151,14 @@ public class TaskView extends SurfaceView implements SurfaceHolder.Callback, @Override public void setResizeBgColor(SurfaceControl.Transaction t, int bgColor) { if (mHandler.getLooper().isCurrentThread()) { // We can only use the transaction if it can updated synchronously, otherwise the tx // will be applied immediately after but also used/updated on the view thread which // will lead to a race and/or crash runOnViewThread(() -> setResizeBackgroundColor(t, bgColor)); } else { runOnViewThread(() -> setResizeBackgroundColor(bgColor)); } } /** Loading libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/TestHandler.java 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.wm.shell; import android.os.Handler; import android.os.Looper; import android.os.Message; /** * Basic test handler that immediately executes anything that is posted on it. */ public class TestHandler extends Handler { public TestHandler(Looper looper) { super(looper); } @Override public boolean sendMessageAtTime(Message msg, long uptimeMillis) { dispatchMessage(msg); return true; } } libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/taskview/TaskViewTest.java +24 −3 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ import android.app.ActivityManager; import android.app.ActivityOptions; import android.app.PendingIntent; import android.content.Context; import android.graphics.Color; import android.graphics.Insets; import android.graphics.Rect; import android.graphics.Region; Loading @@ -58,6 +59,7 @@ import androidx.test.filters.SmallTest; import com.android.wm.shell.ShellTaskOrganizer; import com.android.wm.shell.ShellTestCase; import com.android.wm.shell.TestHandler; import com.android.wm.shell.common.HandlerExecutor; import com.android.wm.shell.common.SyncTransactionQueue; import com.android.wm.shell.common.SyncTransactionQueue.TransactionRunnable; Loading Loading @@ -92,8 +94,7 @@ public class TaskViewTest extends ShellTestCase { Transitions mTransitions; @Mock Looper mViewLooper; @Mock Handler mViewHandler; TestHandler mViewHandler; SurfaceSession mSession; SurfaceControl mLeash; Loading @@ -112,7 +113,7 @@ public class TaskViewTest extends ShellTestCase { mContext = getContext(); doReturn(true).when(mViewLooper).isCurrentThread(); doReturn(mViewLooper).when(mViewHandler).getLooper(); mViewHandler = spy(new TestHandler(mViewLooper)); mTaskInfo = new ActivityManager.RunningTaskInfo(); mTaskInfo.token = mToken; Loading Loading @@ -668,4 +669,24 @@ public class TaskViewTest extends ShellTestCase { mTaskViewTaskController.onTaskInfoChanged(mTaskInfo); verify(mViewHandler).post(any()); } @Test public void testSetResizeBgOnSameUiThread_expectUsesTransaction() { SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class); mTaskView = spy(mTaskView); mTaskView.setResizeBgColor(tx, Color.BLUE); verify(mViewHandler, never()).post(any()); verify(mTaskView, never()).setResizeBackgroundColor(eq(Color.BLUE)); verify(mTaskView).setResizeBackgroundColor(eq(tx), eq(Color.BLUE)); } @Test public void testSetResizeBgOnDifferentUiThread_expectDoesNotUseTransaction() { doReturn(false).when(mViewLooper).isCurrentThread(); SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class); mTaskView = spy(mTaskView); mTaskView.setResizeBgColor(tx, Color.BLUE); verify(mViewHandler).post(any()); verify(mTaskView).setResizeBackgroundColor(eq(Color.BLUE)); } } Loading
libs/WindowManager/Shell/src/com/android/wm/shell/taskview/TaskView.java +8 −1 Original line number Diff line number Diff line Loading @@ -151,7 +151,14 @@ public class TaskView extends SurfaceView implements SurfaceHolder.Callback, @Override public void setResizeBgColor(SurfaceControl.Transaction t, int bgColor) { if (mHandler.getLooper().isCurrentThread()) { // We can only use the transaction if it can updated synchronously, otherwise the tx // will be applied immediately after but also used/updated on the view thread which // will lead to a race and/or crash runOnViewThread(() -> setResizeBackgroundColor(t, bgColor)); } else { runOnViewThread(() -> setResizeBackgroundColor(bgColor)); } } /** Loading
libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/TestHandler.java 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.wm.shell; import android.os.Handler; import android.os.Looper; import android.os.Message; /** * Basic test handler that immediately executes anything that is posted on it. */ public class TestHandler extends Handler { public TestHandler(Looper looper) { super(looper); } @Override public boolean sendMessageAtTime(Message msg, long uptimeMillis) { dispatchMessage(msg); return true; } }
libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/taskview/TaskViewTest.java +24 −3 Original line number Diff line number Diff line Loading @@ -40,6 +40,7 @@ import android.app.ActivityManager; import android.app.ActivityOptions; import android.app.PendingIntent; import android.content.Context; import android.graphics.Color; import android.graphics.Insets; import android.graphics.Rect; import android.graphics.Region; Loading @@ -58,6 +59,7 @@ import androidx.test.filters.SmallTest; import com.android.wm.shell.ShellTaskOrganizer; import com.android.wm.shell.ShellTestCase; import com.android.wm.shell.TestHandler; import com.android.wm.shell.common.HandlerExecutor; import com.android.wm.shell.common.SyncTransactionQueue; import com.android.wm.shell.common.SyncTransactionQueue.TransactionRunnable; Loading Loading @@ -92,8 +94,7 @@ public class TaskViewTest extends ShellTestCase { Transitions mTransitions; @Mock Looper mViewLooper; @Mock Handler mViewHandler; TestHandler mViewHandler; SurfaceSession mSession; SurfaceControl mLeash; Loading @@ -112,7 +113,7 @@ public class TaskViewTest extends ShellTestCase { mContext = getContext(); doReturn(true).when(mViewLooper).isCurrentThread(); doReturn(mViewLooper).when(mViewHandler).getLooper(); mViewHandler = spy(new TestHandler(mViewLooper)); mTaskInfo = new ActivityManager.RunningTaskInfo(); mTaskInfo.token = mToken; Loading Loading @@ -668,4 +669,24 @@ public class TaskViewTest extends ShellTestCase { mTaskViewTaskController.onTaskInfoChanged(mTaskInfo); verify(mViewHandler).post(any()); } @Test public void testSetResizeBgOnSameUiThread_expectUsesTransaction() { SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class); mTaskView = spy(mTaskView); mTaskView.setResizeBgColor(tx, Color.BLUE); verify(mViewHandler, never()).post(any()); verify(mTaskView, never()).setResizeBackgroundColor(eq(Color.BLUE)); verify(mTaskView).setResizeBackgroundColor(eq(tx), eq(Color.BLUE)); } @Test public void testSetResizeBgOnDifferentUiThread_expectDoesNotUseTransaction() { doReturn(false).when(mViewLooper).isCurrentThread(); SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class); mTaskView = spy(mTaskView); mTaskView.setResizeBgColor(tx, Color.BLUE); verify(mViewHandler).post(any()); verify(mTaskView).setResizeBackgroundColor(eq(Color.BLUE)); } }