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
17package com.android.settingslib.widget;
18
19import android.support.v14.preference.PreferenceFragment;
20import android.support.v7.preference.PreferenceManager;
21import android.support.v7.preference.PreferenceScreen;
22
23import com.android.settingslib.SettingLibRobolectricTestRunner;
24import com.android.settingslib.TestConfig;
25import com.android.settingslib.core.lifecycle.Lifecycle;
26
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.mockito.Mock;
31import org.mockito.MockitoAnnotations;
32import org.robolectric.annotation.Config;
33import org.robolectric.shadows.ShadowApplication;
34
35import static com.google.common.truth.Truth.assertThat;
36import static org.mockito.Matchers.any;
37import static org.mockito.Mockito.mock;
38import static org.mockito.Mockito.times;
39import static org.mockito.Mockito.verify;
40import static org.mockito.Mockito.when;
41
42@RunWith(SettingLibRobolectricTestRunner.class)
43@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44public 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}
101