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

Commit 40e1fa45 authored by Robin Lee's avatar Robin Lee
Browse files

Work around Mockito mocking all static methods

Change-Id: I24a950f37ef7403d4d9b2c7f8a7a983a26f75ebc
Test: WM Presubmit run manually with 10 iterations
Bug: 304884570
parent cff22570
Loading
Loading
Loading
Loading
+25 −18
Original line number Diff line number Diff line
@@ -195,6 +195,7 @@ public class SystemServicesTestRule implements TestRule {
    private void setUpSystemCore() {
        doReturn(mock(Watchdog.class)).when(Watchdog::getInstance);
        doAnswer(invocation -> {
            if ("addOnPropertiesChangedListener".equals(invocation.getMethod().getName())) {
                // Exclude CONSTRAIN_DISPLAY_APIS because ActivityRecord#sConstrainDisplayApisConfig
                // only registers once and it doesn't reference to outside.
                if (!NAMESPACE_CONSTRAIN_DISPLAY_APIS.equals(invocation.getArgument(0))) {
@@ -202,8 +203,10 @@ public class SystemServicesTestRule implements TestRule {
                }
                // SizeCompatTests uses setNeverConstrainDisplayApisFlag, and ActivityRecordTests
                // uses splash_screen_exception_list. So still execute real registration.
            }
            return invocation.callRealMethod();
        }).when(() -> DeviceConfig.addOnPropertiesChangedListener(anyString(), any(), any()));
        }).when(() -> DeviceConfig.addOnPropertiesChangedListener(
                anyString(), any(), any(DeviceConfig.OnPropertiesChangedListener.class)));

        mContext = getInstrumentation().getTargetContext();
        spyOn(mContext);
@@ -384,6 +387,7 @@ public class SystemServicesTestRule implements TestRule {
    }

    private void tearDown() {
        if (mWmService != null) {
            for (int i = mWmService.mRoot.getChildCount() - 1; i >= 0; i--) {
                final DisplayContent dc = mWmService.mRoot.getChildAt(i);
                // Unregister SettingsObserver.
@@ -394,10 +398,13 @@ public class SystemServicesTestRule implements TestRule {
                    dc.mDisplayRotationCompatPolicy.dispose();
                }
            }
        }

        if (mAtmService != null) {
            // Unregister display listener from root to avoid issues with subsequent tests.
            mContext.getSystemService(DisplayManager.class)
                    .unregisterDisplayListener(mAtmService.mRootWindowContainer);
        }

        for (int i = mDeviceConfigListeners.size() - 1; i >= 0; i--) {
            DeviceConfig.removeOnPropertiesChangedListener(mDeviceConfigListeners.get(i));