1package com.xtremelabs.robolectric.shadows;
2
3import android.accounts.Account;
4import android.os.Parcel;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.Test;
7import org.junit.runner.RunWith;
8
9import static org.hamcrest.CoreMatchers.equalTo;
10import static org.hamcrest.CoreMatchers.not;
11import static org.junit.Assert.assertThat;
12
13@RunWith(WithTestDefaultsRunner.class)
14public class AccountTest {
15
16    @Test
17    public void shouldHaveStringsConstructor() throws Exception {
18        Account account = new Account("name", "type");
19
20        assertThat(account.name, equalTo("name"));
21        assertThat(account.type, equalTo("type"));
22    }
23
24    @Test
25    public void shouldHaveParcelConstructor() throws Exception {
26        Parcel p = Parcel.obtain();
27        p.writeString("name");
28        p.writeString("type");
29
30        p.setDataPosition(0);
31
32        Account account = new Account(p);
33        assertThat(account.name, equalTo("name"));
34        assertThat(account.type, equalTo("type"));
35    }
36
37    @Test
38    public void shouldHaveCreator() throws Exception {
39        Account expected = new Account("name", "type");
40        Parcel p = Parcel.obtain();
41        expected.writeToParcel(p, 0);
42
43        p.setDataPosition(0);
44
45        Account actual = Account.CREATOR.createFromParcel(p);
46        assertThat(expected, equalTo(actual));
47    }
48
49    @Test(expected = IllegalArgumentException.class)
50    public void shouldThrowIfNameIsEmpty() throws Exception {
51        new Account("", "type");
52    }
53
54    @Test(expected = IllegalArgumentException.class)
55    public void shouldThrowIfTypeIsEmpty() throws Exception {
56        new Account("name", "");
57    }
58
59    @Test
60    public void shouldHaveToString() throws Exception {
61        Account account = new Account("name", "type");
62        assertThat(account.toString(), equalTo("Account {name=name, type=type}"));
63    }
64
65    @Test
66    public void shouldProvideEqualAndHashCode() throws Exception {
67        assertThat(new Account("a", "b"), equalTo(new Account("a", "b")));
68        assertThat(new Account("a", "b"), not(equalTo(new Account("c", "b"))));
69        assertThat(new Account("a", "b").hashCode(), equalTo(new Account("a", "b").hashCode()));
70        assertThat(new Account("a", "b").hashCode(), not(equalTo(new Account("c", "b").hashCode())));
71    }
72}
73