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

Commit af10fe3c authored by Candice's avatar Candice Committed by chihtinglo
Browse files

fix(magnification): Fix the test of saving and restoring window size preference

1. Using FakeSharedPreference in the test to guarantee the functions
   applied to SharedPreferennce are committed properly when we test the
   results.
2. Cleaning up SharedPreference contents to ensure the values stored in
   previous tests won't affect the next tests.
3. Verifying the width and height of view with the magnification frame
   size and margins.

Bug: 331139221
Flag: N/A
Test: Testing following tests several iterations
    atest WindowMagnificationControllerTest
    atest WindowMagnificatinControllerWindowlessMagnifierTest
    atest WindowMagnificationSizePrefsTest
    atest MagnificationTest
Change-Id: I3b287d5a9bf20db083f89a67c8ce5e9b19eec333
parent db7588db
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import android.util.Size;
/**
 * Class to handle SharedPreference for window magnification size.
 */
public final class WindowMagnificationSizePrefs {
final class WindowMagnificationSizePrefs {

    private static final String WINDOW_MAGNIFICATION_PREFERENCES =
            "window_magnification_preferences";
+37 −9
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ import com.android.systemui.kosmos.KosmosJavaAdapter;
import com.android.systemui.model.SysUiState;
import com.android.systemui.res.R;
import com.android.systemui.settings.FakeDisplayTracker;
import com.android.systemui.util.FakeSharedPreferences;
import com.android.systemui.util.leak.ReferenceTestUtils;
import com.android.systemui.util.settings.SecureSettings;
import com.android.systemui.utils.os.FakeHandler;
@@ -169,6 +170,7 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
    private View.OnTouchListener mTouchListener;
    private MotionEventHelper mMotionEventHelper = new MotionEventHelper();
    private KosmosJavaAdapter mKosmos;
    private FakeSharedPreferences mSharedPreferences;

