Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 16 | package android.pim.vcard.test_utils; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 17 | |
| 18 | import android.content.ContentValues; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 19 | import android.pim.vcard.VCardEntry; |
| 20 | import android.pim.vcard.VCardEntryCommitter; |
| 21 | import android.pim.vcard.VCardEntryConstructor; |
| 22 | import android.pim.vcard.VCardEntryHandler; |
| 23 | import android.pim.vcard.VCardParser; |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 24 | import android.pim.vcard.VCardUtils; |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 25 | import android.pim.vcard.exception.VCardException; |
| 26 | import android.provider.ContactsContract.Data; |
| 27 | import android.test.AndroidTestCase; |
| 28 | |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 32 | public class ContentValuesVerifierElem { |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 33 | private final AndroidTestCase mTestCase; |
| 34 | private final ImportTestResolver mResolver; |
| 35 | private final VCardEntryHandler mHandler; |
| 36 | |
| 37 | public ContentValuesVerifierElem(AndroidTestCase androidTestCase) { |
| 38 | mTestCase = androidTestCase; |
| 39 | mResolver = new ImportTestResolver(androidTestCase); |
| 40 | mHandler = new VCardEntryCommitter(mResolver); |
| 41 | } |
| 42 | |
| 43 | public ContentValuesBuilder addExpected(String mimeType) { |
| 44 | ContentValues contentValues = new ContentValues(); |
| 45 | contentValues.put(Data.MIMETYPE, mimeType); |
| 46 | mResolver.addExpectedContentValues(contentValues); |
| 47 | return new ContentValuesBuilder(contentValues); |
| 48 | } |
| 49 | |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 50 | public void verify(int resId, int vcardType) |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 51 | throws IOException, VCardException { |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 52 | verify(mTestCase.getContext().getResources().openRawResource(resId), vcardType); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Daisuke Miyakawa | 69831d9 | 2010-09-02 10:39:00 -0700 | [diff] [blame] | 55 | public void verify(InputStream is, int vcardType) throws IOException, VCardException { |
| 56 | final VCardParser vCardParser = VCardUtils.getAppropriateParser(vcardType); |
| 57 | final VCardEntryConstructor builder = new VCardEntryConstructor(vcardType, null); |
Daisuke Miyakawa | 673c1d1 | 2009-11-20 11:06:39 +0900 | [diff] [blame] | 58 | builder.addEntryHandler(mHandler); |
| 59 | try { |
| 60 | vCardParser.parse(is, builder); |
| 61 | } finally { |
| 62 | if (is != null) { |
| 63 | try { |
| 64 | is.close(); |
| 65 | } catch (IOException e) { |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | verifyResolver(); |
| 70 | } |
| 71 | |
| 72 | public void verifyResolver() { |
| 73 | mResolver.verify(); |
| 74 | } |
| 75 | |
| 76 | public void onParsingStart() { |
| 77 | mHandler.onStart(); |
| 78 | } |
| 79 | |
| 80 | public void onEntryCreated(VCardEntry entry) { |
| 81 | mHandler.onEntryCreated(entry); |
| 82 | } |
| 83 | |
| 84 | public void onParsingEnd() { |
| 85 | mHandler.onEnd(); |
| 86 | } |
Neal Nguyen | 1a44d5d | 2010-01-13 10:42:43 -0800 | [diff] [blame] | 87 | } |