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