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
17package com.android.settings.password;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.robolectric.RuntimeEnvironment.application;
22
23import android.content.Intent;
24import android.os.UserHandle;
25
26import com.android.settings.R;
27import com.android.settings.TestConfig;
28import com.android.settings.password.ChooseLockPattern.ChooseLockPatternFragment;
29import com.android.settings.password.ChooseLockPattern.IntentBuilder;
30import com.android.settings.testutils.SettingsRobolectricTestRunner;
31import com.android.settings.testutils.shadow.SettingsShadowResources;
32import com.android.settings.testutils.shadow.ShadowEventLogWriter;
33import com.android.settings.testutils.shadow.ShadowUtils;
34import com.android.setupwizardlib.GlifLayout;
35
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.robolectric.Robolectric;
39import org.robolectric.Shadows;
40import org.robolectric.annotation.Config;
41import org.robolectric.shadows.ShadowDrawable;
42
43@RunWith(SettingsRobolectricTestRunner.class)
44@Config(
45        manifest = TestConfig.MANIFEST_PATH,
46        sdk = TestConfig.SDK_VERSION,
47        shadows = {
48                SettingsShadowResources.class,
49                SettingsShadowResources.SettingsShadowTheme.class,
50                ShadowEventLogWriter.class,
51                ShadowUtils.class
52        })
53public class ChooseLockPatternTest {
54
55    @Test
56    public void activityCreationTest() {
57        // Basic sanity test for activity created without crashing
58        Robolectric.buildActivity(ChooseLockPattern.class, new IntentBuilder(application).build())
59                .setup().get();
60    }
61
62    @Test
63    public void intentBuilder_setPattern_shouldAddExtras() {
64        Intent intent = new IntentBuilder(application)
65                .setPattern("pattern")
66                .setUserId(123)
67                .build();
68
69        assertThat(intent
70                .getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true))
71                .named("EXTRA_KEY_HAS_CHALLENGE")
72                .isFalse();
73        assertThat(intent
74                .getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD))
75                .named("EXTRA_KEY_PASSWORD")
76                .isEqualTo("pattern");
77        assertThat(intent.getIntExtra(Intent.EXTRA_USER_ID, 0))
78                .named("EXTRA_USER_ID")
79                .isEqualTo(123);
80    }
81
82    @Test
83    public void intentBuilder_setChallenge_shouldAddExtras() {
84        Intent intent = new IntentBuilder(application)
85                .setChallenge(12345L)
86                .setUserId(123)
87                .build();
88
89        assertThat(intent
90                .getBooleanExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false))
91                .named("EXTRA_KEY_HAS_CHALLENGE")
92                .isTrue();
93        assertThat(intent
94                .getLongExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0L))
95                .named("EXTRA_KEY_CHALLENGE")
96                .isEqualTo(12345L);
97        assertThat(intent
98                .getIntExtra(Intent.EXTRA_USER_ID, 0))
99                .named("EXTRA_USER_ID")
100                .isEqualTo(123);
101    }
102
103    @Test
104    public void assertThat_chooseLockIconChanged_WhenFingerprintExtraSet() {
105        ChooseLockPattern activity = createActivity(true);
106        ChooseLockPatternFragment fragment = (ChooseLockPatternFragment)
107                activity.getFragmentManager().findFragmentById(R.id.main_content);
108        ShadowDrawable drawable = Shadows.shadowOf(((GlifLayout) fragment.getView()).getIcon());
109        assertThat(drawable.getCreatedFromResId()).isEqualTo(R.drawable.ic_fingerprint_header);
110    }
111
112    private ChooseLockPattern createActivity(boolean addFingerprintExtra) {
113        return Robolectric.buildActivity(
114                ChooseLockPattern.class,
115                new IntentBuilder(application)
116                        .setUserId(UserHandle.myUserId())
117                        .setForFingerprint(addFingerprintExtra)
118                        .build())
119                .setup().get();
120    }
121}
122