tmfang | 464da58 | 2018-07-04 15:59:21 +0800 | [diff] [blame] | 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 com.android.settingslib.SettingsLibRobolectricTestRunner; |
| 27 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| 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.mockito.MockitoAnnotations; |
| 34 | import org.robolectric.shadows.ShadowApplication; |
| 35 | |
| 36 | import androidx.lifecycle.LifecycleOwner; |
| 37 | import androidx.preference.PreferenceFragmentCompat; |
| 38 | import androidx.preference.PreferenceManager; |
| 39 | import androidx.preference.PreferenceScreen; |
| 40 | |
| 41 | @RunWith(SettingsLibRobolectricTestRunner.class) |
| 42 | public class FooterPreferenceMixinCompatTest { |
| 43 | |
| 44 | @Mock |
| 45 | private PreferenceFragmentCompat mFragment; |
| 46 | @Mock |
| 47 | private PreferenceScreen mScreen; |
| 48 | |
| 49 | private LifecycleOwner mLifecycleOwner; |
| 50 | private Lifecycle mLifecycle; |
| 51 | private FooterPreferenceMixinCompat 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 FooterPreferenceMixinCompat(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 | } |