Fixing leaks in WindowMagnificationControllerTest

The main part of this fix is always using field of mWindowMagnificationController because it's always properly cleaned up in @After. Lack of cleaning in one test was causing the leak.
Additionally, I switched mock of Handler to FakeHandler - this alone fixes the problem but only by hiding the symptom. Anyway it's likely safer to use fake instead of mock and errors should not spill to other tests.
As an extra touch in the process this CL overrides default animator to use 0 time instead of the default one which takes some real time.

Fixes: 214021969
Test: adb shell am instrument -w -e \
class com.android.systemui.accessibility.WindowMagnificationControllerTest,com.android.systemui.controls.management.ControlsRequestDialogTest \
com.android.systemui.tests/androidx.test.runner.AndroidJUnitRunner

Change-Id: I9326e48dccd93acc82e271c9eb7f137090c83dd3
This commit is contained in:
Michal Brzezinski
2022-01-20 14:00:42 +00:00
parent 7b422dc1a1
commit 809b6fd75d

View File

@@ -32,7 +32,6 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
@@ -42,6 +41,7 @@ import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.animation.ValueAnimator;
import android.app.Instrumentation;
import android.content.Context;
import android.content.pm.ActivityInfo;
@@ -52,6 +52,7 @@ import android.graphics.Rect;
import android.os.Handler;
import android.os.SystemClock;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.testing.TestableResources;
import android.text.TextUtils;
import android.view.Display;
@@ -71,6 +72,7 @@ import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.model.SysUiState;
import com.android.systemui.util.leak.ReferenceTestUtils;
import com.android.systemui.utils.os.FakeHandler;
import org.junit.After;
import org.junit.Before;
@@ -85,13 +87,12 @@ import org.mockito.MockitoAnnotations;
import java.util.List;
@LargeTest
@TestableLooper.RunWithLooper
@RunWith(AndroidTestingRunner.class)
public class WindowMagnificationControllerTest extends SysuiTestCase {
private static final int LAYOUT_CHANGE_TIMEOUT_MS = 5000;
@Mock
private Handler mHandler;
@Mock
private SfVsyncFrameCallbackProvider mSfVsyncFrameProvider;
@Mock
private MirrorWindowControl mMirrorWindowControl;
@@ -99,17 +100,21 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
private WindowMagnifierCallback mWindowMagnifierCallback;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private SurfaceControl.Transaction mTransaction = new SurfaceControl.Transaction();
private Handler mHandler;
private TestableWindowManager mWindowManager;
private SysUiState mSysUiState = new SysUiState();
private Resources mResources;
private WindowMagnificationAnimationController mWindowMagnificationAnimationController;
private WindowMagnificationController mWindowMagnificationController;
private Instrumentation mInstrumentation;
private final ValueAnimator mValueAnimator = ValueAnimator.ofFloat(0, 1.0f).setDuration(0);
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = Mockito.spy(getContext());
mHandler = new FakeHandler(TestableLooper.get(this).getLooper());
mInstrumentation = InstrumentationRegistry.getInstrumentation();
final WindowManager wm = mContext.getSystemService(WindowManager.class);
mWindowManager = spy(new TestableWindowManager(wm));
@@ -121,17 +126,11 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
return null;
}).when(mSfVsyncFrameProvider).postFrameCallback(
any(FrameCallback.class));
doAnswer(invocation -> {
final Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(mHandler).post(
any(Runnable.class));
mSysUiState.addCallback(Mockito.mock(SysUiState.SysUiStateCallback.class));
mResources = getContext().getOrCreateTestableResources().getResources();
mWindowMagnificationAnimationController = new WindowMagnificationAnimationController(
mContext);
mContext, mValueAnimator);
mWindowMagnificationController = new WindowMagnificationController(mContext,
mHandler, mWindowMagnificationAnimationController, mSfVsyncFrameProvider,
mMirrorWindowControl, mTransaction, mWindowMagnifierCallback, mSysUiState);
@@ -144,6 +143,7 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
public void tearDown() {
mInstrumentation.runOnMainSync(
() -> mWindowMagnificationController.deleteWindowMagnification());
mValueAnimator.cancel();
}
@Test
@@ -288,12 +288,6 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
@Test
public void setScale_enabled_expectedValueAndUpdateStateDescription() {
doAnswer(invocation -> {
final Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(mHandler).postDelayed(any(Runnable.class), anyLong());
mInstrumentation.runOnMainSync(
() -> mWindowMagnificationController.enableWindowMagnificationInternal(2.0f,
Float.NaN, Float.NaN));