blob: 03c18e1f0c692915272b16dd8286318b535217bc [file] [log] [blame]
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +09001/*
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 Miyakawa69831d92010-09-02 10:39:00 -070016package android.pim.vcard.test_utils;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090017
18import android.content.ContentValues;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090019import android.pim.vcard.VCardEntry;
20import android.pim.vcard.VCardEntryCommitter;
21import android.pim.vcard.VCardEntryConstructor;
22import android.pim.vcard.VCardEntryHandler;
23import android.pim.vcard.VCardParser;
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070024import android.pim.vcard.VCardUtils;
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090025import android.pim.vcard.exception.VCardException;
26import android.provider.ContactsContract.Data;
27import android.test.AndroidTestCase;
28
29import java.io.IOException;
30import java.io.InputStream;
31
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070032public class ContentValuesVerifierElem {
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090033 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 Miyakawa69831d92010-09-02 10:39:00 -070050 public void verify(int resId, int vcardType)
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090051 throws IOException, VCardException {
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070052 verify(mTestCase.getContext().getResources().openRawResource(resId), vcardType);
Daisuke Miyakawa673c1d12009-11-20 11:06:39 +090053 }
54
Daisuke Miyakawa69831d92010-09-02 10:39:00 -070055 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 Miyakawa673c1d12009-11-20 11:06:39 +090058 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 Nguyen1a44d5d2010-01-13 10:42:43 -080087}