OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / core / tests / coretests / src / android / pim / vcard / test_utils / ExportTestProvider.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package android.pim.vcard.test_utils;
17
18 import android.content.ContentResolver;
19 import android.content.ContentValues;
20 import android.content.Entity;
21 import android.content.EntityIterator;
22 import android.database.Cursor;
23 import android.net.Uri;
24 import android.pim.vcard.VCardComposer;
25 import android.provider.ContactsContract.Contacts;
26 import android.provider.ContactsContract.Data;
27 import android.provider.ContactsContract.RawContacts;
28 import android.test.AndroidTestCase;
29 import android.test.mock.MockContentProvider;
30 import android.test.mock.MockCursor;
31
32 import junit.framework.TestCase;
33
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
37
38 public class ExportTestProvider extends MockContentProvider {
39     final private ArrayList<ContactEntry> mContactEntryList = new ArrayList<ContactEntry>();
40
41     private static class MockEntityIterator implements EntityIterator {
42         List<Entity> mEntityList;
43         Iterator<Entity> mIterator;
44
45         public MockEntityIterator(List<ContentValues> contentValuesList) {
46             mEntityList = new ArrayList<Entity>();
47             Entity entity = new Entity(new ContentValues());
48             for (ContentValues contentValues : contentValuesList) {
49                     entity.addSubValue(Data.CONTENT_URI, contentValues);
50             }
51             mEntityList.add(entity);
52             mIterator = mEntityList.iterator();
53         }
54
55         public boolean hasNext() {
56             return mIterator.hasNext();
57         }
58
59         public Entity next() {
60             return mIterator.next();
61         }
62
63         public void remove() {
64             throw new UnsupportedOperationException("remove not supported");
65         }
66
67         public void reset() {
68             mIterator = mEntityList.iterator();
69         }
70
71         public void close() {
72         }
73     }
74
75     public ExportTestProvider(AndroidTestCase androidTestCase) {
76     }
77
78     public ContactEntry buildInputEntry() {
79         ContactEntry contactEntry = new ContactEntry();
80         mContactEntryList.add(contactEntry);
81         return contactEntry;
82     }
83
84     /**
85      * <p>
86      * An old method which had existed but was removed from ContentResolver.
87      * </p>
88      * <p>
89      * We still keep using this method since we don't have a propeer way to know
90      * which value in the ContentValue corresponds to the entry in Contacts database.
91      * </p>
92      */
93     public EntityIterator queryEntities(Uri uri,
94             String selection, String[] selectionArgs, String sortOrder) {
95         TestCase.assertTrue(uri != null);
96         TestCase.assertTrue(ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()));
97         final String authority = uri.getAuthority();
98         TestCase.assertTrue(RawContacts.CONTENT_URI.getAuthority().equals(authority));
99         TestCase.assertTrue((Data.CONTACT_ID + "=?").equals(selection));
100         TestCase.assertEquals(1, selectionArgs.length);
101         final int id = Integer.parseInt(selectionArgs[0]);
102         TestCase.assertTrue(id >= 0 && id < mContactEntryList.size());
103
104         return new MockEntityIterator(mContactEntryList.get(id).getList());
105     }
106
107     @Override
108     public Cursor query(Uri uri,String[] projection,
109             String selection, String[] selectionArgs, String sortOrder) {
110         TestCase.assertTrue(VCardComposer.CONTACTS_TEST_CONTENT_URI.equals(uri));
111         // In this test, following arguments are not supported.
112         TestCase.assertNull(selection);
113         TestCase.assertNull(selectionArgs);
114         TestCase.assertNull(sortOrder);
115
116         return new MockCursor() {
117             int mCurrentPosition = -1;
118
119             @Override
120             public int getCount() {
121                 return mContactEntryList.size();
122             }
123
124             @Override
125             public boolean moveToFirst() {
126                 mCurrentPosition = 0;
127                 return true;
128             }
129
130             @Override
131             public boolean moveToNext() {
132                 if (mCurrentPosition < mContactEntryList.size()) {
133                     mCurrentPosition++;
134                     return true;
135                 } else {
136                     return false;
137                 }
138             }
139
140             @Override
141             public boolean isBeforeFirst() {
142                 return mCurrentPosition < 0;
143             }
144
145             @Override
146             public boolean isAfterLast() {
147                 return mCurrentPosition >= mContactEntryList.size();
148             }
149
150             @Override
151             public int getColumnIndex(String columnName) {
152                 TestCase.assertEquals(Contacts._ID, columnName);
153                 return 0;
154             }
155
156             @Override
157             public int getInt(int columnIndex) {
158                 TestCase.assertEquals(0, columnIndex);
159                 TestCase.assertTrue(mCurrentPosition >= 0
160                         && mCurrentPosition < mContactEntryList.size());
161                 return mCurrentPosition;
162             }
163
164             @Override
165             public String getString(int columnIndex) {
166                 return String.valueOf(getInt(columnIndex));
167             }
168
169             @Override
170             public void close() {
171             }
172         };
173     }
174 }