1/*
2 * Copyright (C) 2010 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 com.android.vcard.tests;
17
18import android.provider.ContactsContract.CommonDataKinds.StructuredName;
19import android.test.AndroidTestCase;
20
21import com.android.vcard.VCardConfig;
22import com.android.vcard.tests.testutils.VCardVerifier;
23
24import junit.framework.AssertionFailedError;
25import junit.framework.TestCase;
26
27import java.util.Arrays;
28
29/**
30 * Tests confirming utilities for vCard tests work fine.
31 *
32 * Now that the foundation classes for vCard test cases became too complicated to
33 * rely on without testing itself.
34 */
35public class VCardTestUtilsTests extends AndroidTestCase {
36    public void testShouldFailAtPropertyNodeVerification() {
37        boolean failureDetected = false;
38        try {
39            final VCardVerifier verifier = new VCardVerifier(this);
40            verifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_GENERIC, R.raw.v21_backslash);
41            verifier.addPropertyNodesVerifierElem()
42                    .addExpectedNodeWithOrder("N", ";A;B\\;C\\;;D;:E;\\\\;--",  // wrong
43                            Arrays.asList("", "A;B\\", "C\\;", "D", ":E", "\\\\", ""))
44                    .addExpectedNodeWithOrder("FN", "A;B\\C\\;D:E\\\\");
45            verifier.verify();
46        } catch (AssertionFailedError e) {
47            failureDetected = true;
48        }
49        if (!failureDetected) {
50            TestCase.fail("Test case that should fail actually succeeded.");
51        }
52    }
53
54    public void testShouldFailAtContentValueVerification() {
55        boolean failureDetected = false;
56        try {
57            final VCardVerifier verifier = new VCardVerifier(this);
58            verifier.initForImportTest(VCardConfig.VCARD_TYPE_V21_GENERIC, R.raw.v21_backslash);
59            verifier.addContentValuesVerifierElem()
60                    .addExpected(StructuredName.CONTENT_ITEM_TYPE)
61                            .put(StructuredName.GIVEN_NAME, "A;B\\")
62                            .put(StructuredName.MIDDLE_NAME, "C\\;")
63                            .put(StructuredName.PREFIX, "D")
64                            .put(StructuredName.SUFFIX, ":E");
65            // DISPLAY_NAME is missing.
66            verifier.verify();
67        } catch (AssertionFailedError e) {
68            failureDetected = true;
69        }
70        if (!failureDetected) {
71            TestCase.fail("Test case that should fail actually succeeded.");
72        }
73    }
74
75    public void testShouldFailAtLineVerification() {
76        boolean failureDetected = false;
77        try {
78            final VCardVerifier verifier = new VCardVerifier(this);
79            verifier.initForExportTest(VCardConfig.VCARD_TYPE_V30_GENERIC);
80            verifier.addInputEntry().addContentValues(StructuredName.CONTENT_ITEM_TYPE)
81                    .put(StructuredName.FAMILY_NAME, "\\")
82                    .put(StructuredName.GIVEN_NAME, ";")
83                    .put(StructuredName.MIDDLE_NAME, ",")
84                    .put(StructuredName.PREFIX, "\n")
85                    .put(StructuredName.DISPLAY_NAME, "[<{Unescaped:Asciis}>]");
86            verifier.addLineVerifierElem()
87                    .addExpected("TEL:1")  // wrong
88                    .addExpected("FN:[<{Unescaped:Asciis}>]");
89            verifier.verify();
90        } catch (AssertionFailedError e) {
91            failureDetected = true;
92        }
93        if (!failureDetected) {
94            TestCase.fail("Test case that should fail actually succeeded.");
95        }
96    }
97
98}
99