1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base.test.util; 6 7 import android.content.ComponentCallbacks; 8 import android.content.ContentResolver; 9 import android.content.Context; 10 import android.content.ContextWrapper; 11 import android.content.SharedPreferences; 12 import android.test.mock.MockContentResolver; 13 import android.test.mock.MockContext; 14 15 import java.util.HashMap; 16 import java.util.Map; 17 18 /** 19 * ContextWrapper that adds functionality for SharedPreferences and a way to set and retrieve flags. 20 */ 21 public class AdvancedMockContext extends ContextWrapper { 22 23 private final MockContentResolver mMockContentResolver = new MockContentResolver(); 24 25 private final Map<String, SharedPreferences> mSharedPreferences = 26 new HashMap<String, SharedPreferences>(); 27 28 private final Map<String, Boolean> mFlags = new HashMap<String, Boolean>(); 29 30 public AdvancedMockContext(Context base) { 31 super(base); 32 } 33 34 public AdvancedMockContext() { 35 super(new MockContext()); 36 } 37 38 @Override 39 public String getPackageName() { 40 return getBaseContext().getPackageName(); 41 } 42 43 @Override 44 public Context getApplicationContext() { 45 return this; 46 } 47 48 @Override 49 public ContentResolver getContentResolver() { 50 return mMockContentResolver; 51 } 52 53 public MockContentResolver getMockContentResolver() { 54 return mMockContentResolver; 55 } 56 57 @Override 58 public SharedPreferences getSharedPreferences(String name, int mode) { 59 synchronized (mSharedPreferences) { 60 if (!mSharedPreferences.containsKey(name)) { 61 // Auto-create shared preferences to mimic Android Context behavior 62 mSharedPreferences.put(name, new InMemorySharedPreferences()); 63 } 64 return mSharedPreferences.get(name); 65 } 66 } 67 68 @Override 69 public void registerComponentCallbacks(ComponentCallbacks callback) { 70 getBaseContext().registerComponentCallbacks(callback); 71 } 72 73 @Override 74 public void unregisterComponentCallbacks(ComponentCallbacks callback) { 75 getBaseContext().unregisterComponentCallbacks(callback); 76 } 77 78 public void addSharedPreferences(String name, Map<String, Object> data) { 79 synchronized (mSharedPreferences) { 80 mSharedPreferences.put(name, new InMemorySharedPreferences(data)); 81 } 82 } 83 84 public void setFlag(String key) { 85 mFlags.put(key, true); 86 } 87 88 public void clearFlag(String key) { 89 mFlags.remove(key); 90 } 91 92 public boolean isFlagSet(String key) { 93 return mFlags.containsKey(key) && mFlags.get(key); 94 } 95 96 /** 97 * Builder for maps of type Map<String, Object> to be used with 98 * {@link #addSharedPreferences(String, java.util.Map)}. 99 */ 100 public static class MapBuilder { 101 102 private final Map<String, Object> mData = new HashMap<String, Object>(); 103 104 public static MapBuilder create() { 105 return new MapBuilder(); 106 } 107 108 public MapBuilder add(String key, Object value) { 109 mData.put(key, value); 110 return this; 111 } 112 113 public Map<String, Object> build() { 114 return mData; 115 } 116 117 } 118 } 119