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 */
16package android.pim.vcard.test_utils;
17
18import android.content.ContentValues;
19import android.pim.vcard.VCardEntry;
20import android.pim.vcard.VCardEntryCommitter;
21import android.pim.vcard.VCardEntryConstructor;
22import android.pim.vcard.VCardEntryHandler;
23import android.pim.vcard.VCardParser;
24import android.pim.vcard.VCardUtils;
25import android.pim.vcard.exception.VCardException;
26import android.provider.ContactsContract.Data;
27import android.test.AndroidTestCase;
28
29import java.io.IOException;
30import java.io.InputStream;
31
32public class ContentValuesVerifierElem {
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
50    public void verify(int resId, int vcardType)
51            throws IOException, VCardException {
52        verify(mTestCase.getContext().getResources().openRawResource(resId), vcardType);
53    }
54
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);
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    }
87}
88