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 */
16package com.android.vcard.tests.testutils;
17
18import android.content.ContentResolver;
19import android.content.ContentValues;
20import android.content.Entity;
21import android.content.EntityIterator;
22import android.database.Cursor;
23import android.net.Uri;
24import android.provider.ContactsContract.Contacts;
25import android.provider.ContactsContract.Data;
26import android.provider.ContactsContract.RawContacts;
27import android.test.AndroidTestCase;
28import android.test.mock.MockContentProvider;
29import android.test.mock.MockCursor;
30import android.util.Log;
31
32import junit.framework.TestCase;
33
34import java.util.ArrayList;
35import java.util.Iterator;
36import java.util.List;
37
38public class ExportTestProvider extends MockContentProvider {
39    final private ArrayList<ContactEntry> mContactEntryList = new ArrayList<ContactEntry>();
40
41    private static class MockEntityIterator implements EntityIterator {
42        private final List<Entity> mEntityList;
43        private 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        @Override
56        public boolean hasNext() {
57            return mIterator.hasNext();
58        }
59
60        @Override
61        public Entity next() {
62            return mIterator.next();
63        }
64
65        @Override
66        public void remove() {
67            throw new UnsupportedOperationException("remove not supported");
68        }
69
70        @Override
71        public void reset() {
72            mIterator = mEntityList.iterator();
73        }
74
75        @Override
76        public void close() {
77        }
78    }
79
80    public ExportTestProvider(AndroidTestCase androidTestCase) {
81    }
82
83    public ContactEntry buildInputEntry() {
84        ContactEntry contactEntry = new ContactEntry();
85        mContactEntryList.add(contactEntry);
86        return contactEntry;
87    }
88
89    /**
90     * <p>
91     * An old method which had existed but was removed from ContentResolver.
92     * </p>
93     * <p>
94     * We still keep using this method since we don't have a propeer way to know
95     * which value in the ContentValue corresponds to the entry in Contacts database.
96     * </p>
97     */
98    public EntityIterator queryEntities(Uri uri,
99            String selection, String[] selectionArgs, String sortOrder) {
100        TestCase.assertTrue(uri != null);
101        TestCase.assertTrue(ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()));
102        final String authority = uri.getAuthority();
103        TestCase.assertTrue(RawContacts.CONTENT_URI.getAuthority().equals(authority));
104        TestCase.assertTrue((Data.CONTACT_ID + "=?").equals(selection));
105        TestCase.assertEquals(1, selectionArgs.length);
106        final int id = Integer.parseInt(selectionArgs[0]);
107        TestCase.assertTrue(id >= 0);
108        TestCase.assertTrue(id < mContactEntryList.size());
109
110        return new MockEntityIterator(mContactEntryList.get(id).getList());
111    }
112
113    @Override
114    public Cursor query(Uri uri,String[] projection,
115            String selection, String[] selectionArgs, String sortOrder) {
116        TestCase.assertTrue(VCardVerifier.CONTACTS_TEST_CONTENT_URI.equals(uri));
117        // In this test, following arguments are not supported.
118        TestCase.assertNull(selection);
119        TestCase.assertNull(selectionArgs);
120        TestCase.assertNull(sortOrder);
121
122        return new MockCursor() {
123            int mCurrentPosition = -1;
124
125            @Override
126            public int getCount() {
127                return mContactEntryList.size();
128            }
129
130            @Override
131            public boolean moveToFirst() {
132                mCurrentPosition = 0;
133                return true;
134            }
135
136            @Override
137            public boolean moveToNext() {
138                if (mCurrentPosition < mContactEntryList.size()) {
139                    mCurrentPosition++;
140                    return true;
141                } else {
142                    return false;
143                }
144            }
145
146            @Override
147            public boolean isBeforeFirst() {
148                return mCurrentPosition < 0;
149            }
150
151            @Override
152            public boolean isAfterLast() {
153                return mCurrentPosition >= mContactEntryList.size();
154            }
155
156            @Override
157            public int getColumnIndex(String columnName) {
158                TestCase.assertEquals(Contacts._ID, columnName);
159                return 0;
160            }
161
162            @Override
163            public int getInt(int columnIndex) {
164                TestCase.assertEquals(0, columnIndex);
165                TestCase.assertTrue(mCurrentPosition >= 0
166                        && mCurrentPosition < mContactEntryList.size());
167                return mCurrentPosition;
168            }
169
170            @Override
171            public String getString(int columnIndex) {
172                return String.valueOf(getInt(columnIndex));
173            }
174
175            @Override
176            public void close() {
177            }
178        };
179    }
180}