1/*
2 * Copyright (C) 2011 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
17package com.android.email;
18
19import com.google.android.collect.Maps;
20
21import android.content.SharedPreferences;
22
23import java.util.HashMap;
24import java.util.Map;
25import java.util.Set;
26
27
28/**
29 * A programmable mock content provider.
30 */
31public class MockSharedPreferences implements SharedPreferences, SharedPreferences.Editor {
32
33    private HashMap<String, Object> mValues = Maps.newHashMap();
34    private HashMap<String, Object> mTempValues = Maps.newHashMap();
35
36    @SuppressWarnings("unchecked")
37    public Editor edit() {
38        mTempValues = (HashMap<String, Object>) mValues.clone();
39        return this;
40    }
41
42    public boolean contains(String key) {
43        return mValues.containsKey(key);
44    }
45
46    public Map<String, ?> getAll() {
47        return new HashMap<String, Object>(mValues);
48    }
49
50    public boolean getBoolean(String key, boolean defValue) {
51        if (mValues.containsKey(key)) {
52            return ((Boolean)mValues.get(key)).booleanValue();
53        }
54        return defValue;
55    }
56
57    public float getFloat(String key, float defValue) {
58        if (mValues.containsKey(key)) {
59            return ((Float)mValues.get(key)).floatValue();
60        }
61        return defValue;
62    }
63
64    public int getInt(String key, int defValue) {
65        if (mValues.containsKey(key)) {
66            return ((Integer)mValues.get(key)).intValue();
67        }
68        return defValue;
69    }
70
71    public long getLong(String key, long defValue) {
72        if (mValues.containsKey(key)) {
73            return ((Long)mValues.get(key)).longValue();
74        }
75        return defValue;
76    }
77
78    public String getString(String key, String defValue) {
79        if (mValues.containsKey(key))
80            return (String)mValues.get(key);
81        return defValue;
82    }
83
84    @SuppressWarnings("unchecked")
85    public Set<String> getStringSet(String key, Set<String> defValues) {
86        if (mValues.containsKey(key)) {
87            return (Set<String>) mValues.get(key);
88        }
89        return defValues;
90    }
91
92    public void registerOnSharedPreferenceChangeListener(
93            OnSharedPreferenceChangeListener listener) {
94        throw new UnsupportedOperationException();
95    }
96
97    public void unregisterOnSharedPreferenceChangeListener(
98            OnSharedPreferenceChangeListener listener) {
99        throw new UnsupportedOperationException();
100    }
101
102    public Editor putBoolean(String key, boolean value) {
103        mTempValues.put(key, Boolean.valueOf(value));
104        return this;
105    }
106
107    public Editor putFloat(String key, float value) {
108        mTempValues.put(key, value);
109        return this;
110    }
111
112    public Editor putInt(String key, int value) {
113        mTempValues.put(key, value);
114        return this;
115    }
116
117    public Editor putLong(String key, long value) {
118        mTempValues.put(key, value);
119        return this;
120    }
121
122    public Editor putString(String key, String value) {
123        mTempValues.put(key, value);
124        return this;
125    }
126
127    public Editor putStringSet(String key, Set<String> values) {
128        mTempValues.put(key, values);
129        return this;
130    }
131
132    public Editor remove(String key) {
133        mTempValues.remove(key);
134        return this;
135    }
136
137    public Editor clear() {
138        mTempValues.clear();
139        return this;
140    }
141
142    @SuppressWarnings("unchecked")
143    public boolean commit() {
144        mValues = (HashMap<String, Object>) mTempValues.clone();
145        return true;
146    }
147
148    public void apply() {
149        commit();
150    }
151}
152