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