PreferenceListCacheTest.java revision 80f8ec612704639f209346f09c208eb3fc8cab56
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.storagemanager.utils;
18
19import android.content.Context;
20import android.support.v7.preference.Preference;
21import android.support.v7.preference.PreferenceScreen;
22import org.junit.Before;
23import org.junit.Test;
24import org.junit.runner.RunWith;
25import org.mockito.Mock;
26import org.mockito.MockitoAnnotations;
27import org.mockito.invocation.InvocationOnMock;
28import org.mockito.stubbing.Answer;
29import org.robolectric.RobolectricTestRunner;
30import org.robolectric.RuntimeEnvironment;
31import org.robolectric.annotation.Config;
32
33import static org.junit.Assert.assertEquals;
34import static org.mockito.Matchers.any;
35import static org.mockito.Matchers.anyInt;
36import static org.mockito.Matchers.eq;
37import static org.mockito.Mockito.never;
38import static org.mockito.Mockito.verify;
39import static org.mockito.Mockito.when;
40
41@RunWith(RobolectricTestRunner.class)
42@Config(manifest="packages/apps/StorageManager/AndroidManifest.xml", sdk=23)
43public class PreferenceListCacheTest {
44    @Mock private PreferenceScreen mGroup;
45    private PreferenceListCache mCache;
46    private Context mContext;
47
48    @Before
49    public void setUp() throws Exception {
50        MockitoAnnotations.initMocks(this);
51        mContext = RuntimeEnvironment.application;
52    }
53
54    @Test
55    public void testEmptyPreferenceGroup() {
56        setupMockPreferenceGroup(new Preference[0]);
57
58        mCache = new PreferenceListCache(mGroup);
59        mCache.removeCachedPrefs();
60
61        verify(mGroup, never()).removePreference(any(Preference.class));
62    }
63
64    @Test
65    public void testCacheAndRestoreAllPreferences() {
66        Preference first = createPreference("first");
67        Preference second = createPreference("second");
68        Preference third = createPreference("third");
69        Preference[] preferences = new Preference[] {first, second, third};
70        setupMockPreferenceGroup(preferences);
71
72        mCache = new PreferenceListCache(mGroup);
73        assertEquals(first, mCache.getCachedPreference("first"));
74        assertEquals(second, mCache.getCachedPreference("second"));
75        assertEquals(third, mCache.getCachedPreference("third"));
76
77        mCache.removeCachedPrefs();
78        verify(mGroup, never()).removePreference(any(Preference.class));
79    }
80
81    @Test
82    public void testRestoreSomePreferences() {
83        Preference first = createPreference("first");
84        Preference second = createPreference("second");
85        Preference third = createPreference("third");
86        Preference[] preferences = new Preference[] {first, second, third};
87        setupMockPreferenceGroup(preferences);
88
89        mCache = new PreferenceListCache(mGroup);
90        assertEquals(first, mCache.getCachedPreference("first"));
91        assertEquals(second, mCache.getCachedPreference("second"));
92
93        mCache.removeCachedPrefs();
94
95        // Because the third preference was left, it should have been removed by the last call.
96        verify(mGroup).removePreference(eq(third));
97    }
98
99    @Test
100    public void testRestoreZeroPreferences() {
101        Preference first = createPreference("first");
102        Preference second = createPreference("second");
103        Preference third = createPreference("third");
104        Preference[] preferences = new Preference[] {first, second, third};
105        setupMockPreferenceGroup(preferences);
106
107        mCache = new PreferenceListCache(mGroup);
108        mCache.removeCachedPrefs();
109
110        // Because none of the cached preferences were used, the call should remove all three
111        // preferences from the group.
112        verify(mGroup).removePreference(eq(first));
113        verify(mGroup).removePreference(eq(second));
114        verify(mGroup).removePreference(eq(third));
115    }
116
117    private Preference createPreference(String key) {
118        Preference newPreference = new Preference(mContext);
119        newPreference.setKey(key);
120        return newPreference;
121    }
122
123    private void setupMockPreferenceGroup(Preference[] preferences) {
124        when(mGroup.getPreferenceCount()).thenReturn(preferences.length);
125        when(mGroup.getPreference(anyInt())).thenAnswer(new Answer<Preference>() {
126            @Override
127            public Preference answer(InvocationOnMock invocation) throws Throwable {
128                int index = (int) invocation.getArguments()[0];
129                return preferences[index];
130            }
131        });
132    }
133}
134