diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardDisplayManager.java b/packages/SystemUI/src/com/android/keyguard/KeyguardDisplayManager.java index e3d9a267a149d..d6fabd63420d6 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardDisplayManager.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardDisplayManager.java @@ -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( diff --git a/packages/SystemUI/tests/src/com/android/keyguard/AdminSecondaryLockScreenControllerTest.java b/packages/SystemUI/tests/src/com/android/keyguard/AdminSecondaryLockScreenControllerTest.java index ce032c94deab6..9be2d124026ca 100644 --- a/packages/SystemUI/tests/src/com/android/keyguard/AdminSecondaryLockScreenControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/keyguard/AdminSecondaryLockScreenControllerTest.java @@ -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 -> { diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchTest.java index de7664c769e69..97714c194c569 100644 --- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchTest.java +++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardClockSwitchTest.java @@ -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); diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPresentationTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPresentationTest.java index 7231b8a143d0c..e6c2ddcf7e656 100644 --- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPresentationTest.java +++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardPresentationTest.java @@ -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 */); } }