Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [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 android.support.v14.preference.PreferenceFragment; |
| 20 | import android.support.v7.preference.PreferenceManager; |
| 21 | import android.support.v7.preference.PreferenceScreen; |
| 22 | |
| 23 | import com.android.settingslib.SettingLibRobolectricTestRunner; |
| 24 | import com.android.settingslib.TestConfig; |
| 25 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| 26 | |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.mockito.Mock; |
| 31 | import org.mockito.MockitoAnnotations; |
| 32 | import org.robolectric.annotation.Config; |
| 33 | import org.robolectric.shadows.ShadowApplication; |
| 34 | |
| 35 | import static com.google.common.truth.Truth.assertThat; |
| 36 | import static org.mockito.Matchers.any; |
| 37 | import static org.mockito.Mockito.mock; |
| 38 | import static org.mockito.Mockito.times; |
| 39 | import static org.mockito.Mockito.verify; |
| 40 | import static org.mockito.Mockito.when; |
| 41 | |
| 42 | @RunWith(SettingLibRobolectricTestRunner.class) |
| 43 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 44 | public class FooterPreferenceMixinTest { |
| 45 | |
| 46 | @Mock |
| 47 | private PreferenceFragment mFragment; |
| 48 | @Mock |
| 49 | private PreferenceScreen mScreen; |
| 50 | |
| 51 | private Lifecycle mLifecycle; |
| 52 | private FooterPreferenceMixin mMixin; |
| 53 | |
| 54 | @Before |
| 55 | public void setUp() { |
| 56 | MockitoAnnotations.initMocks(this); |
| 57 | mLifecycle = new Lifecycle(); |
| 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 | } |