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 Diff line number Diff line
@@ -126,8 +126,8 @@ public class KeyguardDisplayManager {
        final int displayId = display.getDisplayId();
        Presentation presentation = mPresentations.get(displayId);
        if (presentation == null) {
            final Presentation newPresentation =
                    new KeyguardPresentation(mContext, display, mInjectableInflater);
            final Presentation newPresentation = new KeyguardPresentation(mContext, display,
                    mInjectableInflater.injectable(LayoutInflater.from(mContext)));
            newPresentation.setOnDismissListener(dialog -> {
                if (newPresentation.equals(mPresentations.get(displayId))) {
                    mPresentations.remove(displayId);
@@ -243,7 +243,7 @@ public class KeyguardDisplayManager {
    static final class KeyguardPresentation extends Presentation {
        private static final int VIDEO_SAFE_REGION = 80; // Percentage of display width & height
        private static final int MOVE_CLOCK_TIMEOUT = 10000; // 10s
        private final InjectionInflationController mInjectableInflater;
        private final LayoutInflater mInjectableLayoutInflater;
        private View mClock;
        private int mUsableWidth;
        private int mUsableHeight;
@@ -261,9 +261,9 @@ public class KeyguardDisplayManager {
        };

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

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

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

import com.android.systemui.SysuiTestCase;

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

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

    @Test
    public void testShow() throws Exception {
        doAnswer(invocation -> {
+24 −0
Original line number 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.when;

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

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

    @Before
    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(
                SystemUIFactory.getInstance().getRootComponent());
        LayoutInflater layoutInflater = inflationController
                .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 =
                (KeyguardClockSwitch) layoutInflater.inflate(R.layout.keyguard_clock_switch, null);
        mClockContainer = mKeyguardClockSwitch.findViewById(R.id.clock_view);
+60 −5
Original line number Diff line number Diff line
@@ -16,33 +16,88 @@

package com.android.keyguard;

import static org.mockito.Mockito.when;

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

import androidx.test.filters.SmallTest;

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

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

@SmallTest
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
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);
        when(mMockKeyguardClockSwitch.getContext()).thenReturn(mContext);
        when(mMockKeyguardSliceView.getContext()).thenReturn(mContext);
        when(mMockKeyguardStatusView.getContext()).thenReturn(mContext);
        when(mMockKeyguardStatusView.findViewById(R.id.clock)).thenReturn(mMockKeyguardStatusView);
        allowTestableLooperAsMainThread();

        InjectionInflationController inflationController = new InjectionInflationController(
                SystemUIFactory.getInstance().getRootComponent());
        Context context = getContext();
        KeyguardPresentation keyguardPresentation = new KeyguardPresentation(context,
                context.getDisplayNoVerify(), inflationController);
        mLayoutInflater = inflationController.injectable(LayoutInflater.from(mContext));
        mLayoutInflater.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.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 */);
    }
}