    /**
     *  return whether window magnification is supported for current test context.
@@ -180,6 +182,7 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = spy(mContext);
        mKosmos = new KosmosJavaAdapter(this);
        mContext = Mockito.spy(getContext());
        mHandler = new FakeHandler(TestableLooper.get(this).getLooper());
@@ -219,6 +222,10 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {

        mWindowMagnificationAnimationController = new WindowMagnificationAnimationController(
                mContext, mValueAnimator);
        mSharedPreferences = new FakeSharedPreferences();
        when(mContext.getSharedPreferences(
                eq("window_magnification_preferences"), anyInt()))
                .thenReturn(mSharedPreferences);
        mWindowMagnificationController =
                new WindowMagnificationController(
                        mContext,
@@ -249,7 +256,9 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
    @After
    public void tearDown() {
        mInstrumentation.runOnMainSync(
                () -> mWindowMagnificationController.deleteWindowMagnification());
                () -> {
                    mWindowMagnificationController.deleteWindowMagnification();
                });
        mValueAnimator.cancel();
    }

@@ -600,22 +609,41 @@ public class WindowMagnificationControllerTest extends SysuiTestCase {
    }

    @Test
    public void onScreenChangedToSavedDensity_enabled_restoreSavedMagnifierWindow() {
        mContext.getResources().getConfiguration().smallestScreenWidthDp =
    public void onScreenSizeAndDensityChanged_enabled_restoreSavedMagnifierWindow() {
        int newSmallestScreenWidthDp =
                mContext.getResources().getConfiguration().smallestScreenWidthDp * 2;
        int windowFrameSize = mResources.getDimensionPixelSize(
                com.android.internal.R.dimen.accessibility_window_magnifier_min_size);
        mWindowMagnificationController.mWindowMagnificationSizePrefs.saveSizeForCurrentDensity(
                new Size(windowFrameSize, windowFrameSize));
        Size preferredWindowSize = new Size(windowFrameSize, windowFrameSize);
        mSharedPreferences
                .edit()
                .putString(String.valueOf(newSmallestScreenWidthDp),
                        preferredWindowSize.toString())
                .commit();
        mInstrumentation.runOnMainSync(() -> {
            mWindowMagnificationController
                    .enableWindowMagnificationInternal(Float.NaN, Float.NaN, Float.NaN);
        });

        // Change screen density and size to trigger restoring the preferred window size
        mContext.getResources().getConfiguration().smallestScreenWidthDp = newSmallestScreenWidthDp;
        final Rect testWindowBounds = new Rect(
                mWindowManager.getCurrentWindowMetrics().getBounds());
        testWindowBounds.set(testWindowBounds.left, testWindowBounds.top,
                testWindowBounds.right + 100, testWindowBounds.bottom + 100);
        mWindowManager.setWindowBounds(testWindowBounds);
        mInstrumentation.runOnMainSync(() -> {
            mWindowMagnificationController.enableWindowMagnificationInternal(Float.NaN, Float.NaN,
                    Float.NaN);
            mWindowMagnificationController.onConfigurationChanged(ActivityInfo.CONFIG_SCREEN_SIZE);
        });

        // wait for rect update
        waitForIdleSync();
        WindowManager.LayoutParams params = mWindowManager.getLayoutParamsFromAttachedView();
        assertTrue(params.width == windowFrameSize);
        assertTrue(params.height == windowFrameSize);
        final int mirrorSurfaceMargin = mResources.getDimensionPixelSize(
                R.dimen.magnification_mirror_surface_margin);
        // The width and height of the view include the magnification frame and the margins.
        assertTrue(params.width == (windowFrameSize + 2 * mirrorSurfaceMargin));
        assertTrue(params.height == (windowFrameSize + 2 * mirrorSurfaceMargin));
    }

    @Test
+37 −9
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ import com.android.systemui.kosmos.KosmosJavaAdapter;
import com.android.systemui.model.SysUiState;
import com.android.systemui.res.R;
import com.android.systemui.settings.FakeDisplayTracker;
import com.android.systemui.util.FakeSharedPreferences;
import com.android.systemui.util.leak.ReferenceTestUtils;
import com.android.systemui.util.settings.SecureSettings;
import com.android.systemui.utils.os.FakeHandler;
@@ -176,6 +177,7 @@ public class WindowMagnificationControllerWindowlessMagnifierTest extends SysuiT
    // The most recently created SurfaceControlViewHost.
    private SurfaceControlViewHost mSurfaceControlViewHost;
    private KosmosJavaAdapter mKosmos;
    private FakeSharedPreferences mSharedPreferences;

    /**
     *  return whether window magnification is supported for current test context.
@@ -187,6 +189,7 @@ public class WindowMagnificationControllerWindowlessMagnifierTest extends SysuiT
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = spy(mContext);
        mKosmos = new KosmosJavaAdapter(this);
        mContext = Mockito.spy(getContext());
        mHandler = new FakeHandler(TestableLooper.get(this).getLooper());
@@ -228,6 +231,10 @@ public class WindowMagnificationControllerWindowlessMagnifierTest extends SysuiT
            return mSurfaceControlViewHost;
        };
        mTransaction = spy(new SurfaceControl.Transaction());
        mSharedPreferences = new FakeSharedPreferences();
        when(mContext.getSharedPreferences(
                eq("window_magnification_preferences"), anyInt()))
                .thenReturn(mSharedPreferences);
        mWindowMagnificationController =
                new WindowMagnificationController(
                        mContext,
@@ -258,7 +265,9 @@ public class WindowMagnificationControllerWindowlessMagnifierTest extends SysuiT
    @After
    public void tearDown() {
        mInstrumentation.runOnMainSync(
                () -> mWindowMagnificationController.deleteWindowMagnification());
                () -> {
                    mWindowMagnificationController.deleteWindowMagnification();
                });
        mValueAnimator.cancel();
    }

@@ -614,22 +623,41 @@ public class WindowMagnificationControllerWindowlessMagnifierTest extends SysuiT
    }

    @Test
    public void onScreenChangedToSavedDensity_enabled_restoreSavedMagnifierWindow() {
        mContext.getResources().getConfiguration().smallestScreenWidthDp =
    public void onScreenSizeAndDensityChanged_enabled_restoreSavedMagnifierWindow() {
        int newSmallestScreenWidthDp =
                mContext.getResources().getConfiguration().smallestScreenWidthDp * 2;
        int windowFrameSize = mResources.getDimensionPixelSize(
                com.android.internal.R.dimen.accessibility_window_magnifier_min_size);
        mWindowMagnificationController.mWindowMagnificationSizePrefs.saveSizeForCurrentDensity(
                new Size(windowFrameSize, windowFrameSize));
        Size preferredWindowSize = new Size(windowFrameSize, windowFrameSize);
        mSharedPreferences
                .edit()
                .putString(String.valueOf(newSmallestScreenWidthDp),
                        preferredWindowSize.toString())
                .commit();
        mInstrumentation.runOnMainSync(() -> {
            mWindowMagnificationController
                    .enableWindowMagnificationInternal(Float.NaN, Float.NaN, Float.NaN);
        });

        // Screen density and size change
        mContext.getResources().getConfiguration().smallestScreenWidthDp = newSmallestScreenWidthDp;
        final Rect testWindowBounds = new Rect(
                mWindowManager.getCurrentWindowMetrics().getBounds());
        testWindowBounds.set(testWindowBounds.left, testWindowBounds.top,
                testWindowBounds.right + 100, testWindowBounds.bottom + 100);
        mWindowManager.setWindowBounds(testWindowBounds);
        mInstrumentation.runOnMainSync(() -> {
            mWindowMagnificationController.enableWindowMagnificationInternal(Float.NaN, Float.NaN,
                    Float.NaN);
            mWindowMagnificationController.onConfigurationChanged(ActivityInfo.CONFIG_SCREEN_SIZE);
        });

        // wait for rect update
        waitForIdleSync();
        ViewGroup.LayoutParams params = mSurfaceControlViewHost.getView().getLayoutParams();
        assertTrue(params.width == windowFrameSize);
        assertTrue(params.height == windowFrameSize);
        final int mirrorSurfaceMargin = mResources.getDimensionPixelSize(
                R.dimen.magnification_mirror_surface_margin);
        // The width and height of the view include the magnification frame and the margins.
        assertTrue(params.width == (windowFrameSize + 2 * mirrorSurfaceMargin));
        assertTrue(params.height == (windowFrameSize + 2 * mirrorSurfaceMargin));
    }

    @Test
+19 −2
Original line number Diff line number Diff line
@@ -18,13 +18,20 @@ package com.android.systemui.accessibility;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.Size;

import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.FakeSharedPreferences;

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

@@ -33,8 +40,18 @@ import org.junit.runner.RunWith;
@TestableLooper.RunWithLooper
public class WindowMagnificationSizePrefsTest extends SysuiTestCase {

    WindowMagnificationSizePrefs mWindowMagnificationSizePrefs =
            new WindowMagnificationSizePrefs(mContext);
    WindowMagnificationSizePrefs mWindowMagnificationSizePrefs;
    FakeSharedPreferences mSharedPreferences;

    @Before
    public void setUp() {
        mContext = spy(mContext);
        mSharedPreferences = new FakeSharedPreferences();
        when(mContext.getSharedPreferences(
                eq("window_magnification_preferences"), anyInt()))
                .thenReturn(mSharedPreferences);
        mWindowMagnificationSizePrefs = new WindowMagnificationSizePrefs(mContext);
    }

    @Test
    public void saveSizeForCurrentDensity_getExpectedSize() {