PreferenceListCacheTest.java revision c9b6806c51c0c0f9339fa6771898e58567fccf8e
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 com.android.storagemanager.testing.TestingConstants;
23import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.mockito.Mock;
27import org.mockito.MockitoAnnotations;
28import org.mockito.invocation.InvocationOnMock;
29import org.mockito.stubbing.Answer;
30import org.robolectric.RobolectricTestRunner;
31import org.robolectric.RuntimeEnvironment;
32import org.robolectric.annotation.Config;
33
34import static org.junit.Assert.assertEquals;
35import static org.mockito.Matchers.any;
36import static org.mockito.Matchers.anyInt;
37import static org.mockito.Matchers.eq;
38import static org.mockito.Mockito.never;
39import static org.mockito.Mockito.verify;
40import static org.mockito.Mockito.when;
41
42@RunWith(RobolectricTestRunner.class)
43@Config(manifest= TestingConstants.MANIFEST, sdk=23)
44public class PreferenceListCacheTest {
45    @Mock private PreferenceScreen mGroup;
46    private PreferenceListCache mCache;
47    private Context mContext;
48
49    @Before
50    public void setUp() throws Exception {
51        MockitoAnnotations.initMocks(this);
52        mContext = RuntimeEnvironment.application;
53    }
54
55    @Test
56    public void testEmptyPreferenceGroup() {
57        setupMockPreferenceGroup(new Preference[0]);
58
59        mCache = new PreferenceListCache(mGroup);
60        mCache.removeCachedPrefs();
61
62        verify(mGroup, never()).removePreference(any(Preference.class));
63    }
64
65    @Test
66    public void testCacheAndRestoreAllPreferences() {
67        Preference first = createPreference("first");
68        Preference second = createPreference("second");
69        Preference third = createPreference("third");
70        Preference[] preferences = new Preference[] {first, second, third};
71        setupMockPreferenceGroup(preferences);
72
73        mCache = new PreferenceListCache(mGroup);
74        assertEquals(first, mCache.getCachedPreference("first"));
75        assertEquals(second, mCache.getCachedPreference("second"));
76        assertEquals(third, mCache.getCachedPreference("third"));
77
78        mCache.removeCachedPrefs();
79        verify(mGroup, never()).removePreference(any(Preference.class));
80    }
81
82    @Test
83    public void testRestoreSomePreferences() {
84        Preference first = createPreference("first");
85        Preference second = createPreference("second");
86        Preference third = createPreference("third");
87        Preference[] preferences = new Preference[] {first, second, third};
88        setupMockPreferenceGroup(preferences);
89
90        mCache = new PreferenceListCache(mGroup);
91        assertEquals(first, mCache.getCachedPreference("first"));
92        assertEquals(second, mCache.getCachedPreference("second"));
93
94        mCache.removeCachedPrefs();
95
96        // Because the third preference was left, it should have been removed by the last call.
97        verify(mGroup).removePreference(eq(third));
98    }
99
100    @Test
101    public void testRestoreZeroPreferences() {
102        Preference first = createPreference("first");
103        Preference second = createPreference("second");
104        Preference third = createPreference("third");
105        Preference[] preferences = new Preference[] {first, second, third};
106        setupMockPreferenceGroup(preferences);
107
108        mCache = new PreferenceListCache(mGroup);
109        mCache.removeCachedPrefs();
110
111        // Because none of the cached preferences were used, the call should remove all three
112        // preferences from the group.
113        verify(mGroup).removePreference(eq(first));
114        verify(mGroup).removePreference(eq(second));
115        verify(mGroup).removePreference(eq(third));
116    }
117
118    private Preference createPreference(String key) {
119        Preference newPreference = new Preference(mContext);
120        newPreference.setKey(key);
121        return newPreference;
122    }
123
124    private void setupMockPreferenceGroup(Preference[] preferences) {
125        when(mGroup.getPreferenceCount()).thenReturn(preferences.length);
126        when(mGroup.getPreference(anyInt())).thenAnswer(new Answer<Preference>() {
127            @Override
128            public Preference answer(InvocationOnMock invocation) throws Throwable {
129                int index = (int) invocation.getArguments()[0];
130                return preferences[index];
131            }
132        });
133    }
134}
135