aboutsummaryrefslogtreecommitdiff
path: root/tests/carservice_unit_test/src/com/android/car/user/CarUserNoticeServiceTest.java
blob: 412e2b187f71310e5f8877b575f7159ca1ee7282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car.user;

import static android.car.test.mocks.AndroidMockitoHelper.mockContextGetService;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;

import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.car.AbstractExtendedMockitoCarServiceTestCase;
import android.car.builtin.app.KeyguardManagerHelper;
import android.car.hardware.power.CarPowerManager;
import android.car.hardware.power.CarPowerManager.CarPowerStateListener;
import android.car.settings.CarSettings;
import android.car.user.CarUserManager;
import android.car.user.CarUserManager.UserLifecycleEvent;
import android.car.user.CarUserManager.UserLifecycleListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;

import androidx.test.platform.app.InstrumentationRegistry;

import com.android.car.CarLocalServices;
import com.android.car.R;
import com.android.car.power.CarPowerManagementService;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;

public class CarUserNoticeServiceTest extends AbstractExtendedMockitoCarServiceTestCase {

    private static final long TIMEOUT_MS = 15_000;
    private static final int USER_ID = 101;

    @Mock
    private Context mMockContext;
    @Mock
    private Context mOtherMockContext;
    @Mock
    private Resources mMockedResources;
    @Mock
    private CarPowerManagementService mMockCarPowerManagementService;
    @Mock
    private CarUserService mMockCarUserService;
    @Mock
    private PowerManager mMockPowerManager;
    @Mock
    private AppOpsManager mMockAppOpsManager;
    @Mock
    private PackageManager mMockPackageManager;
    @Mock
    private CarPowerManager mCarPowerManager;
    @Mock
    private KeyguardManagerHelper mMockKeyguardManagerHelper;

    @Captor
    private ArgumentCaptor<BroadcastReceiver> mDisplayBroadcastReceiver;

    @Captor
    private ArgumentCaptor<UserLifecycleListener> mUserLifecycleListenerArgumentCaptor;

    @Captor
    private ArgumentCaptor<CarPowerStateListener> mPowerStateListener;

    private CarUserNoticeService mCarUserNoticeService;

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private boolean mIsKeyguardLocked = false;

    @Override
    protected void onSessionBuilder(CustomMockitoSessionBuilder session) {
        session
            .spyStatic(CarLocalServices.class)
            .spyStatic(ActivityManager.class);
    }

    /**
     * Initialize all of the objects with the @Mock annotation.
     */
    @Before
    public void setUpMocks() throws Exception {
        doReturn(mCarPowerManager).when(() -> CarLocalServices.createCarPowerManager(mMockContext));
        mockGetCurrentUser(USER_ID);
        mockGetCarLocalService(CarPowerManagementService.class, mMockCarPowerManagementService);
        mockGetCarLocalService(CarUserService.class, mMockCarUserService);

        putSettingsInt(CarSettings.Secure.KEY_ENABLE_INITIAL_NOTICE_SCREEN_TO_USER, 1);

        when(mMockContext.getResources()).thenReturn(mMockedResources);
        when(mMockContext.getContentResolver())
                .thenReturn(InstrumentationRegistry.getInstrumentation().getTargetContext()
                        .getContentResolver());
        when(mMockedResources.getString(anyInt())).thenReturn("com.foo/.Blah");

        mockContextGetService(mMockContext, PowerManager.class, mMockPowerManager);
        mockContextGetService(mMockContext, AppOpsManager.class, mMockAppOpsManager);
        mockContextGetService(mMockContext, PackageManager.class, mMockPackageManager);
        when(mMockPackageManager.getPackageUidAsUser(any(), anyInt())).thenReturn(1);
        mCarUserNoticeService = new CarUserNoticeService(mMockContext, mHandler,
                mMockKeyguardManagerHelper);
        mCarUserNoticeService.init();
        verify(mMockCarUserService).addUserLifecycleListener(
                mUserLifecycleListenerArgumentCaptor.capture());
        verify(mMockContext).registerReceiver(mDisplayBroadcastReceiver.capture(),
                any(IntentFilter.class));
        verify(mCarPowerManager).setListener(mPowerStateListener.capture());
        when(mMockContext.bindServiceAsUser(any(), any(), anyInt(), any())).thenReturn(true);
        doAnswer(invocation -> mIsKeyguardLocked)
                .when(mMockKeyguardManagerHelper).isKeyguardLocked();
    }

