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.ArgumentMatchers.any;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.Context;
23 import android.hardware.display.DisplayManager;
24 import android.testing.AndroidTestingRunner;
25 import android.testing.TestableLooper;
26 import android.util.AttributeSet;
27 import android.view.Display;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.keyguard.KeyguardDisplayManager.KeyguardPresentation;
34 import com.android.keyguard.dagger.KeyguardStatusViewComponent;
35 import com.android.systemui.R;
36 import com.android.systemui.SysuiTestCase;
37 
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 
45 @SmallTest
46 @RunWith(AndroidTestingRunner.class)
47 @TestableLooper.RunWithLooper
48 public class KeyguardPresentationTest extends SysuiTestCase {
49 
50     @Mock
51     KeyguardClockSwitch mMockKeyguardClockSwitch;
52     @Mock
53     KeyguardSliceView mMockKeyguardSliceView;
54     @Mock
55     KeyguardStatusView mMockKeyguardStatusView;
56     @Mock
57     private KeyguardStatusViewComponent.Factory mKeyguardStatusViewComponentFactory;
58     @Mock
59     private KeyguardStatusViewComponent mKeyguardStatusViewComponent;
60     @Mock
61     private KeyguardClockSwitchController mKeyguardClockSwitchController;
62 
63     LayoutInflater mLayoutInflater;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         when(mMockKeyguardClockSwitch.getContext()).thenReturn(mContext);
69         when(mMockKeyguardSliceView.getContext()).thenReturn(mContext);
70         when(mMockKeyguardStatusView.getContext()).thenReturn(mContext);
71         when(mMockKeyguardStatusView.findViewById(R.id.clock)).thenReturn(mMockKeyguardStatusView);
72         when(mKeyguardStatusViewComponentFactory.build(any(KeyguardStatusView.class)))
73                 .thenReturn(mKeyguardStatusViewComponent);
74         when(mKeyguardStatusViewComponent.getKeyguardClockSwitchController())
75                 .thenReturn(mKeyguardClockSwitchController);
76 
77         allowTestableLooperAsMainThread();
78 
79         mLayoutInflater = LayoutInflater.from(mContext);
80         mLayoutInflater.setPrivateFactory(new LayoutInflater.Factory2() {
81 
82             @Override
83             public View onCreateView(View parent, String name, Context context,
84                     AttributeSet attrs) {
85                 return onCreateView(name, context, attrs);
86             }
87 
88             @Override
89             public View onCreateView(String name, Context context, AttributeSet attrs) {
90                 if ("com.android.keyguard.KeyguardStatusView".equals(name)) {
91                     return mMockKeyguardStatusView;
92                 } else if ("com.android.keyguard.KeyguardClockSwitch".equals(name)) {
93                     return mMockKeyguardClockSwitch;
94                 } else if ("com.android.keyguard.KeyguardSliceView".equals(name)) {
95                     return mMockKeyguardStatusView;
96                 }
97                 return null;
98             }
99         });
100     }
101 
102     @After
tearDown()103     public void tearDown() {
104         disallowTestableLooperAsMainThread();
105     }
106 
107     @Test
testInflation_doesntCrash()108     public void testInflation_doesntCrash() {
109         final Display display = mContext.getSystemService(DisplayManager.class).getDisplay(
110                 Display.DEFAULT_DISPLAY);
111         KeyguardPresentation keyguardPresentation = new KeyguardPresentation(mContext, display,
112                 mKeyguardStatusViewComponentFactory);
113         keyguardPresentation.onCreate(null /*savedInstanceState */);
114     }
115 }
116