1 package com.xtremelabs.robolectric.shadows; 2 3 import android.accounts.Account; 4 import android.os.Parcel; 5 import android.os.Parcelable.Creator; 6 import android.text.TextUtils; 7 8 import com.xtremelabs.robolectric.Robolectric; 9 import com.xtremelabs.robolectric.internal.Implementation; 10 import com.xtremelabs.robolectric.internal.Implements; 11 import com.xtremelabs.robolectric.internal.RealObject; 12 13 import java.lang.reflect.Field; 14 15 @Implements(Account.class) 16 public class ShadowAccount { 17 @RealObject 18 private Account realObject; 19 __constructor__(String name, String type)20 public void __constructor__(String name, String type) throws Exception { 21 set(name, type); 22 } 23 __constructor__(Parcel parcel)24 public void __constructor__(Parcel parcel) throws Exception { 25 set(parcel.readString(), parcel.readString()); 26 } 27 28 @Override 29 @Implementation toString()30 public String toString() { 31 return "Account {name=" + realObject.name + ", type=" + realObject.type + "}"; 32 } 33 34 @Implementation writeToParcel(Parcel dest, int flags)35 public void writeToParcel(Parcel dest, int flags) { 36 dest.writeString(realObject.name); 37 dest.writeString(realObject.type); 38 } 39 40 public static final Creator<Account> CREATOR = 41 new Creator<Account>() { 42 @Override 43 public Account createFromParcel(Parcel source) { 44 return new Account(source); 45 } 46 47 @Override 48 public Account[] newArray(int size) { 49 return new Account[size]; 50 } 51 }; 52 set(String name, String type)53 private void set(String name, String type) throws Exception { 54 if (TextUtils.isEmpty(name) || TextUtils.isEmpty(type)) throw new IllegalArgumentException(); 55 56 Field nameF = realObject.getClass().getField("name"); 57 nameF.setAccessible(true); 58 nameF.set(realObject, name); 59 60 Field typeF = realObject.getClass().getField("type"); 61 typeF.setAccessible(true); 62 typeF.set(realObject, type); 63 } 64 65 @Override 66 @Implementation equals(Object o)67 public boolean equals(Object o) { 68 if (o == this) return true; 69 if (!(o instanceof Account)) return false; 70 final Account other = (Account)o; 71 return realObject.name.equals(other.name) && realObject.type.equals(other.type); 72 } 73 74 @Override 75 @Implementation hashCode()76 public int hashCode() { 77 int result = 17; 78 result = 31 * result + realObject.name.hashCode(); 79 result = 31 * result + realObject.type.hashCode(); 80 return result; 81 } 82 reset()83 public static void reset() { 84 Robolectric.Reflection.setFinalStaticField(Account.class, "CREATOR", CREATOR); 85 } 86 } 87