1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/**
* Copyright (c) 2018 LEAP Encryption Access Project and contributers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package se.leap.bitmaskclient.testutils;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by cyberta on 09.01.18.
*/
public class MockSharedPreferences implements SharedPreferences {
HashMap<String, String> mockedStringPrefs = new HashMap<>();
HashMap<String, Integer> mockedIntPrefs = new HashMap<>();
HashMap<String, Boolean> mockedBooleanPrefs = new HashMap<>();
@Override
public Map<String, ?> getAll() {
return null;
}
@Nullable
@Override
public String getString(String key, @Nullable String defValue) {
String value = mockedStringPrefs.get(key);
return value != null ? value : defValue;
}
@Nullable
@Override
public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
return null;
}
@Override
public int getInt(String key, int defValue) {
Integer value = mockedIntPrefs.get(key);
return value != null ? value : defValue;
}
@Override
public long getLong(String key, long defValue) {
return 0;
}
@Override
public float getFloat(String key, float defValue) {
return 0;
}
@Override
public boolean getBoolean(String key, boolean defValue) {
Boolean value = mockedBooleanPrefs.get(key);
return value != null ? value : defValue;
}
@Override
public boolean contains(String key) {
return mockedStringPrefs.containsKey(key) ||
mockedBooleanPrefs.containsKey(key) ||
mockedIntPrefs.containsKey(key);
}
@Override
public Editor edit() {
return new Editor() {
private HashMap<String, String> tempStrings = new HashMap<>(mockedStringPrefs);
private HashMap<String, Integer> tempIntegers = new HashMap<>(mockedIntPrefs);
private HashMap<String, Boolean> tempBoolean = new HashMap<>(mockedBooleanPrefs);
@Override
public Editor putString(String key, @Nullable String value) {
tempStrings.put(key, value);
return this;
}
@Override
public Editor putStringSet(String key, @Nullable Set<String> values) {
return null;
}
@Override
public Editor putInt(String key, int value) {
tempIntegers.put(key, value);
return this;
}
@Override
public Editor putLong(String key, long value) {
return null;
}
@Override
public Editor putFloat(String key, float value) {
return null;
}
@Override
public Editor putBoolean(String key, boolean value) {
tempBoolean.put(key, value);
return this;
}
@Override
public Editor remove(String key) {
tempBoolean.remove(key);
tempStrings.remove(key);
tempIntegers.remove(key);
return this;
}
@Override
public Editor clear() {
tempBoolean.clear();
tempStrings.clear();
tempIntegers.clear();
return this;
}
@Override
public boolean commit() {
mockedStringPrefs = new HashMap<>(tempStrings);
mockedBooleanPrefs = new HashMap<>(tempBoolean);
mockedIntPrefs = new HashMap<>(tempIntegers);
return true;
}
@Override
public void apply() {
mockedStringPrefs = new HashMap<>(tempStrings);
mockedBooleanPrefs = new HashMap<>(tempBoolean);
mockedIntPrefs = new HashMap<>(tempIntegers);
}
};
}
@Override
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
@Override
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
}
}
|