Home | History | Annotate | Download | only in settings
      1 package com.android.settings;
      2 
      3 
      4 import android.app.Activity;
      5 import android.app.admin.DevicePolicyManager;
      6 import android.content.ComponentName;
      7 import android.content.Context;
      8 import android.content.Intent;
      9 import android.os.UserHandle;
     10 import android.os.UserManager;
     11 import android.test.AndroidTestCase;
     12 
     13 import com.android.internal.widget.LockPatternUtils;
     14 
     15 import org.mockito.ArgumentCaptor;
     16 
     17 import java.util.List;
     18 
     19 import static org.mockito.Matchers.anyInt;
     20 import static org.mockito.Matchers.eq;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.times;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 public class ChooseLockSettingsHelperTest extends AndroidTestCase {
     27 
     28     private static final String SYSTEM_PROPERTY_DEXMAKER_DEXCACHE = "dexmaker.dexcache";
     29 
     30     @Override
     31     protected void setUp() throws Exception {
     32         super.setUp();
     33         System.setProperty(SYSTEM_PROPERTY_DEXMAKER_DEXCACHE, getContext().getCacheDir().getPath());
     34     }
     35 
     36     @Override
     37     protected void tearDown() throws Exception {
     38         super.tearDown();
     39         System.clearProperty(SYSTEM_PROPERTY_DEXMAKER_DEXCACHE);
     40     }
     41 
     42     public void testlaunchConfirmationActivityWithExternalAndChallenge() {
     43 
     44         final int userId = UserHandle.myUserId();
     45         final int request = 100;
     46         final long challenge = 10000L;
     47         {
     48             // Test external == true
     49             final boolean external = true;
     50 
     51             final Activity mockActivity = getMockActivity();
     52             ChooseLockSettingsHelper helper = getChooseLockSettingsHelper(mockActivity);
     53             helper.launchConfirmationActivityWithExternalAndChallenge(
     54                     request, // request
     55                     "title",
     56                     "header",
     57                     "description",
     58                     external,
     59                     challenge,
     60                     userId
     61             );
     62             final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
     63             verify(mockActivity, times(1)).startActivity(intentCaptor.capture());
     64             Intent capturedIntent = getResultIntent(intentCaptor);
     65 
     66             assertEquals(new ComponentName("com.android.settings",
     67                             ConfirmLockPattern.InternalActivity.class.getName()),
     68                     capturedIntent.getComponent());
     69             assertFalse(capturedIntent.getBooleanExtra(
     70                     ChooseLockSettingsHelper.EXTRA_KEY_RETURN_CREDENTIALS, false));
     71             assertTrue(capturedIntent.getBooleanExtra(
     72                     ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false));
     73             assertEquals(challenge, capturedIntent.getLongExtra(
     74                     ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0L));
     75             assertEquals(external,
     76                     (capturedIntent.getFlags() & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0);
     77             assertEquals(external, capturedIntent.getBooleanExtra(
     78                     ConfirmDeviceCredentialBaseFragment.ALLOW_FP_AUTHENTICATION, false));
     79             assertEquals(external, capturedIntent.getBooleanExtra(
     80                     ConfirmDeviceCredentialBaseFragment.DARK_THEME, false));
     81             assertEquals(external, capturedIntent.getBooleanExtra(
     82                     ConfirmDeviceCredentialBaseFragment.SHOW_CANCEL_BUTTON, false));
     83             assertEquals(external, capturedIntent.getBooleanExtra(
     84                     ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false));
     85         }
     86 
     87         {
     88             // Test external == false
     89             final boolean external = false;
     90 
     91             final Activity mockActivity = getMockActivity();
     92             ChooseLockSettingsHelper helper = getChooseLockSettingsHelper(mockActivity);
     93             helper.launchConfirmationActivityWithExternalAndChallenge(
     94                     request, // request
     95                     "title",
     96                     "header",
     97                     "description",
     98                     external,
     99                     challenge,
    100                     userId
    101             );
    102             final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
    103             verify(mockActivity, times(1)).startActivityForResult(intentCaptor.capture(),
    104                     eq(request));
    105             Intent capturedIntent = getResultIntent(intentCaptor);
    106 
    107 
    108             assertEquals(new ComponentName("com.android.settings",
    109                             ConfirmLockPattern.InternalActivity.class.getName()),
    110                     capturedIntent.getComponent());
    111             assertFalse(capturedIntent.getBooleanExtra(
    112                     ChooseLockSettingsHelper.EXTRA_KEY_RETURN_CREDENTIALS, false));
    113             assertTrue(capturedIntent.getBooleanExtra(
    114                     ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false));
    115             assertEquals(challenge, capturedIntent.getLongExtra(
    116                     ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0L));
    117             assertEquals(external,
    118                     (capturedIntent.getFlags() & Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0);
    119             assertEquals(external, capturedIntent.getBooleanExtra(
    120                     ConfirmDeviceCredentialBaseFragment.ALLOW_FP_AUTHENTICATION, false));
    121             assertEquals(external, capturedIntent.getBooleanExtra(
    122                     ConfirmDeviceCredentialBaseFragment.DARK_THEME, false));
    123             assertEquals(external, capturedIntent.getBooleanExtra(
    124                     ConfirmDeviceCredentialBaseFragment.SHOW_CANCEL_BUTTON, false));
    125             assertEquals(external, capturedIntent.getBooleanExtra(
    126                     ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false));
    127         }
    128     }
    129 
    130 
    131     private ChooseLockSettingsHelper getChooseLockSettingsHelper(Activity mockActivity) {
    132         LockPatternUtils mockLockPatternUtils = mock(LockPatternUtils.class);
    133         when(mockLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
    134                 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
    135 
    136         ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(mockActivity);
    137         helper.mLockPatternUtils = mockLockPatternUtils;
    138         return helper;
    139     }
    140 
    141     private Activity getMockActivity() {
    142         Activity mockActivity = mock(Activity.class);
    143         when(mockActivity.getSystemService(Context.USER_SERVICE))
    144                 .thenReturn(getContext().getSystemService(UserManager.class));
    145         when(mockActivity.getContentResolver()).thenReturn(getContext().getContentResolver());
    146         when(mockActivity.getIntent()).thenReturn(new Intent());
    147         return mockActivity;
    148     }
    149 
    150 
    151 
    152     private static Intent getResultIntent(ArgumentCaptor<Intent> intentCaptor) {
    153         List<Intent> capturedIntents = intentCaptor.getAllValues();
    154         assertEquals(1, capturedIntents.size());
    155         return capturedIntents.get(0);
    156     }
    157 }
    158