Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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.background.testhelpers; |
michael@0 | 5 | |
michael@0 | 6 | import android.content.SharedPreferences; |
michael@0 | 7 | |
michael@0 | 8 | import java.util.HashMap; |
michael@0 | 9 | import java.util.Map; |
michael@0 | 10 | import java.util.Set; |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * A programmable mock content provider. |
michael@0 | 14 | */ |
michael@0 | 15 | public class MockSharedPreferences implements SharedPreferences, SharedPreferences.Editor { |
michael@0 | 16 | private HashMap<String, Object> mValues; |
michael@0 | 17 | private HashMap<String, Object> mTempValues; |
michael@0 | 18 | |
michael@0 | 19 | public MockSharedPreferences() { |
michael@0 | 20 | mValues = new HashMap<String, Object>(); |
michael@0 | 21 | mTempValues = new HashMap<String, Object>(); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | public Editor edit() { |
michael@0 | 25 | return this; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | public boolean contains(String key) { |
michael@0 | 29 | return mValues.containsKey(key); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | public Map<String, ?> getAll() { |
michael@0 | 33 | return new HashMap<String, Object>(mValues); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public boolean getBoolean(String key, boolean defValue) { |
michael@0 | 37 | if (mValues.containsKey(key)) { |
michael@0 | 38 | return ((Boolean)mValues.get(key)).booleanValue(); |
michael@0 | 39 | } |
michael@0 | 40 | return defValue; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | public float getFloat(String key, float defValue) { |
michael@0 | 44 | if (mValues.containsKey(key)) { |
michael@0 | 45 | return ((Float)mValues.get(key)).floatValue(); |
michael@0 | 46 | } |
michael@0 | 47 | return defValue; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | public int getInt(String key, int defValue) { |
michael@0 | 51 | if (mValues.containsKey(key)) { |
michael@0 | 52 | return ((Integer)mValues.get(key)).intValue(); |
michael@0 | 53 | } |
michael@0 | 54 | return defValue; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | public long getLong(String key, long defValue) { |
michael@0 | 58 | if (mValues.containsKey(key)) { |
michael@0 | 59 | return ((Long)mValues.get(key)).longValue(); |
michael@0 | 60 | } |
michael@0 | 61 | return defValue; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | public String getString(String key, String defValue) { |
michael@0 | 65 | if (mValues.containsKey(key)) |
michael@0 | 66 | return (String)mValues.get(key); |
michael@0 | 67 | return defValue; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | @SuppressWarnings("unchecked") |
michael@0 | 71 | public Set<String> getStringSet(String key, Set<String> defValues) { |
michael@0 | 72 | if (mValues.containsKey(key)) { |
michael@0 | 73 | return (Set<String>) mValues.get(key); |
michael@0 | 74 | } |
michael@0 | 75 | return defValues; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public void registerOnSharedPreferenceChangeListener( |
michael@0 | 79 | OnSharedPreferenceChangeListener listener) { |
michael@0 | 80 | throw new UnsupportedOperationException(); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | public void unregisterOnSharedPreferenceChangeListener( |
michael@0 | 84 | OnSharedPreferenceChangeListener listener) { |
michael@0 | 85 | throw new UnsupportedOperationException(); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | public Editor putBoolean(String key, boolean value) { |
michael@0 | 89 | mTempValues.put(key, Boolean.valueOf(value)); |
michael@0 | 90 | return this; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | public Editor putFloat(String key, float value) { |
michael@0 | 94 | mTempValues.put(key, value); |
michael@0 | 95 | return this; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | public Editor putInt(String key, int value) { |
michael@0 | 99 | mTempValues.put(key, value); |
michael@0 | 100 | return this; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | public Editor putLong(String key, long value) { |
michael@0 | 104 | mTempValues.put(key, value); |
michael@0 | 105 | return this; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | public Editor putString(String key, String value) { |
michael@0 | 109 | mTempValues.put(key, value); |
michael@0 | 110 | return this; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | public Editor putStringSet(String key, Set<String> values) { |
michael@0 | 114 | mTempValues.put(key, values); |
michael@0 | 115 | return this; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | public Editor remove(String key) { |
michael@0 | 119 | mTempValues.remove(key); |
michael@0 | 120 | return this; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | public Editor clear() { |
michael@0 | 124 | mTempValues.clear(); |
michael@0 | 125 | return this; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | @SuppressWarnings("unchecked") |
michael@0 | 129 | public boolean commit() { |
michael@0 | 130 | mValues = (HashMap<String, Object>)mTempValues.clone(); |
michael@0 | 131 | return true; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | public void apply() { |
michael@0 | 135 | commit(); |
michael@0 | 136 | } |
michael@0 | 137 | } |