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

Commit ad677072 authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Mock views in Keyguard view tests

KeyguardSliceView was being inflated by other tests. This caused real
controllers being created through Dagger. Replace views with mocks.

Bug: 153667872
Test: atest SystemUITests
Change-Id: I41b28bc886f97fab3fa8a0af63e743b07f4ec667
parent d0139b01
Loading
Loading
Loading
Loading
+6 −8
Original line number Original line Diff line number Diff line
@@ -126,8 +126,8 @@ public class KeyguardDisplayManager {
        final int displayId = display.getDisplayId();
        final int displayId = display.getDisplayId();
        Presentation presentation = mPresentations.get(displayId);
        Presentation presentation = mPresentations.get(displayId);
        if (presentation == null) {
        if (presentation == null) {
            final Presentation newPresentation =
            final Presentation newPresentation = new KeyguardPresentation(mContext, display,
                    new KeyguardPresentation(mContext, display, mInjectableInflater);
                    mInjectableInflater.injectable(LayoutInflater.from(mContext)));
            newPresentation.setOnDismissListener(dialog -> {
            newPresentation.setOnDismissListener(dialog -> {
                if (newPresentation.equals(mPresentations.get(displayId))) {
                if (newPresentation.equals(mPresentations.get(displayId))) {
                    mPresentations.remove(displayId);
                    mPresentations.remove(displayId);
@@ -243,7 +243,7 @@ public class KeyguardDisplayManager {
    static final class KeyguardPresentation extends Presentation {
    static final class KeyguardPresentation extends Presentation {
        private static final int VIDEO_SAFE_REGION = 80; // Percentage of display width & height
        private static final int VIDEO_SAFE_REGION = 80; // Percentage of display width & height
        private static final int MOVE_CLOCK_TIMEOUT = 10000; // 10s
        private static final int MOVE_CLOCK_TIMEOUT = 10000; // 10s
        private final InjectionInflationController mInjectableInflater;
        private final LayoutInflater mInjectableLayoutInflater;
        private View mClock;
        private View mClock;
        private int mUsableWidth;
        private int mUsableWidth;
        private int mUsableHeight;
        private int mUsableHeight;
@@ -261,9 +261,9 @@ public class KeyguardDisplayManager {
        };
        };


        KeyguardPresentation(Context context, Display display,
        KeyguardPresentation(Context context, Display display,
                InjectionInflationController injectionInflater) {
                LayoutInflater injectionLayoutInflater) {
            super(context, display, R.style.Theme_SystemUI_KeyguardPresentation);
            super(context, display, R.style.Theme_SystemUI_KeyguardPresentation);
            mInjectableInflater = injectionInflater;
            mInjectableLayoutInflater = injectionLayoutInflater;
            getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            setCancelable(false);
            setCancelable(false);
        }
        }
@@ -289,9 +289,7 @@ public class KeyguardDisplayManager {
            mMarginLeft = (100 - VIDEO_SAFE_REGION) * p.x / 200;
            mMarginLeft = (100 - VIDEO_SAFE_REGION) * p.x / 200;
            mMarginTop = (100 - VIDEO_SAFE_REGION) * p.y / 200;
            mMarginTop = (100 - VIDEO_SAFE_REGION) * p.y / 200;


            LayoutInflater inflater = mInjectableInflater.injectable(
            setContentView(mInjectableLayoutInflater.inflate(R.layout.keyguard_presentation, null));
                    LayoutInflater.from(getContext()));
            setContentView(inflater.inflate(R.layout.keyguard_presentation, null));


            // Logic to make the lock screen fullscreen
            // Logic to make the lock screen fullscreen
            getWindow().getDecorView().setSystemUiVisibility(
            getWindow().getDecorView().setSystemUiVisibility(
+6 −0
Original line number Original line Diff line number Diff line
@@ -48,6 +48,7 @@ import androidx.test.filters.SmallTest;


import com.android.systemui.SysuiTestCase;
import com.android.systemui.SysuiTestCase;


import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
@@ -99,6 +100,11 @@ public class AdminSecondaryLockScreenControllerTest extends SysuiTestCase {
                mContext, mParent, mUpdateMonitor, mKeyguardCallback, mHandler);
                mContext, mParent, mUpdateMonitor, mKeyguardCallback, mHandler);
    }
    }


    @After
    public void tearDown() {
        ViewUtils.detachView(mParent);
    }

    @Test
    @Test
    public void testShow() throws Exception {
    public void testShow() throws Exception {
        doAnswer(invocation -> {
        doAnswer(invocation -> {
+24 −0
Original line number Original line Diff line number Diff line
@@ -27,12 +27,14 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.content.Context;
import android.graphics.Color;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.graphics.Paint.Style;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper.RunWithLooper;
import android.testing.TestableLooper.RunWithLooper;
import android.text.TextPaint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout;
@@ -68,15 +70,37 @@ public class KeyguardClockSwitchTest extends SysuiTestCase {


    @Mock
    @Mock
    TextClock mClockView;
    TextClock mClockView;
    View mMockKeyguardSliceView;
    @InjectMocks
    @InjectMocks
    KeyguardClockSwitch mKeyguardClockSwitch;
    KeyguardClockSwitch mKeyguardClockSwitch;


    @Before
    @Before
    public void setUp() {
    public void setUp() {
        mMockKeyguardSliceView = mock(KeyguardSliceView.class);
        when(mMockKeyguardSliceView.getContext()).thenReturn(mContext);
        when(mMockKeyguardSliceView.findViewById(R.id.keyguard_status_area))
                .thenReturn(mMockKeyguardSliceView);

        InjectionInflationController inflationController = new InjectionInflationController(
        InjectionInflationController inflationController = new InjectionInflationController(
                SystemUIFactory.getInstance().getRootComponent());
                SystemUIFactory.getInstance().getRootComponent());
        LayoutInflater layoutInflater = inflationController
        LayoutInflater layoutInflater = inflationController
                .injectable(LayoutInflater.from(getContext()));
                .injectable(LayoutInflater.from(getContext()));
        layoutInflater.setPrivateFactory(new LayoutInflater.Factory2() {

            @Override
            public View onCreateView(View parent, String name, Context context,
                    AttributeSet attrs) {
                return onCreateView(name, context, attrs);
            }

            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                if ("com.android.keyguard.KeyguardSliceView".equals(name)) {
                    return mMockKeyguardSliceView;
                }
                return null;
            }
        });
        mKeyguardClockSwitch =
        mKeyguardClockSwitch =
                (KeyguardClockSwitch) layoutInflater.inflate(R.layout.keyguard_clock_switch, null);
                (KeyguardClockSwitch) layoutInflater.inflate(R.layout.keyguard_clock_switch, null);
        mClockContainer = mKeyguardClockSwitch.findViewById(R.id.clock_view);
        mClockContainer = mKeyguardClockSwitch.findViewById(R.id.clock_view);
+60 −5
Original line number Original line Diff line number Diff line
@@ -16,33 +16,88 @@


package com.android.keyguard;
package com.android.keyguard;


import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.Context;
import android.testing.AndroidTestingRunner;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.testing.TestableLooper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;


import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;


import com.android.keyguard.KeyguardDisplayManager.KeyguardPresentation;
import com.android.keyguard.KeyguardDisplayManager.KeyguardPresentation;
import com.android.systemui.R;
import com.android.systemui.SystemUIFactory;
import com.android.systemui.SystemUIFactory;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.util.InjectionInflationController;
import com.android.systemui.util.InjectionInflationController;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;


@SmallTest
@SmallTest
@RunWith(AndroidTestingRunner.class)
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@TestableLooper.RunWithLooper
public class KeyguardPresentationTest extends SysuiTestCase {
public class KeyguardPresentationTest extends SysuiTestCase {
    @Test

    public void testInflation_doesntCrash() {
    @Mock
    KeyguardClockSwitch mMockKeyguardClockSwitch;
    @Mock
    KeyguardSliceView mMockKeyguardSliceView;
    @Mock
    KeyguardStatusView mMockKeyguardStatusView;

    LayoutInflater mLayoutInflater;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mDependency.injectMockDependency(KeyguardUpdateMonitor.class);
        mDependency.injectMockDependency(KeyguardUpdateMonitor.class);
        when(mMockKeyguardClockSwitch.getContext()).thenReturn(mContext);
        when(mMockKeyguardSliceView.getContext()).thenReturn(mContext);
        when(mMockKeyguardStatusView.getContext()).thenReturn(mContext);
        when(mMockKeyguardStatusView.findViewById(R.id.clock)).thenReturn(mMockKeyguardStatusView);
        allowTestableLooperAsMainThread();
        allowTestableLooperAsMainThread();

        InjectionInflationController inflationController = new InjectionInflationController(
        InjectionInflationController inflationController = new InjectionInflationController(
                SystemUIFactory.getInstance().getRootComponent());
                SystemUIFactory.getInstance().getRootComponent());
        Context context = getContext();
        mLayoutInflater = inflationController.injectable(LayoutInflater.from(mContext));
        KeyguardPresentation keyguardPresentation = new KeyguardPresentation(context,
        mLayoutInflater.setPrivateFactory(new LayoutInflater.Factory2() {
                context.getDisplayNoVerify(), inflationController);

            @Override
            public View onCreateView(View parent, String name, Context context,
                    AttributeSet attrs) {
                return onCreateView(name, context, attrs);
            }

            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                if ("com.android.keyguard.KeyguardStatusView".equals(name)) {
                    return mMockKeyguardStatusView;
                } else if ("com.android.keyguard.KeyguardClockSwitch".equals(name)) {
                    return mMockKeyguardClockSwitch;
                } else if ("com.android.keyguard.KeyguardSliceView".equals(name)) {
                    return mMockKeyguardStatusView;
                }
                return null;
            }
        });
    }

    @After
    public void tearDown() {
        disallowTestableLooperAsMainThread();
    }

    @Test
    public void testInflation_doesntCrash() {
        KeyguardPresentation keyguardPresentation = new KeyguardPresentation(mContext,
                mContext.getDisplayNoVerify(), mLayoutInflater);
        keyguardPresentation.onCreate(null /*savedInstanceState */);
        keyguardPresentation.onCreate(null /*savedInstanceState */);
    }
    }
}
}