1/*
2 * Copyright (C) 2006 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.internal.telephony;
18
19import android.os.ServiceManager;
20import android.test.suitebuilder.annotation.Suppress;
21
22import java.util.List;
23
24import junit.framework.TestCase;
25
26@Suppress
27public class SimPhoneBookTest extends TestCase {
28
29    public void testBasic() throws Exception {
30        IIccPhoneBook simPhoneBook =
31                IIccPhoneBook.Stub.asInterface(ServiceManager.getService("simphonebook"));
32        assertNotNull(simPhoneBook);
33
34        int size[] = simPhoneBook.getAdnRecordsSize(IccConstants.EF_ADN);
35        assertNotNull(size);
36        assertEquals(3, size.length);
37        assertEquals(size[0] * size[2], size[1]);
38        assertTrue(size[2] >= 100);
39
40        List<AdnRecord> adnRecordList = simPhoneBook.getAdnRecordsInEf(IccConstants.EF_ADN);
41        // do it twice cause the second time shall read from cache only
42        adnRecordList = simPhoneBook.getAdnRecordsInEf(IccConstants.EF_ADN);
43        assertNotNull(adnRecordList);
44
45        // Test for phone book update
46        int adnIndex, listIndex = 0;
47        AdnRecord originalAdn = null;
48        // We need to maintain the state of the SIM before and after the test.
49        // Since this test doesn't mock the SIM we try to get a valid ADN record,
50        // for 3 tries and if this fails, we bail out.
51        for (adnIndex = 3 ; adnIndex >= 1; adnIndex--) {
52            listIndex = adnIndex - 1; // listIndex is zero based.
53            originalAdn = adnRecordList.get(listIndex);
54            assertNotNull("Original Adn is Null.", originalAdn);
55            assertNotNull("Original Adn alpha tag is null.", originalAdn.getAlphaTag());
56            assertNotNull("Original Adn number is null.", originalAdn.getNumber());
57
58            if (originalAdn.getNumber().length() > 0 &&
59                originalAdn.getAlphaTag().length() > 0) {
60                break;
61            }
62        }
63        if (adnIndex == 0) return;
64
65        AdnRecord emptyAdn = new AdnRecord("", "");
66        AdnRecord firstAdn = new AdnRecord("John", "4085550101");
67        AdnRecord secondAdn = new AdnRecord("Andy", "6505550102");
68        String pin2 = null;
69
70        // udpate by index
71        boolean success = simPhoneBook.updateAdnRecordsInEfByIndex(IccConstants.EF_ADN,
72                firstAdn.getAlphaTag(), firstAdn.getNumber(), adnIndex, pin2);
73        adnRecordList = simPhoneBook.getAdnRecordsInEf(IccConstants.EF_ADN);
74         AdnRecord tmpAdn = adnRecordList.get(listIndex);
75        assertTrue(success);
76        assertTrue(firstAdn.isEqual(tmpAdn));
77
78        // replace by search
79        success = simPhoneBook.updateAdnRecordsInEfBySearch(IccConstants.EF_ADN,
80                firstAdn.getAlphaTag(), firstAdn.getNumber(),
81                secondAdn.getAlphaTag(), secondAdn.getNumber(), pin2);
82        adnRecordList = simPhoneBook.getAdnRecordsInEf(IccConstants.EF_ADN);
83        tmpAdn = adnRecordList.get(listIndex);
84        assertTrue(success);
85        assertFalse(firstAdn.isEqual(tmpAdn));
86        assertTrue(secondAdn.isEqual(tmpAdn));
87
88        // erase be search
89        success = simPhoneBook.updateAdnRecordsInEfBySearch(IccConstants.EF_ADN,
90                secondAdn.getAlphaTag(), secondAdn.getNumber(),
91                emptyAdn.getAlphaTag(), emptyAdn.getNumber(), pin2);
92        adnRecordList = simPhoneBook.getAdnRecordsInEf(IccConstants.EF_ADN);
93        tmpAdn = adnRecordList.get(listIndex);
94        assertTrue(success);
95        assertTrue(tmpAdn.isEmpty());
96
97        // restore the orginial adn
98        success = simPhoneBook.updateAdnRecordsInEfByIndex(IccConstants.EF_ADN,
99                originalAdn.getAlphaTag(), originalAdn.getNumber(), adnIndex,
100                pin2);
101        adnRecordList = simPhoneBook.getAdnRecordsInEf(IccConstants.EF_ADN);
102        tmpAdn = adnRecordList.get(listIndex);
103        assertTrue(success);
104        assertTrue(originalAdn.isEqual(tmpAdn));
105    }
106}
107