1 /*
2  * Copyright (C) 2020 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.systemui.biometrics;
18 
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyNoMoreInteractions;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.hardware.fingerprint.IUdfpsRefreshRateRequestCallback;
27 import android.os.RemoteException;
28 import android.testing.TestableLooper.RunWithLooper;
29 
30 import androidx.test.ext.junit.runners.AndroidJUnit4;
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.systemui.RoboPilotTest;
34 import com.android.systemui.SysuiTestCase;
35 import com.android.systemui.util.concurrency.FakeExecution;
36 
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 @RoboPilotTest
45 @RunWith(AndroidJUnit4.class)
46 @RunWithLooper(setAsMainLooper = true)
47 public class UdfpsDisplayModeTest extends SysuiTestCase {
48     private static final int DISPLAY_ID = 0;
49 
50     @Mock
51     private AuthController mAuthController;
52     @Mock
53     private IUdfpsRefreshRateRequestCallback mDisplayCallback;
54     @Mock
55     private UdfpsLogger mUdfpsLogger;
56     @Mock
57     private Runnable mOnEnabled;
58     @Mock
59     private Runnable mOnDisabled;
60 
61     private final FakeExecution mExecution = new FakeExecution();
62     private UdfpsDisplayMode mHbmController;
63 
64     @Before
setUp()65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67 
68         // Force mContext to always return DISPLAY_ID
69         Context contextSpy = spy(mContext);
70         when(contextSpy.getDisplayId()).thenReturn(DISPLAY_ID);
71 
72         // Set up mocks.
73         when(mAuthController.getUdfpsRefreshRateCallback()).thenReturn(mDisplayCallback);
74 
75         // Create a real controller with mock dependencies.
76         mHbmController = new UdfpsDisplayMode(contextSpy, mExecution, mAuthController,
77                 mUdfpsLogger);
78     }
79 
80     @Test
roundTrip()81     public void roundTrip() throws RemoteException {
82         // Enable the UDFPS mode.
83         mHbmController.enable(mOnEnabled);
84 
85         // Should set the appropriate refresh rate for UDFPS and notify the caller.
86         verify(mDisplayCallback).onRequestEnabled(eq(DISPLAY_ID));
87         verify(mOnEnabled).run();
88 
89         // Disable the UDFPS mode.
90         mHbmController.disable(mOnDisabled);
91 
92         // Should unset the refresh rate and notify the caller.
93         verify(mOnDisabled).run();
94         verify(mDisplayCallback).onRequestDisabled(eq(DISPLAY_ID));
95     }
96 
97     @Test
mustNotEnableMoreThanOnce()98     public void mustNotEnableMoreThanOnce() throws RemoteException {
99         // First request to enable the UDFPS mode.
100         mHbmController.enable(mOnEnabled);
101 
102         // Should set the appropriate refresh rate for UDFPS and notify the caller.
103         verify(mDisplayCallback).onRequestEnabled(eq(DISPLAY_ID));
104         verify(mOnEnabled).run();
105 
106         // Second request to enable the UDFPS mode, while it's still enabled.
107         mHbmController.enable(mOnEnabled);
108 
109         // Should ignore the second request.
110         verifyNoMoreInteractions(mDisplayCallback);
111         verifyNoMoreInteractions(mOnEnabled);
112     }
113 
114     @Test
mustNotDisableMoreThanOnce()115     public void mustNotDisableMoreThanOnce() throws RemoteException {
116         // Disable the UDFPS mode.
117         mHbmController.enable(mOnEnabled);
118 
119         // Should set the appropriate refresh rate for UDFPS and notify the caller.
120         verify(mDisplayCallback).onRequestEnabled(eq(DISPLAY_ID));
121         verify(mOnEnabled).run();
122 
123         // First request to disable the UDFPS mode.
124         mHbmController.disable(mOnDisabled);
125 
126         // Should unset the refresh rate and notify the caller.
127         verify(mOnDisabled).run();
128         verify(mDisplayCallback).onRequestDisabled(eq(DISPLAY_ID));
129 
130         // Second request to disable the UDFPS mode, when it's already disabled.
131         mHbmController.disable(mOnDisabled);
132 
133         // Should ignore the second request.
134         verifyNoMoreInteractions(mOnDisabled);
135         verifyNoMoreInteractions(mDisplayCallback);
136     }
137 }
138