1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.preference.Preference; 6 import android.util.AttributeSet; 7 8 import com.xtremelabs.robolectric.internal.Implementation; 9 import com.xtremelabs.robolectric.internal.Implements; 10 import com.xtremelabs.robolectric.internal.RealObject; 11 12 @Implements(Preference.class) 13 public class ShadowPreference { 14 15 @RealObject private Preference realPreference; 16 17 protected Context context; 18 protected AttributeSet attrs; 19 protected int defStyle; 20 21 protected String key; 22 protected CharSequence title; 23 protected CharSequence summary; 24 protected Object defaultValue; 25 protected int order; 26 protected boolean enabled = true; 27 protected String dependencyKey; 28 protected boolean persistent = false; 29 protected int persistedInt; 30 protected Object callChangeListenerValue = null; 31 32 protected Preference.OnPreferenceClickListener onClickListener; 33 private Intent intent; 34 __constructor__(Context context)35 public void __constructor__(Context context) { 36 __constructor__(context, null, 0); 37 } 38 __constructor__(Context context, AttributeSet attributeSet)39 public void __constructor__(Context context, AttributeSet attributeSet) { 40 __constructor__(context, attributeSet, 0); 41 } 42 __constructor__(Context context, AttributeSet attributeSet, int defStyle)43 public void __constructor__(Context context, AttributeSet attributeSet, int defStyle) { 44 this.context = context; 45 this.attrs = attributeSet; 46 this.defStyle = defStyle; 47 48 if (attributeSet != null) { 49 key = attributeSet.getAttributeValue("android", "key"); 50 } 51 } 52 53 @Implementation getContext()54 public Context getContext() { 55 return context; 56 } 57 getAttrs()58 public AttributeSet getAttrs() { 59 return attrs; 60 } 61 getDefStyle()62 public int getDefStyle() { 63 return defStyle; 64 } 65 66 @Implementation setEnabled(boolean enabled)67 public void setEnabled(boolean enabled) { 68 this.enabled = enabled; 69 } 70 71 @Implementation isEnabled()72 public boolean isEnabled() { 73 return enabled; 74 } 75 76 @Implementation shouldPersist()77 public boolean shouldPersist() { 78 return persistent; 79 } 80 81 @Implementation isPersistent()82 public boolean isPersistent() { 83 return persistent; 84 } 85 86 @Implementation setPersistent(boolean persistent)87 public void setPersistent(boolean persistent) { 88 this.persistent = persistent; 89 } 90 91 @Implementation getPersistedInt(int defaultReturnValue)92 public int getPersistedInt(int defaultReturnValue) { 93 return persistent ? persistedInt : defaultReturnValue; 94 } 95 96 @Implementation persistInt(int value)97 public boolean persistInt(int value) { 98 this.persistedInt = value; 99 return persistent; 100 } 101 102 @Implementation callChangeListener(Object newValue)103 public boolean callChangeListener(Object newValue) { 104 callChangeListenerValue = newValue; 105 return true; 106 } 107 getCallChangeListenerValue()108 public Object getCallChangeListenerValue() { 109 return callChangeListenerValue; 110 } 111 112 @Implementation setSummary(int summaryResId)113 public void setSummary(int summaryResId) { 114 this.summary = context.getResources().getText(summaryResId); 115 } 116 117 @Implementation setSummary(CharSequence summary)118 public void setSummary(CharSequence summary) { 119 this.summary = summary; 120 } 121 122 @Implementation getSummary()123 public CharSequence getSummary() { 124 return summary; 125 } 126 127 @Implementation setTitle(int titleResId)128 public void setTitle(int titleResId) { 129 this.title = context.getResources().getText(titleResId); 130 } 131 132 @Implementation setTitle(CharSequence title)133 public void setTitle(CharSequence title) { 134 this.title = title; 135 } 136 137 @Implementation getTitle()138 public CharSequence getTitle() { 139 return title; 140 } 141 142 @Implementation setKey(String key)143 public void setKey(String key) { 144 this.key = key; 145 } 146 147 @Implementation getKey()148 public String getKey() { 149 return key; 150 } 151 152 @Implementation setDefaultValue(Object defaultValue)153 public void setDefaultValue(Object defaultValue) { 154 this.defaultValue = defaultValue; 155 } 156 getDefaultValue()157 public Object getDefaultValue() { 158 return defaultValue; 159 } 160 161 @Implementation getOrder()162 public int getOrder() { 163 return order; 164 } 165 166 @Implementation setOrder(int order)167 public void setOrder(int order) { 168 this.order = order; 169 } 170 171 @Implementation setOnPreferenceClickListener( Preference.OnPreferenceClickListener onPreferenceClickListener )172 public void setOnPreferenceClickListener( Preference.OnPreferenceClickListener onPreferenceClickListener ) { 173 this.onClickListener = onPreferenceClickListener; 174 } 175 176 @Implementation getOnPreferenceClickListener()177 public Preference.OnPreferenceClickListener getOnPreferenceClickListener() { 178 return onClickListener; 179 } 180 click()181 public boolean click() { 182 return onClickListener.onPreferenceClick(realPreference); 183 } 184 185 @Implementation setIntent(Intent i)186 public void setIntent(Intent i) { 187 this.intent = i; 188 } 189 190 @Implementation getIntent()191 public Intent getIntent() { 192 return this.intent; 193 194 } 195 196 @Implementation setDependency(String dependencyKey)197 public void setDependency(String dependencyKey) { 198 this.dependencyKey = dependencyKey; 199 } 200 201 @Implementation getDependency()202 public String getDependency() { 203 return this.dependencyKey; 204 } 205 } 206