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.enterprise; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.support.v7.preference.Preference; 22 23import com.android.settings.R; 24import com.android.settings.core.PreferenceAvailabilityObserver; 25import com.android.settings.testutils.FakeFeatureFactory; 26 27import org.junit.Before; 28import org.junit.Test; 29import org.mockito.Answers; 30import org.mockito.Mock; 31import org.mockito.MockitoAnnotations; 32 33import static com.google.common.truth.Truth.assertThat; 34import static org.mockito.Mockito.verify; 35import static org.mockito.Mockito.when; 36 37/** 38 * Common base for testing subclasses of {@link FailedPasswordWipePreferenceControllerBase}. 39 */ 40public abstract class FailedPasswordWipePreferenceControllerTestBase { 41 42 protected final String mKey; 43 44 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 45 protected Context mContext; 46 protected FakeFeatureFactory mFeatureFactory; 47 @Mock private PreferenceAvailabilityObserver mObserver; 48 49 protected FailedPasswordWipePreferenceControllerBase mController; 50 51 public FailedPasswordWipePreferenceControllerTestBase(String key) { 52 mKey = key; 53 } 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 FakeFeatureFactory.setupForTest(mContext); 59 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 60 } 61 62 @Test 63 public void testGetAvailabilityObserver() { 64 mController.setAvailabilityObserver(mObserver); 65 assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver); 66 } 67 68 public abstract void setMaximumFailedPasswordsBeforeWipe(int maximum); 69 70 @Test 71 public void testUpdateState() { 72 final Preference preference = new Preference(mContext, null, 0, 0); 73 74 setMaximumFailedPasswordsBeforeWipe(10); 75 when(mContext.getResources().getQuantityString( 76 R.plurals.enterprise_privacy_number_failed_password_wipe, 10, 10)) 77 .thenReturn("10 attempts"); 78 mController.updateState(preference); 79 assertThat(preference.getSummary()).isEqualTo("10 attempts"); 80 } 81 82 @Test 83 public void testIsAvailable() { 84 mController.setAvailabilityObserver(mObserver); 85 86 setMaximumFailedPasswordsBeforeWipe(0); 87 assertThat(mController.isAvailable()).isFalse(); 88 verify(mObserver).onPreferenceAvailabilityUpdated(mKey, false); 89 90 setMaximumFailedPasswordsBeforeWipe(10); 91 assertThat(mController.isAvailable()).isTrue(); 92 verify(mObserver).onPreferenceAvailabilityUpdated(mKey, true); 93 } 94 95 @Test 96 public void testHandlePreferenceTreeClick() { 97 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 98 .isFalse(); 99 } 100 101 @Test 102 public void testGetPreferenceKey() { 103 assertThat(mController.getPreferenceKey()).isEqualTo(mKey); 104 } 105} 106