This source file includes following definitions.
- getPackageName
- getApplicationContext
- getContentResolver
- getMockContentResolver
- getSharedPreferences
- addSharedPreferences
- setFlag
- clearFlag
- isFlagSet
- create
- add
- build
package org.chromium.base.test.util;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.test.mock.MockContentResolver;
import android.test.mock.MockContext;
import java.util.HashMap;
import java.util.Map;
public class AdvancedMockContext extends ContextWrapper {
private final MockContentResolver mMockContentResolver = new MockContentResolver();
private final Map<String, SharedPreferences> mSharedPreferences =
new HashMap<String, SharedPreferences>();
private final Map<String, Boolean> mFlags = new HashMap<String, Boolean>();
public AdvancedMockContext(Context base) {
super(base);
}
public AdvancedMockContext() {
super(new MockContext());
}
@Override
public String getPackageName() {
return getBaseContext().getPackageName();
}
@Override
public Context getApplicationContext() {
return this;
}
@Override
public ContentResolver getContentResolver() {
return mMockContentResolver;
}
public MockContentResolver getMockContentResolver() {
return mMockContentResolver;
}
@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
synchronized (mSharedPreferences) {
if (!mSharedPreferences.containsKey(name)) {
mSharedPreferences.put(name, new InMemorySharedPreferences());
}
return mSharedPreferences.get(name);
}
}
public void addSharedPreferences(String name, Map<String, Object> data) {
synchronized (mSharedPreferences) {
mSharedPreferences.put(name, new InMemorySharedPreferences(data));
}
}
public void setFlag(String key) {
mFlags.put(key, true);
}
public void clearFlag(String key) {
mFlags.remove(key);
}
public boolean isFlagSet(String key) {
return mFlags.containsKey(key) && mFlags.get(key);
}
public static class MapBuilder {
private final Map<String, Object> mData = new HashMap<String, Object>();
public static MapBuilder create() {
return new MapBuilder();
}
public MapBuilder add(String key, Object value) {
mData.put(key, value);
return this;
}
public Map<String, Object> build() {
return mData;
}
}
}