blob: 1abbaba441eeab9f976bf0d0915fd2cf4d58b51d [file] [log] [blame]
tmfang464da582018-07-04 15:59:21 +08001/*
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
17package com.android.settingslib.widget;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.any;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import com.android.settingslib.SettingsLibRobolectricTestRunner;
27import com.android.settingslib.core.lifecycle.Lifecycle;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.mockito.Mock;
33import org.mockito.MockitoAnnotations;
34import org.robolectric.shadows.ShadowApplication;
35
36import androidx.lifecycle.LifecycleOwner;
37import androidx.preference.PreferenceFragmentCompat;
38import androidx.preference.PreferenceManager;
39import androidx.preference.PreferenceScreen;
40
41@RunWith(SettingsLibRobolectricTestRunner.class)
42public 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}