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