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