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.fingerprint;
18 
19 
20 import static org.mockito.Mockito.doReturn;
21 
22 import android.app.KeyguardManager;
23 import android.content.Context;
24 import android.content.ContextWrapper;
25 import android.content.Intent;
26 import android.test.ActivityUnitTestCase;
27 import android.view.View;
28 import android.widget.Button;
29 
30 import com.android.settings.R;
31 
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 
35 public class SetupFingerprintEnrollIntroductionTest
36         extends ActivityUnitTestCase<SetupFingerprintEnrollIntroduction> {
37 
38     private TestContext mContext;
39 
40     @Mock
41     private KeyguardManager mKeyguardManager;
42 
43     private SetupFingerprintEnrollIntroduction mActivity;
44 
SetupFingerprintEnrollIntroductionTest()45     public SetupFingerprintEnrollIntroductionTest() {
46         super(SetupFingerprintEnrollIntroduction.class);
47     }
48 
49     @Override
setUp()50     protected void setUp() throws Exception {
51         super.setUp();
52         MockitoAnnotations.initMocks(this);
53         mContext = new TestContext(getInstrumentation().getTargetContext());
54         setActivityContext(mContext);
55 
56         getInstrumentation().runOnMainSync(() -> {
57             final Intent intent = new Intent();
58             mActivity = startActivity(intent,
59                     null /* savedInstanceState */, null /* lastNonConfigurationInstance */);
60         });
61     }
62 
testKeyguardNotSecure_shouldShowSkipDialog()63     public void testKeyguardNotSecure_shouldShowSkipDialog() {
64         doReturn(false).when(mKeyguardManager).isKeyguardSecure();
65 
66         getInstrumentation().runOnMainSync(() -> {
67             getInstrumentation().callActivityOnCreate(mActivity, null);
68             getInstrumentation().callActivityOnResume(mActivity);
69 
70             final Button skipButton =
71                     (Button) mActivity.findViewById(R.id.fingerprint_cancel_button);
72             assertEquals(View.VISIBLE, skipButton.getVisibility());
73             skipButton.performClick();
74         });
75 
76         assertFalse(isFinishCalled());
77     }
78 
testKeyguardSecure_shouldNotShowSkipDialog()79     public void testKeyguardSecure_shouldNotShowSkipDialog() {
80         doReturn(true).when(mKeyguardManager).isKeyguardSecure();
81 
82         getInstrumentation().runOnMainSync(() -> {
83             getInstrumentation().callActivityOnCreate(mActivity, null);
84             getInstrumentation().callActivityOnResume(mActivity);
85 
86             final Button skipButton =
87                     (Button) mActivity.findViewById(R.id.fingerprint_cancel_button);
88             assertEquals(View.VISIBLE, skipButton.getVisibility());
89             skipButton.performClick();
90         });
91 
92         assertTrue(isFinishCalled());
93     }
94 
95     public class TestContext extends ContextWrapper {
96 
TestContext(Context base)97         public TestContext(Context base) {
98             super(base);
99         }
100 
101         @Override
getSystemService(String name)102         public Object getSystemService(String name) {
103             if (Context.KEYGUARD_SERVICE.equals(name)) {
104                 return mKeyguardManager;
105             }
106             return super.getSystemService(name);
107         }
108     }
109 }
110