• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 package com.android.documentsui.prefs;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.fail;
23 
24 import android.content.SharedPreferences;
25 import android.content.SharedPreferences.Editor;
26 import android.support.test.InstrumentationRegistry;
27 import android.support.test.filters.SmallTest;
28 import android.support.test.runner.AndroidJUnit4;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.io.IOException;
35 import java.util.Map;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Set;
39 
40 @RunWith(AndroidJUnit4.class)
41 @SmallTest
42 public class PrefsBackupHelperTest {
43 
44     private static final String LOCAL_PREFERENCE_1 = "rootViewMode-validPreference1";
45     private static final String LOCAL_PREFERENCE_2 = "rootViewMode-validPreference2";
46     private static final String SCOPED_PREFERENCE = "includeDeviceRoot";
47     private static final String NON_BACKUP_PREFERENCE = "notBackup-invalidPreference";
48 
49     private SharedPreferences mDefaultPrefs;
50     private SharedPreferences mBackupPrefs;
51     private PrefsBackupHelper mPrefsBackupHelper;
52 
53     @Before
setUp()54     public void setUp() {
55         mDefaultPrefs = InstrumentationRegistry.getContext().getSharedPreferences("prefs1", 0);
56         mBackupPrefs = InstrumentationRegistry.getContext().getSharedPreferences("prefs2", 0);
57         mPrefsBackupHelper = new PrefsBackupHelper(mDefaultPrefs);
58     }
59 
60     @Test
testPrepareBackupFile_BackupLocalPreferences()61     public void testPrepareBackupFile_BackupLocalPreferences() {
62         mDefaultPrefs.edit().putInt(LOCAL_PREFERENCE_1, 1).commit();
63 
64         mPrefsBackupHelper.getBackupPreferences(mBackupPrefs);
65 
66         assertEquals(mBackupPrefs.getInt(LOCAL_PREFERENCE_1, 0), 1);
67     }
68 
69     @Test
testPrepareBackupFile_BackupScopedPreferences()70     public void testPrepareBackupFile_BackupScopedPreferences() {
71         mDefaultPrefs.edit().putBoolean(SCOPED_PREFERENCE, true).commit();
72 
73         mPrefsBackupHelper.getBackupPreferences(mBackupPrefs);
74 
75         assertEquals(mBackupPrefs.getBoolean(SCOPED_PREFERENCE, false), true);
76     }
77 
78     @Test
testPrepareBackupFile_BackupNotInterestedPreferences()79     public void testPrepareBackupFile_BackupNotInterestedPreferences() {
80         mDefaultPrefs.edit().putBoolean(NON_BACKUP_PREFERENCE, true).commit();
81 
82         mPrefsBackupHelper.getBackupPreferences(mBackupPrefs);
83 
84         assertFalse(mBackupPrefs.contains(NON_BACKUP_PREFERENCE));
85     }
86 
87     @Test
testPrepareBackupFile_BackupUnexpectedType()88     public void testPrepareBackupFile_BackupUnexpectedType() throws Exception {
89         // Currently only Integer and Boolean type are supported.
90         mDefaultPrefs.edit().putString(LOCAL_PREFERENCE_1, "String is not accepted").commit();
91 
92         try {
93             mPrefsBackupHelper.getBackupPreferences(mBackupPrefs);
94             fail();
95         } catch(IllegalArgumentException e) {
96 
97         } finally {
98             assertFalse(mBackupPrefs.contains(LOCAL_PREFERENCE_1));
99         }
100     }
101 
102     @Test
testRestorePreferences_RestoreLocalPreferences()103     public void testRestorePreferences_RestoreLocalPreferences() {
104         mBackupPrefs.edit().putInt(LOCAL_PREFERENCE_1, 1).commit();
105 
106         mPrefsBackupHelper.putBackupPreferences(mBackupPrefs);
107 
108         assertEquals(mDefaultPrefs.getInt(LOCAL_PREFERENCE_1, 0), 1);
109     }
110 
111     @Test
testRestorePreferences_RestoreScopedPreferences()112     public void testRestorePreferences_RestoreScopedPreferences() {
113         mBackupPrefs.edit().putBoolean(SCOPED_PREFERENCE, true).commit();
114 
115         mPrefsBackupHelper.putBackupPreferences(mBackupPrefs);
116 
117         assertEquals(mDefaultPrefs.getBoolean(SCOPED_PREFERENCE, false), true);
118     }
119 
120     @Test
testEndToEnd()121     public void testEndToEnd() {
122         // Simulating an end to end backup & restore process. At the begining, all preferences are
123         // stored in the default shared preferences file, includes preferences that we don't want
124         // to backup.
125         //
126         // On backup, we copy all preferences that we want to backup to the backup shared
127         // preferences file, and then backup that single file.
128         //
129         // On restore, we restore the backup file first, and then copy all preferences in the backup
130         // file to the app's default shared preferences file.
131 
132         SharedPreferences.Editor editor = mDefaultPrefs.edit();
133 
134         // Set preferences to the default file, includes preferences that are not backed up.
135         editor.putInt(LOCAL_PREFERENCE_1, 1);
136         editor.putInt(LOCAL_PREFERENCE_2, 2);
137         editor.putBoolean(SCOPED_PREFERENCE, true);
138         editor.putBoolean(NON_BACKUP_PREFERENCE, true);
139         editor.commit();
140 
141         // Write all backed up preferences to backup shared preferences file.
142         mPrefsBackupHelper.getBackupPreferences(mBackupPrefs);
143 
144         // Assume we are doing backup to the backup file.
145 
146         // Clear all preferences in default shared preferences file.
147         editor.clear().commit();
148 
149         // Assume we are doing restore to the backup file.
150 
151         // Copy all backuped preferences to default shared preferences file.
152         mPrefsBackupHelper.putBackupPreferences(mBackupPrefs);
153 
154         // Check all preferences are correctly restored.
155         assertEquals(mDefaultPrefs.getInt(LOCAL_PREFERENCE_1, 0), 1);
156         assertEquals(mDefaultPrefs.getInt(LOCAL_PREFERENCE_2, 0), 2);
157         assertEquals(mDefaultPrefs.getBoolean(SCOPED_PREFERENCE, false), true);
158         assertFalse(mDefaultPrefs.contains(NON_BACKUP_PREFERENCE));
159     }
160 
161     @Test
testPreferenceTypesSupport()162     public void testPreferenceTypesSupport() {
163         Map<String, Object> map = new HashMap<String, Object>();
164         map.put("int", (Integer) 1);
165         map.put("float", (Float) 0.1f);
166         map.put("long", (Long) 10000000000l);
167         map.put("boolean", true);
168         map.put("String", "String");
169         Set<String> stringSet = new HashSet<String>();
170         stringSet.add("string1");
171         stringSet.add("string2");
172         map.put("StringSet", stringSet);
173 
174         // SharedPreferences accept Integer, Float, Long, Boolean, String, Set<String> types.
175         // Currently in DocumentsUI, only Integer and Boolean preferences are backed up.
176         for (Map.Entry<String, ?> entry : map.entrySet()) {
177             String key = entry.getKey();
178             Object value = entry.getValue();
179             Editor editor = mDefaultPrefs.edit().clear();
180             if (value instanceof Integer) {
181                 mPrefsBackupHelper.setPreference(editor, entry);
182                 editor.apply();
183                 assertEquals(mDefaultPrefs.getInt("int", 0), 1);
184             } else if(value instanceof Boolean) {
185                 mPrefsBackupHelper.setPreference(editor, entry);
186                 editor.apply();
187                 assertEquals(mDefaultPrefs.getBoolean("boolean", false), true);
188             } else {
189                 try {
190                     mPrefsBackupHelper.setPreference(editor, entry);
191                     fail();
192                 } catch(IllegalArgumentException e) {
193 
194                 } finally {
195                     editor.apply();
196                     assertFalse(mDefaultPrefs.contains(key));
197                 }
198             }
199         }
200     }
201 }
202