OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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.ContentResolver; |
| 8 import android.content.Context; |
| 9 import android.content.ContextWrapper; |
| 10 import android.content.SharedPreferences; |
| 11 import android.test.mock.MockContentResolver; |
| 12 import android.test.mock.MockContext; |
| 13 |
| 14 import java.util.HashMap; |
| 15 import java.util.Map; |
| 16 |
| 17 /** |
| 18 * ContextWrapper that adds functionality for SharedPreferences and a way to set
and retrieve flags. |
| 19 */ |
| 20 public class AdvancedMockContext extends ContextWrapper { |
| 21 |
| 22 private final MockContentResolver mMockContentResolver = new MockContentReso
lver(); |
| 23 |
| 24 private final Map<String, SharedPreferences> mSharedPreferences = |
| 25 new HashMap<String, SharedPreferences>(); |
| 26 |
| 27 private final Map<String, Boolean> flags = new HashMap<String, Boolean>(); |
| 28 |
| 29 public AdvancedMockContext(Context base) { |
| 30 super(base); |
| 31 } |
| 32 |
| 33 public AdvancedMockContext() { |
| 34 super(new MockContext()); |
| 35 } |
| 36 |
| 37 @Override |
| 38 public String getPackageName() { |
| 39 return getBaseContext().getPackageName(); |
| 40 } |
| 41 |
| 42 @Override |
| 43 public Context getApplicationContext() { |
| 44 return this; |
| 45 } |
| 46 |
| 47 @Override |
| 48 public ContentResolver getContentResolver() { |
| 49 return mMockContentResolver; |
| 50 } |
| 51 |
| 52 public MockContentResolver getMockContentResolver() { |
| 53 return mMockContentResolver; |
| 54 } |
| 55 |
| 56 @Override |
| 57 public SharedPreferences getSharedPreferences(String name, int mode) { |
| 58 synchronized (mSharedPreferences) { |
| 59 if (!mSharedPreferences.containsKey(name)) { |
| 60 // Auto-create shared preferences to mimic Android Context behav
ior |
| 61 mSharedPreferences.put(name, new InMemorySharedPreferences()); |
| 62 } |
| 63 return mSharedPreferences.get(name); |
| 64 } |
| 65 } |
| 66 |
| 67 public void addSharedPreferences(String name, Map<String, Object> data) { |
| 68 synchronized (mSharedPreferences) { |
| 69 mSharedPreferences.put(name, new InMemorySharedPreferences(data)); |
| 70 } |
| 71 } |
| 72 |
| 73 public void setFlag(String key) { |
| 74 flags.put(key, true); |
| 75 } |
| 76 |
| 77 public void clearFlag(String key) { |
| 78 flags.remove(key); |
| 79 } |
| 80 |
| 81 public boolean isFlagSet(String key) { |
| 82 return flags.containsKey(key) && flags.get(key); |
| 83 } |
| 84 |
| 85 public static class MapBuilder { |
| 86 |
| 87 private final Map<String, Object> mData = new HashMap<String, Object>(); |
| 88 |
| 89 public static MapBuilder create() { |
| 90 return new MapBuilder(); |
| 91 } |
| 92 |
| 93 public MapBuilder add(String key, Object value) { |
| 94 mData.put(key, value); |
| 95 return this; |
| 96 } |
| 97 |
| 98 public Map<String, Object> build() { |
| 99 return mData; |
| 100 } |
| 101 |
| 102 } |
| 103 } |
OLD | NEW |