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.dashboard.suggestions;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.any;
21import static org.mockito.Matchers.anyInt;
22import static org.mockito.Matchers.eq;
23import static org.mockito.Mockito.when;
24
25import android.app.WallpaperManager;
26import android.app.admin.DevicePolicyManager;
27import android.content.ComponentName;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.PackageManager;
31import android.hardware.fingerprint.FingerprintManager;
32
33import com.android.settings.Settings;
34import com.android.settings.TestConfig;
35import com.android.settings.testutils.SettingsRobolectricTestRunner;
36import com.android.settingslib.drawer.Tile;
37
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
43import org.robolectric.annotation.Config;
44import org.robolectric.util.ReflectionHelpers;
45
46
47@RunWith(SettingsRobolectricTestRunner.class)
48@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49public class SuggestionsChecksTest {
50
51    @Mock
52    private Context mContext;
53    @Mock
54    private PackageManager mPackageManager;
55    @Mock
56    private FingerprintManager mFingerprintManager;
57    @Mock
58    private DevicePolicyManager mDevicePolicyManager;
59    @Mock
60    private WallpaperManagerWrapper mWallpaperManager;
61    private SuggestionsChecks mSuggestionsChecks;
62
63    @Before
64    public void setUp() {
65        MockitoAnnotations.initMocks(this);
66
67        when(mContext.getApplicationContext()).thenReturn(mContext);
68        mSuggestionsChecks = new SuggestionsChecks(mContext);
69        when(mContext.getPackageManager()).thenReturn(mPackageManager);
70        when(mContext.getSystemService(eq(Context.DEVICE_POLICY_SERVICE)))
71                .thenReturn(mDevicePolicyManager);
72        when(mDevicePolicyManager.getKeyguardDisabledFeatures(any(), anyInt()))
73                .thenReturn(0);
74        when(mContext.getSystemService(FingerprintManager.class)).thenReturn(mFingerprintManager);
75    }
76
77    @Test
78    public void testFingerprintEnrollmentIntroductionIsCompleteWhenFingerprintAdded() {
79        stubFingerprintSupported(true);
80        when(mFingerprintManager.hasEnrolledFingerprints()).thenReturn(true);
81        when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
82        Tile tile = createFingerprintTile();
83        assertThat(mSuggestionsChecks.isSuggestionComplete(tile)).isTrue();
84    }
85
86    @Test
87    public void testFingerprintEnrollmentIntroductionIsNotCompleteWhenNoFingerprintAdded() {
88        stubFingerprintSupported(true);
89        when(mFingerprintManager.hasEnrolledFingerprints()).thenReturn(false);
90        when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
91        Tile tile = createFingerprintTile();
92        assertThat(mSuggestionsChecks.isSuggestionComplete(tile)).isFalse();
93    }
94
95    @Test
96    public void testFingerprintEnrollmentIntroductionIsCompleteWhenHardwareNotDetected() {
97        stubFingerprintSupported(true);
98        when(mFingerprintManager.hasEnrolledFingerprints()).thenReturn(false);
99        when(mFingerprintManager.isHardwareDetected()).thenReturn(false);
100        Tile tile = createFingerprintTile();
101        assertThat(mSuggestionsChecks.isSuggestionComplete(tile)).isTrue();
102    }
103
104    @Test
105    public void testFingerprintEnrollmentIntroductionIsCompleteWhenFingerprintNotSupported() {
106        stubFingerprintSupported(false);
107        Tile tile = createFingerprintTile();
108        assertThat(mSuggestionsChecks.isSuggestionComplete(tile)).isTrue();
109    }
110
111    @Test
112    public void testFingerprintEnrollmentIntroductionIsCompleteWhenFingerprintDisabled() {
113        stubFingerprintSupported(true);
114        when(mFingerprintManager.hasEnrolledFingerprints()).thenReturn(false);
115        when(mFingerprintManager.isHardwareDetected()).thenReturn(true);
116        when(mDevicePolicyManager.getKeyguardDisabledFeatures(any(), anyInt()))
117                .thenReturn(DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT);
118
119        Tile tile = createFingerprintTile();
120        assertThat(mSuggestionsChecks.isSuggestionComplete(tile)).isTrue();
121    }
122
123    private void stubFingerprintSupported(boolean enabled) {
124        when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT))
125                .thenReturn(enabled);
126    }
127
128    private Tile createFingerprintTile() {
129        final Tile tile = new Tile();
130        tile.intent = new Intent();
131        tile.intent.setComponent(new ComponentName(mContext,
132                Settings.FingerprintEnrollSuggestionActivity.class));
133        return tile;
134    }
135
136    @Test
137    public void hasWallpaperSet_no_shouldReturnFalse() {
138        ReflectionHelpers.setField(mSuggestionsChecks, "mWallpaperManager", mWallpaperManager);
139        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM))
140                .thenReturn(0);
141
142        assertThat(mSuggestionsChecks.hasWallpaperSet())
143                .isFalse();
144    }
145
146    @Test
147    public void hasWallpaperSet_yes_shouldReturnTrue() {
148        ReflectionHelpers.setField(mSuggestionsChecks, "mWallpaperManager", mWallpaperManager);
149        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM))
150                .thenReturn(100);
151
152        assertThat(mSuggestionsChecks.hasWallpaperSet())
153                .isTrue();
154    }
155}
156