1 /* 2 * Copyright (C) 2008 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.internal.policy.impl.keyguard_obsolete; 18 19 import android.content.Context; 20 21 import com.android.internal.policy.impl.keyguard_obsolete.KeyguardScreen; 22 import com.android.internal.policy.impl.keyguard_obsolete.KeyguardUpdateMonitor; 23 import com.android.internal.policy.impl.keyguard_obsolete.KeyguardViewCallback; 24 import com.android.internal.policy.impl.keyguard_obsolete.KeyguardWindowController; 25 import com.android.internal.policy.impl.keyguard_obsolete.LockPatternKeyguardView; 26 import com.android.internal.telephony.IccCardConstants; 27 import android.content.res.Configuration; 28 import android.test.AndroidTestCase; 29 import android.view.View; 30 import android.view.KeyEvent; 31 import com.android.internal.widget.LockPatternUtils; 32 import com.google.android.collect.Lists; 33 34 import java.util.List; 35 36 /** 37 * Tests for {@link com.android.internal.policy.impl.LockPatternKeyguardView}, 38 * which handles the management of screens while the keyguard is showing. 39 */ 40 public class LockPatternKeyguardViewTest extends AndroidTestCase { 41 private MockUpdateMonitor mUpdateMonitor; 42 private LockPatternUtils mLockPatternUtils; 43 private TestableLockPatternKeyguardView mLPKV; 44 private MockKeyguardCallback mKeyguardViewCallback; 45 46 private static class MockUpdateMonitor extends KeyguardUpdateMonitor { 47 48 public IccCardConstants.State simState = IccCardConstants.State.READY; 49 MockUpdateMonitor(Context context)50 private MockUpdateMonitor(Context context) { 51 super(context); 52 } 53 54 @Override getSimState()55 public IccCardConstants.State getSimState() { 56 return simState; 57 } 58 } 59 60 private static class MockLockPatternUtils extends LockPatternUtils { 61 boolean isLockPatternEnabled = true; 62 public boolean isPermanentlyLocked = false; 63 MockLockPatternUtils(Context context)64 public MockLockPatternUtils(Context context) { 65 super(context); 66 } 67 68 @Override isLockPatternEnabled()69 public boolean isLockPatternEnabled() { 70 return isLockPatternEnabled; 71 } 72 73 @Override setLockPatternEnabled(boolean lockPatternEnabled)74 public void setLockPatternEnabled(boolean lockPatternEnabled) { 75 isLockPatternEnabled = lockPatternEnabled; 76 } 77 78 @Override isPermanentlyLocked()79 public boolean isPermanentlyLocked() { 80 return isPermanentlyLocked; 81 } 82 setPermanentlyLocked(boolean permanentlyLocked)83 public void setPermanentlyLocked(boolean permanentlyLocked) { 84 isPermanentlyLocked = permanentlyLocked; 85 } 86 } 87 88 private static class MockKeyguardScreen extends View implements KeyguardScreen { 89 90 private int mOnPauseCount = 0; 91 private int mOnResumeCount = 0; 92 private int mCleanupCount = 0; 93 MockKeyguardScreen(Context context)94 private MockKeyguardScreen(Context context) { 95 super(context); 96 setFocusable(true); 97 } 98 99 /** {@inheritDoc} */ needsInput()100 public boolean needsInput() { 101 return false; 102 } 103 104 /** {@inheritDoc} */ onPause()105 public void onPause() { 106 mOnPauseCount++; 107 } 108 109 /** {@inheritDoc} */ onResume()110 public void onResume() { 111 mOnResumeCount++; 112 } 113 114 /** {@inheritDoc} */ cleanUp()115 public void cleanUp() { 116 mCleanupCount++; 117 } 118 getOnPauseCount()119 public int getOnPauseCount() { 120 return mOnPauseCount; 121 } 122 getOnResumeCount()123 public int getOnResumeCount() { 124 return mOnResumeCount; 125 } 126 getCleanupCount()127 public int getCleanupCount() { 128 return mCleanupCount; 129 } 130 } 131 132 /** 133 * Allows us to inject the lock and unlock views to simulate their behavior 134 * and detect their creation. 135 */ 136 private static class TestableLockPatternKeyguardView extends LockPatternKeyguardView { 137 private List<MockKeyguardScreen> mInjectedLockScreens; 138 private List<MockKeyguardScreen> mInjectedUnlockScreens; 139 140 141 TestableLockPatternKeyguardView(Context context, KeyguardViewCallback callback, KeyguardUpdateMonitor updateMonitor, LockPatternUtils lockPatternUtils, KeyguardWindowController controller)142 private TestableLockPatternKeyguardView(Context context, KeyguardViewCallback callback, 143 KeyguardUpdateMonitor updateMonitor, 144 LockPatternUtils lockPatternUtils, KeyguardWindowController controller) { 145 super(context, callback, updateMonitor, lockPatternUtils, controller); 146 } 147 148 @Override createLockScreen()149 View createLockScreen() { 150 final MockKeyguardScreen newView = new MockKeyguardScreen(getContext()); 151 if (mInjectedLockScreens == null) mInjectedLockScreens = Lists.newArrayList(); 152 mInjectedLockScreens.add(newView); 153 return newView; 154 } 155 156 @Override createUnlockScreenFor(UnlockMode unlockMode)157 View createUnlockScreenFor(UnlockMode unlockMode) { 158 final MockKeyguardScreen newView = new MockKeyguardScreen(getContext()); 159 if (mInjectedUnlockScreens == null) mInjectedUnlockScreens = Lists.newArrayList(); 160 mInjectedUnlockScreens.add(newView); 161 return newView; 162 } 163 getInjectedLockScreens()164 public List<MockKeyguardScreen> getInjectedLockScreens() { 165 return mInjectedLockScreens; 166 } 167 getInjectedUnlockScreens()168 public List<MockKeyguardScreen> getInjectedUnlockScreens() { 169 return mInjectedUnlockScreens; 170 } 171 } 172 173 private static class MockKeyguardCallback implements KeyguardViewCallback { 174 175 private int mPokeWakelockCount = 0; 176 private int mKeyguardDoneCount = 0; 177 pokeWakelock()178 public void pokeWakelock() { 179 mPokeWakelockCount++; 180 } 181 pokeWakelock(int millis)182 public void pokeWakelock(int millis) { 183 mPokeWakelockCount++; 184 } 185 keyguardDone(boolean authenticated)186 public void keyguardDone(boolean authenticated) { 187 mKeyguardDoneCount++; 188 } 189 keyguardDoneDrawing()190 public void keyguardDoneDrawing() { 191 192 } 193 getPokeWakelockCount()194 public int getPokeWakelockCount() { 195 return mPokeWakelockCount; 196 } 197 getKeyguardDoneCount()198 public int getKeyguardDoneCount() { 199 return mKeyguardDoneCount; 200 } 201 } 202 203 @Override setUp()204 protected void setUp() throws Exception { 205 super.setUp(); 206 mUpdateMonitor = new MockUpdateMonitor(getContext()); 207 mLockPatternUtils = new MockLockPatternUtils(getContext()); 208 mKeyguardViewCallback = new MockKeyguardCallback(); 209 210 mLPKV = new TestableLockPatternKeyguardView(getContext(), mKeyguardViewCallback, 211 mUpdateMonitor, mLockPatternUtils, new KeyguardWindowController() { 212 public void setNeedsInput(boolean needsInput) { 213 } 214 }); 215 } 216 testStateAfterCreatedWhileScreenOff()217 public void testStateAfterCreatedWhileScreenOff() { 218 219 assertEquals(1, mLPKV.getInjectedLockScreens().size()); 220 assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); 221 222 MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); 223 MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); 224 225 assertEquals(0, lockScreen.getOnPauseCount()); 226 assertEquals(0, lockScreen.getOnResumeCount()); 227 assertEquals(0, lockScreen.getCleanupCount()); 228 229 assertEquals(0, unlockScreen.getOnPauseCount()); 230 assertEquals(0, unlockScreen.getOnResumeCount()); 231 assertEquals(0, unlockScreen.getCleanupCount()); 232 233 assertEquals(0, mKeyguardViewCallback.getPokeWakelockCount()); 234 assertEquals(0, mKeyguardViewCallback.getKeyguardDoneCount()); 235 } 236 testWokenByNonMenuKey()237 public void testWokenByNonMenuKey() { 238 mLPKV.wakeWhenReadyTq(0); 239 240 // should have poked the wakelock to turn on the screen 241 assertEquals(1, mKeyguardViewCallback.getPokeWakelockCount()); 242 243 // shouldn't be any additional views created 244 assertEquals(1, mLPKV.getInjectedLockScreens().size()); 245 assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); 246 MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); 247 MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); 248 249 // lock screen should be only visible one 250 assertEquals(View.VISIBLE, lockScreen.getVisibility()); 251 assertEquals(View.GONE, unlockScreen.getVisibility()); 252 253 // on resume not called until screen turns on 254 assertEquals(0, lockScreen.getOnPauseCount()); 255 assertEquals(0, lockScreen.getOnResumeCount()); 256 assertEquals(0, lockScreen.getCleanupCount()); 257 258 assertEquals(0, unlockScreen.getOnPauseCount()); 259 assertEquals(0, unlockScreen.getOnResumeCount()); 260 assertEquals(0, unlockScreen.getCleanupCount()); 261 262 // simulate screen turning on 263 mLPKV.onScreenTurnedOn(); 264 265 assertEquals(0, lockScreen.getOnPauseCount()); 266 assertEquals(1, lockScreen.getOnResumeCount()); 267 assertEquals(0, lockScreen.getCleanupCount()); 268 269 assertEquals(0, unlockScreen.getOnPauseCount()); 270 assertEquals(0, unlockScreen.getOnResumeCount()); 271 assertEquals(0, unlockScreen.getCleanupCount()); 272 } 273 testWokenByMenuKeyWhenPatternSet()274 public void testWokenByMenuKeyWhenPatternSet() { 275 assertEquals(true, mLockPatternUtils.isLockPatternEnabled()); 276 277 mLPKV.wakeWhenReadyTq(KeyEvent.KEYCODE_MENU); 278 279 // should have poked the wakelock to turn on the screen 280 assertEquals(1, mKeyguardViewCallback.getPokeWakelockCount()); 281 282 // shouldn't be any additional views created 283 assertEquals(1, mLPKV.getInjectedLockScreens().size()); 284 assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); 285 MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); 286 MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); 287 288 // unlock screen should be only visible one 289 assertEquals(View.GONE, lockScreen.getVisibility()); 290 assertEquals(View.VISIBLE, unlockScreen.getVisibility()); 291 } 292 testScreenRequestsRecreation()293 public void testScreenRequestsRecreation() { 294 mLPKV.wakeWhenReadyTq(0); 295 mLPKV.onScreenTurnedOn(); 296 297 assertEquals(1, mLPKV.getInjectedLockScreens().size()); 298 assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); 299 MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); 300 301 assertEquals(0, lockScreen.getOnPauseCount()); 302 assertEquals(1, lockScreen.getOnResumeCount()); 303 304 // simulate screen asking to be recreated 305 mLPKV.mKeyguardScreenCallback.recreateMe(new Configuration()); 306 307 // should have been recreated 308 assertEquals(2, mLPKV.getInjectedLockScreens().size()); 309 assertEquals(2, mLPKV.getInjectedUnlockScreens().size()); 310 311 // both old screens should have been cleaned up 312 assertEquals(1, mLPKV.getInjectedLockScreens().get(0).getCleanupCount()); 313 assertEquals(1, mLPKV.getInjectedUnlockScreens().get(0).getCleanupCount()); 314 315 // old lock screen should have been paused 316 assertEquals(1, mLPKV.getInjectedLockScreens().get(0).getOnPauseCount()); 317 assertEquals(0, mLPKV.getInjectedUnlockScreens().get(0).getOnPauseCount()); 318 319 // new lock screen should have been resumed 320 assertEquals(1, mLPKV.getInjectedLockScreens().get(1).getOnResumeCount()); 321 assertEquals(0, mLPKV.getInjectedUnlockScreens().get(1).getOnResumeCount()); 322 } 323 testMenuDoesntGoToUnlockScreenOnWakeWhenPukLocked()324 public void testMenuDoesntGoToUnlockScreenOnWakeWhenPukLocked() { 325 // PUK locked 326 mUpdateMonitor.simState = IccCardConstants.State.PUK_REQUIRED; 327 328 // wake by menu 329 mLPKV.wakeWhenReadyTq(KeyEvent.KEYCODE_MENU); 330 331 assertEquals(1, mLPKV.getInjectedLockScreens().size()); 332 assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); 333 MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); 334 MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); 335 336 // lock screen should be only visible one 337 assertEquals(View.VISIBLE, lockScreen.getVisibility()); 338 assertEquals(View.GONE, unlockScreen.getVisibility()); 339 } 340 testMenuGoesToLockScreenWhenDeviceNotSecure()341 public void testMenuGoesToLockScreenWhenDeviceNotSecure() { 342 mLockPatternUtils.setLockPatternEnabled(false); 343 344 // wake by menu 345 mLPKV.wakeWhenReadyTq(KeyEvent.KEYCODE_MENU); 346 347 assertEquals(1, mLPKV.getInjectedLockScreens().size()); 348 assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); 349 MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); 350 MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); 351 352 // lock screen should be only visible one 353 assertEquals(View.VISIBLE, lockScreen.getVisibility()); 354 assertEquals(View.GONE, unlockScreen.getVisibility()); 355 } 356 } 357