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 Original line Diff line number Diff line
@@ -23,7 +23,7 @@ import android.util.Size;
/**
/**
 * Class to handle SharedPreference for window magnification size.
 * Class to handle SharedPreference for window magnification size.
 */
 */
public final class WindowMagnificationSizePrefs {
final class WindowMagnificationSizePrefs {


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


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


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


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


    @Test
    @Test
    public void onScreenChangedToSavedDensity_enabled_restoreSavedMagnifierWindow() {
    public void onScreenSizeAndDensityChanged_enabled_restoreSavedMagnifierWindow() {
        mContext.getResources().getConfiguration().smallestScreenWidthDp =
        int newSmallestScreenWidthDp =
                mContext.getResources().getConfiguration().smallestScreenWidthDp * 2;
                mContext.getResources().getConfiguration().smallestScreenWidthDp * 2;
        int windowFrameSize = mResources.getDimensionPixelSize(
        int windowFrameSize = mResources.getDimensionPixelSize(
                com.android.internal.R.dimen.accessibility_window_magnifier_min_size);
                com.android.internal.R.dimen.accessibility_window_magnifier_min_size);
        mWindowMagnificationController.mWindowMagnificationSizePrefs.saveSizeForCurrentDensity(
        Size preferredWindowSize = new Size(windowFrameSize, windowFrameSize);
                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(() -> {
        mInstrumentation.runOnMainSync(() -> {
            mWindowMagnificationController.enableWindowMagnificationInternal(Float.NaN, Float.NaN,
            mWindowMagnificationController.onConfigurationChanged(ActivityInfo.CONFIG_SCREEN_SIZE);
                    Float.NaN);
        });
        });


        // wait for rect update
        waitForIdleSync();
        WindowManager.LayoutParams params = mWindowManager.getLayoutParamsFromAttachedView();
        WindowManager.LayoutParams params = mWindowManager.getLayoutParamsFromAttachedView();
        assertTrue(params.width == windowFrameSize);
        final int mirrorSurfaceMargin = mResources.getDimensionPixelSize(
        assertTrue(params.height == windowFrameSize);
                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
    @Test
+37 −9
Original line number Original line 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.model.SysUiState;
import com.android.systemui.res.R;
import com.android.systemui.res.R;
import com.android.systemui.settings.FakeDisplayTracker;
import com.android.systemui.settings.FakeDisplayTracker;
import com.android.systemui.util.FakeSharedPreferences;
import com.android.systemui.util.leak.ReferenceTestUtils;
import com.android.systemui.util.leak.ReferenceTestUtils;
import com.android.systemui.util.settings.SecureSettings;
import com.android.systemui.util.settings.SecureSettings;
import com.android.systemui.utils.os.FakeHandler;
import com.android.systemui.utils.os.FakeHandler;
@@ -176,6 +177,7 @@ public class WindowMagnificationControllerWindowlessMagnifierTest extends SysuiT
    // The most recently created SurfaceControlViewHost.
    // The most recently created SurfaceControlViewHost.
    private SurfaceControlViewHost mSurfaceControlViewHost;
    private SurfaceControlViewHost mSurfaceControlViewHost;
    private KosmosJavaAdapter mKosmos;
    private KosmosJavaAdapter mKosmos;
    private FakeSharedPreferences mSharedPreferences;


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


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


    @Test
    @Test
    public void onScreenChangedToSavedDensity_enabled_restoreSavedMagnifierWindow() {
    public void onScreenSizeAndDensityChanged_enabled_restoreSavedMagnifierWindow() {
        mContext.getResources().getConfiguration().smallestScreenWidthDp =
        int newSmallestScreenWidthDp =
                mContext.getResources().getConfiguration().smallestScreenWidthDp * 2;
                mContext.getResources().getConfiguration().smallestScreenWidthDp * 2;
        int windowFrameSize = mResources.getDimensionPixelSize(
        int windowFrameSize = mResources.getDimensionPixelSize(
                com.android.internal.R.dimen.accessibility_window_magnifier_min_size);
                com.android.internal.R.dimen.accessibility_window_magnifier_min_size);
        mWindowMagnificationController.mWindowMagnificationSizePrefs.saveSizeForCurrentDensity(
        Size preferredWindowSize = new Size(windowFrameSize, windowFrameSize);
                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(() -> {
        mInstrumentation.runOnMainSync(() -> {
            mWindowMagnificationController.enableWindowMagnificationInternal(Float.NaN, Float.NaN,
            mWindowMagnificationController.onConfigurationChanged(ActivityInfo.CONFIG_SCREEN_SIZE);
                    Float.NaN);
        });
        });


        // wait for rect update
        waitForIdleSync();
        ViewGroup.LayoutParams params = mSurfaceControlViewHost.getView().getLayoutParams();
        ViewGroup.LayoutParams params = mSurfaceControlViewHost.getView().getLayoutParams();
        assertTrue(params.width == windowFrameSize);
        final int mirrorSurfaceMargin = mResources.getDimensionPixelSize(
        assertTrue(params.height == windowFrameSize);
                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
    @Test
+19 −2
Original line number Original line Diff line number Diff line
@@ -18,13 +18,20 @@ package com.android.systemui.accessibility;


import static com.google.common.truth.Truth.assertThat;
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.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.testing.TestableLooper;
import android.util.Size;
import android.util.Size;


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


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


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


    WindowMagnificationSizePrefs mWindowMagnificationSizePrefs =
    WindowMagnificationSizePrefs mWindowMagnificationSizePrefs;
            new WindowMagnificationSizePrefs(mContext);
    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
    @Test
    public void saveSizeForCurrentDensity_getExpectedSize() {
    public void saveSizeForCurrentDensity_getExpectedSize() {