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).containsAtLeast(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     }
92 
93     @Test
onAttach_notifications_shouldInvokeSetters()94     public void onAttach_notifications_shouldInvokeSetters() {
95         final AmbientDisplayNotificationsPreferenceController controller = spy(
96                 new AmbientDisplayNotificationsPreferenceController(mContext, "key"));
97         doReturn(controller).when(mTestFragment).use(
98                 AmbientDisplayNotificationsPreferenceController.class);
99 
100         mTestFragment.onAttach(mContext);
101         verify(controller).setConfig(any());
102     }
103 
104     @Test
onAttach_doubleTap_shouldInvokeSetters()105     public void onAttach_doubleTap_shouldInvokeSetters() {
106         final DoubleTapScreenPreferenceController controller = spy(
107                 new DoubleTapScreenPreferenceController(mContext, "key"));
108         doReturn(controller).when(mTestFragment).use(DoubleTapScreenPreferenceController.class);
109 
110         mTestFragment.onAttach(mContext);
111         verify(controller).setConfig(any());
112     }
113 
114     @Test
onAttach_pickUp_shouldInvokeSetters()115     public void onAttach_pickUp_shouldInvokeSetters() {
116         final PickupGesturePreferenceController controller = spy(
117                 new PickupGesturePreferenceController(mContext, "key"));
118         doReturn(controller).when(mTestFragment).use(PickupGesturePreferenceController.class);
119 
120         mTestFragment.onAttach(mContext);
121         verify(controller).setConfig(any());
122     }
123 
124     @Test
isPageSearchable_notLocked_shouldBeSearchable()125     public void isPageSearchable_notLocked_shouldBeSearchable() {
126         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
127         when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(true);
128 
129         assertThat(LockscreenDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
130                 .getNonIndexableKeys(mContext))
131                 .doesNotContain("security_lockscreen_settings_screen");
132     }
133 
134     public static class TestFragment extends LockscreenDashboardFragment {
135         @Override
use(Class<T> clazz)136         protected <T extends AbstractPreferenceController> T use(Class<T> clazz) {
137             return super.use(clazz);
138         }
139     }
140 }
141