Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2da2d037 authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Add support to lock test method of WmTests by rule

Window manager could execute something on several threads, e.g.
display, animation, ui. And they cannot be stubbed completely
because that might reduce the similarity to the real case.

The test method usually run without synchronization but in the
real world those methods inside the test should be locked. That
may lead to some race condition such as damaging the mocking and
IndexOutOfBoundsException.

This CL introduces a new runner and rule to make sure the test
method and @Before/After are protected by the WM global lock.
Currently the rule only applies to the classes which are known
to have problems. If it looks stable, we can apply the rule to
more classes and remove the scattered synchronization in each
test method.

Bug: 140284973
Test: atest ActivityStarterTests
            ActivityRecordTests
            DisplayWindowSettingsTests
            RootActivityContainerTests
            RecentsAnimationTest
            WindowStateTests

Change-Id: Ib9dc4bb8a9c3c3cbf0b329d0b05e4fdbe14b2e3e
parent 4448c1df
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ import com.android.server.wm.ActivityStack.ActivityState;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;

import java.util.concurrent.TimeUnit;
@@ -112,6 +113,7 @@ import java.util.concurrent.TimeUnit;
 */
@MediumTest
@Presubmit
@RunWith(WindowTestRunner.class)
public class ActivityRecordTests extends ActivityTestsBase {
    private ActivityStack mStack;
    private TaskRecord mTask;
@@ -644,7 +646,7 @@ public class ActivityRecordTests extends ActivityTestsBase {
        // The override configuration should be reset and the activity's process will be killed.
        assertFalse(mActivity.inSizeCompatMode());
        verify(mActivity).restartProcessIfVisible();
        mService.mH.runWithScissors(() -> { }, TimeUnit.SECONDS.toMillis(3));
        mLockRule.runWithScissors(mService.mH, () -> { }, TimeUnit.SECONDS.toMillis(3));
        verify(mService.mAmInternal).killProcess(
                eq(mActivity.app.mName), eq(mActivity.app.mUid), anyString());
    }
+2 −0
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ import com.android.server.wm.utils.MockTracker;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests for the {@link ActivityStarter} class.
@@ -92,6 +93,7 @@ import org.junit.Test;
 */
@SmallTest
@Presubmit
@RunWith(WindowTestRunner.class)
public class ActivityStarterTests extends ActivityTestsBase {
    private ActivityStarter mStarter;
    private ActivityStartController mController;
+4 −0
Original line number Diff line number Diff line
@@ -62,6 +62,10 @@ class ActivityTestsBase {
    @Rule
    public final SystemServicesTestRule mSystemServicesTestRule = new SystemServicesTestRule();

    @WindowTestRunner.MethodWrapperRule
    public final WindowManagerGlobalLockRule mLockRule =
            new WindowManagerGlobalLockRule(mSystemServicesTestRule);

    final Context mContext = getInstrumentation().getTargetContext();

    ActivityTaskManagerService mService;
+2 −11
Original line number Diff line number Diff line
@@ -51,14 +51,13 @@ import android.view.Surface;

import androidx.test.filters.SmallTest;

import com.android.dx.mockito.inline.extended.ExtendedMockito;
import com.android.server.LocalServices;
import com.android.server.policy.WindowManagerPolicy;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoSession;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
@@ -78,6 +77,7 @@ import java.nio.charset.StandardCharsets;
 */
@SmallTest
@Presubmit
@RunWith(WindowTestRunner.class)
public class DisplayWindowSettingsTests extends WindowTestsBase {

    private static final File TEST_FOLDER = getInstrumentation().getTargetContext().getCacheDir();
@@ -444,8 +444,6 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {
        mTarget.setUserRotation(mPrimaryDisplay, WindowManagerPolicy.USER_ROTATION_LOCKED,
                Surface.ROTATION_0);

        final MockitoSession mockitoSession = ExtendedMockito.mockitoSession()
                .startMocking();
        final DisplayRotation displayRotation = mock(DisplayRotation.class);
        spyOn(mPrimaryDisplay);
        doReturn(displayRotation).when(mPrimaryDisplay).getDisplayRotation();
@@ -454,15 +452,12 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {

        verify(displayRotation).restoreSettings(anyInt(), anyInt(),
                eq(FIXED_TO_USER_ROTATION_DEFAULT));
        mockitoSession.finishMocking();
    }

    @Test
    public void testSetFixedToUserRotationDisabled() {
        mTarget.setFixedToUserRotation(mPrimaryDisplay, FIXED_TO_USER_ROTATION_DISABLED);

        final MockitoSession mockitoSession = ExtendedMockito.mockitoSession()
                .startMocking();
        final DisplayRotation displayRotation = mock(DisplayRotation.class);
        spyOn(mPrimaryDisplay);
        doReturn(displayRotation).when(mPrimaryDisplay).getDisplayRotation();
@@ -471,15 +466,12 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {

        verify(displayRotation).restoreSettings(anyInt(), anyInt(),
                eq(FIXED_TO_USER_ROTATION_DISABLED));
        mockitoSession.finishMocking();
    }

    @Test
    public void testSetFixedToUserRotationEnabled() {
        mTarget.setFixedToUserRotation(mPrimaryDisplay, FIXED_TO_USER_ROTATION_ENABLED);

        final MockitoSession mockitoSession = ExtendedMockito.mockitoSession()
                .startMocking();
        final DisplayRotation displayRotation = mock(DisplayRotation.class);
        spyOn(mPrimaryDisplay);
        doReturn(displayRotation).when(mPrimaryDisplay).getDisplayRotation();
@@ -488,7 +480,6 @@ public class DisplayWindowSettingsTests extends WindowTestsBase {

        verify(displayRotation).restoreSettings(anyInt(), anyInt(),
                eq(FIXED_TO_USER_ROTATION_ENABLED));
        mockitoSession.finishMocking();
    }

    @Test
+2 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ import com.android.server.wm.RecentsAnimationController.RecentsAnimationCallback

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Build/Install/Run:
@@ -63,6 +64,7 @@ import org.junit.Test;
 */
@MediumTest
@Presubmit
@RunWith(WindowTestRunner.class)
public class RecentsAnimationTest extends ActivityTestsBase {

    private static final int TEST_USER_ID = 100;
Loading