1package com.xtremelabs.robolectric.shadows;
2
3import android.accounts.Account;
4import android.os.Parcel;
5import android.os.Parcelable.Creator;
6import android.text.TextUtils;
7
8import com.xtremelabs.robolectric.Robolectric;
9import com.xtremelabs.robolectric.internal.Implementation;
10import com.xtremelabs.robolectric.internal.Implements;
11import com.xtremelabs.robolectric.internal.RealObject;
12
13import java.lang.reflect.Field;
14
15@Implements(Account.class)
16public class ShadowAccount {
17    @RealObject
18    private Account realObject;
19
20    public void __constructor__(String name, String type) throws Exception {
21        set(name, type);
22    }
23
24    public void __constructor__(Parcel parcel) throws Exception {
25        set(parcel.readString(), parcel.readString());
26    }
27
28    @Override
29    @Implementation
30    public String toString() {
31        return "Account {name=" + realObject.name + ", type=" + realObject.type + "}";
32    }
33
34    @Implementation
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
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
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
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
83    public static void reset() {
84        Robolectric.Reflection.setFinalStaticField(Account.class, "CREATOR", CREATOR);
85    }
86}
87