    @Test
    public void featureDisabledTest() {
        // Feature is disabled when the string is empty
        when(mMockedResources.getString(R.string.config_userNoticeUiService)).thenReturn("");
        when(mOtherMockContext.getResources()).thenReturn(mMockedResources);

        CarUserNoticeService otherService = new CarUserNoticeService(mOtherMockContext);
        otherService.init();

        verify(mOtherMockContext, never()).registerReceiver(any(), any());
    }

    @Test
    public void uiHiddenWhenBroadcastOffReceived() throws Exception {
        setUser();
        // UI shown when user switched
        assertUiShownOnce();
        sendBroadcastActionOff();
        // UI hidden when broadcast off
        assertUiHidden();
        // UI shown only once, when user switched
        assertUiShownOnce();
    }

    @Test
    public void uiShownWhenBroadcastOnReceived() throws Exception {
        setUser();
        // UI shown when user switched
        assertUiShownOnce();
        sendBroadcastActionOff();
        // UI hidden when broadcast off
        assertUiHidden();
        sendBroadcastActionOn();
        // UI shown again on broadcast on
        assertUiShownTwice();
    }

    @Test
    public void uiHiddenWhenPowerShutDown() throws Exception {
        setUser();
        // UI shown when user switched
        assertUiShownOnce();
        sendPowerShutDown();
        // UI hidden when power shutdown
        assertUiHidden();
        // UI shown only once, when user switched
        assertUiShownOnce();
    }

    @Test
    public void uiShownWhenPowerOn() throws Exception {
        setUser();
        // UI shown when user switched
        assertUiShownOnce();
        sendPowerShutDown();
        // UI hidden when power shutdown
        assertUiHidden();
        sendPowerOn();
        // UI shown again on power on
        assertUiShownTwice();
    }

    @Test
    public void uiNotShownWhenPowerOnWithKeyguardLocked() throws Exception {
        setUser();
        // UI shown when user switched
        assertUiShownOnce();
        sendPowerShutDown();
        // UI hidden when power shutdown
        assertUiHidden();
        mIsKeyguardLocked = true;
        sendPowerOn();
        // UI shown only once, when user switched
        assertUiShownOnce();
    }

    @Test
    public void uiNotShownIfSettingsDisabled() throws Exception {
        setUser();
        // UI shown when user switched
        assertUiShownOnce();
        sendBroadcastActionOff();
        // UI hidden when broadcast off
        assertUiHidden();
        putSettingsInt(CarSettings.Secure.KEY_ENABLE_INITIAL_NOTICE_SCREEN_TO_USER, 0);
        sendBroadcastActionOn();
        // UI shown only once, when user switched
        assertUiShownOnce();
    }

    private void setDisplayOn() {
        when(mMockPowerManager.isInteractive()).thenReturn(true);
    }

    private void setDisplayOff() {
        when(mMockPowerManager.isInteractive()).thenReturn(false);
    }

    private void sendBroadcast(String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        mDisplayBroadcastReceiver.getValue().onReceive(mMockContext, intent);
    }

    private void sendPowerStateChange(int state) {
        mPowerStateListener.getValue().onStateChanged(state);
    }

    private void sendUserSwitchEvent() throws Exception {
        mUserLifecycleListenerArgumentCaptor.getValue().onEvent(new UserLifecycleEvent(
                CarUserManager.USER_LIFECYCLE_EVENT_TYPE_SWITCHING, USER_ID));
    }

    private void setUser() throws Exception {
        setDisplayOn();
        sendUserSwitchEvent();
        waitForHandlerThreadToFinish();
    }

    private void sendBroadcastActionOff() {
        setDisplayOff();
        sendBroadcast(Intent.ACTION_SCREEN_OFF);
    }

    private void sendBroadcastActionOn() {
        setDisplayOn();
        sendBroadcast(Intent.ACTION_SCREEN_ON);
    }

    private void sendPowerShutDown() {
        setDisplayOff();
        sendPowerStateChange(CarPowerManager.CarPowerStateListener.SHUTDOWN_PREPARE);
        waitForHandlerThreadToFinish();
    }

    private void sendPowerOn() {
        setDisplayOn();
        sendPowerStateChange(CarPowerManager.CarPowerStateListener.ON);
        waitForHandlerThreadToFinish();
    }

    private void assertUiShownOnce() {
        verify(mMockContext).bindServiceAsUser(any(), any(), anyInt(), any());
    }

    private void assertUiShownTwice() {
        verify(mMockContext, times(2)).bindServiceAsUser(any(), any(), anyInt(), any());
    }

    private void assertUiHidden() {
        verify(mMockContext).unbindService(any());
    }

    private void waitForHandlerThreadToFinish() {
        assertWithMessage("handler not idle in %sms", TIMEOUT_MS)
                .that(mHandler.runWithScissors(() -> {}, TIMEOUT_MS)).isTrue();
    }
}