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