1 /*
2  * Copyright (C) 2017 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.security;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 
30 import com.android.internal.widget.LockPatternUtils;
31 import com.android.settings.display.AmbientDisplayAlwaysOnPreferenceController;
32 import com.android.settings.display.AmbientDisplayNotificationsPreferenceController;
33 import com.android.settings.gestures.DoubleTapScreenPreferenceController;
34 import com.android.settings.gestures.PickupGesturePreferenceController;
35 import com.android.settings.testutils.FakeFeatureFactory;
36 import com.android.settings.testutils.XmlTestUtils;
37 import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
38 import com.android.settings.testutils.shadow.ShadowUtils;
39 import com.android.settingslib.core.AbstractPreferenceController;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 
50 import java.util.List;
51 
52 @RunWith(RobolectricTestRunner.class)
53 @Config(shadows = {ShadowUtils.class, ShadowLockPatternUtils.class})
54 public class LockscreenDashboardFragmentTest {
55 
56     @Mock
57     private LockPatternUtils mLockPatternUtils;
58     private FakeFeatureFactory mFeatureFactory;
59     private TestFragment mTestFragment;
60     private Context mContext;
61 
62     @Before
setUp()63     public void setUp() throws Exception {
64         MockitoAnnotations.initMocks(this);
65         mFeatureFactory = FakeFeatureFactory.setupForTest();
66         when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(any(Context.class)))
67                 .thenReturn(mLockPatternUtils);
68         mContext = RuntimeEnvironment.application;
69         mTestFragment = spy(new TestFragment());
70     }
71 
72     @Test
containsNotificationSettingsForPrimaryUserAndWorkProfile()73     public void containsNotificationSettingsForPrimaryUserAndWorkProfile() {
74         List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(RuntimeEnvironment.application,
75                 mTestFragment.getPreferenceScreenResId());
76 
77         assertThat(keys).containsAllOf(LockscreenDashboardFragment.KEY_LOCK_SCREEN_NOTIFICATON,
78                 LockscreenDashboardFragment.KEY_LOCK_SCREEN_NOTIFICATON_WORK_PROFILE,
79                 LockscreenDashboardFragment.KEY_LOCK_SCREEN_NOTIFICATON_WORK_PROFILE_HEADER);
80     }
81 
82     @Test
onAttach_alwaysOn_shouldInvokeSetters()83     public void onAttach_alwaysOn_shouldInvokeSetters() {
84         final AmbientDisplayAlwaysOnPreferenceController controller = spy(
85                 new AmbientDisplayAlwaysOnPreferenceController(mContext, "key"));
86         doReturn(controller).when(mTestFragment).use(
87                 AmbientDisplayAlwaysOnPreferenceController.class);
88 
89         mTestFragment.onAttach(mContext);
90         verify(controller).setConfig(any());
91         verify(controller).setCallback(any());
92     }
93 
94     @Test
onAttach_notifications_shouldInvokeSetters()95     public void onAttach_notifications_shouldInvokeSetters() {
96         final AmbientDisplayNotificationsPreferenceController controller = spy(
97                 new AmbientDisplayNotificationsPreferenceController(mContext, "key"));
98         doReturn(controller).when(mTestFragment).use(
99                 AmbientDisplayNotificationsPreferenceController.class);
100 
101         mTestFragment.onAttach(mContext);
102         verify(controller).setConfig(any());
103     }
104 
105     @Test
onAttach_doubleTap_shouldInvokeSetters()106     public void onAttach_doubleTap_shouldInvokeSetters() {
107         final DoubleTapScreenPreferenceController controller = spy(
108                 new DoubleTapScreenPreferenceController(mContext, "key"));
109         doReturn(controller).when(mTestFragment).use(DoubleTapScreenPreferenceController.class);
110 
111         mTestFragment.onAttach(mContext);
112         verify(controller).setConfig(any());
113     }
114 
115     @Test
onAttach_pickUp_shouldInvokeSetters()116     public void onAttach_pickUp_shouldInvokeSetters() {
117         final PickupGesturePreferenceController controller = spy(
118                 new PickupGesturePreferenceController(mContext, "key"));
119         doReturn(controller).when(mTestFragment).use(PickupGesturePreferenceController.class);
120 
121         mTestFragment.onAttach(mContext);
122         verify(controller).setConfig(any());
123     }
124 
125     @Test
isPageSearchable_notLocked_shouldBeSearchable()126     public void isPageSearchable_notLocked_shouldBeSearchable() {
127         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
128         when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(true);
129 
130         assertThat(LockscreenDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
131                 .getNonIndexableKeys(mContext))
132                 .doesNotContain("security_lockscreen_settings_screen");
133     }
134 
135     public static class TestFragment extends LockscreenDashboardFragment {
136         @Override
use(Class<T> clazz)137         protected <T extends AbstractPreferenceController> T use(Class<T> clazz) {
138             return super.use(clazz);
139         }
140     }
141 }
142