1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.keyguard;
18 
19 import static org.mockito.Mockito.when;
20 
21 import android.content.Context;
22 import android.testing.AndroidTestingRunner;
23 import android.testing.TestableLooper;
24 import android.util.AttributeSet;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 
28 import androidx.test.filters.SmallTest;
29 
30 import com.android.keyguard.KeyguardDisplayManager.KeyguardPresentation;
31 import com.android.systemui.R;
32 import com.android.systemui.SystemUIFactory;
33 import com.android.systemui.SysuiTestCase;
34 import com.android.systemui.util.InjectionInflationController;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 
43 @SmallTest
44 @RunWith(AndroidTestingRunner.class)
45 @TestableLooper.RunWithLooper
46 public class KeyguardPresentationTest extends SysuiTestCase {
47 
48     @Mock
49     KeyguardClockSwitch mMockKeyguardClockSwitch;
50     @Mock
51     KeyguardSliceView mMockKeyguardSliceView;
52     @Mock
53     KeyguardStatusView mMockKeyguardStatusView;
54 
55     LayoutInflater mLayoutInflater;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mDependency.injectMockDependency(KeyguardUpdateMonitor.class);
61         when(mMockKeyguardClockSwitch.getContext()).thenReturn(mContext);
62         when(mMockKeyguardSliceView.getContext()).thenReturn(mContext);
63         when(mMockKeyguardStatusView.getContext()).thenReturn(mContext);
64         when(mMockKeyguardStatusView.findViewById(R.id.clock)).thenReturn(mMockKeyguardStatusView);
65         allowTestableLooperAsMainThread();
66 
67         InjectionInflationController inflationController = new InjectionInflationController(
68                 SystemUIFactory.getInstance().getRootComponent());
69         mLayoutInflater = inflationController.injectable(LayoutInflater.from(mContext));
70         mLayoutInflater.setPrivateFactory(new LayoutInflater.Factory2() {
71 
72             @Override
73             public View onCreateView(View parent, String name, Context context,
74                     AttributeSet attrs) {
75                 return onCreateView(name, context, attrs);
76             }
77 
78             @Override
79             public View onCreateView(String name, Context context, AttributeSet attrs) {
80                 if ("com.android.keyguard.KeyguardStatusView".equals(name)) {
81                     return mMockKeyguardStatusView;
82                 } else if ("com.android.keyguard.KeyguardClockSwitch".equals(name)) {
83                     return mMockKeyguardClockSwitch;
84                 } else if ("com.android.keyguard.KeyguardSliceView".equals(name)) {
85                     return mMockKeyguardStatusView;
86                 }
87                 return null;
88             }
89         });
90     }
91 
92     @After
tearDown()93     public void tearDown() {
94         disallowTestableLooperAsMainThread();
95     }
96 
97     @Test
testInflation_doesntCrash()98     public void testInflation_doesntCrash() {
99         KeyguardPresentation keyguardPresentation = new KeyguardPresentation(mContext,
100                 mContext.getDisplayNoVerify(), mLayoutInflater);
101         keyguardPresentation.onCreate(null /*savedInstanceState */);
102     }
103 }
104