• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.password;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import static org.robolectric.RuntimeEnvironment.application;
23 
24 import android.content.Intent;
25 import android.os.UserHandle;
26 import android.view.View;
27 
28 import com.android.internal.widget.LockPatternUtils;
29 import com.android.internal.widget.LockscreenCredential;
30 import com.android.settings.R;
31 import com.android.settings.password.ChooseLockPattern.ChooseLockPatternFragment;
32 import com.android.settings.password.ChooseLockPattern.IntentBuilder;
33 import com.android.settings.testutils.shadow.ShadowUtils;
34 import com.android.settingslib.testutils.DrawableTestHelper;
35 
36 import com.google.android.setupdesign.GlifLayout;
37 
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.Robolectric;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.annotation.Config;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = ShadowUtils.class)
46 public class ChooseLockPatternTest {
47 
48     @Test
activityCreationTest()49     public void activityCreationTest() {
50         // Basic test for activity created without crashing
51         Robolectric.buildActivity(ChooseLockPattern.class, new IntentBuilder(application).build())
52                 .setup().get();
53     }
54 
55     @Test
intentBuilder_setPattern_shouldAddExtras()56     public void intentBuilder_setPattern_shouldAddExtras() {
57         Intent intent = new IntentBuilder(application)
58                 .setPattern(createPattern("1234"))
59                 .setUserId(123)
60                 .build();
61 
62         assertWithMessage("EXTRA_KEY_FORCE_VERIFY").that(intent
63                 .getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_FORCE_VERIFY, false))
64                 .isFalse();
65         assertWithMessage("EXTRA_KEY_PASSWORD").that((LockscreenCredential) intent
66                 .getParcelableExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD))
67                 .isEqualTo(createPattern("1234"));
68         assertWithMessage("EXTRA_USER_ID").that(intent.getIntExtra(Intent.EXTRA_USER_ID, 0))
69                 .isEqualTo(123);
70     }
71 
72     @Test
intentBuilder_setRequestGatekeeperPassword_shouldAddExtras()73     public void intentBuilder_setRequestGatekeeperPassword_shouldAddExtras() {
74         Intent intent = new IntentBuilder(application)
75                 .setRequestGatekeeperPasswordHandle(true)
76                 .setUserId(123)
77                 .build();
78 
79         assertWithMessage("EXTRA_KEY_REQUEST_GK_PW").that(intent
80                 .getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_REQUEST_GK_PW_HANDLE, false))
81                 .isTrue();
82         assertWithMessage("EXTRA_USER_ID")
83                 .that(intent.getIntExtra(Intent.EXTRA_USER_ID, 0))
84                 .isEqualTo(123);
85     }
86 
87     @Test
intentBuilder_setProfileToUnify_shouldAddExtras()88     public void intentBuilder_setProfileToUnify_shouldAddExtras() {
89         Intent intent = new IntentBuilder(application)
90                 .setProfileToUnify(23, LockscreenCredential.createNone())
91                 .build();
92 
93         assertWithMessage("EXTRA_KEY_UNIFICATION_PROFILE_ID").that(intent
94                 .getIntExtra(ChooseLockSettingsHelper.EXTRA_KEY_UNIFICATION_PROFILE_ID, 0))
95                 .isEqualTo(23);
96         assertWithMessage("EXTRA_KEY_UNIFICATION_PROFILE_CREDENTIAL").that(
97                 (LockscreenCredential) intent.getParcelableExtra(
98                         ChooseLockSettingsHelper.EXTRA_KEY_UNIFICATION_PROFILE_CREDENTIAL))
99                 .isNotNull();
100     }
101 
102     @Config(qualifiers = "sw400dp")
103     @Test
fingerprintExtraSet_shouldDisplayFingerprintIcon()104     public void fingerprintExtraSet_shouldDisplayFingerprintIcon() {
105         ChooseLockPattern activity = createActivity(true);
106         ChooseLockPatternFragment fragment = (ChooseLockPatternFragment)
107                 activity.getSupportFragmentManager().findFragmentById(R.id.main_content);
108         DrawableTestHelper.assertDrawableResId(((GlifLayout) fragment.getView()).getIcon(),
109                 R.drawable.ic_fingerprint_header);
110     }
111 
112     @Config(qualifiers = "sw300dp")
113     @Test
smallScreens_shouldHideIcon()114     public void smallScreens_shouldHideIcon() {
115         ChooseLockPattern activity = createActivity(true);
116         ChooseLockPatternFragment fragment = (ChooseLockPatternFragment)
117                 activity.getSupportFragmentManager().findFragmentById(R.id.main_content);
118 
119         View iconView = fragment.getView().findViewById(R.id.sud_layout_icon);
120         assertThat(iconView.getVisibility()).isEqualTo(View.GONE);
121     }
122 
createActivity(boolean addFingerprintExtra)123     private ChooseLockPattern createActivity(boolean addFingerprintExtra) {
124         return Robolectric.buildActivity(
125                 ChooseLockPattern.class,
126                 new IntentBuilder(application)
127                         .setUserId(UserHandle.myUserId())
128                         .setForFingerprint(addFingerprintExtra)
129                         .build())
130                 .setup().get();
131     }
132 
createPattern(String patternString)133     private LockscreenCredential createPattern(String patternString) {
134         return LockscreenCredential.createPattern(LockPatternUtils.byteArrayToPattern(
135                 patternString.getBytes()));
136     }
137 }
138