Home | History | Annotate | Download | only in content
      1 package com.xtremelabs.robolectric.tester.android.content;
      2 
      3 import android.content.SharedPreferences;
      4 
      5 import java.util.*;
      6 
      7 public class TestSharedPreferences implements SharedPreferences {
      8 
      9     public Map<String, Map<String, Object>> content;
     10     protected String filename;
     11     public int mode;
     12 
     13     private ArrayList<OnSharedPreferenceChangeListener> listeners;
     14 
     15     public TestSharedPreferences(Map<String, Map<String, Object>> content,
     16             String name, int mode) {
     17         this.content = content;
     18         this.filename = name;
     19         this.mode = mode;
     20         if (!content.containsKey(name)) {
     21             content.put(name, new HashMap<String, Object>());
     22         }
     23 
     24         listeners = new ArrayList<OnSharedPreferenceChangeListener>();
     25     }
     26 
     27     @Override
     28     public Map<String, ?> getAll() {
     29         return new HashMap<String, Object>(content.get(filename));
     30     }
     31 
     32     @Override
     33     public String getString(String key, String defValue) {
     34         return (String) getValue(key, defValue);
     35     }
     36 
     37     private Object getValue(String key, Object defValue) {
     38         Map<String, Object> fileHash = content.get(filename);
     39         if (fileHash != null) {
     40             Object value = fileHash.get(key);
     41             if (value != null) {
     42                 return value;
     43             }
     44         }
     45         return defValue;
     46     }
     47 
     48     @Override
     49     public int getInt(String key, int defValue) {
     50         return (Integer) getValue(key, defValue);
     51     }
     52 
     53     @Override
     54     public long getLong(String key, long defValue) {
     55         return (Long) getValue(key, defValue);
     56     }
     57 
     58     @Override
     59     public float getFloat(String key, float defValue) {
     60         return (Float) getValue(key, defValue);
     61     }
     62 
     63     @Override
     64     public boolean getBoolean(String key, boolean defValue) {
     65         return (Boolean) getValue(key, defValue);
     66     }
     67 
     68     @Override
     69     public boolean contains(String key) {
     70         return content.get(filename).containsKey(key);
     71     }
     72 
     73     @Override
     74     public Editor edit() {
     75         return new TestSharedPreferencesEditor();
     76     }
     77 
     78     @Override
     79     public void registerOnSharedPreferenceChangeListener(
     80             OnSharedPreferenceChangeListener listener) {
     81         if(!listeners.contains(listener))
     82             listeners.add(listener);
     83     }
     84 
     85     @Override
     86     public void unregisterOnSharedPreferenceChangeListener(
     87             OnSharedPreferenceChangeListener listener) {
     88         if(listeners.contains(listener))
     89             listeners.remove(listener);
     90     }
     91 
     92     public boolean hasListener(OnSharedPreferenceChangeListener listener) {
     93         return listeners.contains(listener);
     94     }
     95 
     96     private class TestSharedPreferencesEditor implements Editor {
     97 
     98         Map<String, Object> editsThatNeedCommit = new HashMap<String, Object>();
     99         Set<String> editsThatNeedRemove = new HashSet<String>();
    100         private boolean shouldClearOnCommit = false;
    101 
    102         @Override
    103         public Editor putString(String key, String value) {
    104             editsThatNeedCommit.put(key, value);
    105             editsThatNeedRemove.remove(key);
    106             return this;
    107         }
    108 
    109         @Override
    110         public Editor putInt(String key, int value) {
    111             editsThatNeedCommit.put(key, value);
    112             editsThatNeedRemove.remove(key);
    113             return this;
    114         }
    115 
    116         @Override
    117         public Editor putLong(String key, long value) {
    118             editsThatNeedCommit.put(key, value);
    119             editsThatNeedRemove.remove(key);
    120             return this;
    121         }
    122 
    123         @Override
    124         public Editor putFloat(String key, float value) {
    125             editsThatNeedCommit.put(key, value);
    126             editsThatNeedRemove.remove(key);
    127             return this;
    128         }
    129 
    130         @Override
    131         public Editor putBoolean(String key, boolean value) {
    132             editsThatNeedCommit.put(key, value);
    133             editsThatNeedRemove.remove(key);
    134             return this;
    135         }
    136 
    137         @Override
    138         public Editor remove(String key) {
    139             editsThatNeedRemove.add(key);
    140             return this;
    141         }
    142 
    143         @Override
    144         public Editor clear() {
    145             shouldClearOnCommit = true;
    146             return this;
    147         }
    148 
    149         @Override
    150         public boolean commit() {
    151             Map<String, Object> previousContent = content.get(filename);
    152             if (shouldClearOnCommit) {
    153                 previousContent.clear();
    154             } else {
    155                 for (String key : editsThatNeedCommit.keySet()) {
    156                     previousContent.put(key, editsThatNeedCommit.get(key));
    157                 }
    158                 for (String key : editsThatNeedRemove) {
    159                     previousContent.remove(key);
    160                 }
    161             }
    162 
    163             for (String key : editsThatNeedCommit.keySet()) {
    164                 previousContent.put(key, editsThatNeedCommit.get(key));
    165             }
    166 
    167             return true;
    168         }
    169 
    170         @Override
    171         public void apply() {
    172             commit();
    173         }
    174 
    175 		@Override
    176 		public Editor putStringSet(String key, Set<String> values) {
    177 			return this;
    178 		}
    179     }
    180 
    181 	@Override
    182 	public Set<String> getStringSet(String key, Set<String> defValues) {
    183 		return null;
    184 	}
    185 }
    186