1/*
2 * Copyright (C) 2014 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 */
16
17package com.android.exchange.eas;
18
19import android.content.ContentValues;
20import android.provider.ContactsContract.CommonDataKinds.Email;
21import android.provider.ContactsContract.CommonDataKinds.StructuredName;
22import android.test.suitebuilder.annotation.SmallTest;
23
24import com.android.exchange.utility.ExchangeTestCase;
25
26import java.io.IOException;
27import java.util.ArrayList;
28
29/**
30 * You can run this entire test case with:
31 *   runtest -c com.android.exchange.eas.EasSyncContactsTests exchange
32 */
33@SmallTest
34public class EasSyncContactsTests extends ExchangeTestCase {
35
36    // Return null.
37    public void testTryGetStringDataEverythingNull() throws IOException {
38        final String result = EasSyncContacts.tryGetStringData(null, null);
39        assertNull(result);
40    }
41
42    // Return null.
43    public void testTryGetStringDataNullContentValues() throws IOException {
44        final String result = EasSyncContacts.tryGetStringData(null, "TestColumn");
45        assertNull(result);
46    }
47
48    // Return null.
49    public void testTryGetStringDataNullColumnName() throws IOException {
50        final ContentValues contentValues = new ContentValues();
51        contentValues.put("test_column", "test_value");
52        final String result = EasSyncContacts.tryGetStringData(contentValues, null);
53        assertNull(result);
54    }
55
56    // Return null.
57    public void testTryGetStringDataDoesNotContainColumn() throws IOException {
58        final ContentValues contentValues = new ContentValues();
59        contentValues.put("test_column", "test_value");
60        final String result = EasSyncContacts.tryGetStringData(contentValues, "does_not_exist");
61        assertNull(result);
62    }
63
64    // Return null.
65    public void testTryGetStringDataEmptyColumnValue() throws IOException {
66        final String columnName = "test_column";
67        final ContentValues contentValues = new ContentValues();
68        contentValues.put(columnName, "");
69        final String result = EasSyncContacts.tryGetStringData(contentValues, columnName);
70        assertNull(result);
71    }
72
73    // Return the data type forced to be a string.
74    // TODO: Test other data types.
75    public void testTryGetStringDataWrongType() throws IOException {
76        final String columnName = "test_column";
77        final Integer columnValue = new Integer(10);
78        final String columnValueAsString = columnValue.toString();
79        final ContentValues contentValues = new ContentValues();
80        contentValues.put(columnName, columnValue);
81        final String result = EasSyncContacts.tryGetStringData(contentValues, columnName);
82        assert(result.equals(columnValueAsString));
83    }
84
85    // Return the value as a string.
86    public void testTryGetStringDataSuccess() throws IOException {
87        final String columnName = "test_column";
88        final String columnValue = "test_value";
89        final ContentValues contentValues = new ContentValues();
90        contentValues.put(columnName, columnValue);
91        final String result = EasSyncContacts.tryGetStringData(contentValues, columnName);
92        assertTrue(result.equals(columnValue));
93    }
94
95    // Return null.
96    public void testGenerateFileAsNullParameters() throws IOException {
97        final String result = EasSyncContacts.generateFileAs(null, null);
98        assertNull(result);
99    }
100
101    // Should still return null because there is no name and no email.
102    public void testGenerateFileAsNullNameValuesAndEmptyList() throws IOException {
103        final ArrayList<ContentValues> emailList = new ArrayList<ContentValues>();
104        final String result = EasSyncContacts.generateFileAs(null, emailList);
105        assertNull(result);
106    }
107
108    // Just return the first email address that was passed in.
109    public void testGenerateFileAsNullNameValues() throws IOException {
110        final ArrayList<ContentValues> emailList = new ArrayList<ContentValues>();
111        final ContentValues emailValue = new ContentValues();
112        final String emailString = "anthonylee@google.com";
113        emailValue.put(Email.DATA, emailString);
114        emailList.add(emailValue);
115        final String result = EasSyncContacts.generateFileAs(null, emailList);
116        assertTrue(result.equals(emailString));
117    }
118
119    // Just return the formatted name.
120    public void testGenerateFileAsNullEmailValues() throws IOException {
121        final ContentValues nameValues = new ContentValues();
122        final String firstName = "Joe";
123        final String middleName = "Bob";
124        final String lastName = "Smith";
125        final String suffix = "Jr.";
126        nameValues.put(StructuredName.GIVEN_NAME, firstName);
127        nameValues.put(StructuredName.FAMILY_NAME, lastName);
128        nameValues.put(StructuredName.MIDDLE_NAME, middleName);
129        nameValues.put(StructuredName.SUFFIX, suffix);
130        final String result = EasSyncContacts.generateFileAs(nameValues, null);
131        final String generatedName = lastName + " " + suffix + ", " + firstName + " " + middleName;
132        assertTrue(generatedName.equals(result));
133    }
134
135    // This will generate a string that is similar to the full string but with no first name.
136    public void testGenerateFileAsNullFirstName() throws IOException {
137        final ContentValues nameValues = new ContentValues();
138        final String middleName = "Bob";
139        final String lastName = "Smith";
140        final String suffix = "Jr.";
141        nameValues.put(StructuredName.FAMILY_NAME, lastName);
142        nameValues.put(StructuredName.MIDDLE_NAME, middleName);
143        nameValues.put(StructuredName.SUFFIX, suffix);
144        final String result = EasSyncContacts.generateFileAs(nameValues, null);
145        final String generatedName = lastName + " " + suffix + ", " + middleName;
146        assertTrue(generatedName.equals(result));
147    }
148
149    // This will generate a string that is missing both the last name and the suffix.
150    public void testGenerateFileAsNullLastName() throws IOException {
151        final ContentValues nameValues = new ContentValues();
152        final String firstName = "Joe";
153        final String middleName = "Bob";
154        final String suffix = "Jr.";
155        nameValues.put(StructuredName.GIVEN_NAME, firstName);
156        nameValues.put(StructuredName.MIDDLE_NAME, middleName);
157        nameValues.put(StructuredName.SUFFIX, suffix);
158        final String result = EasSyncContacts.generateFileAs(nameValues, null);
159        final String generatedName = firstName + " " + middleName;
160        assertTrue(generatedName.equals(result));
161    }
162
163    // This will generate a string that is similar to the full name but missing the middle name.
164    public void testGenerateFileAsNullMiddleName() throws IOException {
165        final ContentValues nameValues = new ContentValues();
166        final String firstName = "Joe";
167        final String lastName = "Smith";
168        final String suffix = "Jr.";
169        nameValues.put(StructuredName.GIVEN_NAME, firstName);
170        nameValues.put(StructuredName.FAMILY_NAME, lastName);
171        nameValues.put(StructuredName.SUFFIX, suffix);
172        final String result = EasSyncContacts.generateFileAs(nameValues, null);
173        final String generatedName = lastName + " " + suffix + ", " + firstName;
174        assertTrue(generatedName.equals(result));
175    }
176
177    // Similar to the full name but no suffix.
178    public void testGenerateFileAsNullSuffix() throws IOException {
179        final ContentValues nameValues = new ContentValues();
180        final String firstName = "Joe";
181        final String middleName = "Bob";
182        final String lastName = "Smith";
183        nameValues.put(StructuredName.GIVEN_NAME, firstName);
184        nameValues.put(StructuredName.FAMILY_NAME, lastName);
185        nameValues.put(StructuredName.MIDDLE_NAME, middleName);
186        final String result = EasSyncContacts.generateFileAs(nameValues, null);
187        final String generatedName = lastName + ", " + firstName + " " + middleName;
188        assertTrue(generatedName.equals(result));
189    }
190}
191