mobile/android/tests/background/junit3/src/testhelpers/MockSharedPreferences.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/tests/background/junit3/src/testhelpers/MockSharedPreferences.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +package org.mozilla.gecko.background.testhelpers;
     1.8 +
     1.9 +import android.content.SharedPreferences;
    1.10 +
    1.11 +import java.util.HashMap;
    1.12 +import java.util.Map;
    1.13 +import java.util.Set;
    1.14 +
    1.15 +/**
    1.16 + * A programmable mock content provider.
    1.17 + */
    1.18 +public class MockSharedPreferences implements SharedPreferences, SharedPreferences.Editor {
    1.19 +  private HashMap<String, Object> mValues;
    1.20 +  private HashMap<String, Object> mTempValues;
    1.21 +
    1.22 +  public MockSharedPreferences() {
    1.23 +    mValues = new HashMap<String, Object>();
    1.24 +    mTempValues = new HashMap<String, Object>();
    1.25 +  }
    1.26 +
    1.27 +  public Editor edit() {
    1.28 +    return this;
    1.29 +  }
    1.30 +
    1.31 +  public boolean contains(String key) {
    1.32 +    return mValues.containsKey(key);
    1.33 +  }
    1.34 +
    1.35 +  public Map<String, ?> getAll() {
    1.36 +    return new HashMap<String, Object>(mValues);
    1.37 +  }
    1.38 +
    1.39 +  public boolean getBoolean(String key, boolean defValue) {
    1.40 +    if (mValues.containsKey(key)) {
    1.41 +      return ((Boolean)mValues.get(key)).booleanValue();
    1.42 +    }
    1.43 +    return defValue;
    1.44 +  }
    1.45 +
    1.46 +  public float getFloat(String key, float defValue) {
    1.47 +    if (mValues.containsKey(key)) {
    1.48 +      return ((Float)mValues.get(key)).floatValue();
    1.49 +    }
    1.50 +    return defValue;
    1.51 +  }
    1.52 +
    1.53 +  public int getInt(String key, int defValue) {
    1.54 +    if (mValues.containsKey(key)) {
    1.55 +      return ((Integer)mValues.get(key)).intValue();
    1.56 +    }
    1.57 +    return defValue;
    1.58 +  }
    1.59 +
    1.60 +  public long getLong(String key, long defValue) {
    1.61 +    if (mValues.containsKey(key)) {
    1.62 +      return ((Long)mValues.get(key)).longValue();
    1.63 +    }
    1.64 +    return defValue;
    1.65 +  }
    1.66 +
    1.67 +  public String getString(String key, String defValue) {
    1.68 +    if (mValues.containsKey(key))
    1.69 +      return (String)mValues.get(key);
    1.70 +    return defValue;
    1.71 +  }
    1.72 +
    1.73 +  @SuppressWarnings("unchecked")
    1.74 +  public Set<String> getStringSet(String key, Set<String> defValues) {
    1.75 +    if (mValues.containsKey(key)) {
    1.76 +      return (Set<String>) mValues.get(key);
    1.77 +    }
    1.78 +    return defValues;
    1.79 +  }
    1.80 +
    1.81 +  public void registerOnSharedPreferenceChangeListener(
    1.82 +    OnSharedPreferenceChangeListener listener) {
    1.83 +    throw new UnsupportedOperationException();
    1.84 +  }
    1.85 +
    1.86 +  public void unregisterOnSharedPreferenceChangeListener(
    1.87 +    OnSharedPreferenceChangeListener listener) {
    1.88 +    throw new UnsupportedOperationException();
    1.89 +  }
    1.90 +
    1.91 +  public Editor putBoolean(String key, boolean value) {
    1.92 +    mTempValues.put(key, Boolean.valueOf(value));
    1.93 +    return this;
    1.94 +  }
    1.95 +
    1.96 +  public Editor putFloat(String key, float value) {
    1.97 +    mTempValues.put(key, value);
    1.98 +    return this;
    1.99 +  }
   1.100 +
   1.101 +  public Editor putInt(String key, int value) {
   1.102 +    mTempValues.put(key, value);
   1.103 +    return this;
   1.104 +  }
   1.105 +
   1.106 +  public Editor putLong(String key, long value) {
   1.107 +    mTempValues.put(key, value);
   1.108 +    return this;
   1.109 +  }
   1.110 +
   1.111 +  public Editor putString(String key, String value) {
   1.112 +    mTempValues.put(key, value);
   1.113 +    return this;
   1.114 +  }
   1.115 +
   1.116 +  public Editor putStringSet(String key, Set<String> values) {
   1.117 +    mTempValues.put(key, values);
   1.118 +    return this;
   1.119 +  }
   1.120 +
   1.121 +  public Editor remove(String key) {
   1.122 +    mTempValues.remove(key);
   1.123 +    return this;
   1.124 +  }
   1.125 +
   1.126 +  public Editor clear() {
   1.127 +    mTempValues.clear();
   1.128 +    return this;
   1.129 +  }
   1.130 +
   1.131 +  @SuppressWarnings("unchecked")
   1.132 +  public boolean commit() {
   1.133 +    mValues = (HashMap<String, Object>)mTempValues.clone();
   1.134 +    return true;
   1.135 +  }
   1.136 +
   1.137 +  public void apply() {
   1.138 +    commit();
   1.139 +  }
   1.140 +}

mercurial