1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts.model;
18
19import com.google.common.collect.Lists;
20
21import android.os.Bundle;
22import android.test.AndroidTestCase;
23import android.test.MoreAsserts;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import java.util.List;
27
28/**
29 * Test case for {@link AccountWithDataSet}.
30 *
31 * adb shell am instrument -w -e class com.android.contacts.model.AccountWithDataSetTest \
32       com.android.contacts.tests/android.test.InstrumentationTestRunner
33 */
34@SmallTest
35public class AccountWithDataSetTest extends AndroidTestCase {
36    public void testStringifyAndUnstringify() {
37        AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null);
38        AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null);
39        AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset");
40
41        // stringify() & unstringify
42        AccountWithDataSet a1r = AccountWithDataSet.unstringify(a1.stringify());
43        AccountWithDataSet a2r = AccountWithDataSet.unstringify(a2.stringify());
44        AccountWithDataSet a3r = AccountWithDataSet.unstringify(a3.stringify());
45
46        assertEquals(a1, a1r);
47        assertEquals(a2, a2r);
48        assertEquals(a3, a3r);
49
50        MoreAsserts.assertNotEqual(a1, a2r);
51        MoreAsserts.assertNotEqual(a1, a3r);
52
53        MoreAsserts.assertNotEqual(a2, a1r);
54        MoreAsserts.assertNotEqual(a2, a3r);
55
56        MoreAsserts.assertNotEqual(a3, a1r);
57        MoreAsserts.assertNotEqual(a3, a2r);
58    }
59
60    public void testStringifyListAndUnstringify() {
61        AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null);
62        AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null);
63        AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset");
64
65        // Empty list
66        assertEquals(0, stringifyListAndUnstringify().size());
67
68        // 1 element
69        final List<AccountWithDataSet> listA = stringifyListAndUnstringify(a1);
70        assertEquals(1, listA.size());
71        assertEquals(a1, listA.get(0));
72
73        // 2 elements
74        final List<AccountWithDataSet> listB = stringifyListAndUnstringify(a2, a1);
75        assertEquals(2, listB.size());
76        assertEquals(a2, listB.get(0));
77        assertEquals(a1, listB.get(1));
78
79        // 3 elements
80        final List<AccountWithDataSet> listC = stringifyListAndUnstringify(a3, a2, a1);
81        assertEquals(3, listC.size());
82        assertEquals(a3, listC.get(0));
83        assertEquals(a2, listC.get(1));
84        assertEquals(a1, listC.get(2));
85    }
86
87    private static List<AccountWithDataSet> stringifyListAndUnstringify(
88            AccountWithDataSet... accounts) {
89
90        List<AccountWithDataSet> list = Lists.newArrayList(accounts);
91        return AccountWithDataSet.unstringifyList(AccountWithDataSet.stringifyList(list));
92    }
93
94    public void testParcelable() {
95        AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null);
96        AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null);
97        AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset");
98
99        // Parcel them & unpercel.
100        final Bundle b = new Bundle();
101        b.putParcelable("a1", a1);
102        b.putParcelable("a2", a2);
103        b.putParcelable("a3", a3);
104
105        AccountWithDataSet a1r = b.getParcelable("a1");
106        AccountWithDataSet a2r = b.getParcelable("a2");
107        AccountWithDataSet a3r = b.getParcelable("a3");
108
109        assertEquals(a1, a1r);
110        assertEquals(a2, a2r);
111        assertEquals(a3, a3r);
112
113        MoreAsserts.assertNotEqual(a1, a2r);
114        MoreAsserts.assertNotEqual(a1, a3r);
115
116        MoreAsserts.assertNotEqual(a2, a1r);
117        MoreAsserts.assertNotEqual(a2, a3r);
118
119        MoreAsserts.assertNotEqual(a3, a1r);
120        MoreAsserts.assertNotEqual(a3, a2r);
121    }
122}
123