1 /* 2 * Copyright (C) 2018 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.car.settings.security; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.MockitoAnnotations.initMocks; 23 24 import android.view.View; 25 26 import com.android.car.settings.CarSettingsRobolectricTestRunner; 27 import com.android.car.settings.R; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.robolectric.RuntimeEnvironment; 34 35 import java.util.Arrays; 36 37 @RunWith(CarSettingsRobolectricTestRunner.class) 38 public class PinPadViewTest { 39 40 private static int[] sAllKeys = 41 Arrays.copyOf(PinPadView.PIN_PAD_DIGIT_KEYS, PinPadView.NUM_KEYS); 42 static { 43 sAllKeys[PinPadView.PIN_PAD_DIGIT_KEYS.length] = R.id.key_backspace; 44 sAllKeys[PinPadView.PIN_PAD_DIGIT_KEYS.length + 1] = R.id.key_enter; 45 } 46 47 private PinPadView mPinPadView; 48 49 @Mock 50 private PinPadView.PinPadClickListener mClickListener; 51 52 @Before 53 public void initPinPad() { 54 initMocks(this); 55 mPinPadView = new PinPadView(RuntimeEnvironment.application); 56 mPinPadView.setPinPadClickListener(mClickListener); 57 } 58 59 // Verify that when the pin pad is enabled or disabled, all the keys are in the same state. 60 @Test 61 public void testEnableDisablePinPad() { 62 mPinPadView.setEnabled(false); 63 64 for (int id : sAllKeys) { 65 View key = mPinPadView.findViewById(id); 66 assertThat(key.isEnabled()).isFalse(); 67 } 68 69 mPinPadView.setEnabled(true); 70 71 for (int id : sAllKeys) { 72 View key = mPinPadView.findViewById(id); 73 assertThat(key.isEnabled()).isTrue(); 74 } 75 } 76 77 // Verify that the click handler is called when the backspace key is clicked. 78 @Test 79 public void testBackspaceClickHandler() { 80 mPinPadView.findViewById(R.id.key_backspace).performClick(); 81 82 verify(mClickListener).onBackspaceClick(); 83 } 84 85 // Verify that the click handler is called when the enter key is clicked. 86 @Test 87 public void testEnterKeyClickHandler() { 88 mPinPadView.findViewById(R.id.key_enter).performClick(); 89 90 verify(mClickListener).onEnterKeyClick(); 91 } 92 93 // Verify that the click handler is called with the right argument when a digit key is clicked. 94 @Test 95 public void testDigitKeyClickHandler() { 96 for (int i = 0; i < PinPadView.PIN_PAD_DIGIT_KEYS.length; ++i) { 97 mPinPadView.findViewById(PinPadView.PIN_PAD_DIGIT_KEYS[i]).performClick(); 98 verify(mClickListener).onDigitKeyClick(String.valueOf(i)); 99 } 100 } 101 } 102