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