Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2016 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.settingslib.widget;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 import static org.mockito.Matchers.any;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.times;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.arch.lifecycle.LifecycleOwner;
     27 import android.support.v14.preference.PreferenceFragment;
     28 import android.support.v7.preference.PreferenceManager;
     29 import android.support.v7.preference.PreferenceScreen;
     30 
     31 import com.android.settingslib.SettingsLibRobolectricTestRunner;
     32 import com.android.settingslib.core.lifecycle.Lifecycle;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.shadows.ShadowApplication;
     40 
     41 @RunWith(SettingsLibRobolectricTestRunner.class)
     42 public class FooterPreferenceMixinTest {
     43 
     44     @Mock
     45     private PreferenceFragment mFragment;
     46     @Mock
     47     private PreferenceScreen mScreen;
     48 
     49     private LifecycleOwner mLifecycleOwner;
     50     private Lifecycle mLifecycle;
     51     private FooterPreferenceMixin mMixin;
     52 
     53     @Before
     54     public void setUp() {
     55         MockitoAnnotations.initMocks(this);
     56         mLifecycleOwner = () -> mLifecycle;
     57         mLifecycle = new Lifecycle(mLifecycleOwner);
     58         when(mFragment.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
     59         when(mFragment.getPreferenceManager().getContext())
     60                 .thenReturn(ShadowApplication.getInstance().getApplicationContext());
     61         mMixin = new FooterPreferenceMixin(mFragment, mLifecycle);
     62     }
     63 
     64     @Test
     65     public void createFooter_screenNotAvailable_noCrash() {
     66         assertThat(mMixin.createFooterPreference()).isNotNull();
     67     }
     68 
     69     @Test
     70     public void createFooter_screenAvailable_canAttachToScreen() {
     71         when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
     72 
     73         final FooterPreference preference = mMixin.createFooterPreference();
     74 
     75         assertThat(preference).isNotNull();
     76         verify(mScreen).addPreference(preference);
     77     }
     78 
     79     @Test
     80     public void createFooter_screenAvailableDelayed_canAttachToScreen() {
     81         final FooterPreference preference = mMixin.createFooterPreference();
     82 
     83         mLifecycle.setPreferenceScreen(mScreen);
     84 
     85         assertThat(preference).isNotNull();
     86         verify(mScreen).addPreference(preference);
     87     }
     88 
     89     @Test
     90     public void createFooterTwice_screenAvailable_replaceOldFooter() {
     91         when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
     92 
     93         mMixin.createFooterPreference();
     94         mMixin.createFooterPreference();
     95 
     96         verify(mScreen).removePreference(any(FooterPreference.class));
     97         verify(mScreen, times(2)).addPreference(any(FooterPreference.class));
     98     }
     99 
    100 }
    101