1 /*
2  * Copyright (C) 2023 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.settings.devicelock;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.devicelock.DeviceLockManager;
28 import android.os.OutcomeReceiver;
29 
30 import androidx.preference.Preference;
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Captor;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 
42 import java.util.Collections;
43 import java.util.Map;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public final class DeviceLockPreferenceControllerTest {
47 
48     private static final String TEST_PREFERENCE_KEY = "KEY";
49     private static final Map<Integer, String> TEST_KIOSK_APPS = Map.of(0, "test");
50     @Mock
51     private DeviceLockManager mDeviceLockManager;
52     @Captor
53     private ArgumentCaptor<OutcomeReceiver<Map<Integer, String>, Exception>>
54             mOutcomeReceiverArgumentCaptor;
55     private DeviceLockPreferenceController mController;
56     private Context mContext;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         mContext = ApplicationProvider.getApplicationContext();
62         Context context = spy(mContext);
63         when(context.getSystemService(DeviceLockManager.class)).thenReturn(mDeviceLockManager);
64         mController = new DeviceLockPreferenceController(context, TEST_PREFERENCE_KEY);
65     }
66 
67     @Test
testUpdateState_preferenceBecomesInvisibleIfNoKioskAppsPresent()68     public void testUpdateState_preferenceBecomesInvisibleIfNoKioskAppsPresent() {
69         Preference preference = new Preference(mContext, null, 0, 0);
70         preference.setVisible(true);
71 
72         mController.updateState(preference);
73 
74         verify(mDeviceLockManager).getKioskApps(any(), mOutcomeReceiverArgumentCaptor.capture());
75         OutcomeReceiver<Map<Integer, String>, Exception> outcomeReceiver =
76                 mOutcomeReceiverArgumentCaptor.getValue();
77 
78         outcomeReceiver.onResult(Collections.emptyMap());
79         assertThat(preference.isVisible()).isFalse();
80     }
81 
82     @Test
testUpdateState_preferenceBecomesVisibleIfKioskAppsPresent()83     public void testUpdateState_preferenceBecomesVisibleIfKioskAppsPresent() {
84         Preference preference = new Preference(mContext, null, 0, 0);
85         preference.setVisible(false);
86 
87         mController.updateState(preference);
88 
89         verify(mDeviceLockManager).getKioskApps(any(), mOutcomeReceiverArgumentCaptor.capture());
90         OutcomeReceiver<Map<Integer, String>, Exception> outcomeReceiver =
91                 mOutcomeReceiverArgumentCaptor.getValue();
92 
93         outcomeReceiver.onResult(TEST_KIOSK_APPS);
94         assertThat(preference.isVisible()).isTrue();
95     }
96 
97     @Test
testUpdateState_preferenceBecomesInvisibleIfDeviceLockManagerIsNotAvailable()98     public void testUpdateState_preferenceBecomesInvisibleIfDeviceLockManagerIsNotAvailable() {
99         Context context = spy(mContext);
100         when(context.getSystemService(DeviceLockManager.class)).thenReturn(null);
101         mController = new DeviceLockPreferenceController(context, TEST_PREFERENCE_KEY);
102 
103         Preference preference = new Preference(mContext, null, 0, 0);
104         preference.setVisible(true);
105 
106         mController.updateState(preference);
107         assertThat(preference.isVisible()).isFalse();
108     }
109 }
110