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 
17 package com.android.contacts.common.model;
18 
19 import android.os.Bundle;
20 import android.test.AndroidTestCase;
21 import android.test.MoreAsserts;
22 import android.test.suitebuilder.annotation.SmallTest;
23 
24 import com.android.contacts.common.model.account.AccountWithDataSet;
25 import com.google.common.collect.Lists;
26 
27 import java.util.List;
28 
29 /**
30  * Test case for {@link AccountWithDataSet}.
31  *
32  * adb shell am instrument -w -e class com.android.contacts.model.AccountWithDataSetTest \
33        com.android.contacts.tests/android.test.InstrumentationTestRunner
34  */
35 @SmallTest
36 public class AccountWithDataSetTest extends AndroidTestCase {
testStringifyAndUnstringify()37     public void testStringifyAndUnstringify() {
38         AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null);
39         AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null);
40         AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset");
41 
42         // stringify() & unstringify
43         AccountWithDataSet a1r = AccountWithDataSet.unstringify(a1.stringify());
44         AccountWithDataSet a2r = AccountWithDataSet.unstringify(a2.stringify());
45         AccountWithDataSet a3r = AccountWithDataSet.unstringify(a3.stringify());
46 
47         assertEquals(a1, a1r);
48         assertEquals(a2, a2r);
49         assertEquals(a3, a3r);
50 
51         MoreAsserts.assertNotEqual(a1, a2r);
52         MoreAsserts.assertNotEqual(a1, a3r);
53 
54         MoreAsserts.assertNotEqual(a2, a1r);
55         MoreAsserts.assertNotEqual(a2, a3r);
56 
57         MoreAsserts.assertNotEqual(a3, a1r);
58         MoreAsserts.assertNotEqual(a3, a2r);
59     }
60 
testStringifyListAndUnstringify()61     public void testStringifyListAndUnstringify() {
62         AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null);
63         AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null);
64         AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset");
65 
66         // Empty list
67         assertEquals(0, stringifyListAndUnstringify().size());
68 
69         // 1 element
70         final List<AccountWithDataSet> listA = stringifyListAndUnstringify(a1);
71         assertEquals(1, listA.size());
72         assertEquals(a1, listA.get(0));
73 
74         // 2 elements
75         final List<AccountWithDataSet> listB = stringifyListAndUnstringify(a2, a1);
76         assertEquals(2, listB.size());
77         assertEquals(a2, listB.get(0));
78         assertEquals(a1, listB.get(1));
79 
80         // 3 elements
81         final List<AccountWithDataSet> listC = stringifyListAndUnstringify(a3, a2, a1);
82         assertEquals(3, listC.size());
83         assertEquals(a3, listC.get(0));
84         assertEquals(a2, listC.get(1));
85         assertEquals(a1, listC.get(2));
86     }
87 
stringifyListAndUnstringify( AccountWithDataSet... accounts)88     private static List<AccountWithDataSet> stringifyListAndUnstringify(
89             AccountWithDataSet... accounts) {
90 
91         List<AccountWithDataSet> list = Lists.newArrayList(accounts);
92         return AccountWithDataSet.unstringifyList(AccountWithDataSet.stringifyList(list));
93     }
94 
testParcelable()95     public void testParcelable() {
96         AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null);
97         AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null);
98         AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset");
99 
100         // Parcel them & unpercel.
101         final Bundle b = new Bundle();
102         b.putParcelable("a1", a1);
103         b.putParcelable("a2", a2);
104         b.putParcelable("a3", a3);
105 
106         AccountWithDataSet a1r = b.getParcelable("a1");
107         AccountWithDataSet a2r = b.getParcelable("a2");
108         AccountWithDataSet a3r = b.getParcelable("a3");
109 
110         assertEquals(a1, a1r);
111         assertEquals(a2, a2r);
112         assertEquals(a3, a3r);
113 
114         MoreAsserts.assertNotEqual(a1, a2r);
115         MoreAsserts.assertNotEqual(a1, a3r);
116 
117         MoreAsserts.assertNotEqual(a2, a1r);
118         MoreAsserts.assertNotEqual(a2, a3r);
119 
120         MoreAsserts.assertNotEqual(a3, a1r);
121         MoreAsserts.assertNotEqual(a3, a2r);
122     }
123 }
124