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