mobile/android/tests/browser/junit3/src/tests/TestGeckoSharedPrefs.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.browser.tests;
michael@0 5
michael@0 6 import java.util.Arrays;
michael@0 7 import java.util.Collections;
michael@0 8 import java.util.EnumSet;
michael@0 9 import java.util.Map;
michael@0 10 import java.util.HashSet;
michael@0 11 import java.util.Set;
michael@0 12
michael@0 13 import org.mozilla.gecko.GeckoProfile;
michael@0 14 import org.mozilla.gecko.GeckoSharedPrefs;
michael@0 15 import org.mozilla.gecko.GeckoSharedPrefs.Flags;
michael@0 16
michael@0 17 import android.content.Context;
michael@0 18 import android.content.SharedPreferences;
michael@0 19 import android.content.SharedPreferences.Editor;
michael@0 20 import android.os.Build;
michael@0 21 import android.preference.PreferenceManager;
michael@0 22 import android.test.RenamingDelegatingContext;
michael@0 23
michael@0 24 /**
michael@0 25 * Test GeckoSharedPrefs migrations.
michael@0 26 */
michael@0 27 public class TestGeckoSharedPrefs extends BrowserTestCase {
michael@0 28
michael@0 29 private static class TestContext extends RenamingDelegatingContext {
michael@0 30 private static final String PREFIX = "TestGeckoSharedPrefs-";
michael@0 31
michael@0 32 private final Set<String> usedPrefs;
michael@0 33
michael@0 34 public TestContext(Context context) {
michael@0 35 super(context, PREFIX);
michael@0 36 usedPrefs = Collections.synchronizedSet(new HashSet<String>());
michael@0 37 }
michael@0 38
michael@0 39 @Override
michael@0 40 public SharedPreferences getSharedPreferences(String name, int mode) {
michael@0 41 usedPrefs.add(name);
michael@0 42 return super.getSharedPreferences(PREFIX + name, mode);
michael@0 43 }
michael@0 44
michael@0 45 public void clearUsedPrefs() {
michael@0 46 for (String prefsName : usedPrefs) {
michael@0 47 getSharedPreferences(prefsName, 0).edit().clear().commit();
michael@0 48 }
michael@0 49
michael@0 50 usedPrefs.clear();
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 private static final EnumSet<Flags> disableMigrations = EnumSet.of(Flags.DISABLE_MIGRATIONS);
michael@0 55
michael@0 56 private TestContext context;
michael@0 57
michael@0 58 protected void setUp() {
michael@0 59 context = new TestContext(getApplicationContext());
michael@0 60 }
michael@0 61
michael@0 62 protected void tearDown() {
michael@0 63 context.clearUsedPrefs();
michael@0 64 GeckoSharedPrefs.reset();
michael@0 65 }
michael@0 66
michael@0 67 public void testDisableMigrations() {
michael@0 68 // Version is 0 before any migration
michael@0 69 assertEquals(0, GeckoSharedPrefs.getVersion(context));
michael@0 70
michael@0 71 // Get prefs with migrations disabled
michael@0 72 GeckoSharedPrefs.forApp(context, disableMigrations);
michael@0 73 GeckoSharedPrefs.forProfile(context, disableMigrations);
michael@0 74 GeckoSharedPrefs.forProfileName(context, "someProfile", disableMigrations);
michael@0 75
michael@0 76 // Version should still be 0
michael@0 77 assertEquals(0, GeckoSharedPrefs.getVersion(context));
michael@0 78 }
michael@0 79
michael@0 80 public void testPrefsVersion() {
michael@0 81 // Version is 0 before any migration
michael@0 82 assertEquals(0, GeckoSharedPrefs.getVersion(context));
michael@0 83
michael@0 84 // Trigger migration by getting a SharedPreferences instance
michael@0 85 GeckoSharedPrefs.forApp(context);
michael@0 86
michael@0 87 // Version should be current after migration
michael@0 88 assertEquals(GeckoSharedPrefs.PREFS_VERSION, GeckoSharedPrefs.getVersion(context));
michael@0 89 }
michael@0 90
michael@0 91 public void testMigrateFromPreferenceManager() {
michael@0 92 SharedPreferences appPrefs = GeckoSharedPrefs.forApp(context, disableMigrations);
michael@0 93 assertTrue(appPrefs.getAll().isEmpty());
michael@0 94 final Editor appEditor = appPrefs.edit();
michael@0 95
michael@0 96 SharedPreferences profilePrefs = GeckoSharedPrefs.forProfileName(context, GeckoProfile.DEFAULT_PROFILE, disableMigrations);
michael@0 97 assertTrue(profilePrefs.getAll().isEmpty());
michael@0 98 final Editor profileEditor = profilePrefs.edit();
michael@0 99
michael@0 100 final SharedPreferences pmPrefs = PreferenceManager.getDefaultSharedPreferences(context);
michael@0 101 assertTrue(pmPrefs.getAll().isEmpty());
michael@0 102 Editor pmEditor = pmPrefs.edit();
michael@0 103
michael@0 104 // Insert a key for each type to exercise the
michael@0 105 // migration path a bit more thoroughly.
michael@0 106 pmEditor.putInt("int_key", 23);
michael@0 107 pmEditor.putLong("long_key", 23L);
michael@0 108 pmEditor.putString("string_key", "23");
michael@0 109 pmEditor.putFloat("float_key", 23.3f);
michael@0 110
michael@0 111 final String[] profileKeys = {
michael@0 112 "string_profile",
michael@0 113 "int_profile"
michael@0 114 };
michael@0 115
michael@0 116 // Insert keys that are expected to be moved to the
michael@0 117 // PROFILE scope.
michael@0 118 pmEditor.putString(profileKeys[0], "24");
michael@0 119 pmEditor.putInt(profileKeys[1], 24);
michael@0 120
michael@0 121 // Commit changes to PreferenceManager
michael@0 122 pmEditor.commit();
michael@0 123 assertEquals(6, pmPrefs.getAll().size());
michael@0 124
michael@0 125 // Perform actual migration with the given editors
michael@0 126 pmEditor = GeckoSharedPrefs.migrateFromPreferenceManager(context, appEditor,
michael@0 127 profileEditor, Arrays.asList(profileKeys));
michael@0 128
michael@0 129 // Commit changes applied during the migration
michael@0 130 appEditor.commit();
michael@0 131 profileEditor.commit();
michael@0 132 pmEditor.commit();
michael@0 133
michael@0 134 // PreferenceManager should have no keys
michael@0 135 assertTrue(pmPrefs.getAll().isEmpty());
michael@0 136
michael@0 137 // App should have all keys except the profile ones
michael@0 138 assertEquals(4, appPrefs.getAll().size());
michael@0 139
michael@0 140 // Ensure app scope doesn't have any of the profile keys
michael@0 141 for (int i = 0; i < profileKeys.length; i++) {
michael@0 142 assertFalse(appPrefs.contains(profileKeys[i]));
michael@0 143 }
michael@0 144
michael@0 145 // Check app keys
michael@0 146 assertEquals(23, appPrefs.getInt("int_key", 0));
michael@0 147 assertEquals(23L, appPrefs.getLong("long_key", 0L));
michael@0 148 assertEquals("23", appPrefs.getString("string_key", ""));
michael@0 149 assertEquals(23.3f, appPrefs.getFloat("float_key", 0));
michael@0 150
michael@0 151 assertEquals(2, profilePrefs.getAll().size());
michael@0 152 assertEquals("24", profilePrefs.getString(profileKeys[0], ""));
michael@0 153 assertEquals(24, profilePrefs.getInt(profileKeys[1], 0));
michael@0 154 }
michael@0 155 }

mercurial